001    /**
002     * planningtool - A Planning Tool with Critiquing Support.
003     * 
004     * Copyright (C) 2006 olale
005    
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010    
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015    
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019    
020     * Contact information: 
021     * E-mail: olale@ida.liu.se
022     *         olale@lysator.liu.se
023     */
024    package se.liu.ida.critiquer.simulation;
025    
026    import java.util.ArrayList;
027    import java.util.Calendar;
028    import java.util.HashSet;
029    
030    import se.liu.ida.critiquer.activities.parameters.Commander;
031    import se.liu.ida.critiquer.constraints.SimulationCritic;
032    import se.liu.ida.critiquer.constraints.VisualConstraint;
033    import se.liu.ida.critiquer.constraints.VisualConstraints;
034    import se.liu.ida.critiquer.mics.ReferenceHolder;
035    import se.liu.ida.critiquer.resources.Agent;
036    
037    /**
038     * 
039     * This class represents an object that updates the state information of agents
040     * that are part of the current simulation, as well as active constraints
041     * 
042     * @author olale
043     * 
044     */
045    public class SimulationEngine {
046    
047            private ArrayList<SimulationCritic> simulationCritics = new ArrayList<SimulationCritic>();
048            private ArrayList<AgentSimulationState> agentSimulationStates = new ArrayList<AgentSimulationState>();
049            private Calendar currentTime;
050    
051            public void init(Calendar startTime) {
052                    this.currentTime=startTime;
053                    /**
054                     * Populate the simulation elements with critics and agents
055                     */
056                    agentSimulationStates.clear();
057                    simulationCritics.clear();
058                    for (Commander commander : ReferenceHolder.commanders.getCommanders().values()) {
059                            HashSet<Agent> agents = commander.getSubordinates();
060                            for (Agent agent : agents) {
061                                    agent.initAgentSimulationState(this);
062                                    AgentSimulationState agentSimulationState = agent.getAgentSimulationState();
063                                    agentSimulationStates.add(agentSimulationState);
064                            }
065                    }
066                    /**
067                     * Add simulation critics as well
068                     */
069                    for (VisualConstraint constraint : VisualConstraints.getActiveConstraints()) {
070                            if (constraint instanceof SimulationCritic) {
071                                    SimulationCritic simulationCritic = (SimulationCritic) constraint;
072                                    simulationCritic.initSimulation(this);
073                                    simulationCritics.add(simulationCritic);
074                            }
075                    }
076            }
077            /**
078             * 
079             * Tell all simulation elements the simulation is advancing
080             * 
081             * @param step in minutes
082             */
083            public void stepForward(int step) {
084                    for (SimulationElement simulationElement : agentSimulationStates) {
085                            simulationElement.stepForward(step);
086                    }
087                    for (SimulationElement simulationElement : simulationCritics) {
088                            simulationElement.stepForward(step);
089                    }
090                    
091                    currentTime.add(Calendar.MINUTE, step);
092            }
093            
094            /**
095             * 
096             * Tell all simulation elements the simulation is advancing
097             * 
098             * @param step in minutes
099             */
100            public void stepTo(Calendar time) {
101                    for (SimulationElement simulationElement : agentSimulationStates) {
102                            simulationElement.stepTo(time);
103                    }
104                    for (SimulationElement simulationElement : simulationCritics) {
105                            simulationElement.stepTo(time);
106                    }
107                    currentTime=time;
108            }
109            
110            /**
111             * Reverse the simulation
112             * @param step in minutes. Negative figure.
113             */
114            public void stepBackwards(int step) {
115                    for (SimulationElement simulationElement : agentSimulationStates) {
116                            simulationElement.stepForward(step);
117                    }
118                    for (SimulationElement simulationElement : simulationCritics) {
119                            simulationElement.stepBackward(step);
120                    }
121                    currentTime.add(Calendar.MINUTE, step);
122            }
123            
124            /**
125             * @return Returns the currentTime.
126             */
127            public Calendar getCurrentTime() {
128                    return currentTime;
129            }
130            /**
131             * @param currentTime The currentTime to set.
132             */
133            public void setCurrentTime(Calendar currentTime) {
134                    this.currentTime = currentTime;
135            }
136            /**
137             * @return Returns the agentSimulationStates.
138             */
139            public ArrayList<AgentSimulationState> getAgentSimulationStates() {
140                    return agentSimulationStates;
141            }
142            
143            
144    }