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.generator.code;
25  
26  import java.util.*;
27  import javax.swing.JTextArea;
28  import java.io.*;
29  
30  import zeus.concepts.*;
31  import zeus.util.*;
32  import zeus.generator.GeneratorModel;
33  
34  /*** 
35   *  TaskWriter is used to emit the code for either a primitive task or a rule based
36   * task. Agents are then equiped with this task code. 
37   * Since 1.3 there is an addition of a service description field, which is used to 
38   * deploy a service description in the Agentcities (or other) infrastructure. 
39   *@see zeus.ontology.services.DAML_S_Service; 
40   */
41  public class TaskWriter extends Writer {
42      public static final String CH = "_";
43      
44      public TaskWriter(GenerationPlan genplan, GeneratorModel genmodel,
45      String directory, JTextArea textArea) {
46          super(genplan,genmodel,directory,textArea);
47      }
48      
49      public void write() {
50          TaskInfo[] taskInfo = genplan.getSelectedTasks();
51          if ( taskInfo.length == 0 ) return;
52          textArea.append("\n*** Task code generation started ***\n\n");
53          
54          for(int i = 0; i < taskInfo.length; i++ ) {
55              AbstractTask task = genmodel.getTask(taskInfo[i].id);
56              String name = genmodel.getTaskName(taskInfo[i].id);
57              textArea.append("Generating code for task " + name + "\n");
58              if ( !task.isValid() )
59                  textArea.append("Task " + name + "is improperly specified\n");
60              try {
61                  switch( task.getType() ) {
62                      case AbstractTask.PRIMITIVE:
63                          if (!taskInfo[i].task_external.equals( "null")) {
64                              writePrimitiveTask(name,taskInfo[i].task_external,
65                              (PrimitiveTask)task);}
66                          else {
67                              writePrimitiveTask (name,(PrimitiveTask)task); }
68                          break;
69                      case AbstractTask.BEHAVIOUR:
70                          if (!taskInfo[i].task_external.equals( "null")) {
71                              System.out.println("Externals ignored for Rulebased tasks...");
72                              System.out.println("Continuing generation of rulebase task :" + name);
73                              writeReteKB(name,(ReteKB)task);}
74                          else {
75                              writeReteKB (name,(ReteKB) task); }
76                          break;
77                  }
78                  taskInfo[i].status = GenerationPlan.NO_SAVE_NEEDED;
79                  taskInfo[i].generate = false;
80              }
81              catch(IOException e) {
82                  System.err.println(e);
83              }
84          }
85          textArea.append("\n*** Task code generation completed ***\n");
86      }
87      
88      protected void writeReteKB(String name, ReteKB task)
89      throws IOException {
90          PrintWriter out = createFile(name + ".clp");
91          out.println(standard_disclaimer);
92          String old_name = task.getName();
93          task.setName(name);
94          out.println(task.pprint());
95          task.setName(old_name);
96          out.flush();
97          out.close();
98      }
99      
100     protected void writePrimitiveTask(String name, PrimitiveTask task)
101     throws IOException {
102         PrintWriter out = createFile(name + ".java");
103         out.println(standard_disclaimer);
104         out.println("/*");
105         out.println("This stub file was automatically generated by " +
106         "ZeusAgentGenerator version " +
107         SystemProps.getProperty("version.id"));
108         out.println("*/\n");
109         out.println("import java.util.*;");
110         out.println("import zeus.util.*;");
111         out.println("import zeus.concepts.*;");
112         out.println("import zeus.actors.TaskContext;");
113         out.println("import zeus.actors.ZeusTask;");
114         out.println();
115         out.println("public class " + name + " extends ZeusTask {");
116         out.println("   protected void exec() {");
117         out.println("      /*");
118         out.println("      Add the task execution code here. " +
119         "The following variables are defined:");
120         out.println("         protected Fact[][]  inputArgs; ");
121         out.println("         protected Fact[][]  outputArgs; ");
122         out.println("      Before exec() is called inputArgs will " +
123         "contain the input");
124         out.println("      Facts consumed by the task. After execution, " +
125         "set outputArgs to ");
126         out.println("      contain the output Facts produced by the task.");
127         out.println("      */");
128         out.println();
129         out.println("      // The Input Facts:");
130         out.println();
131         
132         Fact[] input = task.getPreconditions();
133         for(int k = 0; k < input.length; k++ )
134             out.println("      Fact[] " + CH + input[k].ID() + " = inputArgs[" +
135             k + "];\t// " + input[k].getType());
136         
137         out.println();
138         out.println("      // The Output Facts:");
139         out.println();
140         
141         Fact[] output = task.getPostconditions();
142         for(int k = 0; k < output.length; k++ )
143             out.println("      Fact[] " + CH + output[k].ID() + ";\t// " +
144             output[k].getType());
145         out.println();
146         out.println("      /* USER CODE STARTS */");
147         out.println();
148         out.println("      System.out.println(\"-Expected Input-\");");
149         out.println("      for(int i = 0; i < expInputArgs.length; i++ )");
150         out.println("         System.out.println(expInputArgs[i].pprint());");
151         out.println();
152         out.println("      System.out.println(\"-Input-\");");
153         out.println("      for(int j = 0; j < inputArgs.length; j++) {");
154         out.println("         System.out.println(\"Input Fact[\"+j+\"]\");");
155         out.println("         for(int i = 0; i < inputArgs[j].length; i++)");
156         out.println("            System.out.println(inputArgs[j][i].pprint());");
157         out.println("      }");
158         out.println();
159         out.println("      System.out.println(\"-Expected Output-\");");
160         out.println("      for(int i = 0; i < expOutputArgs.length; i++ )");
161         out.println("         System.out.println(expOutputArgs[i].pprint());");
162         out.println();
163         out.println("      System.out.println(\"-Output-\");");
164         
165         for(int k = 0; k < output.length; k++ ) {
166             out.println("      " + CH + output[k].ID() + " = new Fact[1];");
167             out.println("      " + CH + output[k].ID() + "[0] = new Fact(Fact.FACT,expOutputArgs[" + k + "]);");
168             out.println("      System.out.println(" + CH + output[k].ID() + "[0].pprint());");
169         }
170         out.println();
171         out.println("      /* USER CODE ENDS */");
172         out.println("      outputArgs = new Fact[" + output.length + "][];");
173         for(int k = 0; k < output.length; k++ )
174             out.println("      outputArgs[" + k + "] = " + CH + output[k].ID() + ";");
175         out.println("   }");
176         out.println("}");
177         out.flush();
178         out.close();
179     }
180     
181     
182     protected void writePrimitiveTask(String name, String task_external,
183 				      PrimitiveTask task) throws IOException {
184 
185         PrintWriter out = createFile(name + ".java");
186         out.println(standard_disclaimer);
187         out.println("/*");
188         out.println("This stub file was automatically generated by " +
189         "ZeusAgentGenerator version " +
190         SystemProps.getProperty("version.id"));
191         out.println("*/\n");
192         out.println(" /* Do not edit this class, it has a TaskExternal : " + task_external);
193         out.println (" which it will call when it is executed. Add the code that you want ");
194         out.println (" to be executed when this task is run there instead. */");
195         out.println ();
196         out.println("import java.util.*;");
197         out.println("import zeus.util.*;");
198         out.println("import zeus.concepts.*;");
199         out.println("import zeus.actors.ZeusTask;");
200         out.println("import zeus.actors.TaskContext;");
201 	out.println("import zeus.actors.TaskExternal;");
202 	out.println("//import zeus.ontology.service.*; not in open source version");
203         
204         out.println();
205         out.println("public class " + name + " extends ZeusTask {\n");
206 
207 	out.println("   protected TaskExternal taskExternal;\n");
208 
209         out.println("   protected void exec() {");
210         out.println();
211         out.println("      // The Input Facts:");
212         out.println();
213         
214         Fact[] input = task.getPreconditions();
215         for(int k = 0; k < input.length; k++ )
216             out.println("      Fact[] " + CH + input[k].ID() + " = inputArgs[" +
217             k + "];\t// " + input[k].getType());
218         
219         out.println();
220         out.println("      // The Output Facts:");
221         out.println();
222         
223         Fact[] output = task.getPostconditions();
224         for(int k = 0; k < output.length; k++ )
225             out.println("      Fact[] " + CH + output[k].ID() + ";\t// " +
226             output[k].getType());
227         
228         out.println("      System.out.println(\"-Expected Input-\");");
229         out.println("      for(int i = 0; i < expInputArgs.length; i++ )");
230         out.println("         System.out.println(expInputArgs[i].pprint());");
231         out.println();
232         out.println("      System.out.println(\"-Input-\");");
233         out.println("      for(int j = 0; j < inputArgs.length; j++) {");
234         out.println("         System.out.println(\"Input Fact[\"+j+\"]\");");
235         out.println("         for(int i = 0; i < inputArgs[j].length; i++)");
236         out.println("            System.out.println(inputArgs[j][i].pprint());");
237         out.println("      }");
238         out.println();
239         out.println("     // calling user defined zeus.concepts.TaskExternal");
240         out.println ("    // first set the TaskContext object up ");
241         out.println("     TaskContext taskContext = new TaskContext(); ");
242         out.println("     taskContext.setAgentContext (this.context);");
243         out.println("     taskContext.setExpInputArgs (expInputArgs); ");
244         out.println("     taskContext.setInputArgs (inputArgs); ");
245         out.println("     taskContext.setExpOutputArgs (expOutputArgs); ");
246         // insertion MS 170101 v1.05
247         out.println();
248         out.println("     outputArgs = new Fact [expOutputArgs.length][];");
249         out.println();
250         out.println("     for (int k=0; k<expOutputArgs.length; k++) {");
251         out.println("         outputArgs[k] = new Fact[1];");
252         out.println("         outputArgs[k][0] = new Fact(Fact.FACT, expOutputArgs[k]);");
253         out.println("     }");
254         out.println();
255         // end of insertion
256         out.println("     taskContext.setOutputArgs (outputArgs); ");
257         
258         out.println("     // now initialise the external object ") ;
259 	out.println("     if(taskExternal == null) {");
260 	out.println("       taskExternal = new " + task_external + "();");
261 	out.println("     }");
262         out.println("     // now call the TaskExternal implementation specified for this task");
263         out.println("     taskExternal.exec(taskContext); ");
264         out.println();
265         
266         out.println("     System.out.println(\"-Expected Output-\");");
267         out.println("     for(int i = 0; i < expOutputArgs.length; i++ )");
268         out.println("        System.out.println(expOutputArgs[i].pprint());");
269         out.println();
270         // inserted MS 160101 v1.05
271         out.println("     outputArgs = taskContext.getOutputArgs();");
272         out.println();
273         // end of insertion
274         out.println("     System.out.println(\"-Output-\");");
275         
276         //following changes by MS 160101 v1.05
277         out.println("     for(int k = 0; k < outputArgs.length; k++ ) ");
278         //         out.println("      " + CH + output[k].ID() + " = new Fact[1];");
279         //         out.println("      " + CH + output[k].ID() + "[0] = new Fact(Fact.FACT,expOutputArgs[" + k + "]);");
280         //  	 out.println("      System.out.println(" + CH + output[k].ID() + "[0].pprint());");
281         out.println("         System.out.println(outputArgs[k][0].pprint());");
282         
283         
284         //      out.println("      outputArgs = new Fact[" + output.length + "][];");
285         //      for(int k = 0; k < output.length; k++ )
286         //         out.println("      outputArgs[" + k + "] = " + CH + output[k].ID() + ";");
287         
288         out.println("   }\n");
289 
290         out.println("}");
291         out.flush();
292         out.close();
293     }
294 }