1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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 }