1 package nl.tudelft.goal.ut2004.agent.module;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Map.Entry;
11
12 import nl.tudelft.goal.EIS2Java.annotation.AsPercept;
13 import nl.tudelft.goal.EIS2Java.util.EIS2JavaUtil;
14 import cz.cuni.amis.pogamut.base.agent.module.SensorModule;
15 import cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController;
16 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
17 import cz.cuni.amis.utils.exception.PogamutException;
18 import eis.exceptions.EntityException;
19
20
21
22
23
24
25
26
27
28
29
30 @SuppressWarnings("rawtypes")
31 public class PerceptModule extends SensorModule<UT2004Bot> {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 private Map<String, Method> perceptProviders = new HashMap<String, Method>();
62
63
64
65
66 private Map<String, Object> perceptBatch = new HashMap<String, Object>();
67
68
69
70
71 private Map<String, List<Object>> eventPerceptBatch = new HashMap<String, List<Object>>();
72
73 public PerceptModule(UT2004Bot agent) {
74 super(agent);
75
76
77
78
79
80 try {
81 Class<? extends IUT2004BotController> controllerClass = agent.getController().getClass();
82 perceptProviders = EIS2JavaUtil.processPerceptAnnotations(controllerClass);
83 } catch (EntityException e) {
84 throw new PogamutException("Could not proccess all annotations", e);
85 }
86 }
87
88
89
90
91 public synchronized void updatePercepts() {
92
93 perceptBatch = new HashMap<String, Object>();
94
95 for (Entry<String, Method> entry : perceptProviders.entrySet()) {
96 String perceptName = entry.getKey();
97 Method method = entry.getValue();
98 AsPercept annotation = method.getAnnotation(AsPercept.class);
99 Object percept;
100 try {
101 percept = method.invoke(agent.getController());
102 } catch (IllegalArgumentException e) {
103 throw new PogamutException("Unable to update " + perceptName, e);
104 } catch (IllegalAccessException e) {
105 throw new PogamutException("Unable to update " + perceptName, e);
106 } catch (InvocationTargetException e) {
107 throw new PogamutException("Unable to update " + perceptName, e);
108 }
109
110
111 if (annotation.event()) {
112 if (!eventPerceptBatch.containsKey(perceptName)) {
113 eventPerceptBatch.put(perceptName, new ArrayList<Object>());
114 }
115
116 List<Object> events = eventPerceptBatch.get(perceptName);
117
118
119 if (!annotation.multiplePercepts()) {
120 throw new PogamutException("Unable to update " + perceptName
121 + " event percept must have multiplePercepts.", this);
122 }
123
124 if (!(percept instanceof Collection<?>)) {
125 throw new PogamutException("Unable to update " + perceptName
126 + " return value must be a collection.", this);
127 }
128
129 events.addAll((Collection<?>) percept);
130
131 }
132
133 else {
134 perceptBatch.put(perceptName, percept);
135 }
136 }
137
138 perceptBatch.putAll(eventPerceptBatch);
139 }
140
141 public synchronized Map<String, Object> getAllPercepts() {
142
143 eventPerceptBatch.clear();
144 return perceptBatch;
145 }
146 }