Source code: org/alicebot/server/core/Multiplexor.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.util.*;
9 import org.alicebot.server.core.logging.Log;
10 import org.alicebot.server.core.node.Nodemapper;
11 import org.alicebot.server.core.parser.TemplateParser;
12 import org.alicebot.server.core.parser.TemplateParserException;
13 import org.alicebot.server.core.processor.ProcessorException;
14 import org.alicebot.server.core.responder.Responder;
15 import org.alicebot.server.core.util.DeveloperError;
16 import org.alicebot.server.core.util.IAmAlivePulse;
17 import org.alicebot.server.core.util.InputNormalizer;
18 import org.alicebot.server.core.util.Match;
19 import org.alicebot.server.core.util.NoMatchException;
20 import org.alicebot.server.core.util.Pulse;
21 import org.alicebot.server.core.util.Toolkit;
22 import org.alicebot.server.core.util.Trace;
23 import org.alicebot.server.core.util.UserError;
24
25 // Referenced classes of package org.alicebot.server.core:
26 // NoSuchPredicateException, ActiveMultiplexor, Bots, Bot,
27 // PredicateMaster, Graphmaster, Globals
28
29 public abstract class Multiplexor
30 {
31
32 public Multiplexor()
33 {
34 }
35
36 public void initialize()
37 {
38 SECRET_KEY = (new Double(Math.random() * (double)System.currentTimeMillis())).toString();
39 File file = new File("secret.key");
40 file.delete();
41 try
42 {
43 file.createNewFile();
44 }
45 catch(IOException ioexception)
46 {
47 throw new UserError("Error creating secret key file.");
48 }
49 PrintWriter printwriter;
50 try
51 {
52 printwriter = new PrintWriter(new FileOutputStream(file));
53 }
54 catch(FileNotFoundException filenotfoundexception)
55 {
56 throw new UserError("Error writing secret key file.");
57 }
58 printwriter.print(SECRET_KEY);
59 printwriter.flush();
60 printwriter.close();
61 proxy = ActiveMultiplexor.getInstance();
62 addPulse(new IAmAlivePulse());
63 }
64
65 public static synchronized String getResponse(String s, String s1, String s2, Responder responder)
66 {
67 Bot bot = Bots.getBot(s2);
68 ArrayList arraylist = bot.sentenceSplit(bot.applyInputSubstitutions(responder.preprocess(s, HOST_NAME)));
69 Iterator iterator = getReplies(arraylist, s1, s2).iterator();
70 String s3 = "";
71 for(Iterator iterator1 = arraylist.iterator(); iterator1.hasNext();)
72 s3 = responder.append((String)iterator1.next(), (String)iterator.next(), s3);
73
74 responder.log(s, s3, HOST_NAME, s1, s2);
75 s3 = responder.postprocess(s3);
76 return s3;
77 }
78
79 public static String getInternalResponse(String s, String s1, String s2, TemplateParser templateparser)
80 {
81 Bot bot = Bots.getBot(s2);
82 ArrayList arraylist = bot.sentenceSplit(PredicateMaster.get("that", 1, s1, s2));
83 String s3 = InputNormalizer.patternFitIgnoreCase((String)arraylist.get(arraylist.size() - 1));
84 if(s3.equals("") || s3.equals(PREDICATE_EMPTY_DEFAULT))
85 s3 = "*";
86 String s4 = InputNormalizer.patternFitIgnoreCase(PredicateMaster.get("topic", s1, s2));
87 if(s4.equals("") || s4.equals(PREDICATE_EMPTY_DEFAULT))
88 s4 = "*";
89 return getMatchResult(s, s3, s4, s1, s2, templateparser);
90 }
91
92 private static ArrayList getReplies(ArrayList arraylist, String s, String s1)
93 {
94 ArrayList arraylist1 = new ArrayList(arraylist.size());
95 Bot bot = Bots.getBot(s1);
96 ArrayList arraylist2 = bot.sentenceSplit(PredicateMaster.get("that", 1, s, s1));
97 String s2 = InputNormalizer.patternFitIgnoreCase((String)arraylist2.get(arraylist2.size() - 1));
98 if(s2.equals("") || s2.equals(PREDICATE_EMPTY_DEFAULT))
99 s2 = "*";
100 String s3 = InputNormalizer.patternFitIgnoreCase(PredicateMaster.get("topic", s, s1));
101 if(s3.equals("") || s3.equals(PREDICATE_EMPTY_DEFAULT))
102 s3 = "*";
103 Iterator iterator = arraylist.iterator();
104 long l = 0L;
105 if(SHOW_MATCH_TRACE)
106 l = System.currentTimeMillis();
107 for(; iterator.hasNext(); arraylist1.add(getReply((String)iterator.next(), s2, s3, s, s1)));
108 responseCount++;
109 if(SHOW_MATCH_TRACE)
110 {
111 l = System.currentTimeMillis() - l;
112 totalTime += l;
113 avgResponseTime = (float)totalTime / (float)responseCount;
114 Trace.userinfo("Response " + responseCount + " in " + l + " ms. (Average: " + avgResponseTime + " ms.)");
115 }
116 if(responseCount % (long)TARGET_SKIP == 0L && USE_TARGETING)
117 Graphmaster.checkpoint();
118 if(arraylist1.size() == 0)
119 arraylist1.add("");
120 return arraylist1;
121 }
122
123 private static String getReply(String s, String s1, String s2, String s3, String s4)
124 {
125 PredicateMaster.push("input", s, s3, s4);
126 TemplateParser templateparser;
127 try
128 {
129 templateparser = new TemplateParser(s, s3, s4);
130 }
131 catch(TemplateParserException templateparserexception)
132 {
133 throw new DeveloperError(templateparserexception);
134 }
135 String s5 = null;
136 try
137 {
138 s5 = getMatchResult(s, s1, s2, s3, s4, templateparser);
139 }
140 catch(DeveloperError developererror)
141 {
142 Log.devfail(developererror);
143 Log.devfail("Exiting due to developer error.", Log.ERROR);
144 System.exit(1);
145 }
146 catch(UserError usererror)
147 {
148 Log.userfail(usererror);
149 Log.devfail("Exiting due to user error.", Log.ERROR);
150 System.exit(1);
151 }
152 catch(RuntimeException runtimeexception)
153 {
154 Log.devfail(runtimeexception);
155 Log.devfail("Exiting due to unforeseen runtime exception.", Log.ERROR);
156 System.exit(1);
157 }
158 if(s5 == null)
159 {
160 Log.devfail("getMatchReply generated a null reply!", Log.ERROR);
161 System.exit(1);
162 }
163 PredicateMaster.push("that", s5, s3, s4);
164 return Toolkit.filterWhitespace(s5);
165 }
166
167 private static String getMatchResult(String s, String s1, String s2, String s3, String s4, TemplateParser templateparser)
168 {
169 if(SHOW_MATCH_TRACE)
170 Trace.userinfo(PredicateMaster.get(Globals.getClientNamePredicate(), s3, s4) + '>' + " " + s + " " + ":" + " " + s1 + " " + ":" + " " + s2 + " " + ":" + " " + s4);
171 String s5 = InputNormalizer.patternFitIgnoreCase(s);
172 Match match = null;
173 try
174 {
175 match = Graphmaster.match(InputNormalizer.patternFitIgnoreCase(s), s1, s2, s4);
176 }
177 catch(NoMatchException nomatchexception)
178 {
179 Log.userinfo(nomatchexception.getMessage(), Log.CHAT);
180 return "";
181 }
182 if(match == null)
183 {
184 Log.userinfo("No match found for input \"" + s + "\".", Log.CHAT);
185 return "";
186 }
187 if(SHOW_MATCH_TRACE)
188 {
189 Trace.userinfo("Match: " + match.getPath());
190 Trace.userinfo("Filename: \"" + match.getFileName() + "\"");
191 }
192 ArrayList arraylist = match.getInputStars();
193 if(arraylist.size() > 0)
194 templateparser.setInputStars(arraylist);
195 arraylist = match.getThatStars();
196 if(arraylist.size() > 0)
197 templateparser.setThatStars(arraylist);
198 arraylist = match.getTopicStars();
199 if(arraylist.size() > 0)
200 templateparser.setTopicStars(arraylist);
201 String s6 = match.getTemplate();
202 String s7 = null;
203 try
204 {
205 s7 = templateparser.processResponse(s6);
206 }
207 catch(ProcessorException processorexception)
208 {
209 Log.userinfo(processorexception.getMessage(), Log.ERROR);
210 return "";
211 }
212 if(USE_TARGETING)
213 {
214 Nodemapper nodemapper = match.getNodemapper();
215 if(nodemapper == null)
216 {
217 Trace.devinfo("Match nodemapper is null!");
218 } else
219 {
220 Object obj = (Set)nodemapper.get("<activations>");
221 if(obj == null)
222 obj = new HashSet();
223 String s8 = match.getPath() + " " + ":" + " " + s5 + " " + ":" + " " + s1 + " " + ":" + " " + s2 + " " + ":" + " " + s4 + " " + ":" + " " + s7;
224 if(!((Set) (obj)).contains(s8))
225 {
226 ((Set) (obj)).add(s8);
227 match.getNodemapper().put("<activations>", obj);
228 Graphmaster.activatedNode(match.getNodemapper());
229 }
230 }
231 }
232 return s7;
233 }
234
235 public float averageResponseTime()
236 {
237 return avgResponseTime;
238 }
239
240 public float queriesPerHour()
241 {
242 return (float)responseCount / ((float)(System.currentTimeMillis() - startTime) / 3600000F);
243 }
244
245 public static void addPulse(Pulse pulse1)
246 {
247 pulses.add(pulse1);
248 }
249
250 public static synchronized void pulse()
251 {
252 for(Iterator iterator = pulses.iterator(); iterator.hasNext(); ((Pulse)iterator.next()).emit());
253 }
254
255 public abstract void savePredicate(String s, String s1, String s2, String s3);
256
257 public abstract String loadPredicate(String s, String s1, String s2)
258 throws NoSuchPredicateException;
259
260 public abstract boolean checkUser(String s, String s1, String s2, String s3);
261
262 public abstract boolean createUser(String s, String s1, String s2, String s3);
263
264 public abstract boolean changePassword(String s, String s1, String s2, String s3);
265
266 public abstract int useridCount(String s);
267
268 protected static final String THAT = "that";
269 protected static final String TOPIC = "topic";
270 protected static final String INPUT = "input";
271 protected static final String STAR = "star";
272 protected static final String EMPTY_STRING = "";
273 protected static final String SPACE = " ";
274 protected static final String VALUE = "value";
275 protected static final String ASTERISK = "*";
276 protected static final String QUOTE_MARK = "\"";
277 private static final String LABEL_MATCH = "Match: ";
278 private static final String LABEL_FILENAME = "Filename: ";
279 private static final String RESPONSE_SPACE = "Response ";
280 private static final String SPACE_IN_SPACE = " in ";
281 private static final String MS_AVERAGE = " ms. (Average: ";
282 private static final String MS = " ms.)";
283 protected static final String HOST_NAME = Globals.getHostName();
284 protected static final boolean SHOW_CONSOLE = Globals.showConsole();
285 protected static final boolean SHOW_MATCH_TRACE = Globals.showMatchTrace();
286 protected static final boolean USE_TARGETING = Globals.useTargeting();
287 private static final int TARGET_SKIP = Globals.getTargetSkip();
288 protected static final String PREDICATE_EMPTY_DEFAULT = Globals.getPredicateEmptyDefault();
289 protected static String SECRET_KEY;
290 protected static long startTime = System.currentTimeMillis();
291 protected static long responseCount = 0L;
292 protected static long totalTime = 0L;
293 protected static float avgResponseTime = 0.0F;
294 private static Multiplexor proxy;
295 private static ArrayList pulses = new ArrayList();
296
297 }