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  
24  package zeus.visualiser.society;
25  
26  import java.awt.*;
27  import java.util.*;
28  
29  import zeus.util.*;
30  import zeus.concepts.*;
31  import zeus.actors.*;
32  import zeus.gui.*;
33  import zeus.gui.graph.*;
34  import zeus.generator.agent.AcquaintanceModel;
35  
36  
37  public class AnimationManager {
38     public static final Hashtable MessageColor = new Hashtable();
39  
40     static {
41        int size = AcquaintanceModel.RELATIONS_LIST.size();
42  
43        MessageColor.put("agree", ColorManager.getColor(size++));
44    
45        
46        MessageColor.put("cancel", ColorManager.getColor(size++));
47  
48        
49        MessageColor.put("confirm", ColorManager.getColor(size++));
50  
51        
52        MessageColor.put("disconfirm", ColorManager.getColor(size++));
53  
54        
55        MessageColor.put("failure", ColorManager.getColor(size++));
56  
57        
58        MessageColor.put("inform", ColorManager.getColor(size++));
59  
60        
61        MessageColor.put("inform-if", ColorManager.getColor(size++));
62        
63        MessageColor.put("inform-ref", ColorManager.getColor(size++));
64        
65        MessageColor.put("not-understood", ColorManager.getColor(size++));
66        
67        MessageColor.put("query-if", ColorManager.getColor(size++));
68        
69        
70        MessageColor.put("query-ref", ColorManager.getColor(size++));
71        MessageColor.put("refuse", ColorManager.getColor(size++));
72        MessageColor.put("request", ColorManager.getColor(size++));
73        MessageColor.put("request-when", ColorManager.getColor(size++));
74        MessageColor.put("request-whenever", ColorManager.getColor(size++));
75        MessageColor.put("subscribe", ColorManager.getColor(size++));
76        MessageColor.put("accept-proposal", ColorManager.getColor(size++));
77        MessageColor.put("cfp", ColorManager.getColor(size++));
78        MessageColor.put("reject-proposal", ColorManager.getColor(size++));
79        MessageColor.put("propose", ColorManager.getColor(size++));
80  
81     };
82  
83     protected AnimationQueue queue;
84     protected Hashtable      dynamicColor = new Hashtable();
85     protected AgentContext   context;
86     protected Graph          graph;
87   
88     public AnimationManager(AgentContext context, Graph graph) {
89        this.context = context;
90        this.graph = graph; 
91        queue = new AnimationQueue(graph);
92     }
93   
94     public void terminate()          { queue.terminate(); }
95     public void flushAnimator()      { queue.flush(); }
96     public void setMode(int mode)    { queue.setMode(mode); }
97     public int  getMode()            { return queue.getMode(); }
98     public void setSpeed(long speed) { queue.setSpeed(speed); }
99     public long getSpeed()           { return queue.getSpeed(); }
100  
101  
102    private void debug (String str) { 
103    //     System.out.println("AnimationManager>> " + str); 
104    }
105  
106    public void animate(Performative msg) {
107            debug("animate 1"); 
108       String sender = msg.getSender();
109          debug("animate 2"); 
110       String receiver = msg.getReceiver();
111     debug("animate 3"); 
112       // check for null send/receive times
113       if ( msg.getSendTime() == null )
114          msg.setSendTime(new Time(0));
115             debug("animate 4"); 
116       if ( msg.getReceiveTime() == null )
117          msg.setReceiveTime(new Time(0.05));
118     debug("animate 5"); 
119       if ( !sender.equals(receiver) ) {
120            debug("animate 6"); 
121          SocietyModel model = (SocietyModel)graph.getModel();
122             debug ("model = " + model.toString()); 
123             debug ("sender = " + sender); 
124             debug ("receiver = " + receiver);
125          GraphNode node1 = model.getNode(sender);
126             debug ("node1 = "  + node1.toString()); 
127          GraphNode node2 = model.getNode(receiver);
128             debug ("node2 = " + node2.toString()); 
129          if ( node1 != null && node2 != null &&
130               graph.isVisible(node1) && graph.isVisible(node2) ) {
131                    debug("animate 7"); 
132  	    Color color = selectColor(msg);
133  	    debug ("adding to queue"); 
134  	    queue.add(msg,node1,node2,color);
135          }
136       }
137    }
138  
139    protected Color selectColor(Performative msg) {
140      Color color = null;
141      String type = msg.getType().toLowerCase();
142      String in_reply_to = msg.getInReplyTo();
143      String reply_with = msg.getReplyWith();
144  
145      if ( in_reply_to != null )
146         color = (Color) dynamicColor.get(in_reply_to);
147      if ( color != null ) return color;
148  
149      if ( reply_with != null )
150         color = (Color) dynamicColor.get(reply_with);
151      if ( color != null ) return color;
152  /*
153      if ( type.equals("cfp") || type.equals("propose") ||
154           type.equals("accept-proposal") || type.equals("reject-proposal") )
155         color = selectDynamicColor(msg);*/
156      else {
157         if (MessageColor.containsKey(type)) { 
158             color = (Color) MessageColor.get(type);}
159             else {
160                 color = selectDynamicColor (msg); }
161             
162 
163         if ( reply_with != null )
164            dynamicColor.put(reply_with,color);
165         else if ( in_reply_to != null )
166            dynamicColor.put(in_reply_to,color);
167      }
168      return color;
169    }
170  
171  
172    protected Color selectDynamicColor(Performative msg) {
173       String reply_with = msg.getReplyWith();
174       String in_reply_to = msg.getInReplyTo();
175       String content = msg.getContent();
176  
177       Vector List = ZeusParser.goalList(context.OntologyDb(),content);
178       Goal g = (Goal)List.elementAt(0);
179       String rootId = g.getRootId();
180       Color color = (Color) dynamicColor.get(rootId);
181       if ( color == null ) {
182  	 color = nextColor();
183          dynamicColor.put(rootId,color);
184          dynamicColor.put(reply_with,color);
185       }
186       return color;
187    }
188  
189    protected Color nextColor() {
190      int r = (int) ((Math.random())*256);
191      int b = (int) ((Math.random())*256);
192      int g = (int) ((Math.random())*256);
193      return new Color(r,b,g);
194    }
195 }