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.io.*;
27  import java.util.*;
28  import zeus.util.*;
29  
30  /***
31      Change Log
32      ----------
33      07/06/01 () init added
34   *  21/06/02 - DAML-S changes
35   * 26/9/02 - DAML-s changes back - moved to a name so that a URL can be 
36   *generated that is used in the service registration
37   *
38      */
39  public class AbilitySpec {
40     protected Fact   fact;
41     protected int    time;
42     protected double cost;
43     protected String name = "noname";
44       
45     public AbilitySpec () {
46      
47     }
48  
49     
50   public AbilitySpec(String name, Fact fact, int time, double cost) {
51        this.fact = new Fact(fact);
52        this.time = time;
53        this.cost = cost;
54        this.name = name;
55     }
56     
57     public AbilitySpec( Fact fact, int time, double cost) {
58        this.fact = new Fact(fact);
59        this.time = time;
60        this.cost = cost;
61     }
62  
63     
64     public AbilitySpec(AbilitySpec ability) {
65        fact = new Fact(ability.getFact());
66        time = ability.getTime();
67        cost = ability.getCost();
68     }
69  
70     
71     public AbilitySpec duplicate(String name, GenSym genSym) {
72        DuplicationTable table = new DuplicationTable(name,genSym);
73        return duplicate(table);
74     }
75     
76     
77     public AbilitySpec duplicate(DuplicationTable table) {
78        return new AbilitySpec(name,fact.duplicate(table), time, cost);
79     }
80  
81     
82     public String getType()     { return fact.getType(); }
83     public String getId()       { return fact.getId(); }
84     public Fact   getFact()     { return fact; }
85     public int    getTime()     { return time; }
86     public double getCost()     { return cost; }
87     
88     public String getName () { return name;} 
89  
90     public void setTime(int t)     { time = t; }
91     public void setCost(double c)  { cost = c; }
92  
93     public void setName(String s) { name = s; }
94     
95     public boolean resolve(Bindings b) {
96        return fact.resolve(b);
97     }
98  
99     public boolean equals(AbilitySpec ability ) {
100       return fact.equals(ability.getFact()) &&
101              time == ability.getTime() &&
102              Math.abs(cost - ability.getCost()) < 1.0e-12; // very small number!
103              // this is because there are rounding errors that mean that sometimes things 
104              // that should be 0 come out as 0.000001 ...
105       
106    }
107 
108    public String toString() {
109     String front = ( "(" +":fact " + fact + " " + ":time " + time + " " +  ":cost " + cost + " " );
110     if (name != null) { 
111         front +=  ":name " + name +  ")";}
112     else front += ")"; 
113     return front; 
114    } 
115 
116    public String pprint() {
117       return pprint(0);
118    }
119    
120    public String pprint(int sp) {
121       String prefix;
122       String tabs = Misc.spaces(sp);
123       String eol  = "\n" + tabs + " ";
124 
125       prefix = "(:fact ";
126       String s = prefix + fact.pprint(sp+prefix.length()) + eol +
127                  ":time " + time + eol +
128                  ":cost " + cost + eol;
129       return s.trim() + "\n" + tabs + ")";
130    }
131 }