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  import zeus.concepts.fn.*;
30  
31  
32  public class Bindings extends Hashtable {
33     protected IdFn  name = null;
34     protected VarFn self = null;
35  
36     public Bindings() {
37      super(); 
38     }
39  
40     public Bindings(String agent) {
41        this.name = new IdFn(agent);
42        this.self = new VarFn(Fact.SELF);
43        set(self,name);
44     }
45  
46     public Bindings(Bindings List) {
47        set(List);
48     }
49  
50     public void clear() {
51        super.clear();
52        if ( self !=null && name != null )
53           set(self,name);
54     }
55  
56     public boolean add(Bindings List) {
57        BindingsRecord rec;
58  
59        for(Enumeration enum = List.elements(); enum.hasMoreElements(); ) {
60           rec = (BindingsRecord) enum.nextElement();
61           if ( rec.lhs.unifiesWith(rec.rhs,this) == null )
62              return false;
63        }
64        return true;
65     }
66  
67     public void set(Bindings List) {
68        Object key;
69        BindingsRecord rec;
70  
71        super.clear();
72        for(Enumeration enum = List.keys(); enum.hasMoreElements(); ) {
73           key = enum.nextElement();
74           rec = (BindingsRecord) List.get(key);
75           put(key,rec);
76        }
77     }
78  
79     public void set(ValueFunction lhs, ValueFunction rhs) {
80        ValueFunction temp;
81  
82        if ( lhs.equals(rhs) ) return; // Loop check
83  
84        if ( lhs.getPD() > rhs.getPD() ) {
85           temp = lhs; lhs = rhs; rhs = temp;
86        }
87  
88        if ( lhs.isDeterminate() && !rhs.isDeterminate() ) {
89          temp = lhs; lhs = rhs; rhs = temp;
90        }
91  
92        switch( lhs.getID() ) {
93           case ValueFunction.LVAR:
94           case ValueFunction.FIELD:
95                put(lhs.toString(), new BindingsRecord(lhs,rhs));
96                break;
97  
98           default:
99                put(lhs, new BindingsRecord(lhs,rhs));
100               break;
101       }
102    }
103 
104    public ValueFunction lookUp(ValueFunction lhs) {
105       BindingsRecord rec;
106 
107       while( (rec = getBindingsRecord(lhs)) != null ) {
108          if ( rec.rhs.equals(lhs) ) {
109             System.err.println("Bindings -- Loop found\n" + this);
110             System.exit(0);
111          }
112          lhs = rec.rhs;
113       }
114       return lhs;
115    }
116 
117    protected BindingsRecord getBindingsRecord(ValueFunction lhs) {
118       switch( lhs.getID() ) {
119          case ValueFunction.LVAR:
120          case ValueFunction.FIELD:
121               return (BindingsRecord)this.get(lhs.toString());
122 
123          default:
124               return (BindingsRecord)this.get(lhs);
125       }
126    }
127 
128    public String toString() {
129       String results = new String("(");
130       BindingsRecord rec;
131 
132       for(Enumeration enum = this.elements(); enum.hasMoreElements(); ) {
133          rec = (BindingsRecord) enum.nextElement();
134          results += rec.lhs + "=" + rec.rhs + " ";
135       }
136       return results.trim() + ")";
137    }
138 
139 /*
140    // Does not work with negations/conjunctions/etc
141 
142    public boolean equals(Object obj) {
143       if ( !(obj instanceof Bindings) ) return false;
144       Bindings b = (Bindings)obj;
145 
146       if ( b.size() != this.size() ) return false;
147 
148       BindingsRecord r1, r2;
149       Object key;
150       Enumeration enum = this.keys();
151       boolean status = true;
152 
153       while( status && enum.hasMoreElements() ) {
154          key = enum.nextElement();
155          r1 = (BindingsRecord)get(key);
156          r2 = (BindingsRecord)b.get(key);
157          status &= r2 != null && r1.lhs.equals(r2.lhs) && r1.rhs.equals(r2.rhs);
158       }
159       return status;
160    }
161 */
162 
163 }
164 
165 class BindingsRecord {
166    public ValueFunction lhs, rhs;
167 
168    public BindingsRecord(ValueFunction left, ValueFunction right) {
169       lhs = left;
170       rhs = right;
171    }
172 }