-
Notifications
You must be signed in to change notification settings - Fork 0
/
naivesolver.go
361 lines (342 loc) · 10.3 KB
/
naivesolver.go
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// The MIT License (MIT)
//
// Copyright (c) 2016, 2017, 2018 Fabian Wenzelmann
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package goel
import (
"log"
"sync"
"github.com/FabianWe/goel/domains"
)
// TODO redefine set interfaces s.t. that accept the components as well
// Functions that check if a rule is applicable.
func CheckCR1(gci *NormalizedCI, sc *BCSet) bool {
// TODO remove once tested
if gci.C2 != nil {
log.Printf("Invalid gci for CR1, must be of the form C ⊑ D, but got C1 ⊓ C2 ⊑ D: %v\n", gci)
return false
}
return sc.Contains(gci.C1)
}
func CheckCR2(gci *NormalizedCI, sc *BCSet) bool {
// TODO remove once tested
if gci.C2 == nil {
log.Printf("Invalid gci for CR1, must be of the form C1 ⊓ C2 ⊑ D, but got C ⊑ D: %v\n", gci)
return false
}
return sc.Contains(gci.C1) && sc.Contains(gci.C2)
}
func CheckCR3(gci *NormalizedCIRightEx, sc *BCSet) bool {
return sc.Contains(gci.C1)
}
func CheckCR4(gci *NormalizedCILeftEx, c, d Concept, sd *BCSet, sr *BCPairSet) bool {
return sr.Contains(c, d) && sd.Contains(gci.C1)
}
func CheckCR5(c, d Concept, sd *BCSet, sr *BCPairSet) bool {
return sd.Contains(Bottom) && sr.Contains(c, d)
}
// func CheckCR6(a NominalConcept, c, d Concept, sc, sd *BCSet, search *GraphSearcher, components *ELBaseComponents) bool {
// return sc.Contains(a) && sd.Contains(a) && search.Search(c.NormalizedID(components), d.NormalizedID(components))
// }
func CheckCR10(sr BCPairSet, c, d Concept) bool {
return sr.Contains(c, d)
}
func CheckCR11(sr1, sr2 *BCPairSet, c, d, e Concept) bool {
return sr1.Contains(c, d) && sr2.Contains(d, e)
}
// TODO remove map interfaces? not required I guess...
type NaiveSolver struct {
S []*BCSet
R []*BCPairSet
graph ConceptGraph
// set in init
searcher *GraphSearcher
searchMethod ReachabilitySearch
}
func NewNaiveSolver(graph ConceptGraph, search ReachabilitySearch) *NaiveSolver {
return &NaiveSolver{
S: nil,
R: nil,
graph: graph,
searcher: nil,
searchMethod: search,
}
}
func (solver *NaiveSolver) init(c *ELBaseComponents) {
// initialize stuff, we do that concurrently
var wg sync.WaitGroup
wg.Add(4)
// we use + 1 here because we want to use the normalized id directly, so
// the bottom concept must be taken into consideration
numBCD := c.NumBCD() + 1
// fmt.Printf("Got %d BCD elements and %d roles\n", numBCD, c.Roles)
go func() {
solver.graph.Init(numBCD)
wg.Done()
}()
go func() {
solver.S = make([]*BCSet, numBCD)
// strictly speaking the bottom concept is not part of this and so
// will be ignored
// for the top concept we initialize S(⊤) = {⊤}
// and for all other concepts C we initialize S(C) = {C, ⊤}
solver.S[1] = NewBCSet(c, 10)
solver.S[1].Add(Top)
var i uint = 2
for ; i < numBCD; i++ {
solver.S[i] = NewBCSet(c, 10)
solver.S[i].Add(Top)
solver.S[i].Add(c.GetConcept(i))
}
wg.Done()
}()
go func() {
solver.R = make([]*BCPairSet, c.Roles)
var i uint = 0
for ; i < c.Roles; i++ {
solver.R[i] = NewBCPairSet(c, 10)
}
wg.Done()
}()
go func() {
solver.searcher = NewGraphSearcher(solver.searchMethod, c)
wg.Done()
}()
wg.Wait()
}
func (solver *NaiveSolver) updateR(r Role, c, d Concept, bc *ELBaseComponents) bool {
cID, dID := c.NormalizedID(bc), d.NormalizedID(bc)
if solver.R[uint(r)].AddID(cID, dID) {
// update graph as well
solver.graph.AddEdge(cID, dID)
return true
}
return false
}
// check again!
func (solver *NaiveSolver) Solve(tbox *NormalizedTBox, manager *domains.CDManager) {
solver.init(tbox.Components)
changed := true
for changed {
changed = false
// no try to apply each rule, if anything changes set changed to true
// first try to apply rules CR1 - CR4, for each cgi there is only one
// rule we can apply here
// so first we iterate over all cgis of the form C1 ⊑ D and C1 ⊓ C2 ⊑ D
for _, gci := range tbox.CIs {
if gci.C2 == nil {
// try CR1
for _, sc := range solver.S[1:] {
if CheckCR1(gci, sc) {
// add to sc
if sc.Add(gci.D) {
changed = true
}
}
}
} else {
// try CR2
for _, sc := range solver.S[1:] {
if CheckCR2(gci, sc) {
// add to sc
if sc.Add(gci.D) {
changed = true
}
}
}
}
}
// now try rule CR3
for _, gci := range tbox.CIRight {
for c, sc := range solver.S[1:] {
if CheckCR3(gci, sc) {
// add
r := gci.R
conceptC, conceptD := tbox.Components.GetConcept(uint(c+1)), gci.C2
if solver.updateR(r, conceptC, conceptD, tbox.Components) {
changed = true
}
}
}
}
// now try rule CR4
// don't use CheckCR4, this way is faster
for _, gci := range tbox.CILeft {
// get the set R(r)
sr := solver.R[uint(gci.R)]
// iterate each pair (C, D) in R(r), then only check if D'
// is in S(D)
for p, _ := range sr.M {
sd := solver.S[p.Second]
if sd.Contains(gci.C1) {
// add
sc := solver.S[p.First]
if sc.Add(gci.D) {
changed = true
}
}
}
}
// now try rule CR5
for _, sr := range solver.R {
// iterate over each pair (C, D) in R(r) and test if ⊥ is in S(D)
for p, _ := range sr.M {
sd := solver.S[p.Second]
if sd.Contains(Bottom) {
sc := solver.S[p.First]
if sc.Add(Bottom) {
changed = true
}
}
}
}
// now try rule CR6
// TODO think this case through again!
// iterate over each nominal
var nextNominal uint = 0
// bottom concept has a place in S, but is not a part of it (see init)
// thus we wish to remove the first element from solver.S
for ; nextNominal < tbox.Components.Nominals; nextNominal++ {
nominal := NewNominalConcept(nextNominal)
// iterate over each S(C) and S(D)
// to do so we iterate over the id of each concept
var i uint = 1
// we start the search with i = 1 and iterate over all possible concept
// ids
for ; i < uint(len(solver.S)); i++ {
sc := solver.S[i]
if sc.Contains(nominal) {
var j uint = i + 1
for ; j < uint(len(solver.S)); j++ {
sd := solver.S[j]
if sd.Contains(nominal) {
// check if C ↝ D
// we also have to check if D ↝ C because we just check
// all pairs (i, j) where j > i.
// Thus we could have that i is not connected to j
// but j is connected to i. In this case we must apply the rule!
searchRes := solver.searcher.BidrectionalSearch(solver.graph, i, j)
switch searchRes {
case BidrectionalBoth:
// update both
// TODO could be done simpler because they should be equal
// but well...
if sc.Union(sd) {
changed = true
}
if sd.Union(sc) {
changed = true
}
case BidrectionalDirect:
// update only first
if sc.Union(sd) {
changed = true
}
case BidrectionalReverse:
if sd.Union(sc) {
changed = true
}
// no default case, we simply do nothing
}
}
}
}
}
}
// now try CR7 and CR8
var i uint = 1
// but only if there are some concrete domain extensions
if tbox.Components.CDExtensions != 0 {
// iterate over each c
for ; i < uint(len(solver.S)); i++ {
// get conjunction
sc := solver.S[i]
conjunctions := sc.GetCDConjunction(manager)
if len(conjunctions) != 1 {
panic("Only support for one concrete domain at the moment")
}
conjunction := conjunctions[0]
// get domain
domain := manager.GetDomainByID(0)
// check if unsatisfiable, if yes apply CR7 and add false
if !domain.ConjSat(conjunction...) {
// a little bit nicer here than before by avoid the if...
changed = sc.Add(Bottom) || changed
// add all formulae from this domain because false implies everything
// (rule CR8)
// this must also include the new formula of course
for _, formula := range manager.GetFormulaeFor(0) {
formulaID := formula.FormulaID
asExtension := NewConcreteDomainExtension(formulaID)
// now add
changed = sc.Add(asExtension) || changed
}
} else {
// can't apply CR7 and we have to check CR8 for each formula
for _, formula := range manager.GetFormulaeFor(0) {
// check if the implication is true
if domain.Implies(formula.Formula, conjunction...) {
// add
formulaID := formula.FormulaID
asExtension := NewConcreteDomainExtension(formulaID)
// add
changed = sc.Add(asExtension) || changed
}
}
}
}
}
// now try rule CR10 and CR11
i = 0
for ; i < uint(len(tbox.RIs)); i++ {
ri := tbox.RIs[i]
if ri.R2 == NoRole {
// rule CR10
r, s := ri.R1, ri.S
// iterate over each pair in R(r)
rr := solver.R[uint(r)]
rs := solver.R[uint(s)]
for pair, _ := range rr.M {
if rs.AddID(pair.First, pair.Second) {
changed = true
}
}
} else {
// rule CR11
r1, r2, r3 := ri.R1, ri.R2, ri.S
rr1 := solver.R[uint(r1)]
rr2 := solver.R[uint(r2)]
rr3 := solver.R[uint(r3)]
// iterate over each pair (C1, D1) in R(r1)
for pair1, _ := range rr1.M {
// iterate over each pair (C2, D2) in R(r2) and check if D1 = C2
for pair2, _ := range rr2.M {
if pair1.Second == pair2.First {
// add (C1, D2) to R(r3)
if rr3.AddID(pair1.First, pair2.Second) {
changed = true
}
}
}
}
}
}
}
}