From 5d54af2638a53721b414a41356a93686a9616272 Mon Sep 17 00:00:00 2001 From: RncLsn Date: Tue, 19 May 2015 13:35:52 +0100 Subject: Backup before changes in MyQueryReasoner. --- src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java | 174 ++++++++++----------- 1 file changed, 85 insertions(+), 89 deletions(-) (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java') diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java index dfe0c5f..d4f4596 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/QueryReasoner.java @@ -19,24 +19,26 @@ import java.util.Collection; // TODO clean APIs public abstract class QueryReasoner { - + + public static final String ImportDataFileSeparator = ";"; + private static final boolean DEFAULT_MULTI_STAGES = true; + private static final boolean DEFAULT_EQUALITIES = true; + public boolean fullReasoner = this instanceof MyQueryReasoner; + protected StringBuilder importedData = new StringBuilder(); // protected boolean forSemFacet = false; Properties properties; + BufferedWriter answerWriter = null; + private QueryManager m_queryManager = new QueryManager(); - private static boolean defaultMultiStages = true; - private static boolean defaultEqualities = true; - - public enum Type { Full, RLU, ELHOU } - public static QueryReasoner getInstance(Properties p) { OWLOntology ontology = OWLHelper.loadOntology(p.getOntologyPath()); QueryReasoner pagoda = getInstance(ontology, p); - pagoda.properties = p; + pagoda.properties = p; pagoda.loadOntology(ontology); pagoda.importData(p.getDataPath()); if (pagoda.preprocess()) { Utility.logInfo("The ontology is consistent!"); - return pagoda; + return pagoda; } else { System.out.println("The ontology is inconsistent!"); @@ -44,60 +46,63 @@ public abstract class QueryReasoner { return null; } } - + public static QueryReasoner getInstance(OWLOntology o) { - QueryReasoner pagoda = getInstance(Type.Full, o, defaultMultiStages, defaultEqualities); - pagoda.properties = new Properties(); - return pagoda; + QueryReasoner pagoda = getInstance(Type.Full, o, DEFAULT_MULTI_STAGES, DEFAULT_EQUALITIES); + pagoda.properties = new Properties(); + return pagoda; } - - public void setToClassify(boolean flag) { - properties.setToClassify(flag); - } - - public void setToCallHermiT(boolean flag) { - properties.setToCallHermiT(flag); - } - + private static QueryReasoner getInstance(OWLOntology o, Properties p) { - return getInstance(Type.Full, o, defaultMultiStages, defaultEqualities); + return getInstance(Type.Full, o, DEFAULT_MULTI_STAGES, DEFAULT_EQUALITIES); } - + public static QueryReasoner getInstance(Type type, OWLOntology o, boolean performMultiStages, boolean considerEqualities) { // Utility.initialise(); - QueryReasoner reasoner; + QueryReasoner reasoner; if (OWLHelper.isInOWL2RL(o)) reasoner = new RLQueryReasoner(); else if (OWLHelper.isInELHO(o)) reasoner = new ELHOQueryReasoner(); - else + else switch (type) { - case RLU: - reasoner = new RLUQueryReasoner(performMultiStages, considerEqualities); break; - case ELHOU: - reasoner = new ELHOUQueryReasoner(performMultiStages, considerEqualities); break; - default: - reasoner = new MyQueryReasoner(performMultiStages, considerEqualities); + case RLU: + reasoner = new RLUQueryReasoner(performMultiStages, considerEqualities); + break; + case ELHOU: + reasoner = new ELHOUQueryReasoner(performMultiStages, considerEqualities); + break; + default: + reasoner = new MyQueryReasoner(performMultiStages, considerEqualities); } - return reasoner; + return reasoner; + } + + public static QueryReasoner getHermiTReasoner(boolean toCheckSatisfiability) { + return new HermiTReasoner(toCheckSatisfiability); + } + + public void setToClassify(boolean flag) { + properties.setToClassify(flag); + } + + public void setToCallHermiT(boolean flag) { + properties.setToCallHermiT(flag); } - - public static final String ImportDataFileSeparator = ";"; - protected StringBuilder importedData = new StringBuilder(); public void importData(String datafile) { if (datafile != null && !datafile.equalsIgnoreCase("null")) - importData(datafile.split(ImportDataFileSeparator)); + importData(datafile.split(ImportDataFileSeparator)); } public void importData(String[] datafiles) { if (datafiles != null) { for (String datafile: datafiles) { - File file = new File(datafile); + File file = new File(datafile); if (file.exists()) { if (file.isFile()) importDataFile(file); else importDataDirectory(file); } else { - Utility.logError("warning: file " + datafile + " doesn't exists."); + Utility.logError("warning: file " + datafile + " doesn't exists."); } } } @@ -115,80 +120,75 @@ public abstract class QueryReasoner { datafile = file.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); - return ; - } - importDataFile(datafile); + return; + } + importDataFile(datafile); } - + protected final void importDataFile(String datafile) { if (importedData.length() == 0) - importedData.append(datafile); - else + importedData.append(datafile); + else importedData.append(ImportDataFileSeparator).append(datafile); } - - public abstract void loadOntology(OWLOntology ontology); - - public abstract boolean preprocess(); - public abstract boolean isConsistent(); + public abstract void loadOntology(OWLOntology ontology); - public boolean fullReasoner = this instanceof MyQueryReasoner; + public abstract boolean preprocess(); - public abstract void evaluate(QueryRecord record); + public abstract boolean isConsistent(); - public abstract void evaluateUpper(QueryRecord record); + public abstract void evaluate(QueryRecord record); + + public abstract void evaluateUpper(QueryRecord record); public AnswerTuples evaluate(String queryText, boolean forFacetGeneration) { if (forFacetGeneration) { QueryRecord record = m_queryManager.create(queryText); Utility.logInfo("---------- start evaluating upper bound for Query " + record.getQueryID() + " ----------", queryText); - if (!record.processed()) + if(!record.processed()) evaluateUpper(record); // AnswerTuples tuples = record.getUpperBoundAnswers(); // for (AnswerTuple tuple; tuples.isValid(); tuples.moveNext()) { -// tuple = tuples.getTuple(); +// tuple = tuples.getTuple(); // if (tuple.toString().contains("NC")) -// System.out.println(tuple.toString()); +// System.out.println(tuple.toString()); // } - return record.getUpperBoundAnswers(); - } - else - return evaluate(queryText); + return record.getUpperBoundAnswers(); + } else + return evaluate(queryText); } +// public void evaluate(Collection queryRecords) { +// evaluate(queryRecords); +// } + public AnswerTuples evaluate(String queryText) { - QueryRecord record = m_queryManager.create(queryText); + QueryRecord record = m_queryManager.create(queryText); Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); - if (!record.processed()) + if(!record.processed()) evaluate(record); - AnswerTuples answer = record.getAnswers(); + AnswerTuples answer = record.getAnswers(); record.dispose(); return answer; - + } - + public void evaluate_shell(String queryText) { - QueryRecord record = m_queryManager.create(queryText); + QueryRecord record = m_queryManager.create(queryText); Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", queryText); - if (!record.processed()) + if(!record.processed()) evaluate(record); Utility.logInfo("Answers to this query: ", record.outputSoundAnswerTuple()); record.dispose(); - + } -// public void evaluate(Collection queryRecords) { -// evaluate(queryRecords); -// } - - BufferedWriter answerWriter = null; - public void evaluate(Collection queryRecords) { if (!isConsistent()) { - Utility.logDebug("The ontology and dataset is inconsistent."); - return ; + Utility.logDebug("The ontology and dataset is inconsistent."); + return; } if(properties.getAnswerPath() != null && answerWriter == null) { @@ -199,21 +199,21 @@ public abstract class QueryReasoner { e.printStackTrace(); } } - + Timer t = new Timer(); Gson gson = QueryRecord.GsonCreator.getInstance(); for (QueryRecord record: queryRecords) { -// if (Integer.parseInt(record.getQueryID()) != 218) continue; - Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", +// if (Integer.parseInt(record.getQueryID()) != 218) continue; + Utility.logInfo("---------- start evaluating Query " + record.getQueryID() + " ----------", record.getQueryText()); if (!record.processed()) { t.reset(); if (!record.processed()) - evaluate(record); - Utility.logInfo("Total time to answer this query: " + t.duration()); + evaluate(record); + Utility.logInfo("Total time to answer this query: " + t.duration()); if (!fullReasoner && !record.processed()) { - Utility.logInfo("The query has not been fully answered in " + t.duration() + " seconds."); - continue; + Utility.logInfo("The query has not been fully answered in " + t.duration() + " seconds."); + continue; } } record.outputAnswerStatistics(); @@ -225,7 +225,7 @@ public abstract class QueryReasoner { // queryRecords.stream().forEach(record -> Utility.logDebug(gson.toJson(record))); queryRecords.stream().forEach(record -> record.dispose()); } - + public void dispose() { if (answerWriter != null) { try { @@ -235,17 +235,13 @@ public abstract class QueryReasoner { } } // Utility.cleanup(); - } - - private QueryManager m_queryManager = new QueryManager(); + } public QueryManager getQueryManager() { return m_queryManager; } - public static QueryReasoner getHermiTReasoner(boolean toCheckSatisfiability) { - return new HermiTReasoner(toCheckSatisfiability); - } + public enum Type {Full, RLU, ELHOU} } -- cgit v1.2.3