-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ontology.groovy
154 lines (139 loc) · 4.27 KB
/
Ontology.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
@Grab(group='org.apache.commons', module='commons-lang3', version='3.4')
import org.apache.commons.lang3.RandomStringUtils
class Ontology {
def ABox = []
def TBox = []
def rGen = new Random()
def setABox(@DelegatesTo(DLSpec) Closure cl) {
def dl = new ABoxSpec()
def code = cl.rehydrate(dl, this, this)
code.resolveStrategy = Closure.DELEGATE_ONLY
code()
ABox = dl.structure
}
def setTBox(@DelegatesTo(DLSpec) Closure cl) {
def dl = new TBoxSpec()
def code = cl.rehydrate(dl, this, this)
code.resolveStrategy = Closure.DELEGATE_ONLY
code()
TBox = dl.structure
}
def expandABox() {
ABox.each {
if(it.type == 'instance') {
it.definition = expand(it.definition, [])
}
}
}
// Here we use concept expansion on the generalised concept inclusions in the TBox,
// converting them to fully expanded ABox axioms
def convertTBox() {
TBox.each {
// Reduce to satisfiability via concept expansion
['left', 'right'].each { rule ->
it[rule] = expand(it[rule], it)
}
// Reduce satisfiability to consistency
// Negate the right, AND it with the left
negate(it['right'])
def newDefinition = [
'type': 'operation',
'operation': '⊓',
'left': it['left'],
'right': it['right']
]
// Random instance name, from https://bowerstudios.com/node/1100
def charset = (('a'..'z') + ('A'..'Z') + ('0'..'9')).join()
def instance = RandomStringUtils.random(5, charset.toCharArray())
ABox << [
'type': 'instance',
'definition': newDefinition,
'instance': instance
]
}
}
def checkConsistency() {
return new Reasoner(this).checkConsistency()
}
def checkSubsumption(emptyTBox) {
return new Reasoner(this).checkSubsumption(emptyTBox)
}
private expand(rule, wgci) {
if(rule.type == 'literal') {
def expander = TBox.find { gci -> // find a gci with a left which is == to our thing
return rule == gci.left && gci != wgci && gci.type != 'literal'
}
if(expander) {
rule = expander.right.clone()
return expand(rule, wgci)
}
} else if(rule.type == 'operation' && (rule.operation == '∃' || rule.operation == '∀' || rule.operation == '≤' || rule.operation == '≥')) {
rule.definition = expand(rule.definition, wgci)
} else {
['left', 'right'].each { rule[it] = expand(rule[it], wgci).clone() }
}
return rule
}
// Convert to negation normal form recursively
private negate(rule) {
if(rule.type == 'literal') {
rule.negate = !rule.negate
} else if(rule.type == 'operation' && (rule.operation == '∃' || rule.operation == '∀' )) {
negate(rule.definition)
} else {
['left', 'right'].each { negate(rule[it]) }
}
}
static printRules(rules) {
rules.each { rule ->
if(rule.type == 'instance') {
printRule(rule.definition)
printRule(rule.instance)
} else if(rule.type == 'gci') {
printRule(rule.left)
print ' ⊑ '
printRule(rule.right)
} else if(rule.type == 'distinction') {
printRule(rule.left)
print ' ≠ '
printRule(rule.right)
} else if(rule.type == 'equivalence') {
printRule(rule.left)
print ' ≡ '
printRule(rule.right)
} else {
printRule(rule)
}
println ''
}
}
private static printRule(rule) {
if(rule instanceof String) {
print "($rule)"
} else if(rule.type == 'literal') {
if(rule.negate) {
print '¬'
}
print rule.value
} else if(rule.type == 'operation' && (rule.operation == '∃' || rule.operation == '∀' )) {
print "$rule.operation$rule.relation"+"."
printRule(rule.definition)
} else if(rule.type == 'operation' && (rule.operation == '≤' || rule.operation == '≥' )) {
print "$rule.operation$rule.amount$rule.relation"+"."
printRule(rule.definition)
} else if(rule.type == 'relation') {
printRule(rule.relation)
print "("
printRule(rule.left)
print ", "
printRule(rule.right)
print ")"
} else {
print '('
printRule(rule.left)
print " $rule.operation "
printRule(rule.right)
print ')'
}
}
}