View Javadoc

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 nl.tudelft.goal.EIS2Java.environment.AbstractEnvironment;
25  
26  import eis.exceptions.ManagementException;
27  import eis.iilang.EnvironmentState;
28  import eis.iilang.Parameter;
29  
30  /**
31   * Convenience implementation to simplify dealing with EIS state transitions.
32   * This environment hides all transitions from its subclasses and provides a
33   * better transition model.
34   * 
35   * It guarantees that initialize, connect, pause, start and kill are be called in
36   * this order. The arrow indicates the direction, the brackets indicate these
37   * calls are optional.
38   * 
39   * initalizeEnvironment() ---> connectEnvironment() ---> [pauseEnvironment
40   * [<---> startEnvironment] --->] killEnvironment() ---> initalizeEnvironment()
41   * --->
42   * 
43   * @author mpkorstanje
44   * 
45   */
46  public abstract class SimpleTransitioningEnvironment extends AbstractEnvironment {
47  
48  	/**
49  	 * Generated serialVersionUID.
50  	 */
51  	private static final long serialVersionUID = -8671600742206011276L;
52  
53  	protected abstract void connectEnvironment() throws ManagementException;
54  
55  	@Override
56  	public final synchronized void init(Map<String, Parameter> parameters) throws ManagementException {
57  		super.init(parameters);
58  
59  		// Delegate actual initialization to abstract method.
60  		initializeEnvironment(parameters);
61  
62  		// Delegate connection to abstract method.
63  		connectEnvironment();
64  
65  		// Transition to paused because the EIS spec requires this.
66  		setState(EnvironmentState.PAUSED);
67  
68  		// Transition to running because UT can't be started paused.
69  		setState(EnvironmentState.RUNNING);
70  	}
71  
72  	protected abstract void initializeEnvironment(Map<String, Parameter> parameters) throws ManagementException;
73  
74  	@Override
75  	public final synchronized void kill() throws ManagementException {
76  
77  		// Delegate kill to abstract method.
78  		killEnvironment();
79  		// Transition complete, set the state.
80  		super.kill();
81  
82  	}
83  
84  	protected abstract void killEnvironment() throws ManagementException;
85  
86  	/**
87  	 * {@inheritDoc}
88  	 */
89  	@Override
90  	public final synchronized void pause() throws ManagementException {
91  		// Delegate kill to abstract method.
92  		pauseEvironment();
93  		// Transition complete, set the state.
94  		super.pause();
95  	}
96  
97  	protected abstract void pauseEvironment() throws ManagementException;
98  
99  	/**
100 	 * {@inheritDoc}
101 	 */
102 	@Override
103 	public final synchronized void start() throws ManagementException {
104 		// Delegate kill to abstract method.
105 		startEnvironment();
106 		// Transition complete, set the state.
107 		super.start();
108 	}
109 
110 	protected abstract void startEnvironment() throws ManagementException;
111 }