/*---------------------------- Ingeneue source code ?Copyright 2000 by George von Dassow, Eli Meir, Edwin Munro, and Garrett Odell. Permission is granted for use by private individuals and by individuals and groups at all non-profit institutions, as long as those using the software or examining the code do not intend to make a profit from its use, and subject to all other conditions given on the website www.ingenenue.org. www.ingeneue.org contact@ingeneue.org ----------------------------*/ /** Holds all the components of a model, including the network and the cells. This class also knows how to load and construct a model from a stream (using the NetInput class), and knows how to save a model to a stream. */ package main; import affectors.Affector; import genegui.GuiInterface; import genegui.ModelState; import genegui.ModelStateChangeListener; import initialconditions.InitialConditionSet; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Enumeration; import parameters.Parameter; import parameters.ParameterSet; public class Model extends Object implements ModelStateChangeListener { public final static int END_OF_RUN = 0; public final static int NEW_RUN = 1; public final static int UPDATE_CELLS = 2; /** A ModelState if there is one associated. This is used to communicate changes in the state of the model at the end of runs. */ private ModelState AModelState; public int arrayWidth; // must be even public int arrayHeight; public int numCells; public Cell[] cells = null; public Network net = null; private InitialConditionSet initializers; private ParameterSet itsParameters; public PrintWriter out = null; // These are used while running the model private boolean good = false; /** Note - after making a new Model, you should immediately call the load() function. Operations using the Model are undefined before load is called and will probably crash. You can check isGood() to see if a model is in a usable state. */ public Model() {} public void load(BetterTokenizer tokenizer) throws ModelLoadingException, Exception { int i; try { NetInput input = new NetInput(tokenizer); net = input.net; initializers = input.initializers; itsParameters = input.parameters; arrayWidth = input.width; arrayHeight = input.height; if((Globals.cellNumSides = input.numsides) == 2) arrayHeight = 1; // Make the cells System.out.println("Making cells"); numCells = arrayWidth * arrayHeight; makeCells(); // Arrange connections between neighboring cells System.out.println("Attaching neighbors"); attachNeighbors(Globals.cellNumSides); // Now that the network and all its nodes has a specific instance in each cell, you can make all the abstract connections // into real connections and set intial levels of each node in each cell // System.out.println("Fixing arrays"); // Node.fixArrays(); // shrink all node arrays to the right size // Add in our net to each cell System.out.println("Adding nodes to cells"); for(i = 0; i < numCells; i++) cells[i].addNodes(net); System.out.println("Adding affectors to cells"); for(i = 0; i < numCells; i++) cells[i].addAffectors(net); // After each cell has a copy of the net, connect everything up // System.out.println("Connecting affectors"); // for(i = 0; i < numCells; i++) cells[i].fixAllAffectors(net); // Set the initial conditions System.out.println("Setting initial conditions"); initializers.setModel(cells); good = true; } catch(ModelLoadingException e) { throw e; } catch(Exception e) { throw e; } } public Network getNetwork() { return net; } public String getName() { return net.getName(); } public ParameterSet getModelParameterSet() { return itsParameters; } public void setParameterSet(ParameterSet parameters) { itsParameters = parameters; Parameter p; for (int i=0;i