1 package cz.cuni.amis.pogamut.ut2004.agent.module.utils;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Map;
7 import java.util.Set;
8
9
10 import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
11 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
12 import cz.cuni.amis.pogamut.base.utils.math.DistanceUtils;
13 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
14 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BeginMessage;
15 import cz.cuni.amis.utils.IFilter;
16
17
18
19
20
21
22
23
24
25 public class TabooSet<T> implements IFilter<T> {
26
27 public static interface IRelaxedTaboo<T> {
28
29 public boolean isTaboo(T item, double remainingTabooTime);
30
31 }
32
33
34
35
36 private Map<T, Double> taboo = new HashMap<T, Double>();
37
38
39
40
41
42 public int size() {
43 return taboo.size();
44 }
45
46
47
48
49
50
51
52
53
54
55
56 public double getTabooTime(T item) {
57 Double tabooTime = taboo.get(item);
58 if (tabooTime == null) return 0;
59 if (tabooTime < 0) return Double.POSITIVE_INFINITY;
60 if (tabooTime < time) {
61 taboo.remove(item);
62 return 0;
63 } else {
64 return tabooTime - time;
65 }
66 }
67
68
69
70
71
72 public void add(T item) {
73 taboo.put(item, (double)-1);
74 }
75
76
77
78
79
80
81 public void add(T item, double timeout) {
82 taboo.put(item, time+timeout);
83 }
84
85
86
87
88
89 public void remove(T item) {
90 taboo.remove(item);
91 }
92
93
94
95
96
97
98
99 public boolean isTaboo(T item) {
100 if (taboo.containsKey(item)) {
101 double tabooTime = taboo.get(item);
102 if (tabooTime < 0) {
103 return true;
104 }
105 if (tabooTime < time) {
106 taboo.remove(item);
107 return false;
108 } else {
109 return true;
110 }
111 } else {
112 return false;
113 }
114 }
115
116
117
118
119
120
121
122 public boolean isTaboo(T item, IRelaxedTaboo estimator) {
123 if (!isTaboo(item)) {
124 return false;
125 } else {
126 return estimator.isTaboo(item, getTabooTime(item));
127 }
128 }
129
130
131
132
133
134
135 @Override
136 public boolean isAccepted(T item) {
137 return !isTaboo(item);
138 }
139
140
141
142
143
144
145
146
147
148 public Set<T> filter(Collection<T> collection) {
149 Set<T> set = new HashSet<T>();
150 for (T t : collection) {
151 if (isTaboo(t)) continue;
152 set.add(t);
153 }
154 return set;
155 }
156
157
158
159
160
161
162
163
164 public Set<T> filter(Collection<T> collection, IRelaxedTaboo estimator) {
165 Set<T> set = new HashSet<T>();
166 for (T t : collection) {
167 if (!isTaboo(t)) {
168 set.add(t);
169 } else {
170 if (!estimator.isTaboo(t, getTabooTime(t))) {
171 set.add(t);
172 }
173 }
174 }
175 return set;
176 }
177
178
179
180
181
182
183 public void clear() {
184 taboo.clear();
185 }
186
187
188
189
190 public double getTime() {
191 return time;
192 }
193
194
195
196
197 private double time;
198
199
200
201
202
203 private class BeginMessageListener implements IWorldEventListener<BeginMessage> {
204
205
206 public BeginMessageListener(IWorldView worldView) {
207 worldView.addEventListener(BeginMessage.class, this);
208 }
209
210 @Override
211 public void notify(BeginMessage event) {
212 time = event.getTime();
213 }
214
215 };
216
217
218
219
220 BeginMessageListener beginMessageListener;
221
222
223
224
225
226 public TabooSet(UT2004Bot bot) {
227 beginMessageListener = new BeginMessageListener(bot.getWorldView());
228 }
229
230 }