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
|
package uk.ac.ox.cs.acqua.reasoner
import java.util.LinkedList
import scala.collection.JavaConverters._
import org.semanticweb.HermiT.model.{
Atom,
AtomicConcept,
DLClause,
Variable
}
import org.semanticweb.owlapi.model.{
OWLOntology,
OWLOntologyCreationException,
OWLOntologyManager
}
import uk.ac.ox.cs.JRDFox.JRDFStoreException
// import uk.ac.ox.cs.JRDFox.store.DataStore;
import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType
import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper
import uk.ac.ox.cs.pagoda.query.{
AnswerTuples,
QueryRecord
}
// import uk.ac.ox.cs.pagoda.query.QueryManager;
import uk.ac.ox.cs.pagoda.reasoner.full.Checker
import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine
// import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram;
import uk.ac.ox.cs.pagoda.summary.HermitSummaryFilter
import uk.ac.ox.cs.pagoda.tracking.QueryTracker
// import uk.ac.ox.cs.pagoda.tracking.TrackingRuleEncoder;
import uk.ac.ox.cs.pagoda.util.{
Timer,
Utility
}
import uk.ac.ox.cs.pagoda.util.disposable.{
Disposable,
DisposedException
}
// import uk.ac.ox.cs.pagoda.util.disposable.DisposedException;
/** Consistency checker inspired by [[uk.ac.ox.cs.pagoda.reaseoner.ConsistencyManager]].
*
* @param reasoner an [[AcquaQueryReasoner]] instance
*
* TODO: document public methods and rework the code to be more
* Scala-esque.
*/
class ConsistencyManager(
protected val reasoner: AcquaQueryReasoner
) extends Disposable {
protected val queryManager = reasoner.getQueryManager
private val timer = new Timer()
private var fragmentExtracted = false
private var fullQueryRecord: Option[QueryRecord] = None
private var botQueryRecords = Array.empty[QueryRecord]
private var toAddClauses = new LinkedList[DLClause]()
override def dispose(): Unit = {
super.dispose()
fullQueryRecord.map(_.dispose())
}
/**
*
*/
def extractBottomFragment(): Unit = {
if (isDisposed) throw new DisposedException
if (!fragmentExtracted) {
fragmentExtracted = true
val upperProgram = reasoner.datalog.getUpper
val number = upperProgram.getBottomNumber
if (number <= 1) {
botQueryRecords = Array[QueryRecord](fullQueryRecord.get)
} else {
var record: QueryRecord = null
var tempQueryRecords = new Array[QueryRecord](number-1)
for(i <- 0 until (number-1)) {
record = queryManager.create(QueryRecord.botQueryText.replace("Nothing", s"Nothing${i+1}"), 0, i + 1)
tempQueryRecords(i) = record
var iter: AnswerTuples = null
try {
iter = reasoner.trackingStore.evaluate(record.getQueryText, record.getAnswerVariables)
record updateUpperBoundAnswers iter
} finally {
if (iter != null) iter.dispose()
}
}
var bottomNumber = 0;
val group = (0 until (number-1)).toArray
for(i <- 0 until (number-1))
if (tempQueryRecords(i).isProcessed)
tempQueryRecords(i).dispose()
else if(group(i) == i) {
bottomNumber += 1
record = tempQueryRecords(i)
for (j <- i until (number-1))
if (record hasSameGapAnswers tempQueryRecords(j))
group(j) = i
}
Utility logInfo s"There are $bottomNumber different bottom fragments."
toAddClauses = new LinkedList[DLClause]()
var bottomCounter = 0
botQueryRecords = new Array[QueryRecord](bottomNumber)
val X = Variable.create("X")
for(i <- 0 until (number-1)) {
if (!tempQueryRecords(i).isDisposed() && !tempQueryRecords(i).isProcessed()) {
if (group(i) == i) {
record = tempQueryRecords(i)
botQueryRecords(bottomCounter) = record
bottomCounter += 1
group(i) = bottomCounter
record.resetInfo(
QueryRecord.botQueryText.replace(
"Nothing",
s"Nothing_final$bottomCounter"
), 0, bottomCounter)
toAddClauses.add(
DLClause.create(
Array[Atom](Atom.create(AtomicConcept.create(s"${AtomicConcept.NOTHING.getIRI}_final$bottomCounter"), X)),
Array[Atom](Atom.create(AtomicConcept.create(s"${AtomicConcept.NOTHING.getIRI}${i+1}"), X))
)
)
} else {
toAddClauses.add(
DLClause.create(
Array[Atom](Atom.create(AtomicConcept.create(s"${AtomicConcept.NOTHING.getIRI}_final${group(group(i))}"), X)),
Array[Atom](Atom.create(AtomicConcept.create(s"${AtomicConcept.NOTHING.getIRI}${i+1}"), X))
)
)
tempQueryRecords(i).dispose()
}
}
}
upperProgram updateDependencyGraph toAddClauses
}
val programs: Array[String] = collectTrackingProgramAndImport()
if (programs.length > 0) {
val datastore = reasoner.trackingStore.getDataStore
var oldTripleCount: Long = 0
var tripleCount: Long = 0
try {
val t1 = new Timer();
oldTripleCount = datastore.getTriplesCount
for(program <- programs)
datastore.importRules(program, UpdateType.ScheduleForAddition)
datastore applyReasoning true
tripleCount = datastore.getTriplesCount
Utility logDebug s"tracking store after materialising tracking program: $tripleCount (${tripleCount - oldTripleCount} new)"
Utility logDebug s"tracking store finished the materialisation of tracking program in ${t1.duration()} seconds."
extractAxioms()
datastore.clearRulesAndMakeFactsExplicit()
} catch {
case e: JRDFStoreException => e.printStackTrace()
case e: OWLOntologyCreationException => e.printStackTrace()
}
}
}
}
/**
*
* @note provided for compatibility reasons
*/
val getQueryRecords: Array[QueryRecord] = botQueryRecords
/** RL lower bound check for satisfiability. */
lazy val checkRLLowerBound: Boolean = {
if (isDisposed) throw new DisposedException
val record: QueryRecord = queryManager.create(QueryRecord.botQueryText, 0)
fullQueryRecord = Some(record)
var iter: AnswerTuples = null
try {
iter = reasoner.rlLowerStore.evaluate(record.getQueryText, record.getAnswerVariables)
record updateLowerBoundAnswers iter
} finally {
iter.dispose()
}
if (record.getNoOfSoundAnswers > 0) {
Utility logInfo s"Answers to bottom in the lower bound: ${record.outputSoundAnswerTuple}"
}
record.getNoOfSoundAnswers <= 0
}
/** ELHO lower bound check for satisfiability */
lazy val checkELLowerBound: Boolean = {
if (isDisposed) throw new DisposedException
val record: QueryRecord = fullQueryRecord.get
val answers: AnswerTuples =
reasoner.elLowerStore.evaluate(
record.getQueryText,
record.getAnswerVariables
)
record updateLowerBoundAnswers answers
if (record.getNoOfSoundAnswers > 0) {
Utility logInfo s"Answers to bottom in the lower bound: ${record.outputSoundAnswerTuple}"
}
record.getNoOfSoundAnswers <= 0
}
/**
*
*/
def checkUpper(upperStore: BasicQueryEngine): Boolean = {
if (isDisposed) throw new DisposedException
val record = fullQueryRecord.get
if (upperStore != null) {
var tuples: AnswerTuples = null
try {
tuples = upperStore.evaluate(record.getQueryText, record.getAnswerVariables)
if (!tuples.isValid) {
Utility logInfo s"There are no contradictions derived in ${upperStore.getName} materialisation."
Utility logDebug "The ontology and dataset is satisfiable."
return true
}
} finally {
if (tuples != null) tuples.dispose()
}
}
false
}
/** True if the KB associate with the [[reasoner]] is consistent.
*
* This is the main entry point of the consistency checker.
*/
lazy val check: Boolean = {
if (isDisposed) throw new DisposedException
val record = fullQueryRecord.get
var tuples: AnswerTuples = null
try {
tuples = reasoner.trackingStore.evaluate(
record.getQueryText,
record.getAnswerVariables
)
record updateUpperBoundAnswers tuples
} finally {
if (tuples != null) tuples.dispose()
}
var satisfiability = true
if (record.getNoOfCompleteAnswers != 0) {
extractBottomFragment()
try {
extractAxioms4Full()
} catch {
case e: OWLOntologyCreationException => e.printStackTrace()
}
var checker: Checker = null
for (r <- getQueryRecords) {
checker = new HermitSummaryFilter(r, true)
satisfiability = checker.isConsistent()
checker.dispose()
}
}
satisfiability
}
/**
*
*/
private def extractAxioms4Full(): Unit = {
val manager: OWLOntologyManager =
reasoner.encoder.get
.getProgram
.getOntology
.getOWLOntologyManager
val fullOntology: OWLOntology = manager.createOntology()
for (record <- botQueryRecords) {
for (clause <- record.getRelevantClauses.asScala) {
fullQueryRecord.get addRelevantClauses clause
}
manager.addAxioms(
fullOntology,
record.getRelevantOntology.getAxioms()
)
}
fullQueryRecord.get.setRelevantOntology(fullOntology)
}
/**
*
*/
private def extractAxioms(): Unit = {
val manager: OWLOntologyManager =
reasoner.encoder.get
.getProgram
.getOntology
.getOWLOntologyManager
for (record <- botQueryRecords) {
record setRelevantOntology manager.createOntology()
val tracker: QueryTracker = new QueryTracker(reasoner.encoder.get, reasoner.rlLowerStore, record)
reasoner.encoder.get setCurrentQuery record
tracker extractAxioms reasoner.trackingStore
Utility logInfo s"finish extracting axioms for bottom ${record.getQueryID}"
}
}
/**
*
*/
private def collectTrackingProgramAndImport(): Array[String] = {
val programs = new Array[String](botQueryRecords.length)
val encoder = reasoner.encoder.get
val currentClauses: LinkedList[DLClause] = new LinkedList[DLClause]()
botQueryRecords.zipWithIndex map { case (record,i) => {
encoder setCurrentQuery record
val builder = new StringBuilder(encoder.getTrackingProgram)
val currentClauses = toAddClauses.asScala.filter{clause =>
clause.getHeadAtom(0).getDLPredicate.toString().contains(s"_final${i + 1}")
}.asJava
builder append (DLClauseHelper toString currentClauses)
builder.toString
}}
}
}
|