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  
29  
30  public class ReportRec {
31     protected String  name = null;
32     protected String  goal = null;
33     protected String  task = null;
34     protected String  agent = null;
35     protected int     state;
36     protected String  owner = null;
37     protected String  root_id = null;
38     protected int     start_time = 0;
39     protected int     end_time = 0;
40     protected double  cost = 0;
41     protected String  parent = null;
42     protected Vector  children = new Vector();
43     protected Vector  siblings = new Vector();
44     protected Vector  parents = new Vector();
45     protected Vector  consumed = new Vector();
46     protected Vector  produced = new Vector();
47  
48     ReportRec(String name, String goal) {
49        Assert.notNull(name);
50        Assert.notNull(goal);
51        this.name = name;
52        this.goal = goal;
53     }
54     public ReportRec(String name, String goal, String task, String agent,
55                      int state, String owner, String root_id, String parent,
56                      int s, int e, double c, Vector children, Vector siblings,
57                      Vector parents, Fact[] consumed, Fact[] produced) {
58        this(name,goal);
59        setTask(task);
60        setAgent(agent);
61        setState(state);
62        setOwner(owner);
63        setRootId(root_id);
64        setStartTime(s);
65        setEndTime(e);
66        setCost(c);
67        setParent(parent);
68        setChildren(children);
69        setSiblings(siblings);
70        setParents(parents);
71        setPreconditions(consumed);
72        setPostconditions(produced);
73     }
74     public ReportRec(ReportRec rec) {
75        this(rec.getName(),rec.getGoal());
76        setTask(rec.getTask());
77        setAgent(rec.getAgent());
78        setState(rec.getState());
79        setOwner(rec.getOwner());
80        setStartTime(rec.getStartTime());
81        setEndTime(rec.getEndTime());
82        setCost(rec.getCost());
83        setRootId(rec.getRootId());
84        setParent(rec.getParent());
85        setChildren(rec.getChildren());
86        setSiblings(rec.getSiblings());
87        setParents(rec.getParents());
88        setPreconditions(rec.getPreconditions());
89        setPostconditions(rec.getPostconditions());
90     }
91  
92     public boolean hasNoParents() {
93        return parents.isEmpty();
94     }
95     public boolean hasOneParentOnly(String name) {
96        Assert.notNull(name);
97        return (parents.contains(name) && parents.size() == 1);
98     }
99     public void removeParent(String name) {
100       Assert.notNull(name);
101       parents.removeElement(name);
102    }
103    public void setTask(String task) {
104       Assert.notNull(task);
105       this.task = task;
106    }
107    public void setAgent(String agent) {
108       Assert.notNull(agent);
109       this.agent = agent;
110    }
111    public void setState(int state) {
112       this.state = state;
113    }
114    public void setOwner(String owner) {
115       Assert.notNull(owner);
116       this.owner = owner;
117    }
118    public void setParent(String parent) {
119       String prev = this.parent;
120       this.parent = parent;
121       if ( prev != null )
122          parents.removeElement(prev);
123       if ( parent != null )
124          parents.addElement(parent);
125    }
126    public void setRootId(String root) {
127       Assert.notNull(root);
128       root_id = root;
129    }
130    public void setSiblings(Vector List) {
131       siblings.removeAllElements();
132       for(int i = 0; List != null && i < List.size(); i++ )
133          siblings.addElement(List.elementAt(i));
134    }
135    public void setChildren(Vector List) {
136       children.removeAllElements();
137       for(int i = 0; List != null && i < List.size(); i++ )
138          children.addElement(List.elementAt(i));
139    }
140    public void setParents(Vector List) {
141       parents.removeAllElements();
142       for(int i = 0; List != null && i < List.size(); i++ )
143          parents.addElement(List.elementAt(i));
144       if ( parent != null && !parents.contains(parent) )
145          parents.addElement(parent);
146    }
147    public void setSiblings(String[] List) {
148       siblings.removeAllElements();
149       for(int i = 0; List != null && i < List.length; i++ )
150          siblings.addElement(List[i]);
151    }
152    public void setChildren(String[] List) {
153       children.removeAllElements();
154       for(int i = 0; List != null && i < List.length; i++ )
155          children.addElement(List[i]);
156    }
157    public void setParents(String[] List) {
158       parents.removeAllElements();
159       for(int i = 0; List != null && i < List.length; i++ )
160          parents.addElement(List[i]);
161       if ( parent != null && !parents.contains(parent) )
162          parents.addElement(parent);
163    }
164    public void setPostconditions(Vector List) {
165       produced.removeAllElements();
166       for(int i = 0; List != null && i < List.size(); i++ )
167          produced.addElement((Fact) List.elementAt(i));
168    }
169    public void setPostconditions(Fact[] List) {
170       produced.removeAllElements();
171       for(int i = 0; List != null && i < List.length; i++ )
172          produced.addElement(List[i]);
173    }
174    public void setPreconditions(Vector List) {
175       consumed.removeAllElements();
176       for(int i = 0; List != null && i < List.size(); i++ )
177          consumed.addElement((Fact) List.elementAt(i));
178    }
179    public void setPreconditions(Fact[] List) {
180       consumed.removeAllElements();
181       for(int i = 0; List != null && i < List.length; i++ )
182          consumed.addElement(List[i]);
183    }
184    public void addChild(String child) {
185       if ( !children.contains(child) )
186          children.addElement(child);
187    }
188    public boolean hasChild(String child) {
189       return children.contains(child);
190    }
191    public void addSibling(String sibling) {
192       if ( !siblings.contains(sibling) )
193          siblings.addElement(sibling);
194    }
195    public boolean hasSibling(String sibling) {
196       return siblings.contains(sibling);
197    }
198    public void addParent(String name) {
199       if ( !parents.contains(name) )
200          parents.addElement(name);
201    }
202    public boolean hasParent(String name) {
203       return parents.contains(name);
204    }
205    public void setEndTime(int time) {
206       Assert.notFalse(time >= 0);
207       end_time = time;
208    }
209    public void setStartTime(int time) {
210       Assert.notFalse(time >= 0);
211       start_time = time;
212    }
213    public void setCost(double cost) {
214       Assert.notFalse(cost >= 0);
215       this.cost = cost;
216    }
217 
218    public String   getName()       { return name; }
219    public String   getGoal()       { return goal; }
220    public String   getTask()       { return task; }
221    public String   getAgent()      { return agent; }
222    public int      getState()      { return state; }
223    public String   getOwner()      { return owner; }
224    public String   getParent()     { return parent; }
225    public String   getRootId()     { return root_id; }
226    public boolean  isRoot()        { return name.equals(root_id); }
227    public int      getStartTime()  { return start_time; }
228    public int      getEndTime()    { return end_time; }
229    public double   getCost()       { return cost; }
230    
231    public String[] getChildren() {
232       String[] out = new String[children.size()];
233       for(int i = 0; i < children.size(); i++ )
234          out[i] = (String)children.elementAt(i);
235       return out;
236 
237    }
238    public String[] getSiblings() {
239       String[] out = new String[siblings.size()];
240       for(int i = 0; i < siblings.size(); i++ )
241          out[i] = (String)siblings.elementAt(i);
242       return out;
243 
244    }
245    public String[] getParents()   {
246       String[] out = new String[parents.size()];
247       for(int i = 0; i < parents.size(); i++ )
248          out[i] = (String)parents.elementAt(i);
249       return out;
250 
251    }
252    public Fact[]  getPreconditions() {
253       Fact[] out = new Fact[consumed.size()];
254       for(int i = 0; i < consumed.size(); i++ )
255          out[i] = (Fact)consumed.elementAt(i);
256       return out;
257    }
258    public Fact[] getPostconditions() {
259       Fact[] out = new Fact[produced.size()];
260       for(int i = 0; i < produced.size(); i++ )
261          out[i] = (Fact)produced.elementAt(i);
262       return out;
263    }
264 
265    public String toString() {
266       String s = new String("(");
267 
268       s += ":name " + name + " ";
269       s += ":goal " + goal + " ";
270       s += ":task " + task + " ";
271       s += ":agent " + agent + " ";
272       s += ":state " + state + " ";
273       s += ":owner " + owner + " ";
274       s += ":root_id " + root_id + " ";
275 
276       if ( parent != null )
277          s += ":parent " + parent + " ";
278 
279       s += ":start_time " + start_time + " ";
280       s += ":end_time " + end_time + " ";
281       s += ":cost " + cost + " ";
282 
283       if ( !children.isEmpty() ) {
284          s += ":children (";
285          for(int i = 0; i < children.size(); i++ )
286             s += (String)children.elementAt(i) + " ";
287          s = s.trim() + ") ";
288       }
289       if ( !siblings.isEmpty() ) {
290          s += ":siblings (";
291          for(int i = 0; i < siblings.size(); i++ )
292             s += (String)siblings.elementAt(i) + " ";
293          s = s.trim() + ") ";
294       }
295       if ( !parents.isEmpty() ) {
296          s += ":parents (";
297          for(int i = 0; i < parents.size(); i++ )
298             s += (String)parents.elementAt(i) + " ";
299          s = s.trim() + ") ";
300       }
301       if ( !consumed.isEmpty() ) {
302          s += ":consumed_facts (";
303          for(int i = 0; i < consumed.size(); i++ )
304             s += ((Fact) consumed.elementAt(i)).toString();
305          s += ") ";
306       }
307       if ( !produced.isEmpty() ) {
308          s += ":produced_facts (";
309          for(int i = 0; i < produced.size(); i++ )
310             s += ((Fact) produced.elementAt(i)).toString();
311          s += ") ";
312       }
313       return s.trim() + ")";
314    }
315 
316    public String pprint() {
317       return pprint(0);
318    }
319    public String pprint(int sp) {
320       String suffix, prefix;
321       String tabs = Misc.spaces(sp);
322       String eol  = "\n" + tabs + " ";
323 
324       String s = new String("(");
325 
326       s += ":name " + name + eol;
327       s += ":goal " + goal + eol;
328       s += ":task " + task + eol;
329       s += ":agent " + agent + eol;
330       s += ":state " + state + eol;
331       s += ":owner " + owner + eol;
332       s += ":root_id " + root_id + eol;
333 
334       if ( parent != null )
335          s += ":parent " + parent + eol;
336 
337       s += ":start_time " + start_time + eol;
338       s += ":end_time " + end_time + eol;
339       s += ":cost " + cost + eol;
340 
341       if ( !children.isEmpty() ) {
342          prefix = ":children ";
343          suffix = Misc.spaces(1 + sp + prefix.length());
344          s += prefix + "(";
345          for(int i = 0; i < children.size(); i++ )
346             s += (String)children.elementAt(i) + "\n" + suffix + " ";
347          s = s.trim() + "\n" + suffix + ")" + eol;
348       }
349 
350       if ( !siblings.isEmpty() ) {
351          prefix = ":siblings ";
352          suffix = Misc.spaces(1 + sp + prefix.length());
353          s += prefix + "(";
354          for(int i = 0; i < siblings.size(); i++ )
355             s += (String)siblings.elementAt(i) + "\n" + suffix + " ";
356          s = s.trim() + "\n" + suffix + ")" + eol;
357       }
358 
359       if ( !parents.isEmpty() ) {
360          prefix = ":parents ";
361          suffix = Misc.spaces(1 + sp + prefix.length());
362          s += prefix + "(";
363          for(int i = 0; i < parents.size(); i++ )
364             s += (String)parents.elementAt(i) + "\n" + suffix + " ";
365          s = s.trim() + "\n" + suffix + ")" + eol;
366       }
367       if ( !consumed.isEmpty() ) {
368          prefix = ":consumed_facts ";
369          suffix = Misc.spaces(1 + sp + prefix.length());
370          s += prefix + "(";
371          for(int i = 0; i < consumed.size(); i++ )
372             s += ((Fact)consumed.elementAt(i)).pprint(1+suffix.length()) +
373                  "\n" + suffix + " ";
374          s = s.trim() + "\n" + suffix + ")" + eol;
375       }
376       if ( !produced.isEmpty() ) {
377          prefix = ":produced_facts ";
378          suffix = Misc.spaces(1 + sp + prefix.length());
379          s += prefix + "(";
380          for(int i = 0; i < produced.size(); i++ )
381             s += ((Fact)produced.elementAt(i)).pprint(1+suffix.length()) +
382                  "\n" + suffix + " ";
383          s = s.trim() + "\n" + suffix + ")" + eol;
384       }
385       return s.trim() + "\n" + tabs + ")";
386    }
387 }