// Path: cas-core/src/main/java/com/ghjansen/cas/core/ca/CellularAutomaton.java
// public abstract class CellularAutomaton<R extends Rule, S extends Space, T extends Time, O extends Combination,
//         N extends Transition> {
// 
//     private R rule;
// 
//     protected CellularAutomaton(R rule) throws InvalidRuleException {
//         if (rule == null) {
//             throw new InvalidRuleException();
//         }
//         this.rule = rule;
//     }
// 
//     public void executeRule(S space, T time) throws InvalidStateException, InvalidCombinationException,
//             InvalidTransitionException, TimeLimitReachedException {
//         O combination = (O) space.getCombination(time);
//         N transition = (N) this.rule.getTransition(combination);
//         space.setState(time, transition);
//         time.increase();
//     }
// 
//     public R getRule() {
//         return this.rule;
//     }
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidCellularAutomataException.java
// public class InvalidCellularAutomataException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -4104738409967924193L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidCombinationException.java
// public class InvalidCombinationException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 884762124844706448L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidStateException.java
// public class InvalidStateException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -7435119307025647318L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidTransitionException.java
// public class InvalidTransitionException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 1848434169756515614L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidUniverseException.java
// public class InvalidUniverseException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -5140302667570723804L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/TimeLimitReachedException.java
// public class TimeLimitReachedException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -6359890788325087840L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/physics/Universe.java
// public abstract class Universe<S extends Space, T extends Time> {
// 
//     private S space;
//     private T time;
// 
//     protected Universe(S space, T time) throws InvalidSpaceException, InvalidTimeException {
//         if (space == null) {
//             throw new InvalidSpaceException();
//         }
//         if (time == null) {
//             throw new InvalidTimeException();
//         }
//         this.space = space;
//         this.time = time;
//     }
// 
//     public S getSpace() {
//         return space;
//     }
// 
//     public T getTime() {
//         return time;
//     }
// 
// }


import com.ghjansen.cas.core.ca.CellularAutomaton;
import com.ghjansen.cas.core.exception.InvalidCellularAutomataException;
import com.ghjansen.cas.core.exception.InvalidCombinationException;
import com.ghjansen.cas.core.exception.InvalidStateException;
import com.ghjansen.cas.core.exception.InvalidTransitionException;
import com.ghjansen.cas.core.exception.InvalidUniverseException;
import com.ghjansen.cas.core.exception.TimeLimitReachedException;
import com.ghjansen.cas.core.physics.Universe;
/*
 * CAS - Cellular Automata Simulator
 * Copyright (C) 2016  Guilherme Humberto Jansen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ghjansen.cas.core.simulation;


/**
 * @author Guilherme Humberto Jansen (contact.ghjansen@gmail.com)
 */
public abstract class Simulation<U extends Universe, C extends CellularAutomaton> {

    private U universe;
    private C ca;
    private boolean active;

    protected Simulation(U universe, C ca)
            throws InvalidUniverseException, InvalidCellularAutomataException {
        if (universe == null) {
            throw new InvalidUniverseException();
        }
        if (ca == null) {
            throw new InvalidCellularAutomataException();
        }
        this.universe = universe;
        this.ca = ca;
        this.active = false;
    }

    public void simulateComplete() throws InvalidStateException, InvalidCombinationException, InvalidTransitionException,
            TimeLimitReachedException {
        this.active = true;
        while (active) {
            executeRule();
        }
    }

    public void simulateIteration() throws InvalidStateException, InvalidCombinationException,
            InvalidTransitionException, TimeLimitReachedException {
        this.active = true;
        final int referenceTime = this.universe.getTime().getAbsolute();
        while (active) {
// Path: cas-core/src/main/java/com/ghjansen/cas/core/ca/CellularAutomaton.java
// public abstract class CellularAutomaton<R extends Rule, S extends Space, T extends Time, O extends Combination,
//         N extends Transition> {
// 
//     private R rule;
// 
//     protected CellularAutomaton(R rule) throws InvalidRuleException {
//         if (rule == null) {
//             throw new InvalidRuleException();
//         }
//         this.rule = rule;
//     }
// 
//     public void executeRule(S space, T time) throws InvalidStateException, InvalidCombinationException,
//             InvalidTransitionException, TimeLimitReachedException {
//         O combination = (O) space.getCombination(time);
//         N transition = (N) this.rule.getTransition(combination);
//         space.setState(time, transition);
//         time.increase();
//     }
// 
//     public R getRule() {
//         return this.rule;
//     }
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidCellularAutomataException.java
// public class InvalidCellularAutomataException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -4104738409967924193L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidCombinationException.java
// public class InvalidCombinationException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 884762124844706448L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidStateException.java
// public class InvalidStateException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -7435119307025647318L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidTransitionException.java
// public class InvalidTransitionException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = 1848434169756515614L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/InvalidUniverseException.java
// public class InvalidUniverseException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -5140302667570723804L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/exception/TimeLimitReachedException.java
// public class TimeLimitReachedException extends Exception {
// 
//     /**
//      * Serialization management
//      */
//     private static final long serialVersionUID = -6359890788325087840L;
// 
// }
// 
// Path: cas-core/src/main/java/com/ghjansen/cas/core/physics/Universe.java
// public abstract class Universe<S extends Space, T extends Time> {
// 
//     private S space;
//     private T time;
// 
//     protected Universe(S space, T time) throws InvalidSpaceException, InvalidTimeException {
//         if (space == null) {
//             throw new InvalidSpaceException();
//         }
//         if (time == null) {
//             throw new InvalidTimeException();
//         }
//         this.space = space;
//         this.time = time;
//     }
// 
//     public S getSpace() {
//         return space;
//     }
// 
//     public T getTime() {
//         return time;
//     }
// 
// }


// Path: cas-core/src/main/java/com/ghjansen/cas/core/simulation/Simulation.java
import com.ghjansen.cas.core.ca.CellularAutomaton;
import com.ghjansen.cas.core.exception.InvalidCellularAutomataException;
import com.ghjansen.cas.core.exception.InvalidCombinationException;
import com.ghjansen.cas.core.exception.InvalidStateException;
import com.ghjansen.cas.core.exception.InvalidTransitionException;
import com.ghjansen.cas.core.exception.InvalidUniverseException;
import com.ghjansen.cas.core.exception.TimeLimitReachedException;
import com.ghjansen.cas.core.physics.Universe;

/*
 * CAS - Cellular Automata Simulator
 * Copyright (C) 2016  Guilherme Humberto Jansen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ghjansen.cas.core.simulation;


/**
 * @author Guilherme Humberto Jansen (contact.ghjansen@gmail.com)
 */
public abstract class Simulation<U extends Universe, C extends CellularAutomaton> {

    private U universe;
    private C ca;
    private boolean active;

    protected Simulation(U universe, C ca)
            throws InvalidUniverseException, InvalidCellularAutomataException {
        if (universe == null) {
            throw new InvalidUniverseException();
        }
        if (ca == null) {
            throw new InvalidCellularAutomataException();
        }
        this.universe = universe;
        this.ca = ca;
        this.active = false;
    }

    public void simulateComplete() throws InvalidStateException, InvalidCombinationException, InvalidTransitionException,
            TimeLimitReachedException {
        this.active = true;
        while (active) {
            executeRule();
        }
    }

    public void simulateIteration() throws InvalidStateException, InvalidCombinationException,
            InvalidTransitionException, TimeLimitReachedException {
        this.active = true;
        final int referenceTime = this.universe.getTime().getAbsolute();
        while (active) {
            if (referenceTime == this.universe.getTime().getAbsolute()) {