-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRuleBase.java
More file actions
178 lines (139 loc) · 5.6 KB
/
Copy pathRuleBase.java
File metadata and controls
178 lines (139 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.util.*;
import java.io.*;
import java.awt.* ;
import java.io.*;
import java.applet.*;
public class RuleBase {
String name ;
Hashtable variableList ; // all variables in the rulebase
Clause clauseVarList[];
Vector ruleList ; // list of all rules
Vector conclusionVarList ; // queue of variables
Rule rulePtr ; // working pointer to current rule
Clause clausePtr ; // working pointer to current clause
Stack goalClauseStack; // for goals (cons clauses ) and subgoals
static TextArea textArea1;
public void setDisplay(TextArea txtArea) {
textArea1 = txtArea;
}
RuleBase(String Name) {
name = Name;
}
public static void appendText(String text) {
textArea1.appendText(text);
}
// for trace purposes - display all variables and their value
public void displayVariables(TextArea textArea) {
Enumeration enume = variableList.elements();
while(enume.hasMoreElements()) {
RuleVariable temp = (RuleVariable)enume.nextElement() ;
textArea.appendText("\n" + temp.name + " value = " + temp.value) ;
}
}
// for trace purposes - display all rules in text format
public void displayRules(TextArea textArea) {
textArea.appendText("\n" + name + " Rule Base: " + "\n");
Enumeration enume = ruleList.elements() ;
while(enume.hasMoreElements()) {
Rule temp = (Rule)enume.nextElement() ;
temp.display(textArea) ;
}
}
// for trace purposes - display all rules in the conflict set
public void displayConflictSet(Vector ruleSet) {
textArea1.appendText("\n" + " -- Rules in conflict set:\n");
Enumeration enume = ruleSet.elements();
while(enume.hasMoreElements()) {
Rule temp = (Rule)enume.nextElement() ;
textArea1.appendText(temp.name + "(" + temp.numAntecedents()+ "), ");
}
}
// reset the rule base for another round of inferencing
// by setting all variable values to null
public void reset() {
textArea1.appendText("\n Setting all " + name + " variables to null");
Enumeration enume = variableList.elements() ;
while(enume.hasMoreElements()) {
RuleVariable temp = (RuleVariable)enume.nextElement() ;
temp.setValue(null) ;
}
}
public void forwardChain() {
Vector conflictRuleSet = new Vector() ;
// first test all rules, based on initial data
conflictRuleSet = match(true); // see which rules can fire
while(conflictRuleSet.size() > 0) {
Rule selected = selectRule(conflictRuleSet); // select the "best" rule
selected.fire() ; // fire the rule
// do the consequent action/assignment
// update all clauses and rules
conflictRuleSet = match(false); // see which rules can fire
// displayVariables("Forward Chaining") ; // display variable bindings
}
}
//used for forward chaining only
//determine which rules can fire, return a Vector
public Vector match(boolean test) {
Vector matchList = new Vector() ;
Enumeration enume = ruleList.elements() ;
while (enume.hasMoreElements()) {
Rule testRule = (Rule)enume.nextElement() ;
if (test)
testRule.check() ; // test the rule antecedents
if (testRule.truth == null)
continue ;
// fire the rule only once for now
if ((testRule.truth.booleanValue() == true) &&
(testRule.fired == false))
matchList.addElement(testRule);
}
displayConflictSet(matchList) ;
return matchList ;
}
//used for forward chaining only
//select a rule to fire based on specificity
public Rule selectRule(Vector ruleSet) {
Enumeration enume = ruleSet.elements() ;
long numClauses ;
Rule nextRule ;
Rule bestRule = (Rule)enume.nextElement() ;
long max = bestRule.numAntecedents() ;
while (enume.hasMoreElements()) {
nextRule = (Rule)enume.nextElement() ;
if ((numClauses = nextRule.numAntecedents()) > max) {
max = numClauses ;
bestRule = nextRule ;
}
}
return bestRule ;
}
public void backwardChain(String goalVarName) {
RuleVariable goalVar = (RuleVariable)variableList.get(goalVarName);
Enumeration goalClauses = goalVar.clauseRefs.elements() ;
while (goalClauses.hasMoreElements()) {
Clause goalClause = (Clause)goalClauses.nextElement() ;
if (goalClause.consequent.booleanValue() == false) continue ;
goalClauseStack.push(goalClause) ;
Rule goalRule = goalClause.getRule();
Boolean ruleTruth = goalRule.backChain() ; // find rule truth value
if (ruleTruth == null)
textArea1.appendText("\nRule " + goalRule.name + " is null, can't determine truth value.");
else if (ruleTruth.booleanValue() == true) {
// rule is OK, assign consequent value to variable
goalVar.setValue(goalClause.rhs) ;
goalVar.setRuleName(goalRule.name) ;
goalClauseStack.pop() ; // clear item from subgoal stack
textArea1.appendText("\nRule " + goalRule.name + " is true, setting " + goalVar.name + ": =" + goalVar.value);
if (goalClauseStack.empty() == true) {
textArea1.appendText("\n +++ Found Solution for goal: " + goalVar.name);
break ; // for now, only find first solution, then stop
}
} else {
goalClauseStack.pop() ; // clear item from subgoal stack
textArea1.appendText("\nRule " + goalRule.name + " is false, can't set " + goalVar.name);
}
}
if (goalVar.value == null)
textArea1.appendText("\n +++ Could Not Find Solution for goal: " + goalVar.name);
}
}