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.concepts;
25  
26  import java.util.*;
27  import zeus.util.*;
28  import zeus.concepts.fn.*;
29  
30  
31  public class SummaryTask extends Task {
32     protected Vector  nodes = new Vector();
33     protected Vector  links = new Vector();
34     protected boolean autorun = false;
35  
36     public SummaryTask() {
37        type = SUMMARY;
38  
39        TaskNode node;
40        node = new TaskNode(TaskNode.BEGIN);
41        nodes.addElement(node);
42  
43        node = new TaskNode(TaskNode.END);
44        nodes.addElement(node);
45     }
46  
47     public SummaryTask(String name, ValueFunction time, ValueFunction cost,
48                        TaskNode[] nodes, TaskLink[] links,
49                        LogicalFn[] constraints) {
50        type = SUMMARY;
51        setName(name);
52        setTimeFn(time);
53        setCostFn(cost);
54        setNodes(nodes);
55        setLinks(links);
56        setConstraints(constraints);
57     }
58     public SummaryTask(String name, String time, String cost,
59                        TaskNode[] nodes, TaskLink[] links,
60                        LogicalFn[] constraints) {
61        type = SUMMARY;
62        setName(name);
63        setTimeFn(time);
64        setCostFn(cost);
65        setNodes(nodes);
66        setLinks(links);
67        setConstraints(constraints);
68     }
69     public SummaryTask(String name, ValueFunction time, ValueFunction cost,
70                        Vector nodes, Vector links, Vector constraints) {
71        type = SUMMARY;
72        setName(name);
73        setTimeFn(time);
74        setCostFn(cost);
75        setNodes(nodes);
76        setLinks(links);
77        setConstraints(constraints);
78     }
79     public SummaryTask(String name, String time, String cost,
80                        Vector nodes, Vector links, Vector constraints) {
81        type = SUMMARY;
82        setName(name);
83        setTimeFn(time);
84        setCostFn(cost);
85        setNodes(nodes);
86        setLinks(links);
87        setConstraints(constraints);
88     }
89  
90     public SummaryTask(SummaryTask task) {
91        type = SUMMARY;
92        name = task.getName();
93        cost = task.getCostFn();
94        time = task.getTimeFn();
95        setNodes( task.getNodes() );
96        setLinks( task.getLinks() );
97        setConstraints( task.getConstraints() );
98     }
99  
100    public Vector  links()       { return links; }
101    public Vector  constraints() { return constraints; }
102 
103    // Note: for getPreconditions() and getPostconditions() only what
104    // the user considered to be significant pre- and postconditions are
105    // returned - by extracting then from the 'begin' and 'end' nodes
106 
107    public Fact[] getPostconditions() {
108       TaskNode node = getNode(TaskNode.END);
109       return node.getPreconditions();
110    }
111    public Fact[] getPreconditions() {
112       TaskNode node = getNode(TaskNode.BEGIN);
113       return node.getPostconditions();
114    }
115 
116    public TaskNode getNode(String name) {
117       TaskNode node = null;
118       for(int j = 0; j < nodes.size(); j++ ) {
119          node = (TaskNode)nodes.elementAt(j);
120          if ( node.getName().equals(name) )
121             return node;
122       }
123       return null;
124    }
125 
126    public Fact[] allFacts() {
127       TaskNode node;
128       Fact[] out;
129       Vector data = new Vector();
130       for(int i = 0; i < nodes.size(); i++ ) {
131          node = (TaskNode)nodes.elementAt(i);
132          if ( !node.getName().equals(TaskNode.BEGIN) &&
133               !node.getName().equals(TaskNode.END) ) {
134             out = node.getPreconditions();
135             for(int j = 0; j < out.length; j++ )
136                data.addElement(out[j]);
137             if ( !node.isConditionalNode() ) {
138 	       out = node.getPostconditions();
139                for(int j = 0; j < out.length; j++ )
140                   data.addElement(out[j]);
141             }
142          }
143       }
144       out = new Fact[data.size()];
145       for(int i = 0; i < data.size(); i++ )
146          out[i] = new Fact((Fact)data.elementAt(i));
147       data = null; // GC
148       return out;
149    }
150 
151    public boolean applyConstraints(Bindings bindings) {
152       Bindings b = new Bindings(bindings);
153       if ( !super.applyConstraints(b) ) return false;
154 
155       TaskLink link;
156       TaskNode node1, node2;
157       String lhs, rhs;
158       Fact f1, f2;
159       for(int i = 0; i < links.size(); i++ ) {
160          link = (TaskLink)links.elementAt(i);
161          lhs = link.getLeftNode();
162          rhs = link.getRightNode();
163          if ( !lhs.equals(TaskNode.BEGIN) && !rhs.equals(TaskNode.END) ) {
164             node1 = getNode(lhs);
165             node2 = getNode(rhs);
166             f1 = node1.getPostcondition(link.getLeftGroup(),link.getLeftArg());
167             f2 = node2.getPrecondition(link.getRightGroup(),link.getRightArg());
168             if ( !f1.unifiesWith(f2,b) ) return false;
169          }
170       }
171 
172       if ( resolve(b) ) {
173          bindings.set(b);
174          return true;
175       }
176       else
177          return false;
178    }
179 
180    public ResolutionContext getContext() {
181       if ( resolution_context != null ) return resolution_context;
182 
183       resolution_context = new ResolutionContext();
184       TaskNode node;
185       for(int i = 0; i < nodes.size(); i++ ) {
186          node = (TaskNode)nodes.elementAt(i);
187          resolution_context.add(node.produced());
188          resolution_context.add(node.consumed());
189       }
190       return resolution_context;
191    }
192 
193    public boolean resolve(Bindings bindings) {
194       ResolutionContext context = getContext();
195 
196       time = time.resolve(context,bindings);
197       if ( time == null ) return false;
198 
199       cost = cost.resolve(context,bindings);
200       if ( cost == null ) return false;
201 
202       TaskNode node;
203       for(int i = 0; i < nodes.size(); i++ ) {
204          node = (TaskNode)nodes.elementAt(i);
205          if ( !node.resolve(context,bindings) )
206             return false;
207       }
208       return true;
209    }
210 
211    public TaskNode[] getNodes() {
212       TaskNode[] out = new TaskNode[nodes.size()];
213       for(int i = 0; i < nodes.size(); i++ )
214          out[i] = new TaskNode((TaskNode)nodes.elementAt(i));
215       return out;
216    }
217 
218    public void setNodes(Vector List) {
219       nodes.removeAllElements();
220       for(int i = 0; i < List.size(); i++ )
221          nodes.addElement(new TaskNode((TaskNode)List.elementAt(i)));
222    }
223 
224    public void setNodes(TaskNode[] List) {
225       nodes.removeAllElements();
226       for(int i = 0; i < List.length; i++ )
227          nodes.addElement(new TaskNode(List[i]));
228    }
229 
230    public TaskLink[] getLinks() {
231       TaskLink[] out = new TaskLink[links.size()];
232       for(int i = 0; i < links.size(); i++ )
233          out[i] = new TaskLink((TaskLink)links.elementAt(i));
234       return out;
235    }
236 
237    public void setLinks(Vector List) {
238       links.removeAllElements();
239       for(int i = 0; i < List.size(); i++ )
240          links.addElement(new TaskLink((TaskLink)List.elementAt(i)));
241 
242    }
243 
244    public void setLinks(TaskLink[] List) {
245       links.removeAllElements();
246       for(int i = 0; i < List.length; i++ )
247          links.addElement(new TaskLink(List[i]));
248    }
249 
250    public boolean isValid() {
251       return true;
252    }
253 
254    public String toString() {
255       String s = "(:" + TaskTypes[type] + " " + name + " ";
256 
257       s += ":is_autorun " + autorun + " ";
258       s += ":time (" + time + ") ";
259       s += ":cost (" + cost + ") ";
260 
261       if ( !nodes.isEmpty() ) {
262          s += ":nodes (";
263          for(int i = 0; i < nodes.size(); i++ )
264             s += nodes.elementAt(i);
265          s += ") ";
266       }
267       if ( !links.isEmpty() ) {
268          s += ":links (";
269          for(int i = 0; i < links.size(); i++ )
270             s += links.elementAt(i);
271          s += ") ";
272       }
273       if ( !constraints.isEmpty() ) {
274          s += ":constraints (";
275          for(int i = 0; i < constraints.size(); i++ )
276             s += "(" + constraints.elementAt(i) + ")";
277          s += ") ";
278       }
279       return s.trim() + ")";
280    }
281 
282 
283    public String pprint(int sp) {
284       String suffix, prefix;
285       String tabs = Misc.spaces(sp);
286       String eol  = "\n" + tabs + " ";
287 
288       String s = "(:" + TaskTypes[type] + " " + name + eol;
289 
290       s += ":is_autorun " + autorun + eol;
291       s += ":time (" + time + ")" + eol;
292       s += ":cost (" + cost + ")" + eol;
293 
294       if ( !nodes.isEmpty() ) {
295          prefix = ":nodes ";
296          suffix = Misc.spaces(1 + sp + prefix.length());
297          s += prefix + "(";
298          for(int i = 0; i < nodes.size(); i++ )
299             s += ((TaskNode)nodes.elementAt(i)).pprint(1+suffix.length()) +
300                  "\n" + suffix + " ";
301          s = s.trim() + "\n" + suffix + ")" + eol;
302       }
303       if ( !links.isEmpty() ) {
304          prefix = ":links ";
305          suffix = Misc.spaces(1 + sp + prefix.length());
306          s += prefix + "(";
307          for(int i = 0; i < links.size(); i++ )
308             s += ((TaskLink)links.elementAt(i)).pprint(1+suffix.length()) +
309                  "\n" + suffix + " ";
310          s = s.trim() + "\n" + suffix + ")" + eol;
311       }
312       if ( !constraints.isEmpty() ) {
313          prefix = ":constraints ";
314          suffix = Misc.spaces(1 + sp + prefix.length());
315          s += prefix + "(" + "\n" + suffix + " ";
316          for(int i = 0; i < constraints.size(); i++ )
317             s += "(" + ((LogicalFn)constraints.elementAt(i)) + ")" +
318                  "\n" + suffix + " ";
319          s = s.trim() + "\n" + suffix + ")" + eol;
320       }
321       return tabs + s.trim() + "\n" + tabs + ")";
322    }
323 
324    public AbstractTask duplicate(DuplicationTable table) {
325       TaskNode[]  Xnodes = new TaskNode[nodes.size()];
326       TaskLink[]  Xlinks = new TaskLink[links.size()];
327       LogicalFn[] Xconstraints = new LogicalFn[constraints.size()];
328 
329       ValueFunction Xtime = time.duplicate(table);
330       ValueFunction Xcost = cost.duplicate(table);
331 
332       for(int i = 0; i < nodes.size(); i++ )
333          Xnodes[i] = ((TaskNode)nodes.elementAt(i)).duplicate(table);
334 
335       for(int i = 0; i < links.size(); i++ )
336          Xlinks[i] = ((TaskLink)links.elementAt(i)).duplicate(table);
337 
338       for(int i = 0; i < constraints.size(); i++ )
339          Xconstraints[i] = (LogicalFn)((LogicalFn)constraints.elementAt(i)).duplicate(table);
340 
341       return new SummaryTask(name,Xtime,Xcost,Xnodes,Xlinks,Xconstraints);
342    }
343 }