/*
 * Encog(tm) Core v3.4 - Java Version
 * http://www.heatonresearch.com/encog/
 * https://github.com/encog/encog-java-core
 
 * Copyright 2008-2016 Heaton Research, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *   
 * For more information on Heaton Research copyrights, licenses 
 * and trademarks visit:
 * http://www.heatonresearch.com/copyright
 */
package org.encog.ca.universe.basic;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import org.encog.ca.CellularAutomataError;
import org.encog.ca.universe.UniverseCell;
import org.encog.ca.universe.UniverseCellFactory;
import org.encog.persist.EncogFileSection;
import org.encog.persist.EncogPersistor;
import org.encog.persist.EncogReadHelper;
import org.encog.persist.EncogWriteHelper;
import org.encog.persist.PersistConst;
import org.encog.util.csv.CSVFormat;
import org.encog.util.csv.NumberList;

public class PersistBasicUniverse implements EncogPersistor {

	@Override
	public String getPersistClassString() {
		return "BasicUniverse";
	}

	@Override
	public Object read(InputStream is) {
		int rows=0; 
		int cols=0;
		int elementCount=0;
		int size=0;
		double min=0;
		double max=0;
		Map objParams=null;
		BasicUniverse result = null;
		final EncogReadHelper in = new EncogReadHelper(is);
		EncogFileSection section;

		while ((section = in.readNextSection()) != null) {
			if (section.getSectionName().equals("BasicUniverse")
					&& section.getSubSectionName().equals("PARAMS")) {
				objParams = section.parseParams();				
			}
			if (section.getSectionName().equals("BasicUniverse")
					&& section.getSubSectionName().equals("UNIVERSE-PARAM")) {
				final Map params = section.parseParams();
				cols = EncogFileSection.parseInt(params,PersistConst.COLS);
				rows = EncogFileSection.parseInt(params,PersistConst.ROWS);				
			} else if (section.getSectionName().equals("BasicUniverse")
					&& section.getSubSectionName().equals("UNIVERSE-CELLS")) {
				final Map params = section.parseParams();
				min = EncogFileSection.parseDouble(params,PersistConst.MIN);
				max = EncogFileSection.parseDouble(params,PersistConst.MAX);
				elementCount = EncogFileSection.parseInt(params,BasicUniverse.ELEMENT_COUNT);
				size = EncogFileSection.parseInt(params,PersistConst.SIZE);
				
			} else if (section.getSectionName().equals("BasicUniverse")
					&& section.getSubSectionName().equals("UNIVERSE")) {
				
				// first create the universe
				BasicCellFactory factory;
				
				if( elementCount==-1 ) {
					factory = new BasicCellFactory(size,min,max);
				} else {
					factory = new BasicCellFactory(size,elementCount);
				}
				
				result = new BasicUniverse(rows,cols,factory);
				result.getProperties().putAll(objParams);
				
				// now fill the universe
				int ec = 1;
				if( elementCount!=-1 ) {
					ec = elementCount;
				}
				int row = 0;
				for(String line : section.getLines() ) {
					double[] d = NumberList.fromList(CSVFormat.EG_FORMAT, line);
					int idx = 0;
					for(int col=0;col 

Ads help maintain this website.