Source code: org/alicebot/server/core/PredicateMaster.java
1 // Decompiled by Jad v1.5.8c. Copyright 2001 Pavel Kouznetsov.
2 // Jad home page: http://www.geocities.com/kpdus/jad.html
3 // Decompiler options: packimports(3)
4
5 package org.alicebot.server.core;
6
7 import java.util.*;
8 import org.alicebot.server.core.logging.Log;
9 import org.alicebot.server.core.util.DeveloperError;
10 import org.alicebot.server.core.util.Trace;
11
12 // Referenced classes of package org.alicebot.server.core:
13 // NoSuchPredicateException, PredicateInfo, Bots, Bot,
14 // ActiveMultiplexor, Multiplexor, Globals
15
16 public class PredicateMaster
17 {
18
19 private PredicateMaster()
20 {
21 }
22
23 protected Object clone()
24 throws CloneNotSupportedException
25 {
26 throw new CloneNotSupportedException();
27 }
28
29 public static String set(String s, String s1, String s2, String s3)
30 {
31 Map map = Bots.getBot(s3).predicatesFor(s2);
32 map.put(s, s1);
33 cacheSize++;
34 checkCache();
35 return nameOrValue(s, s1, s3);
36 }
37
38 public static synchronized String set(String s, int i, String s1, String s2, String s3)
39 {
40 Map map = Bots.getBot(s3).predicatesFor(s2);
41 ArrayList arraylist = getLoadOrCreateValueList(s, map, s2, s3);
42 try
43 {
44 arraylist.add(i - 1, s1);
45 }
46 catch(IndexOutOfBoundsException indexoutofboundsexception)
47 {
48 pushOnto(arraylist, s1);
49 }
50 cacheSize++;
51 checkCache();
52 return nameOrValue(s, s1, s3);
53 }
54
55 public static synchronized String push(String s, String s1, String s2, String s3)
56 {
57 Map map = Bots.getBot(s3).predicatesFor(s2);
58 ArrayList arraylist = getLoadOrCreateValueList(s, map, s2, s3);
59 pushOnto(arraylist, s1);
60 cacheSize++;
61 checkCache();
62 return nameOrValue(s, s1, s3);
63 }
64
65 public static synchronized String get(String s, String s1, String s2)
66 {
67 Map map = Bots.getBot(s2).predicatesFor(s1);
68 Object obj = map.get(s);
69 String s3;
70 if(obj == null)
71 {
72 try
73 {
74 s3 = ActiveMultiplexor.getInstance().loadPredicate(s, s1, s2);
75 }
76 catch(NoSuchPredicateException nosuchpredicateexception)
77 {
78 s3 = bestAvailableDefault(s, s2);
79 }
80 if(s3 != null)
81 map.put(s, s3);
82 else
83 throw new DeveloperError("Null string found in user predicates cache!");
84 } else
85 if(obj instanceof String)
86 s3 = (String)obj;
87 else
88 if(obj instanceof ArrayList)
89 {
90 ArrayList arraylist = (ArrayList)obj;
91 s3 = (String)arraylist.get(0);
92 } else
93 {
94 throw new DeveloperError("Something other than a String or a ArrayList found in user predicates cache!");
95 }
96 checkCache();
97 return s3;
98 }
99
100 public static synchronized String get(String s, int i, String s1, String s2)
101 {
102 Map map = Bots.getBot(s2).predicatesFor(s1);
103 String s3 = null;
104 ArrayList arraylist = null;
105 try
106 {
107 arraylist = getValueList(s, map);
108 }
109 catch(NoSuchPredicateException nosuchpredicateexception)
110 {
111 try
112 {
113 arraylist = loadValueList(s, map, s1, s2);
114 }
115 catch(NoSuchPredicateException nosuchpredicateexception1)
116 {
117 s3 = bestAvailableDefault(s, s2);
118 map.put(s, s3);
119 }
120 }
121 if(arraylist != null)
122 try
123 {
124 s3 = (String)arraylist.get(i - 1);
125 }
126 catch(IndexOutOfBoundsException indexoutofboundsexception)
127 {
128 s3 = bestAvailableDefault(s, s2);
129 }
130 checkCache();
131 return s3;
132 }
133
134 private static void pushOnto(ArrayList arraylist, Object obj)
135 {
136 arraylist.add(0, obj);
137 for(; arraylist.size() > 5; arraylist.remove(arraylist.size() - 1));
138 }
139
140 private static ArrayList getValueList(String s, Map map)
141 throws NoSuchPredicateException
142 {
143 ArrayList arraylist;
144 if(map.size() > 0 && map.containsKey(s))
145 {
146 Object obj = map.get(s);
147 if(obj instanceof String)
148 {
149 if(obj != null)
150 {
151 arraylist = createValueList(s, map);
152 arraylist.add((String)obj);
153 } else
154 {
155 throw new DeveloperError("Null String found as value in predicates!");
156 }
157 } else
158 if(obj instanceof ArrayList)
159 {
160 if(obj != null)
161 arraylist = (ArrayList)obj;
162 else
163 throw new DeveloperError("Null ArrayList found as value in predicates!");
164 } else
165 {
166 throw new DeveloperError("Something other than a String or ArrayList found in predicates!");
167 }
168 } else
169 {
170 throw new NoSuchPredicateException(s);
171 }
172 return arraylist;
173 }
174
175 private static ArrayList loadValueList(String s, Map map, String s1, String s2)
176 throws NoSuchPredicateException, NullPointerException
177 {
178 if(map == null)
179 throw new NullPointerException("Cannot call loadValueList with null userPredicates!");
180 int i = 1;
181 String s3;
182 try
183 {
184 s3 = ActiveMultiplexor.getInstance().loadPredicate(s + '.' + i, s1, s2);
185 }
186 catch(NoSuchPredicateException nosuchpredicateexception)
187 {
188 throw new NoSuchPredicateException(s);
189 }
190 ArrayList arraylist = createValueList(s, map);
191 arraylist.add(s3);
192 try
193 {
194 while(i <= 5)
195 {
196 i++;
197 arraylist.add(ActiveMultiplexor.getInstance().loadPredicate(s + '.' + i, s1, s2));
198 }
199 }
200 catch(NoSuchPredicateException nosuchpredicateexception1) { }
201 return arraylist;
202 }
203
204 private static ArrayList createValueList(String s, Map map)
205 {
206 ArrayList arraylist = new ArrayList();
207 map.put(s, arraylist);
208 return arraylist;
209 }
210
211 private static ArrayList getLoadOrCreateValueList(String s, Map map, String s1, String s2)
212 {
213 ArrayList arraylist;
214 try
215 {
216 arraylist = getValueList(s, map);
217 }
218 catch(NoSuchPredicateException nosuchpredicateexception)
219 {
220 try
221 {
222 arraylist = loadValueList(s, map, s1, s2);
223 }
224 catch(NoSuchPredicateException nosuchpredicateexception1)
225 {
226 arraylist = createValueList(s, map);
227 }
228 }
229 return arraylist;
230 }
231
232 private static String bestAvailableDefault(String s, String s1)
233 {
234 HashMap hashmap = Bots.getBot(s1).getPredicatesInfo();
235 if(hashmap.containsKey(s))
236 {
237 String s2 = ((PredicateInfo)hashmap.get(s)).defaultValue;
238 if(s2 != null)
239 return s2;
240 }
241 return PREDICATE_EMPTY_DEFAULT;
242 }
243
244 private static String nameOrValue(String s, String s1, String s2)
245 {
246 HashMap hashmap = Bots.getBot(s2).getPredicatesInfo();
247 if(hashmap.containsKey(s) && ((PredicateInfo)hashmap.get(s)).returnNameWhenSet)
248 return s;
249 else
250 return s1;
251 }
252
253 private static int save(int i)
254 {
255 int j = 0;
256 for(Iterator iterator = Bots.keysIterator(); iterator.hasNext() && j < i;)
257 {
258 String s = (String)iterator.next();
259 Bot bot = Bots.getBot(s);
260 Map map = bot.getPredicateCache();
261 if(!map.isEmpty())
262 {
263 for(Iterator iterator1 = map.keySet().iterator(); iterator1.hasNext() && j < i;)
264 {
265 String s1 = null;
266 try
267 {
268 s1 = (String)iterator1.next();
269 }
270 catch(ConcurrentModificationException concurrentmodificationexception)
271 {
272 Log.log("Some problem with PredicateMaster design: ConcurrentModificationException in save() [1].", Log.RUNTIME);
273 }
274 Map map1 = (Map)map.get(s1);
275 for(Iterator iterator2 = map1.keySet().iterator(); iterator2.hasNext();)
276 {
277 String s2 = (String)iterator2.next();
278 Object obj = map1.get(s2);
279 if(obj instanceof String)
280 {
281 String s3 = (String)map1.get(s2);
282 if(!s3.equals(bestAvailableDefault(s2, s)))
283 {
284 ActiveMultiplexor.getInstance().savePredicate(s2, s3, s1, s);
285 j++;
286 }
287 } else
288 if(obj instanceof ArrayList)
289 {
290 ArrayList arraylist = (ArrayList)map1.get(s2);
291 int k = arraylist.size();
292 for(int l = k; --l > 0;)
293 {
294 String s4 = (String)arraylist.get(l - 1);
295 if(!s4.equals(bestAvailableDefault(s2, s)))
296 {
297 ActiveMultiplexor.getInstance().savePredicate(s2 + '.' + l, s4, s1, s);
298 j++;
299 }
300 }
301
302 } else
303 {
304 throw new DeveloperError("Something other than a String or ArrayList found in predicates!");
305 }
306 }
307
308 try
309 {
310 iterator1.remove();
311 }
312 catch(ConcurrentModificationException concurrentmodificationexception1)
313 {
314 Log.log("Some problem with PredicateMaster design: ConcurrentModificationException in save() [2].", Log.RUNTIME);
315 }
316 }
317
318 }
319 }
320
321 cacheSize -= j;
322 return cacheSize;
323 }
324
325 static void saveAll()
326 {
327 Trace.userinfo("Saving all cached predicates (" + cacheSize + ")");
328 save(cacheSize);
329 }
330
331 private static void checkCache()
332 {
333 if(cacheSize >= CACHE_MAX)
334 {
335 int i = save(cacheSize - cacheMin);
336 if(i < cacheMin)
337 cacheMin = (i + cacheMin) / 2;
338 }
339 }
340
341 private static final int MAX_INDEX = 5;
342 private static final int CACHE_MAX;
343 private static int cacheMin;
344 protected static int cacheSize = 0;
345 private static HashSet listeners = new HashSet();
346 private static final PredicateMaster myself = new PredicateMaster();
347 protected static final String PREDICATE_EMPTY_DEFAULT = Globals.getPredicateEmptyDefault();
348 private static final String EMPTY_STRING = "";
349
350 static
351 {
352 CACHE_MAX = Globals.predicateValueCacheMax();
353 cacheMin = Math.max(CACHE_MAX / 2, 1);
354 }
355 }