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  package zeus.rete.action;
24  import zeus.rete.*;
25  import zeus.util.*; 
26  import zeus.concepts.*;
27  import zeus.concepts.fn.*;
28  import java.util.*;
29  import java.io.*;
30  
31  
32  /*** 
33      
34      @author Simon Thompson
35      @since 1.1
36      */
37  public class  MessageAction extends ReteAction{
38      
39      /***
40          Send a message
41          */
42      public void executeAction (Action a, Info info) { 
43              //  System.out.println("1"); 
44                ValueFunction msg_type = (ValueFunction)a.table.get("type");
45                if ( msg_type == null ) {
46                   Core.USER_ERROR("Type not specified in Rete action send_message: " + a);
47                   return; 
48                }
49                //System.out.println("2"); 
50                
51                ValueFunction receiver = (ValueFunction)a.table.get("receiver");
52                if ( receiver == null ) {
53                   Core.USER_ERROR("Receiver not specified in Rete action send_message: " + a);
54                   return; 
55                }
56                //System.out.println("3"); 
57                
58                ValueFunction content = (ValueFunction)a.table.get("content");
59                if ( receiver == null ) {
60                   Core.USER_ERROR("Content not specified in Rete action send_message: " + a);
61                   return; 
62                }
63                //System.out.println("4"); 
64                
65  
66                msg_type = msg_type.resolve(info.getBindings());
67                Performative msg = new Performative(msg_type.toString());
68                Enumeration enum = a.table.keys();
69                //System.out.println("5");
70                
71                while( enum.hasMoreElements() ) {
72                   String attribute = (String)enum.nextElement();
73                   ValueFunction value = (ValueFunction)a.table.get(attribute);
74  
75                   ValueFunction type_var = value.resolve(info.getBindings());
76                   boolean found = false;
77                   Fact f1 = null;
78                   if (type_var.getID() == ValueFunction.TYPE) {
79                      for(int k = 0; !found && k < info.getInput().size(); k++ ) {
80                         f1 = (Fact)info.getInput().elementAt(k);
81                         found = (f1.functor()).equals(type_var);
82                      }
83                   }
84                //System.out.println("6"); 
85                   
86                   Object result = null; 
87                   if (found) 
88                          result = (Object) f1;
89                      else 
90                          result = (Object) type_var; 
91                //System.out.println("7"); 
92                          
93                   /*** 
94                      old version 
95                   Object result = found ? (Object)f1 : (Object)type_var;
96                   */
97                   if ( attribute.equals("content") )
98  		            msg.setAttribute(attribute,"data " + result);
99                   else
100 		            msg.setAttribute(attribute,result.toString());
101               }
102               //System.out.println("8"); 
103               
104               Core.DEBUG(2," ==> " + msg);
105               if ( context != null && context.MailBox() != null )
106 		        context.MailBox().sendMsg(msg);
107               //System.out.println("9"); 
108               //System.out.println(msg.toString());
109               
110 		        
111     }
112     
113     
114 }