View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  /*
23      this is a test of the Sourceforge CVS.
24      Please ignore this comment!
25  */
26  
27  package zeus.visualiser.statistics;
28  
29  import java.util.*;
30  import zeus.util.*;
31  import zeus.concepts.*;
32  import zeus.actors.rtn.Engine;
33  
34  public class TrafficVolume {
35     protected Hashtable  goalTraffic = new Hashtable();
36     protected Hashtable  negotiationTraffic = new Hashtable();
37     protected Hashtable  allTraffic = new Hashtable();
38     protected Hashtable  referenceTable = new Hashtable();
39     protected boolean    updatingGoalTraffic =  true;
40     protected OntologyDb ontology;
41  
42  
43     public TrafficVolume(OntologyDb ontology) {
44        this.ontology = ontology;
45     }
46  
47     public synchronized void clear() {
48        allTraffic.clear();
49        clearGoalTraffic();
50     }
51  
52     public synchronized void clearGoalTraffic() {
53        goalTraffic.clear();
54        negotiationTraffic.clear();
55        referenceTable.clear();
56     }
57  
58     public boolean isUpdatingGoalTraffic() {
59        return updatingGoalTraffic;
60     }
61  
62     public synchronized void setUpdatingGoalTraffic(boolean set) {
63        updatingGoalTraffic = set;
64     }
65  
66     public synchronized void update(Performative msg) {
67        updateTraffic(msg);
68        updateGoalTraffic(msg);
69     }
70  
71     protected void updateTraffic(Performative msg) {
72  /***
73        allTraffic structure:
74  
75        sender --> Hashtable
76                      |
77                   receiver --> int[Performative.MESSAGE_TYPES]
78  */
79        String sender = msg.getSender();
80        String receiver = msg.getReceiver();
81        String type = msg.getType();
82  
83        Hashtable inner = (Hashtable)allTraffic.get(sender);
84        if ( inner == null ) {
85           inner = new Hashtable();
86           allTraffic.put(sender,inner);
87        }
88        int[] data = (int[])inner.get(receiver);
89        if ( data == null ) {
90           data = new int[Performative.MESSAGE_TYPES.length];
91           for(int i = 0; i < data.length; i++ )
92              data[i] = 0;
93           inner.put(receiver,data);
94        }
95        int j = Misc.whichPosition(type,Performative.MESSAGE_TYPES);
96        Core.ERROR(j != -1,1,this);
97        data[j] += 1;
98     }
99  
100    protected void updateGoalTraffic(Performative msg) {
101       if ( !updatingGoalTraffic ) return;
102 
103       MsgContentHandler hd;
104       Goal g;
105       String rootId;
106 
107       String sender = msg.getSender();
108       String receiver = msg.getReceiver();
109       String type = msg.getType();
110       String reply_with = msg.getReplyWith();
111       String in_reply_to = msg.getInReplyTo();
112       String content = msg.getContent();
113       String key = (reply_with != null) ? reply_with : in_reply_to;
114 
115       if ( type.equals("cfp") || type.equals("propose") ||
116            type.equals("accept-proposal") || type.equals("reject-proposal") ) {
117          g = ZeusParser.goal(ontology,content);
118          rootId = g.getRootId();
119          updateGoalTraffic(sender,receiver,type,rootId,key);
120          updateNegotiationTraffic(sender,receiver,type,key,g);
121       }
122       else if ( referenceTable.containsKey(key) ) {
123          if ( type.equals("cancel") || type.equals("failure") ) {
124             g = ZeusParser.goal(ontology,content);
125             rootId = g.getRootId();
126             updateGoalTraffic(sender,receiver,type,rootId,key);
127          }
128          else if ( type.equals("inform") ) {
129             hd = new MsgContentHandler(content);
130             updateGoalTraffic(sender,receiver,hd.tag(),null,key);
131          }
132       }
133    }
134 
135    protected void updateGoalTraffic(String sender, String receiver, String type,
136                                     String rootId, String reply_tag) {
137 /***
138       goalTraffic structure:
139 
140       rootId --> Hashtable
141                     |
142                   sender --> Hashtable
143                                 |
144                              receiver --> int[Engine.COORDINATION_MESSAGE_TYPES]
145 */
146 
147       if ( rootId == null ) {
148          if ( (rootId = (String)referenceTable.get(reply_tag)) == null )
149             rootId = "--UnknownId--";
150       }
151 
152       referenceTable.put(reply_tag,rootId);
153 
154       Hashtable outer = (Hashtable)goalTraffic.get(rootId);
155       if ( outer == null ) {
156          outer = new Hashtable();
157          goalTraffic.put(rootId,outer);
158       }
159       Hashtable  inner = (Hashtable)outer.get(sender);
160       if ( inner == null ) {
161          inner = new Hashtable();
162          outer.put(sender,inner);
163       }
164       int[] data = (int[])inner.get(receiver);
165       if ( data == null ) {
166          data = new int[Engine.COORDINATION_MESSAGE_TYPES.length];
167          for(int i = 0; i < data.length; i++ ) data[i] = 0;
168          inner.put(receiver,data);
169       }
170       int j = Misc.whichPosition(type,Engine.COORDINATION_MESSAGE_TYPES);
171       Core.ERROR(j != -1,2,this);
172       data[j] += 1;
173    }
174    protected void updateNegotiationTraffic(String sender, String receiver,
175       String msg_type, String reply_tag, Goal g) {
176 /***
177       negotiationTraffic structure:
178 
179       rootId --> Hashtable
180                     |
181                goalId[Fact] --> Hashtable
182                                  |
183                            sender receiver --> Hashtable
184                                                    |
185                                                  sender --> Vector
186 */
187       String rootId = g.getRootId();
188       String goalId = g.getId() + "[" + g.getFactType() + "]";
189       double cost = g.getCost();
190 
191       // REM temp hack to avoid zero-value cost in graphs
192       if ( Math.abs(cost) < 1.0E-12 ) return;
193 
194       if ( rootId == null ) {
195          if ( (rootId = (String)referenceTable.get(reply_tag)) == null )
196             rootId = "--UnknownId--";
197       }
198 
199       referenceTable.put(reply_tag,rootId);
200 
201       Hashtable outer = (Hashtable)negotiationTraffic.get(rootId);
202       if ( outer == null ) {
203          outer = new Hashtable();
204          negotiationTraffic.put(rootId,outer);
205       }
206       Hashtable inner = (Hashtable)outer.get(goalId);
207       if ( inner == null ) {
208          inner = new Hashtable();
209          outer.put(goalId,inner);
210       }
211       String Id = sender.compareTo(receiver) > 0 ?
212                   sender + " " + receiver : receiver + " " + sender;
213 
214       Hashtable innermost = (Hashtable)inner.get(Id);
215       if ( innermost == null ) {
216          innermost = new Hashtable();
217          inner.put(Id,innermost);
218       }
219 
220       Vector List = (Vector)innermost.get(sender);
221       if ( List == null ) {
222          List = new Vector();
223          innermost.put(sender,List);
224       }
225       List.addElement(new Double(cost));
226    }
227 
228    public String[] getDistributionByTypeLabels() {
229       return Performative.MESSAGE_TYPES;
230    }
231 
232    public synchronized double[] getDistributionByTypeData() {
233       double result[] = new double[Performative.MESSAGE_TYPES.length];
234       for(int i = 0; i < result.length; i++ ) result[i] = 0.0;
235       Enumeration enum = allTraffic.elements();
236       Hashtable inner;
237       while( enum.hasMoreElements() ) {
238          inner = (Hashtable)enum.nextElement();
239          Enumeration elements = inner.elements();
240          while( elements.hasMoreElements() ) {
241             int[] data = (int[])elements.nextElement();
242             for(int i = 0; i < result.length; i++ )
243                result[i] += data[i];
244          }
245       }
246       return result;
247    }
248 
249    public synchronized String[] getDistributionByAgentLabels() {
250       String[] labels = new String[allTraffic.size()];
251       Enumeration enum = allTraffic.keys();
252       for(int i = 0; i < labels.length; i++ )
253          labels[i] = (String)enum.nextElement();
254       return labels;
255    }
256 
257    public String[] getDistributionByAgentKeys() {
258       return Performative.MESSAGE_TYPES;
259    }
260 
261    public synchronized double[][] getDistributionByAgentData() {
262       if ( allTraffic.isEmpty() ) return null;
263 
264       double[][] result =
265          new double[allTraffic.size()][Performative.MESSAGE_TYPES.length];
266       for(int i = 0; i < result.length; i++ )
267       for(int j = 0; j < result[i].length; j++ )
268          result[i][j] = 0.0;
269 
270       Enumeration enum = allTraffic.elements();
271       Hashtable inner;
272       for(int i = 0; i < result.length; i++ ) {
273          inner = (Hashtable) enum.nextElement();
274          Enumeration elements = inner.elements();
275          int[] data;
276          while( elements.hasMoreElements() ) {
277             data = (int[])elements.nextElement();
278             for(int j = 0; j < result[i].length; j++ )
279                result[i][j] += data[j];
280          }
281       }
282       return result;
283    }
284 
285    public synchronized String[] getCurrentGoals() {
286       String[] goals = new String[goalTraffic.size()];
287       Enumeration enum = goalTraffic.keys();
288       for(int i = 0; i < goals.length; i++ )
289          goals[i] = (String)enum.nextElement();
290       return goals;
291    }
292 
293    public String[] getDistributionByGoalKeys() {
294       return Engine.COORDINATION_MESSAGE_TYPES;
295    }
296 
297    public synchronized String[] getDistributionByGoalLabels(String[] goals) {
298       if ( goals == null || goals.length == 0 ) return null;
299       Vector List = new Vector();
300       for(int i = 0; i < goals.length; i++ ) {
301          Hashtable outer = (Hashtable)goalTraffic.get(goals[i]);
302          if ( outer != null ) {
303             Enumeration enum = outer.keys();
304             while( enum.hasMoreElements() ) {
305                String sender = (String)enum.nextElement();
306                if ( !List.contains(sender) )
307                   List.addElement(sender);
308             }
309          }
310       }
311       return Misc.stringArray(List);
312    }
313 
314    public synchronized Hashtable getNegotiationGoals() {
315       Hashtable output = new Hashtable();
316       String rootId, goalId, Id;
317       Hashtable outer, inner;
318       HSet entry;
319       Enumeration goals, agents;
320       Enumeration enum = negotiationTraffic.keys();
321       while( enum.hasMoreElements() ) {
322          rootId = (String)enum.nextElement();
323 	 outer = (Hashtable)negotiationTraffic.get(rootId);
324          goals = outer.keys();
325          entry = new HSet();
326          while( goals.hasMoreElements() ) {
327             goalId = (String)goals.nextElement();
328             inner = (Hashtable)outer.get(goalId);
329             agents = inner.keys();
330             while( agents.hasMoreElements() ) {
331                Id = (String)agents.nextElement();
332                entry.add(goalId + " " + Id);
333             }
334          }
335          output.put(rootId,entry);
336       }
337       return output;
338    }
339 
340    public synchronized double[][] getDistributionByNegotiationDialogueXData(
341       String[] user_goals) {
342 
343       if ( user_goals == null || user_goals.length == 0 ) return null;
344 
345       /*
346          user_goals[0] : rootId
347          user_goals[1] : goalId[Fact] sender receiver
348       */
349 
350       MsgContentHandler hd = new MsgContentHandler(user_goals[1]);
351       String goalId = hd.tag();
352       String Id = hd.data();
353       String sender = hd.data(0);
354       String receiver = hd.data(1);
355 
356       Hashtable outer = (Hashtable)negotiationTraffic.get(user_goals[0]);
357       Hashtable inner = (Hashtable)outer.get(goalId);
358       Hashtable innermost = (Hashtable)inner.get(Id);
359       Vector senderList = (Vector)innermost.get(sender);
360       Vector receiverList = (Vector)innermost.get(receiver);
361 
362       int senderSize = (senderList != null) ? senderList.size() : 0;
363       int receiverSize = (receiverList != null) ? receiverList.size() : 0;
364       int num = Math.max(senderSize,receiverSize);
365 
366       if ( num == 0 ) return null;
367 
368       double[][] output = new double[2][num];
369       for(int i = 0; i < output.length; i++ )
370       for(int j = 0; j < output[i].length; j++ )
371          output[i][j] = (double)j;
372       return output;
373    }
374 
375    public synchronized String[] getDistributionByNegotiationDialogueKeys(
376       String[] user_goals) {
377       if ( user_goals == null || user_goals.length == 0 )
378          return null;
379 
380       /*
381          user_goals[0] : rootId
382          user_goals[1] : goalId[Fact] sender receiver
383       */
384 
385       MsgContentHandler hd = new MsgContentHandler(user_goals[1]);
386       String sender = hd.data(0);
387       String receiver = hd.data(1);
388 
389       String[] output = new String[2];
390       output[0] = sender;
391       output[1] = receiver;
392       return output;
393    }
394 
395    public synchronized double[][] getDistributionByNegotiationDialogueData(
396       String[] goals) {
397 
398       if ( goals == null || goals.length == 0 ) return null;
399 
400       MsgContentHandler hd = new MsgContentHandler(goals[1]);
401       String goalId = hd.tag();
402       String Id = hd.data();
403       String sender = hd.data(0);
404       String receiver = hd.data(1);
405 
406       Hashtable outer = (Hashtable)negotiationTraffic.get(goals[0]);
407       Hashtable inner = (Hashtable)outer.get(goalId);
408       Hashtable innermost = (Hashtable)inner.get(Id);
409       Vector senderList = (Vector)innermost.get(sender);
410       Vector receiverList = (Vector)innermost.get(receiver);
411 
412       double[][] result = new double[2][];
413       Double value;
414 
415       int senderSize = (senderList != null) ? senderList.size() : 0;
416       result[0] = new double[senderSize];
417       for(int i = 0; senderList != null && i < senderSize; i++ ) {
418          value = (Double)senderList.elementAt(i);
419          result[0][i] = value.doubleValue();
420       }
421 
422       int receiverSize = (receiverList != null) ? receiverList.size() : 0;
423       result[1] = new double[receiverSize];
424       for(int i = 0; receiverList != null && i < receiverSize; i++ ) {
425          value = (Double)receiverList.elementAt(i);
426          result[1][i] = value.doubleValue();
427       }
428       return result;
429    }
430 
431    public synchronized double[][] getDistributionByGoalData(String[] goals) {
432       if ( goals == null || goals.length == 0 ) return null;
433       String[] senders = getDistributionByGoalLabels(goals);
434       if ( senders == null || senders.length == 0 ) return null;
435 
436       double[][] result = new double[senders.length][Engine.COORDINATION_MESSAGE_TYPES.length];
437       for(int i = 0; i < result.length; i++ )
438       for(int j = 0; j < result[i].length; j++ )
439          result[i][j] = 0.0;
440 
441       Hashtable outer, inner;
442       int[] data;
443       for(int i = 0; i < goals.length; i++ ) {
444          outer = (Hashtable)goalTraffic.get(goals[i]);
445          if ( outer != null ) {
446             for(int j = 0; j < senders.length; j++ ) {
447                inner = (Hashtable)outer.get(senders[j]);
448                if ( inner != null ) {
449                   Enumeration enum = inner.elements();
450                   while( enum.hasMoreElements() ) {
451                      data = (int[])enum.nextElement();
452                      for(int k = 0; k < data.length; k++ )
453                         result[j][k] += data[k];
454                   }
455                }
456             }
457          }
458       }
459       return result;
460    }
461    public synchronized String[] getInterAgentTrafficLabels() {
462       String[] labels = new String[allTraffic.size()];
463       Enumeration enum = allTraffic.keys();
464       for(int i = 0; i < labels.length; i++ )
465          labels[i] = (String)enum.nextElement();
466       return labels;
467    }
468    public synchronized double[][] getInterAgentTrafficData() {
469       if ( allTraffic.isEmpty() ) return null;
470 
471       String[] labels = getInterAgentTrafficLabels();
472       double[][] result = new double[labels.length][labels.length];
473       for(int i = 0; i < result.length; i++ )
474       for(int j = 0; j < result[i].length; j++ )
475          result[i][j] = 0.0;
476 
477       Hashtable inner;
478       int[] data;
479       int sum;
480       for(int i = 0; i < result.length; i++ ) {
481          inner = (Hashtable) allTraffic.get(labels[i]);
482          for(int j = 0; j < result[i].length; j++ ) {
483             sum = 0;
484             data = (int[])inner.get(labels[j]);
485             if ( data != null ) {
486                for(int k = 0; k < data.length; k++ )
487                   sum += data[k];
488             }
489             result[i][j] = sum;
490          }
491       }
492       return result;
493    }
494 
495 }