From 1b6a128137e5d7a6ff75566869232fc054afabef Mon Sep 17 00:00:00 2001 From: RncLsn Date: Fri, 15 May 2015 17:32:22 +0100 Subject: Testing and fixing. Executed successfully on UOBM{1,2,3,4,5,6,7,8}. --- src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java | 61 ++++--------------- .../pagoda/multistage/MultiStageQueryEngine.java | 4 +- src/uk/ac/ox/cs/pagoda/query/QueryRecord.java | 18 ++++-- .../ox/cs/pagoda/reasoner/ConsistencyManager.java | 68 +++++++++------------- .../ox/cs/pagoda/reasoner/ConsistencyManager2.java | 6 +- .../ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java | 44 +++++++------- src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | 13 +++-- .../cs/pagoda/reasoner/light/KarmaQueryEngine.java | 26 +++++---- .../cs/pagoda/reasoner/light/RDFoxQueryEngine.java | 22 ++++--- .../pagoda/reasoner/light/RDFoxTripleManager.java | 54 +++++++++++------ src/uk/ac/ox/cs/pagoda/rules/Program.java | 38 ++---------- src/uk/ac/ox/cs/pagoda/util/Utility.java | 49 ++++++++++------ 12 files changed, 183 insertions(+), 220 deletions(-) (limited to 'src') diff --git a/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java b/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java index a60b664..1e17dac 100644 --- a/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java +++ b/src/uk/ac/ox/cs/pagoda/approx/RLPlusOntology.java @@ -1,64 +1,23 @@ package uk.ac.ox.cs.pagoda.approx; -import java.io.BufferedOutputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectOutput; -import java.io.ObjectOutputStream; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.Map; -import java.util.Random; -import java.util.Set; - import org.semanticweb.HermiT.Configuration; import org.semanticweb.HermiT.model.DLClause; import org.semanticweb.HermiT.model.DLOntology; import org.semanticweb.HermiT.structural.OWLClausification; -import org.semanticweb.owlapi.model.IRI; -import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; -import org.semanticweb.owlapi.model.OWLAxiom; -import org.semanticweb.owlapi.model.OWLClass; -import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; -import org.semanticweb.owlapi.model.OWLClassExpression; -import org.semanticweb.owlapi.model.OWLDataFactory; -import org.semanticweb.owlapi.model.OWLDataHasValue; -import org.semanticweb.owlapi.model.OWLDataMaxCardinality; -import org.semanticweb.owlapi.model.OWLDataMinCardinality; -import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLDatatype; -import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom; -import org.semanticweb.owlapi.model.OWLIndividual; -import org.semanticweb.owlapi.model.OWLNamedIndividual; -import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; -import org.semanticweb.owlapi.model.OWLObjectComplementOf; -import org.semanticweb.owlapi.model.OWLObjectHasValue; -import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; -import org.semanticweb.owlapi.model.OWLObjectMinCardinality; -import org.semanticweb.owlapi.model.OWLObjectOneOf; -import org.semanticweb.owlapi.model.OWLObjectProperty; -import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; -import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; -import org.semanticweb.owlapi.model.OWLOntology; -import org.semanticweb.owlapi.model.OWLOntologyCreationException; -import org.semanticweb.owlapi.model.OWLOntologyManager; -import org.semanticweb.owlapi.model.OWLOntologyStorageException; -import org.semanticweb.owlapi.model.OWLSameIndividualAxiom; +import org.semanticweb.owlapi.model.*; import org.semanticweb.owlapi.profiles.OWL2RLProfile; import org.semanticweb.owlapi.profiles.OWLProfileReport; import org.semanticweb.owlapi.profiles.OWLProfileViolation; - import uk.ac.ox.cs.pagoda.constraints.NullaryBottom; import uk.ac.ox.cs.pagoda.constraints.UnaryBottom; import uk.ac.ox.cs.pagoda.owl.OWLHelper; import uk.ac.ox.cs.pagoda.util.Namespace; import uk.ac.ox.cs.pagoda.util.Utility; +import java.io.*; +import java.nio.file.Paths; +import java.util.*; + public class RLPlusOntology implements KnowledgeBase { OWLOntologyManager manager; @@ -111,7 +70,7 @@ public class RLPlusOntology implements KnowledgeBase { if (!tOntoIRI.endsWith(originalExtension)) tOntoIRI += originalExtension; String rlOntologyIRI = originalExtension.isEmpty() ? tOntoIRI + "-RL.owl" : tOntoIRI.replaceFirst(originalExtension, "-RL.owl"); - String rlDocumentIRI = (outputPath = Utility.TempDirectory + "RL.owl"); + String rlDocumentIRI = (outputPath = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "RL.owl").toString()); outputOntology = manager.createOntology(IRI.create(rlOntologyIRI)); manager.setOntologyDocumentIRI(outputOntology, IRI.create(Utility.toFileIRI(rlDocumentIRI))); @@ -119,8 +78,8 @@ public class RLPlusOntology implements KnowledgeBase { tBoxOntologyIRI = originalExtension.isEmpty() ? tOntoIRI + "-TBox.owl" : tOntoIRI.replaceFirst(originalExtension, "-TBox.owl"); aBoxOntologyIRI = originalExtension.isEmpty() ? tOntoIRI + "-ABox.owl" : tOntoIRI.replaceFirst(originalExtension, "-ABox.owl"); - String tBoxDocumentIRI = (Utility.TempDirectory + "TBox.owl"); - String aBoxDocumentIRI = (aBoxPath = Utility.TempDirectory + "ABox.owl"); + String tBoxDocumentIRI = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "TBox.owl").toString(); + String aBoxDocumentIRI = (aBoxPath = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "ABox.owl").toString()); tBox = manager.createOntology(IRI.create(tBoxOntologyIRI)); aBox = manager.createOntology(IRI.create(aBoxOntologyIRI)); manager.setOntologyDocumentIRI(tBox, IRI.create(Utility.toFileIRI(tBoxDocumentIRI))); @@ -488,7 +447,7 @@ public class RLPlusOntology implements KnowledgeBase { addedAxioms.add(factory.getOWLObjectPropertyRangeAxiom(r, tExp)); } else if (botStrategy != BottomStrategy.TOREMOVE) { - OWLClass cls = (OWLClass) ((OWLObjectComplementOf) tExp).getComplementNNF(); + OWLClass cls = (OWLClass) tExp.getComplementNNF(); OWLClass neg; if ((neg = atomic2negation.get(cls)) == null) { neg = getNewConcept(outputOntology, rlCounter); @@ -632,6 +591,6 @@ public class RLPlusOntology implements KnowledgeBase { corrFileName = path; } - private static enum BottomStrategy { TOREMOVE, NULLARY, UNARY } + private enum BottomStrategy { TOREMOVE, NULLARY, UNARY } } diff --git a/src/uk/ac/ox/cs/pagoda/multistage/MultiStageQueryEngine.java b/src/uk/ac/ox/cs/pagoda/multistage/MultiStageQueryEngine.java index 4ba2715..50996d0 100644 --- a/src/uk/ac/ox/cs/pagoda/multistage/MultiStageQueryEngine.java +++ b/src/uk/ac/ox/cs/pagoda/multistage/MultiStageQueryEngine.java @@ -133,8 +133,8 @@ public class MultiStageQueryEngine extends StageQueryEngine { subTimer.reset(); if ((violations = program.isIntegrated(this, incrementally)) == null || violations.size() == 0) { store.clearRulesAndMakeFactsExplicit(); - Utility.logDebug(name + " store after materialising " + programName + ": " + tripleCount + " (" + (tripleCount - tripleCountBeforeMat) + " new)"); - Utility.logInfo(name + " store is DONE for multi-stage materialising in " + t.duration() + " seconds."); + Utility.logInfo(name + " store after materialising " + programName + ": " + tripleCount + " (" + (tripleCount - tripleCountBeforeMat) + " new)"); + Utility.logInfo(name + " store is DONE for multi-stage materialising in " + t.duration() + " seconds."); return isValid() ? 1 : 0; } Utility.logDebug("Time to detect violations: " + subTimer.duration()); diff --git a/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java b/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java index 6c87eb5..15b2c01 100644 --- a/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java +++ b/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java @@ -69,7 +69,7 @@ public class QueryRecord { } public boolean updateLowerBoundAnswers(AnswerTuples answerTuples) { - if (answerTuples == null) return false; + if (answerTuples == null) return false; boolean update = false; for (AnswerTuple tuple; answerTuples.isValid(); answerTuples.moveNext()) { tuple = answerTuples.getTuple(); @@ -77,8 +77,12 @@ public class QueryRecord { soundAnswerTuples.add(tuple); if (gapAnswerTuples != null) gapAnswerTuples.remove(tuple); - update = true; + update = true; } + // TODO could be wrong, but if possible add the check +// else if (! gapAnswerTuples.contains(tuple)) { +// throw new IllegalArgumentException("The lower bound answers must be contained in the upper ones!"); +// } } Utility.logInfo("The number of answers in the lower bound: " + soundAnswerTuples.size()); @@ -103,14 +107,16 @@ public class QueryRecord { for (; answerTuples.isValid(); answerTuples.moveNext()) { ++number; } - Utility.logInfo("The number of answers returned by the upper bound: " + number); + Utility.logInfo("The number of answers returned by an upper bound: " + number); if (number <= soundAnswerTuples.size()) { if (gapAnswerTuples != null) gapAnswerTuples.clear(); else gapAnswerTuples = new HashSet(); - Utility.logInfo("The number of answers in the upper bound: " + (soundAnswerTuples.size() + gapAnswerTuples.size())); + Utility.logInfo("The number of upper bound answers: " + (soundAnswerTuples.size() + gapAnswerTuples.size())); return false; } + else if (number < soundAnswerTuples.size()) + throw new IllegalArgumentException("The upper bound answers must contain all the lower bound ones!"); answerTuples.reset(); } @@ -362,8 +368,8 @@ public class QueryRecord { } public enum Step {LowerBound, UpperBound, ELLowerBound, - Fragment, FragmentRefinement, Summarisation, Dependency, FullReasoning}; - + Fragment, FragmentRefinement, Summarisation, Dependency, FullReasoning} + double[] timer; public void addProcessingTime(Step step, double time) { diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java b/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java index b7a3667..409a2c9 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager.java @@ -15,6 +15,7 @@ import uk.ac.ox.cs.pagoda.query.AnswerTuples; import uk.ac.ox.cs.pagoda.query.QueryManager; import uk.ac.ox.cs.pagoda.query.QueryRecord; 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; @@ -54,7 +55,7 @@ public class ConsistencyManager { if (fullQueryRecord.getNoOfSoundAnswers() > 0) { Utility.logInfo("Answers to bottom in the lower bound: ", fullQueryRecord.outputSoundAnswerTuple()); - return unsatisfiability(t.duration()); + return false; } return true; } @@ -63,39 +64,20 @@ public class ConsistencyManager { fullQueryRecord.updateLowerBoundAnswers(m_reasoner.elLowerStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables())); if (fullQueryRecord.getNoOfSoundAnswers() > 0) { Utility.logInfo("Answers to bottom in the lower bound: ", fullQueryRecord.outputSoundAnswerTuple()); - return unsatisfiability(t.duration()); + return true; } return true; } - - boolean checkLazyUpper() { - if (m_reasoner.lazyUpperStore != null) { - AnswerTuples tuples = null; - try { - tuples = m_reasoner.lazyUpperStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables()); - - Utility.logDebug("CheckLazyUpperBound: answerVars=" + fullQueryRecord.getAnswerVariables()); - - if (!tuples.isValid()) { - Utility.logInfo("There are no contradictions derived in the lazy upper bound materialisation."); - return satisfiability(t.duration()); - } - } - finally { - if (tuples != null) tuples.dispose(); - } - } - return false; - } - boolean checkSkolemUpper() { - if (m_reasoner.limitedSkolemUpperStore != null) { + boolean checkUpper(BasicQueryEngine upperStore) { + if (upperStore != null) { AnswerTuples tuples = null; try { - tuples = m_reasoner.limitedSkolemUpperStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables()); + tuples = upperStore.evaluate(fullQueryRecord.getQueryText(), fullQueryRecord.getAnswerVariables()); if (!tuples.isValid()) { - Utility.logInfo("There are no contradictions derived in the limited-skolem upper bound materialisation."); - return satisfiability(t.duration()); + Utility.logInfo("There are no contradictions derived in "+ upperStore.getName() +" materialisation."); + Utility.logDebug("The ontology and dataset is satisfiable."); + return true; } } finally { @@ -104,7 +86,11 @@ public class ConsistencyManager { } return false; } - + + void dispose() { + fullQueryRecord.dispose(); + } + boolean check() { // if (!checkRLLowerBound()) return false; // if (!checkELLowerBound()) return false; @@ -119,7 +105,7 @@ public class ConsistencyManager { } if (fullQueryRecord.getNoOfCompleteAnswers() == 0) - return satisfiability(t.duration()); + return true; extractBottomFragment(); @@ -139,7 +125,7 @@ public class ConsistencyManager { checker = new HermitSummaryFilter(r, true); // m_reasoner.factory.getSummarisedReasoner(r); satisfiability = checker.isConsistent(); checker.dispose(); - if (!satisfiability) return unsatisfiability(t.duration()); + if (!satisfiability) return false; } // Checker checker = m_reasoner.factory.getSummarisedReasoner(fullQueryRecord); @@ -147,20 +133,20 @@ public class ConsistencyManager { // checker.dispose(); // if (!satisfiable) return unsatisfiability(t.duration()); - return satisfiability(t.duration()); + return true; } - protected boolean unsatisfiability(double duration) { - fullQueryRecord.dispose(); - Utility.logDebug("The ontology and dataset is unsatisfiable."); - return false; - } +// protected boolean unsatisfiability(double duration) { +// fullQueryRecord.dispose(); +// Utility.logDebug("The ontology and dataset is unsatisfiable."); +// return false; +// } - protected boolean satisfiability(double duration) { - fullQueryRecord.dispose(); - Utility.logDebug("The ontology and dataset is satisfiable."); - return true; - } +// protected boolean satisfiability(double duration) { +// fullQueryRecord.dispose(); +// Utility.logDebug("The ontology and dataset is satisfiable."); +// return true; +// } boolean fragmentExtracted = false; diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager2.java b/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager2.java index 9c335f3..9191067 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager2.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/ConsistencyManager2.java @@ -2,7 +2,6 @@ package uk.ac.ox.cs.pagoda.reasoner; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyManager; - import uk.ac.ox.cs.pagoda.query.AnswerTuples; import uk.ac.ox.cs.pagoda.query.QueryRecord; import uk.ac.ox.cs.pagoda.reasoner.full.Checker; @@ -31,8 +30,9 @@ public class ConsistencyManager2 extends ConsistencyManager { @Override boolean check() { // if (!checkRLLowerBound()) return false; -// if (!checkELLowerBound()) return false; - if (checkLazyUpper()) return true; +// if (!checkELLowerBound()) return false; + // TODO test + if (checkUpper(m_reasoner.lazyUpperStore) && checkUpper(m_reasoner.limitedSkolemUpperStore)) return true; AnswerTuples iter = null; try { diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java index 36ea7de..1f435b7 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/MyQueryReasoner.java @@ -50,6 +50,7 @@ public class MyQueryReasoner extends QueryReasoner { private Collection predicatesWithGap = null; private Boolean satisfiable; private ConsistencyManager consistency = new ConsistencyManager(this); + private boolean useUpperStores = false; public MyQueryReasoner() { setup(true, true); @@ -101,9 +102,9 @@ public class MyQueryReasoner extends QueryReasoner { // program.getUpper().save(); // program.getGeneral().save(); - if (multiStageTag && !program.getGeneral().isHorn()) { + useUpperStores = multiStageTag && !program.getGeneral().isHorn(); + if (useUpperStores) { lazyUpperStore = getUpperStore("lazy-upper-bound", true); // new MultiStageQueryEngine("lazy-upper-bound", true); // - // TODO CHECK limitedSkolemUpperStore = getUpperStore("limited-skolem-upper-bound", true); } @@ -120,7 +121,7 @@ public class MyQueryReasoner extends QueryReasoner { @Override public boolean preprocess() { t.reset(); - Utility.logInfo("Preprocessing ... checking satisfiability ... "); + Utility.logInfo("Preprocessing... checking satisfiability... "); String name = "data", datafile = importedData.toString(); rlLowerStore.importRDFData(name, datafile); @@ -147,12 +148,11 @@ public class MyQueryReasoner extends QueryReasoner { } if (tag == -1) return false; } - if (consistency.checkLazyUpper()) { + if (consistency.checkUpper(lazyUpperStore)) { satisfiable = true; Utility.logInfo("time for satisfiability checking: " + t.duration()); } - // TODO check if (limitedSkolemUpperStore != null) { limitedSkolemUpperStore.importRDFData(name, datafile); limitedSkolemUpperStore.materialise("saturate named individuals", originalMarkProgram); @@ -163,16 +163,14 @@ public class MyQueryReasoner extends QueryReasoner { } if (tag == -1) return false; } - // FIXME nullPointerException -// if (consistency.checkSkolemUpper()) { -// satisfiable = true; -// Utility.logInfo("time for satisfiability checking: " + t.duration()); -// } + if (consistency.checkUpper(limitedSkolemUpperStore)) { + satisfiable = true; + Utility.logInfo("time for satisfiability checking: " + t.duration()); + } trackingStore.importRDFData(name, datafile); trackingStore.materialise("saturate named individuals", originalMarkProgram); -// materialiseFullUpper(); GapByStore4ID gap = new GapByStore4ID(trackingStore); trackingStore.materialiseFoldedly(program, gap); predicatesWithGap = gap.getPredicatesWithGap(); @@ -192,6 +190,7 @@ public class MyQueryReasoner extends QueryReasoner { return false; consistency.extractBottomFragment(); + consistency.dispose(); return true; } @@ -204,6 +203,9 @@ public class MyQueryReasoner extends QueryReasoner { return satisfiable; } + /** + * Returns the relevant part of the ontology, while computing the bound answers. + * */ private OWLOntology relevantPart(QueryRecord queryRecord) { AnswerTuples rlAnswer = null, elAnswer = null; @@ -216,13 +218,13 @@ public class MyQueryReasoner extends QueryReasoner { if (rlAnswer != null) rlAnswer.dispose(); } queryRecord.addProcessingTime(Step.LowerBound, t.duration()); - rlAnswer = null; t.reset(); BasicQueryEngine upperStore = queryRecord.isBottom() || lazyUpperStore == null ? trackingStore : lazyUpperStore; String[] extendedQuery = queryRecord.getExtendedQueryText(); - + + // TODO why the following??? queryUpperBound(upperStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); if (!queryRecord.processed() && !queryRecord.getQueryText().equals(extendedQuery[0])) { @@ -232,25 +234,21 @@ public class MyQueryReasoner extends QueryReasoner { queryUpperBound(upperStore, queryRecord, extendedQuery[1], queryRecord.getDistinguishedVariables()); } - Utility.logDebug(toJsonKeyValuePair("upperBound1", queryRecord)); +// Utility.logDebug(toJsonKeyValuePair("upperBound", queryRecord)); - // TODO check whether it is harmful. In case is not, implement it properly - // BEGIN: trying to intersect + // TODO test intersection and new upper bound if (!queryRecord.isBottom() && lazyUpperStore != null) { queryUpperBound(trackingStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); } if (!queryRecord.isBottom() && limitedSkolemUpperStore != null) { queryUpperBound(limitedSkolemUpperStore, queryRecord, queryRecord.getQueryText(), queryRecord.getAnswerVariables()); } - // END: trying to intersect queryRecord.addProcessingTime(Step.UpperBound, t.duration()); if (queryRecord.processed()) { queryRecord.setDifficulty(Step.UpperBound); return null; } - - // TODO add evaluation on new upper store t.reset(); try { @@ -327,15 +325,15 @@ public class MyQueryReasoner extends QueryReasoner { @Override public void evaluate(QueryRecord queryRecord) { - OWLOntology knowledgebase = relevantPart(queryRecord); + OWLOntology knowledgeBase = relevantPart(queryRecord); - if (knowledgebase == null) { + if (knowledgeBase == null) { Utility.logDebug("Difficulty of this query: " + queryRecord.getDifficulty()); return ; } - int aboxcount = knowledgebase.getABoxAxioms(true).size(); - Utility.logDebug("ABox axioms: " + aboxcount + " TBox axioms: " + (knowledgebase.getAxiomCount() - aboxcount)); + int aBoxCount = knowledgeBase.getABoxAxioms(true).size(); + Utility.logDebug("ABox axioms: " + aBoxCount + " TBox axioms: " + (knowledgeBase.getAxiomCount() - aBoxCount)); // queryRecord.saveRelevantOntology("fragment_query" + queryRecord.getQueryID() + ".owl"); Timer t = new Timer(); diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java index 326bf7e..97bab50 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java @@ -25,8 +25,8 @@ public abstract class QueryReasoner { private static boolean defaultMultiStages = true; private static boolean defaultEqualities = true; - public static enum Type { Full, RLU, ELHOU }; - + public enum Type { Full, RLU, ELHOU } + public static QueryReasoner getInstance(Properties p) { OWLOntology ontology = OWLHelper.loadOntology(p.getOntologyPath()); QueryReasoner pagoda = getInstance(ontology, p); @@ -63,7 +63,7 @@ public abstract class QueryReasoner { } public static QueryReasoner getInstance(Type type, OWLOntology o, boolean performMultiStages, boolean considerEqualities) { - Utility.initialise(); +// Utility.initialise(); QueryReasoner reasoner; if (OWLHelper.isInOWL2RL(o)) reasoner = new RLQueryReasoner(); else if (OWLHelper.isInELHO(o)) reasoner = new ELHOQueryReasoner(); @@ -218,9 +218,10 @@ public abstract class QueryReasoner { record.outputAnswerStatistics(); record.outputTimes(); } - // TODO it can handle one call only - // if you call twice, you will end up with a json file with multiple roots + /* TODO it can handle one call only + if you call twice, you will end up with a json file with multiple roots */ if(answerWriter != null) gson.toJson(queryRecords, answerWriter); +// queryRecords.stream().forEach(record -> Utility.logDebug(gson.toJson(record))); queryRecords.stream().forEach(record -> record.dispose()); } @@ -232,7 +233,7 @@ public abstract class QueryReasoner { e.printStackTrace(); } } - Utility.cleanup(); +// Utility.cleanup(); } private QueryManager m_queryManager = new QueryManager(); diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java index f70dde9..f068164 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java @@ -1,20 +1,22 @@ -package uk.ac.ox.cs.pagoda.reasoner.light; +package uk.ac.ox.cs.pagoda.reasoner.light; -import java.io.File; -import java.io.FileNotFoundException; -import java.util.*; - -import org.semanticweb.karma2.*; +import org.semanticweb.karma2.MyKarma; import org.semanticweb.karma2.clausifier.OntologyProcesser; import org.semanticweb.karma2.exception.IllegalInputOntologyException; import org.semanticweb.karma2.model.ConjunctiveQuery; import org.semanticweb.owlapi.model.OWLOntology; - -import uk.ac.ox.cs.pagoda.query.*; -import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; -import uk.ac.ox.cs.pagoda.util.Utility; import uk.ac.ox.cs.JRDFox.JRDFStoreException; import uk.ac.ox.cs.JRDFox.store.DataStore; +import uk.ac.ox.cs.pagoda.query.AnswerTuple; +import uk.ac.ox.cs.pagoda.query.AnswerTuples; +import uk.ac.ox.cs.pagoda.query.AnswerTuplesImp; +import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; +import uk.ac.ox.cs.pagoda.util.Utility; + +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.file.Paths; +import java.util.Set; public class KarmaQueryEngine extends RDFoxQueryEngine { @@ -29,8 +31,8 @@ public class KarmaQueryEngine extends RDFoxQueryEngine { // int index = (new Random().nextInt() % Base + Base) % Base; // karmaDataFile = "karma_data" + index + ".ttl"; // karmaRuleFile = "karma_rule" + index + ".dlog"; - karmaDataFile = Utility.TempDirectory + "karma_data.ttl"; - karmaRuleFile = Utility.TempDirectory + "karma_rule.dlog"; + karmaDataFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_data.ttl").toString(); + karmaRuleFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_rule.dlog").toString(); reasoner = new MyKarma(); } diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java index 70d0cc9..63773d9 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java @@ -1,8 +1,9 @@ package uk.ac.ox.cs.pagoda.reasoner.light; -import java.io.File; -import java.util.Collection; - +import uk.ac.ox.cs.JRDFox.JRDFStoreException; +import uk.ac.ox.cs.JRDFox.Prefixes; +import uk.ac.ox.cs.JRDFox.store.DataStore; +import uk.ac.ox.cs.JRDFox.store.DataStore.StoreType; import uk.ac.ox.cs.pagoda.MyPrefixes; import uk.ac.ox.cs.pagoda.query.AnswerTuples; import uk.ac.ox.cs.pagoda.reasoner.QueryEngine; @@ -10,16 +11,19 @@ import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner; import uk.ac.ox.cs.pagoda.tracking.AnswerTuplesWriter; import uk.ac.ox.cs.pagoda.util.Timer; import uk.ac.ox.cs.pagoda.util.Utility; -import uk.ac.ox.cs.JRDFox.JRDFStoreException; -import uk.ac.ox.cs.JRDFox.Prefixes; -import uk.ac.ox.cs.JRDFox.store.DataStore; -import uk.ac.ox.cs.JRDFox.store.DataStore.StoreType; + +import java.io.File; +import java.util.Collection; public abstract class RDFoxQueryEngine implements QueryEngine { public static final int matNoOfThreads = Runtime.getRuntime().availableProcessors() * 2; - - protected String name; + + public String getName() { + return name; + } + + protected String name; protected Prefixes prefixes = MyPrefixes.PAGOdAPrefixes.getRDFoxPrefixes(); public RDFoxQueryEngine(String name) { diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java index 232bc31..85f8ef9 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxTripleManager.java @@ -1,5 +1,8 @@ package uk.ac.ox.cs.pagoda.reasoner.light; +import net.sf.ehcache.Cache; +import net.sf.ehcache.CacheManager; +import net.sf.ehcache.Element; import org.semanticweb.HermiT.model.*; import uk.ac.ox.cs.JRDFox.JRDFStoreException; import uk.ac.ox.cs.JRDFox.model.Datatype; @@ -11,11 +14,20 @@ import uk.ac.ox.cs.JRDFox.store.Resource; import uk.ac.ox.cs.pagoda.owl.OWLHelper; import uk.ac.ox.cs.pagoda.util.Namespace; -import java.util.*; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; public class RDFoxTripleManager { - - UpdateType m_incrementally; + + private final Cache termsCache; + private static final int TERMS_CACHE_SIZE = 10000; + private static final int CACHE_TTL_DEFAULT = 0; + private static final int CACHE_TTI_DEFAULT = 0; + private static final boolean CACHE_ETERNAL = true; + private static final boolean CACHE_USE_DISK = false; + + UpdateType m_incrementally; // boolean m_incrementally; DataStore m_store; @@ -24,7 +36,19 @@ public class RDFoxTripleManager { public RDFoxTripleManager(DataStore store, boolean incrementally) { m_store = store; -// m_incrementally = incrementally; +// m_incrementally = incrementally; + + CacheManager cacheManager = CacheManager.getInstance(); + String cacheName = "RDFoxTripleManager_" + store.hashCode(); + if(! cacheManager.cacheExists(cacheName)) { + termsCache = new Cache(cacheName, + TERMS_CACHE_SIZE, CACHE_USE_DISK, CACHE_ETERNAL, + CACHE_TTL_DEFAULT, CACHE_TTI_DEFAULT); + cacheManager.addCache(termsCache); + } + else + termsCache = cacheManager.getCache(cacheName); + if (incrementally) m_incrementally = UpdateType.ScheduleForAddition; else @@ -164,29 +188,25 @@ public class RDFoxTripleManager { return m_dict.resolveResources(lexicalForms, types)[0]; } - Map termCache = new HashMap(); - Queue termList = new LinkedList(); - int sizeLimit = 10000; +// Map termCache = new HashMap(); +// Queue termQueue = new LinkedList(); private int getResourceID(Term arg, Map assignment) { - // FIXME infinite loop -// while (termCache.size() > sizeLimit) -// termCache.remove(termList.poll()); - if (arg instanceof Variable) return assignment.get(arg); - Integer id = null; - if ((id = termCache.get(arg)) != null) - return id; - + int id = -1; + if(termsCache.isKeyInCache(arg)) + return ((int) termsCache.get(arg).getObjectValue()); + // if (arg instanceof Individual) { try { if (arg instanceof Individual) - termCache.put(arg, id = resolveResource(((Individual) arg).getIRI(), Datatype.IRI_REFERENCE.value())); + termsCache.put(new Element(arg, id = resolveResource(((Individual) arg).getIRI(), Datatype.IRI_REFERENCE.value()))); else if (arg instanceof Constant) - termCache.put(arg, id = resolveResource(((Constant) arg).getLexicalForm(), getDatatypeID(((Constant) arg).getDatatypeURI()))); + termsCache.put(new Element(arg, id = resolveResource(((Constant) arg).getLexicalForm(), getDatatypeID(((Constant) arg).getDatatypeURI())))); } catch (JRDFStoreException e) { e.printStackTrace(); + System.exit(1); } // } diff --git a/src/uk/ac/ox/cs/pagoda/rules/Program.java b/src/uk/ac/ox/cs/pagoda/rules/Program.java index 83cd21a..2e5302b 100644 --- a/src/uk/ac/ox/cs/pagoda/rules/Program.java +++ b/src/uk/ac/ox/cs/pagoda/rules/Program.java @@ -1,39 +1,10 @@ package uk.ac.ox.cs.pagoda.rules; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; - import org.semanticweb.HermiT.Configuration; -import org.semanticweb.HermiT.model.AnnotatedEquality; -import org.semanticweb.HermiT.model.Atom; -import org.semanticweb.HermiT.model.AtomicConcept; -import org.semanticweb.HermiT.model.AtomicDataRange; -import org.semanticweb.HermiT.model.AtomicNegationDataRange; -import org.semanticweb.HermiT.model.AtomicRole; -import org.semanticweb.HermiT.model.ConstantEnumeration; -import org.semanticweb.HermiT.model.DLClause; -import org.semanticweb.HermiT.model.DLOntology; -import org.semanticweb.HermiT.model.DLPredicate; -import org.semanticweb.HermiT.model.Equality; -import org.semanticweb.HermiT.model.Inequality; -import org.semanticweb.HermiT.model.InverseRole; -import org.semanticweb.HermiT.model.Term; -import org.semanticweb.HermiT.model.Variable; +import org.semanticweb.HermiT.model.*; import org.semanticweb.HermiT.structural.OWLClausification; import org.semanticweb.owlapi.model.*; - +import org.semanticweb.simpleETL.SimpleETL; import uk.ac.ox.cs.pagoda.MyPrefixes; import uk.ac.ox.cs.pagoda.approx.KnowledgeBase; import uk.ac.ox.cs.pagoda.approx.RLPlusOntology; @@ -44,7 +15,8 @@ import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; import uk.ac.ox.cs.pagoda.owl.OWLHelper; import uk.ac.ox.cs.pagoda.util.Utility; -import org.semanticweb.simpleETL.SimpleETL; +import java.io.*; +import java.util.*; public abstract class Program implements KnowledgeBase { @@ -377,7 +349,7 @@ public abstract class Program implements KnowledgeBase { } public final String getDirectory() { - return Utility.TempDirectory; + return Utility.getGlobalTempDirAbsolutePath(); } public void deleteABoxTurtleFile() { diff --git a/src/uk/ac/ox/cs/pagoda/util/Utility.java b/src/uk/ac/ox/cs/pagoda/util/Utility.java index b3a4df1..7b23e0d 100644 --- a/src/uk/ac/ox/cs/pagoda/util/Utility.java +++ b/src/uk/ac/ox/cs/pagoda/util/Utility.java @@ -5,9 +5,9 @@ import org.apache.log4j.Logger; import org.semanticweb.HermiT.model.Atom; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; import java.text.SimpleDateFormat; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.*; public class Utility { @@ -21,8 +21,9 @@ public class Utility { public static final String JAVA_FILE_SEPARATOR = "/"; public static final String FILE_SEPARATOR = System.getProperty("file.separator"); public static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - public static final String TempDirectory = (new File("tmp" + DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now()))).getAbsolutePath() + FILE_SEPARATOR; + + private static final String TEMP_DIR_PATH= "pagoda_tmp"; + private static String tempDir; public static final int TEST = -1; public static final int FLY = 0; @@ -31,6 +32,20 @@ public class Utility { public static final int AEO = 3; public static final int WINE = 4; + public static String getGlobalTempDirAbsolutePath() { + if(tempDir == null) { + try { + Path path = Files.createTempDirectory(TEMP_DIR_PATH); + tempDir = path.toString(); + new File(tempDir).deleteOnExit(); + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + } + return tempDir; + } + public static Set toSet(Atom[] data) { HashSet ret = new HashSet(); @@ -228,19 +243,19 @@ public class Utility { LOGS.error(getLogMessage(messages)); } - public static void initialise() { - File tmp = new File(TempDirectory); - if (!tmp.exists()) tmp.mkdirs(); - } - - public static void cleanup() { - File tmp = new File(TempDirectory); - if (tmp.exists()) { - for (File file: tmp.listFiles()) - file.delete(); - tmp.delete(); - } - } +// public static void initialise() { +// File tmp = new File(TempDirectory); +// if (!tmp.exists()) tmp.mkdirs(); +// } +// +// public static void cleanup() { +// File tmp = new File(TempDirectory); +// if (tmp.exists()) { +// for (File file: tmp.listFiles()) +// file.delete(); +// tmp.delete(); +// } +// } public static String toFileIRI(String path) { String iri; -- cgit v1.2.3