-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRules.groovy
293 lines (249 loc) · 9.86 KB
/
Rules.groovy
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import org.apache.commons.lang3.RandomStringUtils
// These are the rules for the reasoner
class Rules {
// OR rule:
// Condition: A contains (C OR D)(a) but neither C(a) or D(a)
// Action: A' = A UNION { C(a) } and A'' = A UNION { D(a) }
static or(ABoxen, ABox) {
def vRule = ABox.findAll { it.type == 'instance' && it.definition.type == 'operation' && it.definition.operation == '⊔' }.find { instance ->
def cA = ABox.find { it.definition == instance.definition.left && it.instance == instance.instance }
def cB = ABox.find { it.definition == instance.definition.right && it.instance == instance.instance }
return !(cA || cB)
}
if(vRule) {
print '⊔'
def firstNewABox = ABox.clone() << [
'type': 'instance',
'definition': vRule.definition.left,
'instance': vRule.instance
]
def secondNewABox = ABox.clone() << [
'type': 'instance',
'definition': vRule.definition.right,
'instance': vRule.instance
]
ABoxen.remove(ABoxen.indexOf(ABox))
ABoxen << firstNewABox << secondNewABox
}
return vRule
}
// AND rule:
// Condition: A contains (C AND D)(a) but not both C(a) and D(a)
// Action: A' = A UNION { C(a), D(a) }
static and(ABoxen, ABox) {
def vRule = ABox.findAll { it.type == 'instance' && it.definition.type == 'operation' && it.definition.operation == '⊓' }.find { instance ->
def cA = ABox.find { it.definition == instance.definition.left && it.instance == instance.instance }
def cB = ABox.find { it.definition == instance.definition.right && it.instance == instance.instance }
return !(cA && cB)
}
if(vRule) {
print '⊓'
def newABox = ABox.clone() << [
'type': 'instance',
'definition': vRule.definition.left,
'instance': vRule.instance
] << [
'type': 'instance',
'definition': vRule.definition.right,
'instance': vRule.instance
]
ABoxen.remove(ABoxen.indexOf(ABox))
ABoxen << newABox
}
return vRule
}
// TODO: Existential Quantifier rule:
// Condition: A contains (EQr.C)(a) but no c for which { r(a,c), C(c) }
// Action: A' = A UNION { r(a, b), C(b) } where b is a new individual name
static eq(ABoxen, ABox) {
def vRule = ABox.findAll { it.type == 'instance' && it.definition.type == 'operation' && it.definition.operation == '∃' }.find { eq ->
!ABox.any { relation -> relation.type == 'relation' && relation.relation == eq.definition.relation && relation.left == eq.instance && ABox.find { it.type == 'instance' && it.definition == eq.definition.definition && it.instance == relation.right } }
}
if(vRule) {
print '∃'
// Random instance name, from https://bowerstudios.com/node/1100
def charset = (('a'..'z') + ('A'..'Z') + ('0'..'9')).join()
def newInstance = RandomStringUtils.random(5, charset.toCharArray())
def newABox = ABox.clone() << [
'type': 'relation',
'relation': vRule.definition.relation,
'left': vRule.instance,
'right': newInstance,
'negate': false
] << [
'type': 'instance',
'definition': vRule.definition.definition,
'instance': newInstance,
'negate': false
]
ABoxen.remove(ABoxen.indexOf(ABox))
ABoxen << newABox
}
return vRule
}
// ∀: Univeral Quantifier rule:
// Condition: A contains (UQr.C)(a) and r(a, b) but not C(b)
// Action: A' = A UNION {C(b)}
static uq(ABoxen, ABox) {
def relation
def quantifier = ABox.findAll { it.type == 'instance' && it.definition.type == 'operation' && it.definition.operation == '∀' }.find { uq ->
ABox.find {
def res = it.type == 'relation' && it.relation == uq.definition.relation && it.left == uq.instance && !ABox.any { ins -> ins.instance == it.right && ins.definition == uq.definition.definition }
if(res) {
relation = it
}
return res
}
}
if(quantifier && relation) {
print '∀'
def newABox = ABox.clone() << [
'type': 'instance',
'definition': quantifier.definition.definition,
'instance': relation.right
]
ABoxen.remove(ABoxen.indexOf(ABox))
ABoxen << newABox
}
return quantifier && relation
}
// Greater than equal to rule
// Condition: A contains GTEnr.C(a) but there are no c1..cn with { r(a,c1), C(c1),..., r(a,cn), C(cn) } U { ci != cj | 1 <= i <= n, 1 <= j <= n, i != j }
// Action: A' = A U { r(a, b1), C(b1) ... r(a, bn), C(bn) } U { bi != bj | 1 <= i <= n, 1 <= j <= n, i != j } where b1..bn are few individual names
static gte(ABoxen, ABox) {
def vRule = ABox.findAll { it.type == 'instance' && it.definition.type == 'operation' && it.definition.operation == '≥' }.find { gte ->
def instances = []
def relations = []
def inequalities = []
// Find all the relevant relations
ABox.each {
if(it.type == 'relation' && it.left == gte.instance && !relations.contains(it.right)) {
relations << it.right
}
}
// Find all the concepts relevant to the relations
ABox.each {
if(it.type == 'instance' && relations.contains(it.instance) && !instances.contains(it.instance) && it.definition == gte.definition.definition) {
instances << it.instance
}
}
// Find explicit inequalities
ABox.each {
if(it.type == 'distinction' && instances.contains(it.left) && instances.contains(it.right) && !inequalities.contains(it)) {
inequalities << it
}
}
def expectedDistinctions = 0
(0..instances.size()-2).each { i ->
(i+1..instances.size()-1).each { j ->
expectedDistinctions++
}
}
return !(instances.size() == relations.size() && instances.size() == gte.definition.amount && inequalities.size() == expectedDistinctions)
}
if(vRule) {
print '≥'
def newABox = ABox.clone()
// Add a new relation and concept for each amt in the gte rule
def newInstances = []
(1..vRule.definition.amount).each {
// Random instance name, from https://bowerstudios.com/node/1100
def charset = (('a'..'z') + ('A'..'Z') + ('0'..'9')).join()
def newInstance = RandomStringUtils.random(5, charset.toCharArray())
// Add the relation and the instance
newABox << [
'type': 'relation',
'relation': vRule.definition.relation,
'left': vRule.instance,
'right': newInstance,
'negate': false
] << [
'type': 'instance',
'definition': vRule.definition.definition,
'instance': newInstance,
'negate': false
]
newInstances << newInstance
}
// Add distinctions for generated items
(0..newInstances.size()-2).each { i ->
(i+1..newInstances.size()-1).each { j ->
newABox << [
'type': 'distinction',
'left': newInstances[i],
'right': newInstances[j],
'negate': false
]
}
}
ABoxen.remove(ABoxen.indexOf(ABox))
ABoxen << newABox
}
return vRule
}
// Less than equal to rule
// Condition: A contains LTEnr.C(a), and there are b1,..,bn+1 with { r(a, b1), C(b1), ... r(a,bn+1), C(bn+1) } but NO { bi != bj | 1 <= i <= n, 1 <=j <= n, i != j}
// Action: for all i != j with bi != bj NOT EXIST IN A, Ai,j = A[bi / bj] (rather, replace bi with bj)
static lte(ABoxen, ABox) {
def instances
def relations
def inequalities
def labels
def vRule = ABox.findAll { it.type == 'instance' && it.definition.type == 'operation' && it.definition.operation == '≤' }.find { lte ->
instances = 0
relations = 0
labels = []
inequalities = []
// Find all the relevant relations
ABox.each {
if(it.type == 'relation' && it.left.value == lte.instance && !labels.contains(it.right.value)) {
labels << it.right.value
relations++
}
}
// Find all the concepts relevant to the relations
ABox.each {
if(it.type == 'instance' && labels.contains(it.instance) && it.definition == lte.definition.definition) {
instances++
}
}
// Find explicit inequalities
ABox.each {
if(it.type == 'distinction' && labels.contains(it.left) && labels.contains(it.right) && !inequalities.contains(it)) {
inequalities << it
}
}
def expectedDistinctions = 0
(0..relations-2).each { i ->
(i+1..relations-1).each { j ->
expectedDistinctions++
}
}
// We are happy if we found an instance for every relation, the number of these are n+1 and there is some duplicate in the instances
// If the relation is top, we don't have to have
return (instances == relations || lte.definition.definition.type == 'literal' && lte.definition.definition.value == '⊤') &&
(relations == lte.definition.amount + 1) && (inequalities.size() < expectedDistinctions)
}
if(vRule) {
print '≤'
def newABox = ABox.clone()
(0..relations-2).each { i ->
(i+1..relations-1).each { j ->
// if there is no inequality for this pair of instances
if(!inequalities.find { ((it.left == labels[i] && it.right == labels[j]) || (it.left == labels[j] && it.right == labels[i])) }) {
// we will simply update the instance of j to = the instance of i
newABox.each { rule ->
if(rule.instance && rule.instance == labels[j]) {
rule.instance = labels[i]
} else if(rule.type == 'relation' && rule.right.value == labels[j]) {
rule.right.value = labels[i]
}
}
}
}
}
ABoxen.remove(ABoxen.indexOf(ABox))
ABoxen << newABox
}
}
}