-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefClauseKB.java
More file actions
136 lines (85 loc) · 3 KB
/
Copy pathDefClauseKB.java
File metadata and controls
136 lines (85 loc) · 3 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
import java.util.ArrayList;
import java.util.Iterator;
/*
* a knowledge base that consists of definite clauses
*
*/
public class DefClauseKB
{
public ArrayList<DefiniteClause> theClauses = new ArrayList<DefiniteClause>();
public ArrayList<DefiniteClause> getDefClauses()
{
return theClauses;
}
public Iterator<DefiniteClause> getIter() {
return theClauses.iterator();
}
/**
*
* @param k size of sublists
* @return a list that contains all sublists of facts of size k in this knowledge base
*/
public ArrayList<ArrayList<Atomic>> getAllKFacts(int k) {
ArrayList<Atomic> facts = this.getFacts();
if(facts.size() < k) {
System.out.println("Not enough facts in the knowledge base.");
return null;
}
return getAllKFacts(k, facts);
}
/**
* recursively return all(n!/(k!(n - k)!)) lists of size k from an initial list(of size n)
* @param k size of sublists
* @param facts initial list of facts
* @return a list containing all lists of facts of size k
*/
public ArrayList<ArrayList<Atomic>> getAllKFacts(int k, ArrayList<Atomic> facts) {
ArrayList<ArrayList<Atomic>> list = new ArrayList<ArrayList<Atomic>>();
ArrayList<Atomic> copy = new ArrayList<Atomic>(facts);
// if(copy.size() < k || copy.isEmpty()) {
//
// return new ArrayList<ArrayList<Atomic>>();
// }
int size = copy.size();
if(k == 1) {
ArrayList<ArrayList<Atomic>> l = new ArrayList<ArrayList<Atomic>>();
for(int i = 0; i < copy.size(); i++) {
ArrayList<Atomic> l1 = new ArrayList<Atomic>();
l1.add(copy.get(i));
l.add(l1);
}
return l;
}
for(int i = 0; i < size - k + 1; i++) {
Atomic first = copy.remove(0);
ArrayList<ArrayList<Atomic>> rest = getAllKFacts(k - 1, copy);
for(int j = 0; j < rest.size(); j++) {
rest.get(j).add(0, first);
list.add(rest.get(j));
}
}
return list;
}
/**
*
* @return all facts in this knowledge base
*/
public ArrayList<Atomic> getFacts() {
ArrayList<Atomic> facts = new ArrayList<Atomic>();
for(int i = 0; i < this.theClauses.size(); i++) {
if(this.theClauses.get(i).isFact()) {
facts.add(this.theClauses.get(i).getHead());
}
}
return facts;
}
public void print() {
Iterator<DefiniteClause> iter = this.getIter();
System.out.println("Knowledge base: ");
while(iter.hasNext()) {
DefiniteClause next = iter.next();
next.print();
//System.out.println("\n");
}
}
}