1 /** 2 * BaseUnrealEnvironment, an implementation of the environment interface standard that 3 * facilitates the connection between GOAL and the UT2004 engine. 4 * 5 * Copyright (C) 2012 BaseUnrealEnvironment authors. 6 * 7 * This program is free software: you can redistribute it and/or modify it under 8 * the terms of the GNU General Public License as published by the Free Software 9 * Foundation, either version 3 of the License, or (at your option) any later 10 * version. 11 * 12 * This program is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 15 * details. 16 * 17 * You should have received a copy of the GNU General Public License along with 18 * this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 package nl.tudelft.goal.unreal.environment; 21 22 import java.util.Map; 23 24 import eis.eis2java.environment.AbstractEnvironment; 25 import eis.exceptions.ManagementException; 26 import eis.iilang.EnvironmentState; 27 import eis.iilang.Parameter; 28 29 /** 30 * Convenience implementation to simplify dealing with EIS state transitions. 31 * This environment hides all transitions from its subclasses and provides a 32 * better transition model. 33 * 34 * It guarantees that initialize, connect, pause, start and kill are be called in 35 * this order. The arrow indicates the direction, the brackets indicate these 36 * calls are optional. 37 * 38 * initalizeEnvironment() ---> connectEnvironment() ---> [pauseEnvironment 39 * [<---> startEnvironment] --->] killEnvironment() ---> initalizeEnvironment() 40 * ---> 41 * 42 * @author mpkorstanje 43 * 44 */ 45 public abstract class SimpleTransitioningEnvironment extends AbstractEnvironment { 46 47 /** 48 * Generated serialVersionUID. 49 */ 50 private static final long serialVersionUID = -8671600742206011276L; 51 52 53 @Override 54 public final synchronized void init(Map<String, Parameter> parameters) throws ManagementException { 55 super.init(parameters); 56 57 // Delegate actual initialization to abstract method. 58 initializeEnvironment(parameters); 59 60 // Connect to environment 61 connectEnvironment(); 62 63 // Transition to paused because the EIS spec requires this. 64 setState(EnvironmentState.PAUSED); 65 66 // Transition to running because UT can't be started paused. 67 setState(EnvironmentState.RUNNING); 68 69 // Connect agents once environment state is set to running. 70 // This allows agents to start from the get go. 71 connectAgents(); 72 } 73 74 protected abstract void initializeEnvironment(Map<String, Parameter> parameters) throws ManagementException; 75 76 77 protected abstract void connectEnvironment() throws ManagementException; 78 79 80 protected abstract void connectAgents() throws ManagementException; 81 82 83 @Override 84 public final synchronized void kill() throws ManagementException { 85 86 // Delegate kill to abstract method. 87 killEnvironment(); 88 // Transition complete, set the state. 89 super.kill(); 90 91 } 92 93 protected abstract void killEnvironment() throws ManagementException; 94 95 /** 96 * {@inheritDoc} 97 */ 98 @Override 99 public final synchronized void pause() throws ManagementException { 100 // Delegate kill to abstract method. 101 pauseEvironment(); 102 // Transition complete, set the state. 103 super.pause(); 104 } 105 106 protected abstract void pauseEvironment() throws ManagementException; 107 108 /** 109 * {@inheritDoc} 110 */ 111 @Override 112 public final synchronized void start() throws ManagementException { 113 // Delegate start to abstract method. 114 startEnvironment(); 115 // Transition complete, set the state. 116 super.start(); 117 } 118 119 protected abstract void startEnvironment() throws ManagementException; 120 }