-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInferenceEngine.java
More file actions
167 lines (145 loc) · 6.12 KB
/
InferenceEngine.java
File metadata and controls
167 lines (145 loc) · 6.12 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
import java.util.ArrayList;
public class InferenceEngine {
KnowledgeBase kb;
public ArrayList<Rule> convertToCNF(Rule rule) {
ArrayList<Rule> cnfRules = new ArrayList<>();
ArrayList<Rule> toConvertRules = new ArrayList<>();
toConvertRules.add(rule);
//Step 1: break iff into two implication cases
convertToCNFStepOne(toConvertRules);
return cnfRules;
}
public void convertToCNFStepOne(ArrayList<Rule> rules) {
//Convert every iff to two implies statements
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));
for (Rule rule : allRules) {
if (rule.connector == Rule.IFF) { //for each a iff b we change it to ((a=>b)&&(b=>a))
Rule newRule1 = new Rule();
newRule1.leftRule = rule.leftRule;
newRule1.rightRule = rule.rightRule;
newRule1.connector = Rule.IMPLIES;
Rule newRule2 = new Rule();
newRule2.leftRule = rule.rightRule;
newRule2.rightRule = rule.leftRule;
newRule2.connector = Rule.IMPLIES;
rule.leftRule = newRule1;
rule.rightRule = newRule2;
rule.connector = Rule.AND;
}
}
}
}
public void convertToCNFStepTwo(ArrayList<Rule> rules) {
//Convert every a=>b to !a V b
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));
for (Rule rule : allRules) {
if (rule.connector == Rule.IMPLIES) {
rule.leftRule.negated = !rule.leftRule.negated;
rule.connector = Rule.OR;
}
}
}
}
public void convertToCNFStepThree(ArrayList<Rule> rules) {
//Deal with negated quantifiers, ie !(EXISTS x, p) to (Vx, !p) or !(Vx, p) to (Exists x, !p)
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));
for (Rule rule : allRules) {
for (Quantifier quantifier : rule.quantifiers) {
if (quantifier.not) { //we have a negated quantifier, move inwards
quantifier.not = false;
quantifier.isExistential = !quantifier.isExistential;
rule.negated = !rule.negated;
}
}
}
}
}
public void convertToCNFStepFour(ArrayList<Rule> rules) {
//Move negation inwards, ie !(A&&B) to (!A V !B) also !(A V B) to (!A && !B)
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));
for (Rule rule : allRules) {//Maybe we have to order outter terms before inner terms for this to work??
if (rule.negated) { //We assume only connectors left are OR and AND
rule.negated = false;
rule.leftRule.negated = !rule.leftRule.negated;
rule.rightRule.negated = !rule.rightRule.negated;
if (rule.connector == Rule.OR) {
rule.connector = Rule.AND;
} else
rule.connector = Rule.OR;
}
}
}
}
public void convertToCNFStepFive(ArrayList<Rule> rules) {
//Pair every quantifier to a unique variable
int uniqueVariableId = 0;//We will increment this for each varId used.
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));
for (int i = allRules.size() - 1; i >= 0; i--) {//We want to work with most inner rules prior to the ones containing them
Rule rule = allRules.get(i);
for (Quantifier quantifier : rule.quantifiers) {
ArrayList<Rule> subRules = getAllRules(rule);
for (Rule subRule : subRules) {
if (subRule.justFact) {
for (Variable var : subRule.fact.variables) {
if (var.variableId == quantifier.variableId) {
var.variableId = uniqueVariableId;
}
}
}
}
uniqueVariableId++;
}
}
}
}
public void convertToCNFStepSix(ArrayList<Rule> rules) {
//Move all quantifiers to 'top' rule
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));//assume allRules.get(0) is topMost rule.
ArrayList<Quantifier> allQuantifiers = new ArrayList<>();
for (Rule rule : allRules) {
allQuantifiers.addAll(rule.quantifiers);
rule.quantifiers = new ArrayList<>();
}
allRules.get(0).quantifiers = allQuantifiers;
}
}
public void convertToCNFStepSeven(ArrayList<Rule> rules) {
//Skolemize existentials... this will be a bitch...
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));
for (Rule rule : allRules) {
}
}
}
public void convertToCNFStepEight(ArrayList<Rule> rules) {
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));
for (Rule rule : allRules) {
}
}
}
public void convertToCNFStepNine(ArrayList<Rule> rules) {
while (!rules.isEmpty()) {
ArrayList<Rule> allRules = getAllRules(rules.get(0));
for (Rule rule : allRules) {
}
}
}
public ArrayList<Rule> getAllRules(Rule rule) {
ArrayList<Rule> allRules = new ArrayList<>();
if (rule.leftRule != null) {
allRules.addAll(getAllRules(rule.leftRule));
}
if (rule.rightRule != null) {
allRules.addAll(getAllRules(rule.rightRule));
}
allRules.add(rule);
return allRules;
}
}