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.rete;
25
26 import java.util.*;
27 import zeus.util.*;
28 import zeus.concepts.*;
29 import zeus.concepts.fn.*;
30
31
32 /***
33 ReteFact is used as an internal store for a fact for processing by the Rete engine.
34 */
35 public class ReteFact {
36
37 /***
38 type of fact - variable, fact?
39 This is needed to allow extensions of the language
40 */
41 String type = null;
42
43
44 public Hashtable data = null;
45
46
47 public ReteFact(String type) {
48 this.type = type;
49 data = new Hashtable();
50 }
51
52
53 public ReteFact(ReteFact t) {
54 type = t.type;
55 data = new Hashtable();
56 String attribute;
57 ValueFunction value;
58 Enumeration enum = t.data.keys();
59 while( enum.hasMoreElements() ) {
60 attribute = (String)enum.nextElement();
61 value = (ValueFunction)t.data.get(attribute);
62 data.put(attribute,value.mirror());
63 }
64 }
65
66
67 public void setValue(String attribute, ValueFunction value) {
68 data.put(attribute,value);
69 }
70
71
72 ValueFunction getValue(String attribute) {
73 return (ValueFunction)data.get(attribute);
74 }
75
76
77 ValueFunction[] listValues() {
78 ValueFunction[] value = new ValueFunction[data.size()];
79 Enumeration enum = data.elements();
80 for(int i = 0; enum.hasMoreElements(); i++ )
81 value[i] = (ValueFunction)enum.nextElement();
82 return value;
83 }
84
85
86 String[] listAttributes() {
87 String[] value = new String[data.size()];
88 Enumeration enum = data.keys();
89 for(int i = 0; enum.hasMoreElements(); i++ )
90 value[i] = (String)enum.nextElement();
91 return value;
92 }
93
94
95 ValueFunction[] variables() {
96 Vector out = new Vector();
97 Enumeration enum = data.elements();
98 while( enum.hasMoreElements()) {
99 out = Misc.union(out,((ValueFunction)enum.nextElement()).variables());}
100
101 ValueFunction[] result = new ValueFunction[out.size()];
102 for(int i = 0; i < result.length; i++ )
103 result[i] = (ValueFunction)out.elementAt(i);
104 return result;
105 }
106
107
108 public ReteFact duplicate(String name, GenSym genSym) {
109 DuplicationTable table = new DuplicationTable(name,genSym);
110 return duplicate(table);
111 }
112
113
114 public ReteFact duplicate(DuplicationTable table) {
115 ReteFact t = new ReteFact(type);
116 String attribute;
117 ValueFunction value;
118 Enumeration enum = this.data.keys();
119 while( enum.hasMoreElements() ) {
120 attribute = (String)enum.nextElement();
121 value = (ValueFunction)this.data.get(attribute);
122 t.data.put(attribute,value.duplicate(table));
123 }
124 return t;
125 }
126
127
128 /***
129 provide get wrapper for type of fact
130 */
131 public String getType () {
132 return type;
133 }
134
135
136 /***
137 provide set wrapper for type of fact
138 */
139 public void setType (String type) {
140 this.type = type;
141 }
142
143
144 /***
145 this seems to be called when rete is trying to process
146 a rule
147 */
148 public boolean resolve(Bindings b) {
149 String attribute;
150 ValueFunction value;
151 Enumeration enum = data.keys();
152 while( enum.hasMoreElements() ) {
153 attribute = (String)enum.nextElement();
154 value = (ValueFunction)data.get(attribute);
155 if ( (value = value.resolve(b)) == null )
156 return false;
157 data.put(attribute,value);
158 }
159 return true;
160 }
161
162
163 public String toString() {
164 String s = "(" + type + " ";
165 String attribute;
166 ValueFunction value;
167 Enumeration enum = data.keys();
168 while( enum.hasMoreElements() ) {
169 attribute = (String)enum.nextElement();
170 value = (ValueFunction)data.get(attribute);
171 s += "(" + attribute + " " + value + ") ";
172 }
173 return s.trim() + ")";
174 }
175
176
177 public String pprint() {
178 return pprint(0);
179 }
180
181
182 public String pprint(int sp) {
183 return Misc.spaces(sp) + toString();
184 }
185 }