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 protected abstract void connectEnvironment() throws ManagementException;
53
54 @Override
55 public final synchronized void init(Map<String, Parameter> parameters) throws ManagementException {
56 super.init(parameters);
57
58 // Delegate actual initialization to abstract method.
59 initializeEnvironment(parameters);
60
61 // Delegate connection to abstract method.
62 connectEnvironment();
63
64 // Transition to paused because the EIS spec requires this.
65 setState(EnvironmentState.PAUSED);
66
67 // Transition to running because UT can't be started paused.
68 setState(EnvironmentState.RUNNING);
69 }
70
71 protected abstract void initializeEnvironment(Map<String, Parameter> parameters) throws ManagementException;
72
73 @Override
74 public final synchronized void kill() throws ManagementException {
75
76 // Delegate kill to abstract method.
77 killEnvironment();
78 // Transition complete, set the state.
79 super.kill();
80
81 }
82
83 protected abstract void killEnvironment() throws ManagementException;
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public final synchronized void pause() throws ManagementException {
90 // Delegate kill to abstract method.
91 pauseEvironment();
92 // Transition complete, set the state.
93 super.pause();
94 }
95
96 protected abstract void pauseEvironment() throws ManagementException;
97
98 /**
99 * {@inheritDoc}
100 */
101 @Override
102 public final synchronized void start() throws ManagementException {
103 // Delegate start to abstract method.
104 startEnvironment();
105 // Transition complete, set the state.
106 super.start();
107 }
108
109 protected abstract void startEnvironment() throws ManagementException;
110 }