private ICAConfig BuildConfiguration(IProjection <ICell> grid) { CAConfig configuration = new CAConfig(_memory.GetObjects().Count, grid.GetObjects()[0].GetDefaultState()); foreach (ICell cell in grid.GetObjects()) { configuration.AddCellState(cell, cell.GetState()); } return(configuration); }
//private static Token newLine = new Token("\n"); /** * <returns> IPattern </returns> */ public Object Parse(ITokenStream tokenStream) { Token token; CAConfig pattern = new CAConfig(0, null); //LivingCell.DeadState);//Determine the size of the configuration! //CellState[,] patternState = new CellState[,]; IList <IList <CellState> > rows = new List <IList <CellState> >(); IList <CellState> row = null; Boolean rowOpened = false; int rowIndex = 0; int columnIndex = 0; while ((token = tokenStream.NextToken()) != null) { if (token.Equals(stateSeparator)) { //token=tokenStream.NextToken(); //do nothing } else if (token.Equals(rowStart)) //init a new row { if (rowOpened) { throw new InvalidOperationException(string.Format("Missing enclosing bracket! Please, enclose the row, before creating a new one")); } row = new List <CellState>(); rowOpened = true; rowIndex++; columnIndex = 0; } else if (token.Equals(rowEnd)) //add the existing row to the list of rows { rows.Add(row); rowOpened = false; } else if (!char.IsWhiteSpace(token.ToString(), 0)) //State Token { if (row == null || !rowOpened) { throw new InvalidOperationException("You should define a cell state within a Row. Use the following format: {D,A,D,D} "); } CellState cellState = null;//CellState.ValueOf(token.ToString()); row.Add(cellState); pattern.AddCellState(null, cellState); columnIndex++; } } return(pattern); }
//private ICAConfig CreateICAConfig(IList<On3dPoint> latticePoints, IList<On3dPoint> configurationPoints) //creates an ICAConfig instance out of the OpenNurbs state configuration private ICAConfig CreateICAConfig(OnStateConfig stateConfig) { CAConfig cfg = new CAConfig(0, cellPrototype.GetState()); int latticePointIndex = 0; foreach (Point3d latticePoint in pointLattice) { foreach (Point3d configurationPoint in stateConfig.GetPoints()) { if (latticePoint.X == configurationPoint.X && latticePoint.Y == configurationPoint.Y && latticePoint.Z == configurationPoint.Z) { cfg.AddCellState(cellularGrid.GetObject(latticePointIndex), stateConfig.GetState()); } } latticePointIndex++; }//foreach return(cfg); }
/** * The CA evolves to a new state, according to the behavior of each cell. * The old generations is archieved in the list of generations */ public ICAConfig Update() { currentTime++; CAConfig nextConfiguration = new CAConfig(_memory.GetObjects().Count, grid.GetObjects()[0].GetDefaultState()); foreach (ICell cell in grid.GetObjects()) { //do not alter the cells' states directly //as the states affect the evolution //the result of the evolution is saved //and the cells are updated when all cells evolved CellState evolvedCellState = cell.UpdateState(); nextConfiguration.AddCellState(cell, evolvedCellState); } //update the cells's states according to the evolution foreach (ICell cell in grid.GetObjects()) { cell.SetState(nextConfiguration.GetCellState(cell)); } return(nextConfiguration); }