Source code: org/alicebot/server/core/Graphmaster.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.io.*;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.util.*;
11 import org.alicebot.server.core.loader.AIMLLoader;
12 import org.alicebot.server.core.loader.AIMLWatcher;
13 import org.alicebot.server.core.logging.Log;
14 import org.alicebot.server.core.node.Nodemapper;
15 import org.alicebot.server.core.node.Nodemaster;
16 import org.alicebot.server.core.parser.AIMLReader;
17 import org.alicebot.server.core.targeting.TargetMaster;
18 import org.alicebot.server.core.util.Match;
19 import org.alicebot.server.core.util.NoMatchException;
20 import org.alicebot.server.core.util.Toolkit;
21 import org.alicebot.server.core.util.Trace;
22
23 // Referenced classes of package org.alicebot.server.core:
24 // Globals, Bots, PredicateMaster, Bot
25
26 public class Graphmaster
27 {
28
29 private Graphmaster()
30 {
31 }
32
33 public static Nodemapper add(String s, String s1, String s2, String s3)
34 {
35 ArrayList arraylist = Toolkit.wordSplit(s);
36 arraylist.add("<that>");
37 arraylist.addAll(Toolkit.wordSplit(s1));
38 arraylist.add("<topic>");
39 arraylist.addAll(Toolkit.wordSplit(s2));
40 arraylist.add("<botid>");
41 arraylist.add(s3);
42 Nodemapper nodemapper = add(arraylist.listIterator(), ROOT);
43 return nodemapper;
44 }
45
46 private static Nodemapper add(ListIterator listiterator, Nodemapper nodemapper)
47 {
48 if(!listiterator.hasNext())
49 {
50 nodemapper.setTop();
51 return nodemapper;
52 }
53 String s = (String)listiterator.next();
54 Object obj;
55 if(nodemapper.containsKey(s))
56 {
57 obj = (Nodemapper)nodemapper.get(s);
58 } else
59 {
60 obj = new Nodemaster();
61 nodemapper.put(s, obj);
62 ((Nodemapper) (obj)).setParent(nodemapper);
63 }
64 patternVocabulary.add(s);
65 return add(listiterator, ((Nodemapper) (obj)));
66 }
67
68 private static void remove(Nodemapper nodemapper)
69 {
70 Nodemapper nodemapper1 = nodemapper.getParent();
71 if(nodemapper1 != null)
72 {
73 nodemapper1.remove(nodemapper);
74 if(nodemapper1.size() == 0)
75 remove(nodemapper1);
76 }
77 nodemapper = null;
78 }
79
80 public static Match match(String s, String s1, String s2, String s3)
81 throws NoMatchException
82 {
83 ArrayList arraylist;
84 if(s.length() > 0)
85 {
86 arraylist = Toolkit.wordSplit(s);
87 } else
88 {
89 arraylist = new ArrayList();
90 arraylist.add("*");
91 }
92 arraylist.add("<that>");
93 if(s1.length() > 0)
94 arraylist.addAll(Toolkit.wordSplit(s1));
95 else
96 arraylist.add("*");
97 arraylist.add("<topic>");
98 if(s2.length() > 0)
99 arraylist.addAll(Toolkit.wordSplit(s2));
100 else
101 arraylist.add("*");
102 arraylist.add("<botid>");
103 arraylist.add(s3);
104 Match match1 = match(ROOT, ROOT, ((List) (arraylist)), "", new StringBuffer(), 0, System.currentTimeMillis() + (long)RESPONSE_TIMEOUT);
105 if(match1 != null)
106 {
107 return match1;
108 } else
109 {
110 Trace.devinfo("Match is null.");
111 throw new NoMatchException(s);
112 }
113 }
114
115 private static Match match(Nodemapper nodemapper, Nodemapper nodemapper1, List list, String s, StringBuffer stringbuffer, int i, long l)
116 {
117 if(System.currentTimeMillis() >= l)
118 return null;
119 if(list.size() < nodemapper.getHeight())
120 return null;
121 if(list.size() == 0)
122 if(nodemapper.containsKey("<template>"))
123 {
124 Match match1 = new Match();
125 match1.setBotID(stringbuffer.toString());
126 match1.setNodemapper(nodemapper);
127 return match1;
128 } else
129 {
130 return null;
131 }
132 String s1 = ((String)list.get(0)).trim();
133 List list1 = list.subList(1, list.size());
134 if(nodemapper.containsKey("_"))
135 {
136 StringBuffer stringbuffer1 = new StringBuffer();
137 synchronized(stringbuffer1)
138 {
139 stringbuffer1.append(stringbuffer);
140 stringbuffer1.append(' ');
141 stringbuffer1.append('_');
142 }
143 Match match2 = match((Nodemapper)nodemapper.get("_"), nodemapper, list1, s1, stringbuffer1, i, l);
144 if(match2 != null)
145 {
146 switch(i)
147 {
148 default:
149 break;
150
151 case 0: // '\0'
152 if(s.length() > 0)
153 match2.pushInputStar(s);
154 break;
155
156 case 1: // '\001'
157 if(s.length() > 0)
158 match2.pushThatStar(s);
159 break;
160
161 case 2: // '\002'
162 if(s.length() > 0)
163 match2.pushTopicStar(s);
164 break;
165 }
166 return match2;
167 }
168 }
169 if(nodemapper.containsKey(s1))
170 if(s1.startsWith("<"))
171 {
172 if(s1.equals("<that>"))
173 i = 1;
174 else
175 if(s1.equals("<topic>"))
176 i = 2;
177 else
178 if(s1.equals("<botid>"))
179 i = 3;
180 Match match3 = match((Nodemapper)nodemapper.get(s1), nodemapper, list1, "", new StringBuffer(), i, l);
181 if(match3 != null)
182 {
183 switch(i)
184 {
185 default:
186 break;
187
188 case 1: // '\001'
189 if(s.length() > 0)
190 match3.pushInputStar(s);
191 match3.setPattern(stringbuffer.toString());
192 break;
193
194 case 2: // '\002'
195 if(s.length() > 0)
196 match3.pushThatStar(s);
197 match3.setThat(stringbuffer.toString());
198 break;
199
200 case 3: // '\003'
201 if(s.length() > 0)
202 match3.pushTopicStar(s);
203 match3.setTopic(stringbuffer.toString());
204 break;
205 }
206 return match3;
207 }
208 } else
209 {
210 StringBuffer stringbuffer2 = new StringBuffer();
211 synchronized(stringbuffer2)
212 {
213 stringbuffer2.append(stringbuffer);
214 stringbuffer2.append(' ');
215 stringbuffer2.append(s1);
216 }
217 Match match4 = match((Nodemapper)nodemapper.get(s1), nodemapper, list1, s, stringbuffer2, i, l);
218 if(match4 != null)
219 return match4;
220 }
221 if(nodemapper.containsKey("*"))
222 {
223 StringBuffer stringbuffer3 = new StringBuffer();
224 synchronized(stringbuffer3)
225 {
226 stringbuffer3.append(stringbuffer);
227 stringbuffer3.append(' ');
228 stringbuffer3.append('*');
229 }
230 Match match5 = match((Nodemapper)nodemapper.get("*"), nodemapper, list1, s1, stringbuffer3, i, l);
231 if(match5 != null)
232 {
233 switch(i)
234 {
235 default:
236 break;
237
238 case 0: // '\0'
239 if(s.length() > 0)
240 match5.pushInputStar(s);
241 break;
242
243 case 1: // '\001'
244 if(s.length() > 0)
245 match5.pushThatStar(s);
246 break;
247
248 case 2: // '\002'
249 if(s.length() > 0)
250 match5.pushTopicStar(s);
251 break;
252 }
253 return match5;
254 }
255 }
256 if(nodemapper.equals(nodemapper1.get("*")) || nodemapper.equals(nodemapper1.get("_")))
257 return match(nodemapper, nodemapper1, list1, s + " " + s1, stringbuffer, i, l);
258 else
259 return null;
260 }
261
262 public static void markReady()
263 {
264 loadtime = false;
265 if(Globals.showConsole())
266 Log.userinfo(Bots.getCount() + " bots thinking with " + TOTAL_CATEGORIES + " categories.", Log.STARTUP);
267 Trace.insist(COPYRIGHT);
268 Log.userinfo("Charliebot version 4.1.8 Build [00]", Log.STARTUP);
269 }
270
271 public static void shutdown()
272 {
273 PredicateMaster.saveAll();
274 }
275
276 public static void activatedNode(Nodemapper nodemapper)
277 {
278 ACTIVATED_NODES.add(nodemapper);
279 }
280
281 public static void checkpoint()
282 {
283 Log.log("Targeting checkpoint.", Log.TARGETING);
284 for(Iterator iterator = ACTIVATED_NODES.iterator(); iterator.hasNext();)
285 {
286 Nodemapper nodemapper = (Nodemapper)iterator.next();
287 Set set = (Set)nodemapper.get("<activations>");
288 for(Iterator iterator1 = set.iterator(); iterator1.hasNext(); iterator1.remove())
289 {
290 String s = (String)iterator1.next();
291 StringTokenizer stringtokenizer = new StringTokenizer(s, ":");
292 String s1 = stringtokenizer.nextToken().trim();
293 String s2 = stringtokenizer.nextToken().trim();
294 String s3 = stringtokenizer.nextToken().trim();
295 stringtokenizer.nextToken().trim();
296 String s4 = (String)nodemapper.get("<template>");
297 String s5 = stringtokenizer.nextToken().trim();
298 String s6 = stringtokenizer.nextToken().trim();
299 String s7 = stringtokenizer.nextToken().trim();
300 stringtokenizer.nextToken().trim();
301 String s8 = stringtokenizer.nextToken().trim();
302 TargetMaster.add(s1, s2, s3, s4, s5, s6, s7, s8);
303 }
304
305 }
306
307 }
308
309 public static void load(String s, String s1)
310 {
311 if(s.length() < 1)
312 Log.userinfo("Cannot open a file whose name has zero length.", Log.ERROR);
313 Bot bot = null;
314 boolean flag = true;
315 if(s.equals(Globals.getStartupFilePath()))
316 {
317 if(startupLoaded)
318 {
319 Log.userinfo("Cannot reload startup file.", Log.ERROR);
320 } else
321 {
322 startupLoaded = true;
323 Log.userinfo("Starting up with \"" + s + "\".", Log.STARTUP);
324 }
325 } else
326 {
327 bot = Bots.getBot(s1);
328 }
329 BufferedReader bufferedreader = null;
330 if(s.indexOf("://") != -1)
331 {
332 URL url = null;
333 try
334 {
335 url = new URL(s);
336 }
337 catch(MalformedURLException malformedurlexception)
338 {
339 Log.userinfo("Malformed URL: \"" + s + "\"", Log.ERROR);
340 }
341 try
342 {
343 String s2 = Toolkit.getDeclaredXMLEncoding(url.openStream());
344 bufferedreader = new BufferedReader(new InputStreamReader(url.openStream(), s2));
345 }
346 catch(IOException ioexception)
347 {
348 Log.userinfo("I/O error trying to read \"" + s + "\"", Log.ERROR);
349 }
350 if(!loadCheck(url, bot))
351 return;
352 flag = false;
353 } else
354 {
355 if(s.indexOf("*") != -1)
356 {
357 String as[] = null;
358 try
359 {
360 as = Toolkit.glob(s, (String)WORKING_DIRECTORY.peek());
361 }
362 catch(FileNotFoundException filenotfoundexception)
363 {
364 Log.userinfo(filenotfoundexception.getMessage(), Log.ERROR);
365 }
366 if(as != null)
367 {
368 for(int i = as.length; --i >= 0;)
369 load(as[i], s1);
370
371 return;
372 }
373 }
374 File file = new File(s);
375 if(!file.exists())
376 {
377 file = new File(WORKING_DIRECTORY.peek() + File.separator + s);
378 if(file.exists())
379 {
380 s = WORKING_DIRECTORY.peek() + File.separator + s;
381 } else
382 {
383 Trace.userinfo("Couldn't find \"" + s + "\".");
384 return;
385 }
386 }
387 if(file.exists() && !file.isDirectory())
388 {
389 if(!loadCheck(file, bot))
390 return;
391 try
392 {
393 String s3 = Toolkit.getDeclaredXMLEncoding(new FileInputStream(s));
394 bufferedreader = new BufferedReader(new InputStreamReader(new FileInputStream(s), s3));
395 }
396 catch(IOException ioexception1)
397 {
398 Log.userinfo("I/O error trying to read \"" + s + "\"", Log.ERROR);
399 return;
400 }
401 if(Globals.isWatcherActive())
402 AIMLWatcher.addWatchFile(s, s1);
403 WORKING_DIRECTORY.push(file.getParent());
404 } else
405 {
406 if(!file.exists())
407 Log.userinfo("\"" + s + "\" does not exist!", Log.ERROR);
408 if(file.isDirectory())
409 Log.userinfo("\"" + s + "\" is a directory!", Log.ERROR);
410 }
411 }
412 (new AIMLReader(s, bufferedreader, new AIMLLoader(s, s1), Globals.warnAboutDeprecatedTags())).read();
413 if(flag && WORKING_DIRECTORY.size() > 1)
414 WORKING_DIRECTORY.pop();
415 }
416
417 private static boolean loadCheck(Object obj, Bot bot)
418 {
419 if(bot == null)
420 return true;
421 HashMap hashmap = bot.getLoadedFilesMap();
422 if(hashmap.keySet().contains(obj))
423 {
424 if(loadtime)
425 return false;
426 unload(obj, bot);
427 } else
428 {
429 hashmap.put(obj, new HashSet());
430 }
431 return true;
432 }
433
434 public static void unload(Object obj, Bot bot)
435 {
436 HashSet hashset = (HashSet)bot.getLoadedFilesMap().get(obj);
437 for(Iterator iterator = hashset.iterator(); iterator.hasNext();)
438 {
439 remove((Nodemapper)iterator.next());
440 TOTAL_CATEGORIES--;
441 }
442
443 }
444
445 public static int getTotalCategories()
446 {
447 return TOTAL_CATEGORIES;
448 }
449
450 public static int incrementTotalCategories()
451 {
452 return TOTAL_CATEGORIES++;
453 }
454
455 public static int patternVocabularySize()
456 {
457 return patternVocabulary.size();
458 }
459
460 public static String getWorkingDirectory()
461 {
462 return (String)WORKING_DIRECTORY.peek();
463 }
464
465 public static final String COPYRIGHT[] = {
466 "Alicebot (c) 1995-2002 A.L.I.C.E. AI Foundation", "All Rights Reserved.", "This program is free software; you can redistribute it and/or", "modify it under the terms of the GNU General Public License", "as published by the Free Software Foundation; either version 2", "of the License, or (at your option) any later version."
467 };
468 public static final String VERSION = "4.1.8";
469 public static final String BUILD = "00";
470 public static final String TEMPLATE = "<template>";
471 public static final String THAT = "<that>";
472 public static final String TOPIC = "<topic>";
473 public static final String BOTID = "<botid>";
474 public static final String FILENAME = "<filename>";
475 public static final String ASTERISK = "*";
476 public static final String UNDERSCORE = "_";
477 public static final String PATH_SEPARATOR = ":";
478 private static final String EMPTY_STRING = "";
479 private static final String MARKER_START = "<";
480 private static final String MARKER_END = ">";
481 private static final String SPACE = " ";
482 private static final int S_INPUT = 0;
483 private static final int S_THAT = 1;
484 private static final int S_TOPIC = 2;
485 private static final int S_BOTID = 3;
486 private static Nodemapper ROOT = new Nodemaster();
487 private static int TOTAL_CATEGORIES = 0;
488 private static TreeSet patternVocabulary = new TreeSet();
489 private static boolean loadtime = true;
490 private static boolean startupLoaded = false;
491 private static Stack WORKING_DIRECTORY;
492 private static Set ACTIVATED_NODES = new HashSet();
493 public static final String ACTIVATIONS = "<activations>";
494 protected static int RESPONSE_TIMEOUT;
495
496 static
497 {
498 WORKING_DIRECTORY = new Stack();
499 WORKING_DIRECTORY.push(System.getProperty("user.dir"));
500 try
501 {
502 RESPONSE_TIMEOUT = Integer.parseInt(Globals.getProperty("programd.response-timeout", "1000"));
503 }
504 catch(NumberFormatException numberformatexception)
505 {
506 RESPONSE_TIMEOUT = 1000;
507 }
508 }
509 }