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}. --- .../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 +++++++++++------ 7 files changed, 122 insertions(+), 111 deletions(-) (limited to 'src/uk/ac/ox/cs/pagoda/reasoner') 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); } // } -- cgit v1.2.3