1 package cz.cuni.amis.pogamut.usar2004.agent.module.master;
2
3 import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
4 import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
5 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
6 import cz.cuni.amis.pogamut.usar2004.agent.USAR2004Bot;
7 import cz.cuni.amis.pogamut.usar2004.agent.module.datatypes.StateContainer;
8 import cz.cuni.amis.pogamut.usar2004.agent.module.datatypes.VehicleType;
9 import cz.cuni.amis.pogamut.usar2004.agent.module.state.SuperState;
10 import cz.cuni.amis.pogamut.usar2004.communication.messages.usarinfomessages.StateMessage;
11 import java.util.List;
12
13 /**
14 * Master module for gathering every state message server sends. They are saved
15 * respectively by their type and than name. There is a listener for STA
16 * messages that updates data in StateContainer. There is also Mission package
17 * module because STA and MISSTA messages are both State messages. All
18 * mastermodules are singletons.
19 *
20 * @author vejmanm
21 */
22 public class StateMasterModule extends SensorModule<USAR2004Bot>
23 {
24 protected StateMessageListener stateListener;
25 protected static StateMasterModule singleton = null;
26 //DataStructure representing the list of all availible StateTypes, each is listed by name.
27 protected StateContainer stateModule;
28 protected MissionPackageModule packageModule;
29
30 /**
31 * Private ctor
32 *
33 * @param bot USAR2004Bot variable for creating instance of each new record
34 * in stateModules(due to inheritance)
35 */
36 public StateMasterModule(USAR2004Bot bot)
37 {
38 super(bot);
39 stateModule = new StateContainer();
40 stateListener = new StateMessageListener(worldView);
41 packageModule = MissionPackageModule.getModuleInstance(bot);
42 }
43
44 /**
45 * Returns singleton instance of this module if it already exists. If not it
46 * is created.
47 *
48 * @param bot Parameter for possible creating of new instance.
49 * @return Returns singleton instance of this module.
50 */
51 public static StateMasterModule getModuleInstance(USAR2004Bot bot)
52 {
53 if(singleton == null)
54 {
55 singleton = new StateMasterModule(bot);
56 }
57 return singleton;
58 }
59
60 /**
61 * Check method for making sure the acces to this module is possible.Returns
62 * false if either state collection is empty or null;
63 *
64 * @return Returns false if either state collection is empty or null;
65 */
66 public Boolean isReady()
67 {
68 return (stateModule != null && !stateModule.isEmpty());
69 }
70
71 /**
72 * Gets state message representative from local hashmap. Returns null if
73 * none matches.
74 *
75 * @param type String representation of state type.
76 * @return Returns specific state message matching input string <b>type</b>
77 */
78 public SuperState getStatesByType(String type)
79 {
80 if(type == null)
81 {
82 return null;
83 }
84 return stateModule.getStatesByType(type.toLowerCase());
85 }
86
87 /**
88 * Iterates through local hashmap values and seeks match. Note that every
89 * SuperState offspring has its own VehicleType property. Returns null if
90 * none matches.
91 *
92 * @param type VehicleType representation of state type - easier to use
93 * since we don't have to watch after propper spelling.
94 * @return Returns specific state message matching input VehicleType
95 * <b>type</b>
96 */
97 public SuperState getStatesByVehilceType(VehicleType type)
98 {
99 return stateModule.getStatesByVehicleType(type);
100 }
101
102 /**
103 * Iterates through local hahmap values and takes note of every SuperState
104 * object that is Instance of specified class. Returns null if none matches
105 * or <b>c</b> is null.
106 *
107 * @param c <B>c</B> is class that the state message should extend, So when
108 * SuperState is inserted as c, it should return list of all types of
109 * vehicles that this holds data of.
110 * @return Returns list of state objects that implements class <B>c</B>
111 */
112 public List<SuperState> getStatesByClass(Class c)
113 {
114 return stateModule.getStatesByClass(c);
115 }
116
117 /**
118 * Returns list of availible state object names.
119 *
120 * @return Returns list of availible state object names.
121 */
122 public List<String> getAvailibleTypes()
123 {
124 return stateModule.getAvailibleTypes();
125 }
126
127 /**
128 * Asks VehicleType (enum) if it knows VehicleType represented by string
129 * <B>type</B>. If it does, it also contains Class reference. This reference
130 * is then instantiated and returned. If it does not, it returns instance of
131 * SuperState which is represented by VehicleType.UNKNOW.
132 *
133 * @param message StateMessage object containing valid VehicleType.
134 * @return Returns Class instance relevant to input message.
135 */
136 protected SuperState createNewState(StateMessage message)
137 {
138 return ModuleInstanceProvider.getStateInstanceByType(message.getType());
139 }
140
141 /**
142 * Returns a flag that indicates if stateUpdate was successful.
143 *
144 * @param message new StateMessage object.
145 * @return Return false if this message type with this name does not exist
146 * yet.
147 */
148 protected boolean updateStateCollection(StateMessage message)
149 {
150 if(!stateModule.containsKey(message.getType().toLowerCase()))
151 {
152 return false;
153 }
154 stateModule.get(message.getType().toLowerCase()).updateMessage(message);
155 return true;
156 }
157
158 /**
159 * Updates previous State or creates a new Record.
160 *
161 * @param message This ought to be StateMessage caught by listener.
162 */
163 protected void fileMessage(StateMessage message)
164 {
165 if(updateStateCollection(message))
166 {
167 return;
168 }
169
170 SuperState newState = createNewState(message);
171 if(newState == null)
172 {
173 System.out.println("This state is not supported! " + message.getType());
174 return;
175 }
176 String type = message.getType().toLowerCase();
177 newState.updateMessage(message);//fill the object
178 stateModule.put(type, newState);
179 }
180
181 /**
182 * Mission package module for information about misspkgs.
183 *
184 * @return Returns Map<String,MissionPackageState> singleton that holds
185 * information abou MissPkg.
186 */
187 public MissionPackageModule getMissionPackageModule()
188 {
189 return packageModule;
190 }
191
192 @Override
193 protected void cleanUp()
194 {
195 super.cleanUp();
196 stateListener = null;
197 stateModule = null;
198 singleton = null;
199 }
200
201 private class StateMessageListener implements IWorldEventListener<StateMessage>
202 {
203 @Override
204 public void notify(StateMessage event)
205 {
206 fileMessage(event);
207 }
208
209 public StateMessageListener(IWorldView worldView)
210 {
211 worldView.addEventListener(StateMessage.class, this);
212 }
213 }
214 }