diff options
| author | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
|---|---|---|
| committer | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
| commit | 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch) | |
| tree | 47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java | 472 |
1 files changed, 472 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java b/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java new file mode 100644 index 0000000..0e19e30 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/tracking/QueryTracker.java | |||
| @@ -0,0 +1,472 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import java.util.HashSet; | ||
| 4 | import java.util.Iterator; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Set; | ||
| 7 | |||
| 8 | import org.semanticweb.HermiT.model.DLClause; | ||
| 9 | import org.semanticweb.owlapi.model.IRI; | ||
| 10 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 11 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 12 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 13 | import org.semanticweb.owlapi.model.OWLDataProperty; | ||
| 14 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 15 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 16 | import org.semanticweb.owlapi.model.OWLObject; | ||
| 17 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 18 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 19 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 20 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 21 | |||
| 22 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 23 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 24 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 25 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 26 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 27 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 28 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager; | ||
| 29 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 30 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 31 | import uk.ac.ox.cs.pagoda.util.UFS; | ||
| 32 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 33 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 34 | import uk.ac.ox.cs.JRDFox.model.Datatype; | ||
| 35 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 36 | import uk.ac.ox.cs.JRDFox.store.Resource; | ||
| 37 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 38 | |||
| 39 | public class QueryTracker { | ||
| 40 | |||
| 41 | QueryRecord m_record; | ||
| 42 | BasicQueryEngine m_dataStore; | ||
| 43 | TrackingRuleEncoder m_encoder; | ||
| 44 | |||
| 45 | OWLOntologyManager m_manager; | ||
| 46 | Set<OWLAxiom> m_tBoxAxioms; | ||
| 47 | |||
| 48 | UFS<String> equalityGroups; | ||
| 49 | |||
| 50 | public QueryTracker(TrackingRuleEncoder encoder, BasicQueryEngine lowerStore, QueryRecord queryRecord) { | ||
| 51 | m_encoder = encoder; | ||
| 52 | m_dataStore = lowerStore; | ||
| 53 | m_record = queryRecord; | ||
| 54 | |||
| 55 | m_manager = m_encoder.getOntology().getOWLOntologyManager(); | ||
| 56 | equalityGroups = m_dataStore.getEqualityGroups(); | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | public OWLOntology extract(BasicQueryEngine trackingStore, QueryRecord[] botQuerRecords, boolean incrementally) { | ||
| 61 | try { | ||
| 62 | if (m_record.getRelevantOntology() != null) { | ||
| 63 | m_manager.removeOntology(m_record.getRelevantOntology()); | ||
| 64 | m_record.setRelevantOntology(null); | ||
| 65 | m_record.clearClauses(); | ||
| 66 | } | ||
| 67 | m_record.setRelevantOntology(m_manager.createOntology()); | ||
| 68 | } catch (OWLOntologyCreationException e) { | ||
| 69 | e.printStackTrace(); | ||
| 70 | } | ||
| 71 | |||
| 72 | m_encoder.setCurrentQuery(m_record); | ||
| 73 | |||
| 74 | // m_encoder.encodingQuery(botQuerRecords); | ||
| 75 | // m_encoder.saveTrackingRules("tracking_query" + m_record.getQueryID() + ".dlog"); | ||
| 76 | |||
| 77 | DataStore store = trackingStore.getDataStore(); | ||
| 78 | long oldTripleCount, tripleCount; | ||
| 79 | try { | ||
| 80 | Timer t1 = new Timer(); | ||
| 81 | oldTripleCount = store.getTriplesCount(); | ||
| 82 | // store.addRules(new String[] {m_encoder.getTrackingProgram()}); | ||
| 83 | store.importRules(m_encoder.getTrackingProgram()); | ||
| 84 | store.applyReasoning(incrementally); | ||
| 85 | tripleCount = store.getTriplesCount(); | ||
| 86 | |||
| 87 | Utility.logDebug("tracking store after materialising tracking program: " | ||
| 88 | + tripleCount | ||
| 89 | + " (" | ||
| 90 | + (tripleCount - oldTripleCount) | ||
| 91 | + " new)"); | ||
| 92 | Utility.logDebug("tracking store finished the materialisation of tracking program in " | ||
| 93 | + t1.duration() + " seconds."); | ||
| 94 | } catch (JRDFStoreException e) { | ||
| 95 | e.printStackTrace(); | ||
| 96 | } | ||
| 97 | |||
| 98 | extractAxioms(trackingStore); | ||
| 99 | |||
| 100 | trackingStore.clearRulesAndIDBFacts(m_encoder.getAddedData()); | ||
| 101 | |||
| 102 | if (!m_record.isBottom() | ||
| 103 | && !(m_encoder instanceof TrackingRuleEncoderDisj2)) | ||
| 104 | addRelatedAxiomsAndClauses(botQuerRecords); | ||
| 105 | |||
| 106 | return m_record.getRelevantOntology(); | ||
| 107 | } | ||
| 108 | |||
| 109 | public void extractAxioms(BasicQueryEngine trackingStore) { | ||
| 110 | Set<String> unaryPredicates = new HashSet<String>(); | ||
| 111 | Set<String> binaryPredicates = new HashSet<String>(); | ||
| 112 | |||
| 113 | getDerivedPredicates(trackingStore, unaryPredicates, binaryPredicates); | ||
| 114 | |||
| 115 | /** | ||
| 116 | * To add all the relavant ABox assertions to the fragment | ||
| 117 | */ | ||
| 118 | |||
| 119 | int aboxAxiomCounter = extractUnaryTuples(trackingStore, | ||
| 120 | m_manager.getOWLDataFactory(), unaryPredicates) | ||
| 121 | + extractBinaryTuples(trackingStore, | ||
| 122 | m_manager.getOWLDataFactory(), binaryPredicates); | ||
| 123 | |||
| 124 | /** | ||
| 125 | * To all all the relavant TBox rules to the fragment | ||
| 126 | */ | ||
| 127 | String queryText = m_encoder.getSelectedSPARQLQuery(); | ||
| 128 | |||
| 129 | DLClause clause; | ||
| 130 | m_tBoxAxioms = new HashSet<OWLAxiom>(); | ||
| 131 | OWLAxiom tBoxAxiom; | ||
| 132 | TupleIterator answers = null; | ||
| 133 | String answer; | ||
| 134 | try { | ||
| 135 | answers = trackingStore.internal_evaluate(queryText); | ||
| 136 | for (long multi = answers.open(); multi != 0; multi = answers.getNext()) { | ||
| 137 | answer = answers.getResource(0).m_lexicalForm; | ||
| 138 | clause = m_encoder.getSelectedClause(MyPrefixes.PAGOdAPrefixes.expandIRI(answer)); | ||
| 139 | if (DLClauseHelper.isTautologyAboutDifferentFrom(clause)) | ||
| 140 | continue; | ||
| 141 | tBoxAxiom = m_encoder.getProgram().getEquivalentAxiom(clause); | ||
| 142 | m_record.addRelevantClauses(clause); | ||
| 143 | m_tBoxAxioms.add(tBoxAxiom); | ||
| 144 | } | ||
| 145 | } catch (JRDFStoreException e) { | ||
| 146 | e.printStackTrace(); | ||
| 147 | } finally { | ||
| 148 | if (answers != null) | ||
| 149 | answers.dispose(); | ||
| 150 | } | ||
| 151 | |||
| 152 | Utility.logTrace("Extracted TBox axioms: "); | ||
| 153 | for (OWLAxiom axiom : m_tBoxAxioms) { | ||
| 154 | Utility.logTrace(axiom); | ||
| 155 | m_manager.addAxiom(m_record.getRelevantOntology(), axiom); | ||
| 156 | } | ||
| 157 | Utility.logDebug("TBox extraction Done"); | ||
| 158 | |||
| 159 | Utility.logDebug("Before adding bottom fragment:\nABoxAxiomsCount = " | ||
| 160 | + aboxAxiomCounter + ", TBoxAxiomsCount = " | ||
| 161 | + m_tBoxAxioms.size()); | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | private int extractBinaryTuples(BasicQueryEngine trackingStore, OWLDataFactory factory, Set<String> binaryPredicates) { | ||
| 166 | OWLOntology fragment = m_record.getRelevantOntology(); | ||
| 167 | int count; | ||
| 168 | int aboxAxiomCounter = 0; | ||
| 169 | Resource sub, obj; | ||
| 170 | OWLAxiom aboxAxiom; | ||
| 171 | String trackingIRI; | ||
| 172 | Set<Integer> trackedIDEqualities = new HashSet<Integer>(); | ||
| 173 | Set<String> trackedEntityEqualities = new HashSet<String>(); | ||
| 174 | TupleIterator trackingAnswers, lowerAnswers; | ||
| 175 | |||
| 176 | for (Iterator<String> iter = binaryPredicates.iterator(); iter.hasNext(); ) { | ||
| 177 | trackingIRI = iter.next(); | ||
| 178 | String propIRI = m_encoder.getOriginalPredicate(trackingIRI); | ||
| 179 | if (propIRI == null) continue; | ||
| 180 | if (!propIRI.equals(Namespace.EQUALITY_QUOTED)) continue; | ||
| 181 | trackingAnswers = null; | ||
| 182 | try { | ||
| 183 | trackingAnswers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Binary(trackingIRI)); | ||
| 184 | for (long multi = trackingAnswers.open(); multi != 0; multi = trackingAnswers.getNext()) { | ||
| 185 | if (trackingAnswers.getResourceID(0) != trackingAnswers.getResourceID(1)) { | ||
| 186 | for (int i = 0; i < 2; ++i) | ||
| 187 | if (trackedIDEqualities.add(trackingAnswers.getResourceID(i))) { | ||
| 188 | trackedEntityEqualities.add(trackingAnswers.getResource(i).m_lexicalForm); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } catch (JRDFStoreException e) { | ||
| 193 | // TODO Auto-generated catch block | ||
| 194 | e.printStackTrace(); | ||
| 195 | } finally { | ||
| 196 | if (trackingAnswers != null) trackingAnswers.dispose(); | ||
| 197 | } | ||
| 198 | iter.remove(); | ||
| 199 | break; | ||
| 200 | } | ||
| 201 | |||
| 202 | String sub_rep, obj_rep; | ||
| 203 | |||
| 204 | for (Iterator<String> iter = binaryPredicates.iterator(); iter.hasNext(); ) { | ||
| 205 | trackingIRI = iter.next(); | ||
| 206 | count = 0; | ||
| 207 | String propIRI = m_encoder.getOriginalPredicate(trackingIRI); | ||
| 208 | if (propIRI == null) continue; | ||
| 209 | iter.remove(); | ||
| 210 | lowerAnswers = null; trackingAnswers = null; | ||
| 211 | Set<String> lower = new HashSet<String>(); | ||
| 212 | OWLObject prop = null; | ||
| 213 | try { | ||
| 214 | trackingAnswers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Binary(trackingIRI)); | ||
| 215 | trackingAnswers.open(); | ||
| 216 | if (trackingAnswers.getMultiplicity() == 0) continue; | ||
| 217 | |||
| 218 | lowerAnswers = m_dataStore.internal_evaluateNotExpanded(getSPARQLQuery4Binary(propIRI)); | ||
| 219 | lowerAnswers.open(); | ||
| 220 | if (lowerAnswers.getMultiplicity() == 0) continue; | ||
| 221 | |||
| 222 | StringBuilder builder = new StringBuilder(); | ||
| 223 | for (long multi = lowerAnswers.getMultiplicity(); multi != 0; multi = lowerAnswers.getNext()) { | ||
| 224 | sub = lowerAnswers.getResource(0); | ||
| 225 | obj = lowerAnswers.getResource(1); | ||
| 226 | builder.setLength(0); | ||
| 227 | builder.append(equalityGroups.find(sub.m_lexicalForm)).append(AnswerTuple.SEPARATOR).append(equalityGroups.find(obj.m_lexicalForm)); | ||
| 228 | lower.add(builder.toString()); | ||
| 229 | } | ||
| 230 | |||
| 231 | for (long multi = trackingAnswers.getMultiplicity(); multi != 0; multi = trackingAnswers.getNext()) { | ||
| 232 | sub = trackingAnswers.getResource(0); | ||
| 233 | obj = trackingAnswers.getResource(1); | ||
| 234 | builder.setLength(0); | ||
| 235 | sub_rep = equalityGroups.find(sub.m_lexicalForm); | ||
| 236 | obj_rep = equalityGroups.find(obj.m_lexicalForm); | ||
| 237 | if (!sub_rep.equals(sub.m_lexicalForm) || !obj_rep.equals(obj.m_lexicalForm)) continue; | ||
| 238 | |||
| 239 | builder.append(sub_rep).append(AnswerTuple.SEPARATOR).append(obj_rep); | ||
| 240 | if (lower.contains(builder.toString())) { | ||
| 241 | OWLObject owlObj = getOWLObject(obj, factory); | ||
| 242 | if (owlObj instanceof OWLIndividual) { | ||
| 243 | if (prop == null) | ||
| 244 | prop = factory.getOWLObjectProperty(IRI.create(propIRI.startsWith("<") ? OWLHelper.removeAngles(propIRI) : propIRI)); | ||
| 245 | aboxAxiom = factory.getOWLObjectPropertyAssertionAxiom( | ||
| 246 | (OWLObjectProperty) prop, | ||
| 247 | factory.getOWLNamedIndividual(IRI.create(sub_rep)), | ||
| 248 | factory.getOWLNamedIndividual(IRI.create(obj_rep))); | ||
| 249 | } | ||
| 250 | else if (owlObj instanceof OWLLiteral) { | ||
| 251 | if (prop == null) | ||
| 252 | prop = factory.getOWLDataProperty(IRI.create(propIRI.startsWith("<") ? OWLHelper.removeAngles(propIRI) : propIRI)); | ||
| 253 | aboxAxiom = factory.getOWLDataPropertyAssertionAxiom( | ||
| 254 | (OWLDataProperty) prop, | ||
| 255 | factory.getOWLNamedIndividual(IRI.create(sub_rep)), | ||
| 256 | (OWLLiteral) owlObj); | ||
| 257 | } | ||
| 258 | else { | ||
| 259 | Utility.logError("There might be an error here ... "); | ||
| 260 | continue; | ||
| 261 | } | ||
| 262 | if (!fragment.containsAxiom(aboxAxiom)) { | ||
| 263 | m_manager.addAxiom(fragment, aboxAxiom); | ||
| 264 | ++aboxAxiomCounter; | ||
| 265 | ++count; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | } | ||
| 269 | } catch (JRDFStoreException e) { | ||
| 270 | e.printStackTrace(); | ||
| 271 | } finally { | ||
| 272 | if (trackingAnswers != null) trackingAnswers.dispose(); | ||
| 273 | if (lowerAnswers != null) lowerAnswers.dispose(); | ||
| 274 | lower.clear(); | ||
| 275 | } | ||
| 276 | Utility.logDebug("property: " + propIRI + " " + count); | ||
| 277 | } | ||
| 278 | |||
| 279 | count = 0; | ||
| 280 | String value; | ||
| 281 | OWLObjectProperty sameAs = factory.getOWLObjectProperty(IRI.create(Namespace.EQUALITY)); | ||
| 282 | for (String key: equalityGroups.keySet()) { | ||
| 283 | if (!trackedEntityEqualities.contains(key)) continue; | ||
| 284 | value = equalityGroups.find(key); | ||
| 285 | m_manager.addAxiom(fragment, factory.getOWLObjectPropertyAssertionAxiom( | ||
| 286 | sameAs, | ||
| 287 | factory.getOWLNamedIndividual(IRI.create(key)), | ||
| 288 | factory.getOWLNamedIndividual(IRI.create(value)))); | ||
| 289 | ++aboxAxiomCounter; | ||
| 290 | ++count; | ||
| 291 | } | ||
| 292 | Utility.logDebug("property: " + Namespace.EQUALITY_QUOTED + " " + count); | ||
| 293 | |||
| 294 | trackedEntityEqualities.clear(); | ||
| 295 | trackedIDEqualities.clear(); | ||
| 296 | Utility.logTrace(Namespace.EQUALITY_QUOTED + " " + count); | ||
| 297 | |||
| 298 | Utility.logDebug("ABox extraction Done"); | ||
| 299 | return aboxAxiomCounter; | ||
| 300 | } | ||
| 301 | |||
| 302 | private OWLObject getOWLObject(Resource rdfoxTerm, OWLDataFactory factory) { | ||
| 303 | if (rdfoxTerm.m_datatype.equals(Datatype.IRI_REFERENCE)) | ||
| 304 | return factory.getOWLNamedIndividual(IRI.create(rdfoxTerm.m_lexicalForm)); | ||
| 305 | // if (rdfoxTerm.m_datatype.equals(Datatype.OWL_REAL) || rdfoxTerm.m_datatype.equals(Datatype.OWL_RATIONAL) || | ||
| 306 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_DECIMAL) || rdfoxTerm.m_datatype.equals(Datatype.XSD_INTEGER) || | ||
| 307 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_NON_NEGATIVE_INTEGER) || rdfoxTerm.m_datatype.equals(Datatype.XSD_POSITIVE_INTEGER) || | ||
| 308 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_NEGATIVE_INTEGER) || rdfoxTerm.m_datatype.equals(Datatype.XSD_LONG) || | ||
| 309 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_INT) || rdfoxTerm.m_datatype.equals(Datatype.XSD_SHORT) || | ||
| 310 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_BYTE) || rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_LONG) || | ||
| 311 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_INT) || rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_SHORT) || | ||
| 312 | // rdfoxTerm.m_datatype.equals(Datatype.XSD_UNSIGNED_BYTE)) | ||
| 313 | if (rdfoxTerm.m_datatype.equals(Datatype.XSD_DATE)) | ||
| 314 | return factory.getOWLLiteral(rdfoxTerm.m_lexicalForm, factory.getOWLDatatype(IRI.create(Namespace.XSD_STRING))); | ||
| 315 | |||
| 316 | else return factory.getOWLLiteral(rdfoxTerm.m_lexicalForm, factory.getOWLDatatype(IRI.create(rdfoxTerm.m_datatype.getIRI()))); | ||
| 317 | } | ||
| 318 | |||
| 319 | private int extractUnaryTuples(BasicQueryEngine trackingStore, OWLDataFactory factory, Set<String> unaryPredicates) { | ||
| 320 | OWLOntology fragment = m_record.getRelevantOntology(); | ||
| 321 | int count; | ||
| 322 | int aboxAxiomCounter = 0; | ||
| 323 | String answer; | ||
| 324 | OWLAxiom aboxAxiom; | ||
| 325 | for (String trackingIRI : unaryPredicates) { | ||
| 326 | count = 0; | ||
| 327 | String clsIRI = m_encoder.getOriginalPredicate(trackingIRI); | ||
| 328 | if (clsIRI == null) | ||
| 329 | continue; | ||
| 330 | TupleIterator answers = null, lowerAnswers = null; | ||
| 331 | Set<String> lower = new HashSet<String>(); | ||
| 332 | OWLClass cls = factory | ||
| 333 | .getOWLClass(IRI.create(clsIRI.startsWith("<") ? OWLHelper | ||
| 334 | .removeAngles(clsIRI) : clsIRI)); | ||
| 335 | try { | ||
| 336 | answers = trackingStore.internal_evaluateAgainstIDBs(getSPARQLQuery4Unary(trackingIRI)); | ||
| 337 | answers.open(); | ||
| 338 | if (answers.getMultiplicity() == 0) continue; | ||
| 339 | |||
| 340 | lowerAnswers = m_dataStore.internal_evaluateNotExpanded(getSPARQLQuery4Unary(clsIRI)); | ||
| 341 | lowerAnswers.open(); | ||
| 342 | if (lowerAnswers.getMultiplicity() == 0) continue; | ||
| 343 | |||
| 344 | for (long multi = lowerAnswers.getMultiplicity(); multi != 0; multi = lowerAnswers.getNext()) | ||
| 345 | lower.add(equalityGroups.find(lowerAnswers.getResource(0).m_lexicalForm)); | ||
| 346 | |||
| 347 | for (long multi = answers.getMultiplicity(); multi != 0; multi = answers.getNext()) { | ||
| 348 | answer = equalityGroups.find(answers.getResource(0).m_lexicalForm); | ||
| 349 | if (lower.contains(answer)) { | ||
| 350 | OWLIndividual instance = factory.getOWLNamedIndividual(IRI.create(answer)); | ||
| 351 | aboxAxiom = factory.getOWLClassAssertionAxiom(cls,instance); | ||
| 352 | if (!fragment.containsAxiom(aboxAxiom)) { | ||
| 353 | m_manager.addAxiom(fragment, aboxAxiom); | ||
| 354 | ++aboxAxiomCounter; | ||
| 355 | ++count; | ||
| 356 | } | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } catch (JRDFStoreException e) { | ||
| 360 | e.printStackTrace(); | ||
| 361 | } finally { | ||
| 362 | if (answers != null) | ||
| 363 | answers.dispose(); | ||
| 364 | if (lowerAnswers != null) | ||
| 365 | lowerAnswers.dispose(); | ||
| 366 | lower.clear(); | ||
| 367 | } | ||
| 368 | Utility.logDebug("class: " + clsIRI + " " + count); | ||
| 369 | } | ||
| 370 | return aboxAxiomCounter; | ||
| 371 | } | ||
| 372 | |||
| 373 | private void getDerivedPredicates(BasicQueryEngine trackingStore, | ||
| 374 | Set<String> unaryPredicates, Set<String> binaryPredicates) { | ||
| 375 | |||
| 376 | TupleIterator derivedTuples = null; | ||
| 377 | String selectedPredicate = OWLHelper.addAngles(m_encoder.getSelectedPredicate()); | ||
| 378 | try { | ||
| 379 | derivedTuples = trackingStore | ||
| 380 | .internal_evaluateAgainstIDBs("select distinct ?z where { ?x <" | ||
| 381 | + Namespace.RDF_TYPE + "> ?z . }"); | ||
| 382 | for (long multi = derivedTuples.open(); multi != 0; multi = derivedTuples.getNext()) { | ||
| 383 | String p = RDFoxTripleManager.getQuotedTerm(derivedTuples.getResource(0)); | ||
| 384 | if (p.equals(selectedPredicate)) | ||
| 385 | ; | ||
| 386 | else if (m_encoder.isAuxPredicate(p)) | ||
| 387 | ; | ||
| 388 | else | ||
| 389 | unaryPredicates.add(p); | ||
| 390 | } | ||
| 391 | } catch (JRDFStoreException e) { | ||
| 392 | e.printStackTrace(); | ||
| 393 | } finally { | ||
| 394 | if (derivedTuples != null) | ||
| 395 | derivedTuples.dispose(); | ||
| 396 | } | ||
| 397 | |||
| 398 | derivedTuples = null; | ||
| 399 | try { | ||
| 400 | derivedTuples = trackingStore | ||
| 401 | .internal_evaluateAgainstIDBs("select distinct ?y where { ?x ?y ?z . }"); | ||
| 402 | for (long multi = derivedTuples.open(); multi != 0; multi = derivedTuples.getNext()) { | ||
| 403 | String p = RDFoxTripleManager.getQuotedTerm(derivedTuples.getResource(0)); | ||
| 404 | if (p.equals(Namespace.RDF_TYPE_ABBR) | ||
| 405 | || p.equals(Namespace.RDF_TYPE_QUOTED)) | ||
| 406 | ; | ||
| 407 | else if (p.equals(Namespace.EQUALITY_ABBR) | ||
| 408 | || p.equals(Namespace.EQUALITY_QUOTED)) | ||
| 409 | ; | ||
| 410 | else if (m_encoder.isAuxPredicate(p)) | ||
| 411 | ; | ||
| 412 | else | ||
| 413 | binaryPredicates.add(p); | ||
| 414 | } | ||
| 415 | } catch (JRDFStoreException e) { | ||
| 416 | e.printStackTrace(); | ||
| 417 | } finally { | ||
| 418 | if (derivedTuples != null) | ||
| 419 | derivedTuples.dispose(); | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | public void addRelatedAxiomsAndClauses(QueryRecord[] botQueryRecords) { | ||
| 424 | LinkedList<QueryRecord> toAddedRecords = new LinkedList<QueryRecord>(); | ||
| 425 | |||
| 426 | for (QueryRecord botQueryRecord : botQueryRecords) | ||
| 427 | if (overlappingDisjunctiveClauses(botQueryRecord) != null) | ||
| 428 | toAddedRecords.add(botQueryRecord); | ||
| 429 | |||
| 430 | for (QueryRecord botQueryRecord : toAddedRecords) { | ||
| 431 | m_manager.addAxioms(m_record.getRelevantOntology(), botQueryRecord | ||
| 432 | .getRelevantOntology().getAxioms()); | ||
| 433 | for (DLClause clause : botQueryRecord.getRelevantClauses()) | ||
| 434 | m_record.addRelevantClauses(clause); | ||
| 435 | } | ||
| 436 | |||
| 437 | if (!toAddedRecords.isEmpty()) | ||
| 438 | Utility.logDebug("Part of bottom fragments is added for this query."); | ||
| 439 | else | ||
| 440 | Utility.logDebug("None of bottom fragments is added for this query."); | ||
| 441 | } | ||
| 442 | |||
| 443 | private Set<DLClause> overlappingDisjunctiveClauses( | ||
| 444 | QueryRecord botQueryRecord) { | ||
| 445 | if (m_tBoxAxioms == null) | ||
| 446 | return null; | ||
| 447 | |||
| 448 | Set<DLClause> disjunctiveRules = new HashSet<DLClause>(); | ||
| 449 | Set<DLClause> clauses = m_record.getRelevantClauses(); | ||
| 450 | for (DLClause clause : botQueryRecord.getRelevantClauses()) | ||
| 451 | if (clause.getHeadLength() > 1 && clauses.contains(clause)) | ||
| 452 | disjunctiveRules.add(clause); | ||
| 453 | |||
| 454 | return disjunctiveRules.isEmpty() ? null : disjunctiveRules; | ||
| 455 | } | ||
| 456 | |||
| 457 | private String getSPARQLQuery4Unary(String p) { | ||
| 458 | StringBuilder builder = new StringBuilder(); | ||
| 459 | builder.append("select ?x where { ?x <") | ||
| 460 | .append(Namespace.RDF_TYPE).append("> "); | ||
| 461 | builder.append(p).append(" . }"); | ||
| 462 | return builder.toString(); | ||
| 463 | } | ||
| 464 | |||
| 465 | private String getSPARQLQuery4Binary(String p) { | ||
| 466 | StringBuilder builder = new StringBuilder(); | ||
| 467 | builder.append("select ?x ?y where { ?x ").append(p) | ||
| 468 | .append(" ?y . }"); | ||
| 469 | return builder.toString(); | ||
| 470 | } | ||
| 471 | |||
| 472 | } | ||
