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  import zeus.actors.ZeusTask;
30  import zeus.actors.AgentContext;
31  //import zeus.ontology.service.*; not in open source version
32  
33  public abstract class Task extends AbstractTask {
34     protected Vector constraints = new Vector();
35     protected ValueFunction time = ZeusParser.Expression(SystemProps.getProperty("task.default.time"));
36     protected ValueFunction cost = ZeusParser.Expression(SystemProps.getProperty("task.default.cost"));
37  
38     transient ResolutionContext resolution_context = null;
39  
40     public void setTimeFn(String time) {
41        ValueFunction fn = ZeusParser.Expression(time);
42        if ( fn != null )
43           this.time = fn;
44     }
45     
46     
47     public void setTimeFn(ValueFunction fn) {
48        this.time = fn;
49     }
50     
51     
52     public void setCostFn(String cost) {
53        ValueFunction fn = ZeusParser.Expression(cost);
54        if ( fn != null )
55           this.cost = fn;
56     }
57     
58     
59     public void setCostFn(ValueFunction fn) {
60        this.cost = fn;
61     }
62  
63     public ValueFunction getTimeFn() { return time; }
64     public ValueFunction getCostFn() { return cost; }
65  
66     public int getTime() {
67        int t = SystemProps.getInt("task.default.time");
68        // added by simon 14/2/01 - deal with elaborations
69        if (time instanceof ElseFn) { 
70          ValueFunction evaledFn = time.evaluationFn(); 
71          time = evaledFn; }
72          
73        if (time instanceof ArithmeticFn) { 
74          ValueFunction timef = ((ArithmeticFn)time).evaluationFn(); 
75          if (timef instanceof  IntFn) {  
76                t = ((IntFn)time).getValue(); 
77          } 
78          else if (timef instanceof RealFn) { 
79              double td = ((RealFn) time).getValue(); 
80              Double tconv = new Double (td); 
81              t = tconv.intValue(); 
82          }
83        }
84       
85        // end of add
86        if ( !time.isDeterminate() || !(time instanceof IntFn) ) {
87           Core.USER_ERROR("Task " +  name +
88              " is improperly defined.\nCannot evaluates its " +
89              " duration given required effect.\nCurrent value: \'" +
90  	    time + "\'\nSetting duration to " + t );
91        }
92         else {
93           t = ((IntFn)time).getValue();
94        }
95        return t;
96     }
97     
98  
99     public double getCost() {
100       
101       double c = SystemProps.getDouble("task.default.cost");
102           // added by simon 14/2/01 - deal with elaborations
103       debug ("cost= " + cost.toString()); 
104       Fact [] postConds = getPostconditions(); 
105       
106       for (int i = 0 ; i<postConds.length; i++) { 
107         debug ("post  = " +  postConds[i].toString()); 
108       }
109       if (cost instanceof ElseFn) { 
110         ValueFunction evaledFn = cost.evaluationFn(); 
111         debug ("elseed" + evaledFn.toString()); 
112         cost = evaledFn; 
113         }
114         
115       if (cost instanceof ArithmeticFn) { 
116         ValueFunction costf = ((ArithmeticFn)cost).evaluationFn(); 
117         if (costf instanceof  IntFn) {  
118               c = ((IntFn)time).getValue(); 
119               debug ("arith then int"); 
120               return c; 
121         } 
122         else if (costf instanceof RealFn) { 
123          c = ((RealFn) time).getValue(); 
124          debug ("arith then real"); 
125          return c;    
126       }
127       }
128       // end of add
129       if ( !cost.isDeterminate() || !(cost instanceof PrimitiveNumericFn) ) {
130          Core.USER_ERROR("Task " +  name +
131             " is improperly defined.\nCannot evaluates its " +
132             " cost given required effect.\nCurrent value: \'" +
133 	    cost + "\'\nSetting cost to " + c );
134       }
135       else {
136          c =  ((PrimitiveNumericFn)cost).doubleValue();
137       }
138       return c;
139    }
140 
141    public abstract Fact[]            getPostconditions();
142    public abstract Fact[]            getPreconditions();
143    public abstract ResolutionContext getContext();
144 
145    public boolean applyConstraints(Bindings bindings) {
146       Bindings local = new Bindings(bindings);
147 
148       ResolutionContext context = getContext();
149       LogicalFn fn;
150       for(int i = 0; i < constraints.size(); i++ ) {
151          fn = (LogicalFn)constraints.elementAt(i);
152          fn = (LogicalFn)fn.resolve(context,local);
153 	 if ( fn == null )
154 	    return false;
155 	 if ( fn.evaluate() != LogicalFn.TRUE )
156             return false;
157       }
158       return bindings.add(local);
159    }
160 
161    public void setConstraints(Vector List) {
162       constraints.removeAllElements();
163       for(int i = 0; i < List.size(); i++)
164          constraints.addElement((LogicalFn)List.elementAt(i));
165    }
166    
167    
168    public void setConstraints(LogicalFn[] List) {
169       constraints.removeAllElements();
170       for(int i = 0; i < List.length; i++)
171          constraints.addElement(List[i]);
172 
173    }
174    
175    
176    public LogicalFn[] getConstraints() {
177       LogicalFn[] out = new LogicalFn[constraints.size()];
178 
179       for(int i = 0; i < constraints.size(); i++)
180          out[i] = (LogicalFn)constraints.elementAt(i);
181       return out;
182    }
183    
184    /***
185     *This generates the service description from the task stub
186     *by using reflection... 
187     *@since 1.3
188     *@author Simon Thompson
189     *but not in the opensource version it doesn't
190     */
191     public String getServiceDesc(AgentContext context) {
192 
193         return null; // sorry!
194     }
195    
196   
197   /*** 
198    *since 2.0 
199    *@author Nick Giles
200    *not opensource. 
201    */
202   public String getInstanceDetails(AgentContext context) {
203 
204       return null;
205     
206   }
207 
208   /*** 
209    *since 2.0 
210    *@author Nick Giles
211    *not opensource. 
212    */
213      public String getInstanceRange(AgentContext context) {
214 
215       return null; 
216   }
217   
218   /*** 
219    *since 2.0 
220    *@author Nick Giles
221    *not opensource. 
222    */
223   public String getProcessModel(AgentContext context) {
224 
225       return null;
226 
227   }
228 
229    public void debug (String str) { 
230   //  System.out.println("task>> " + str); 
231    } 
232    
233 }