简体   繁体   English

Java:带有键入单元格的细胞自动机

[英]Java: Cellular automata with typed cells

I'm working on a Java-based cellular automata-like implementation (not sure it technically is, given what follows) in which the individual cells may have different types that encapsulate different data and CA rules. 我正在研究基于Java的类似细胞自动机的实现(在技术上不确定,如下所示),其中各个单元可能具有封装不同数据和CA规则的不同类型。 There may be a large number of types, and I want to be able to dynamically plug in new ones without having to maintain other code. 可能有很多类型,我希望能够动态插入新的类型,而无需维护其他代码。

All cell types derive from a common base class. 所有细胞类型都来自共同的基类。 Each cell's update() method is called exactly once per frame of the simulation: 每个单元格的update()方法在每个模拟帧中只调用一次:

public abstract class Cell
{
    public abstract void update(Cell[] neighbors);
}

This works fine when the CA rules only need the data of the cell in question, eg: 当CA规则仅需要相关单元格的数据时,这可以正常工作,例如:

public class CellTypeA extends Cell
{
    public int Data = 0;

    @Override
    public void update(Cell[] neighbors)
    {
        Data++;
    }
}

However, I have some simulation rules that require the cell to query adjacent neighbor cells for data, but only if they are of a type that has said data. 但是,我有一些模拟规则要求单元格查询相邻的相邻单元格的数据,但前提是它们是具有所述数据的类型。 There is a strong temptation to use the instanceof operator to accomplish this: 很有诱惑力使用instanceof运算符来实现这一目标:

public class CellTypeB extends Cell
{
    public boolean State = false;

    private void update(Cell[] neighbors)
    {
        for (Cell c : neighbors)
        {
            if (c instanceof CellTypeA)
            {
                State = (((CellTypeA)c).getData() > 10);
            }
        } 
    }
}

I'd prefer to avoid the smelly instanceof if possible. 如果可能的话,我宁愿避免臭臭的情况。 I also can't just promote getData() to the superclass to achieve polymorphism, as the actual data structure of these cells will be somewhat more complex and varied. 我也不能只是将getData()提升到超类来实现多态,因为这些单元格的实际数据结构会稍微复杂多变。 I've been reading about the GoF Visitor pattern to solve instanceof abuse, but I can't seem to figure out how to apply it to this problem. 我一直在阅读有关GoF访问者模式以解决滥用实例的问题,但我似乎无法弄清楚如何将其应用于此问题。 Thoughts on how to do this, or on other ways to approach the problem? 关于如何做到这一点的想法,或其他方法来解决问题?

Thanks! 谢谢! Steve 史蒂夫

I played around, and couldn't figure out how you'd make the visitor pattern a) neatly deal with two items to visit and b) be pluggable like you required. 我玩了,并且无法弄清楚你是如何制作访客模式a)整齐地处理两个要访问的项目和b)像你所要求的可插拔。

This works, but probably hides the instanceof inside Guava stuff: 这可行,但可能隐藏了番石榴内部的instanceof

import com.google.common.collect.Iterables;
import java.util.Arrays;

public class CellTypeB extends Cell
{
    public boolean State = false;

    @Override
    public void update(Cell[] neighbors) {
        Iterable<CellTypeA> onlyAs = Iterables.filter(Arrays.asList(neighbors), CellTypeA.class);
        for(CellTypeA a: onlyAs) {
            State = (a.getData() > 10);
        }   
    }   
}   

PS Did you mean to use |= when assigning a value to State in the loop? PS你是否想在循环中为State赋值时使用|=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM