diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-05-29 18:35:51 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-05-29 18:35:51 +0100 |
| commit | 4b7253559c290b6fdd1c4122830f153fda85dd62 (patch) | |
| tree | 37c1487e4682be719dec532ca3d7e1af353fb9a1 /src/uk/ac/ox/cs/pagoda/reasoner/light | |
| parent | 6a559a415b3bdb3048021839e5bcf5bbf5aa4bbd (diff) | |
| download | ACQuA-4b7253559c290b6fdd1c4122830f153fda85dd62.tar.gz ACQuA-4b7253559c290b6fdd1c4122830f153fda85dd62.zip | |
Disposable.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/light')
4 files changed, 578 insertions, 560 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java index 79be8aa..fe43e09 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java | |||
| @@ -1,375 +1,377 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner.light; | 1 | package uk.ac.ox.cs.pagoda.reasoner.light; |
| 2 | 2 | ||
| 3 | import java.util.Arrays; | ||
| 4 | import java.util.Collection; | ||
| 5 | import java.util.HashSet; | ||
| 6 | import java.util.Iterator; | ||
| 7 | import java.util.Set; | ||
| 8 | |||
| 9 | import org.semanticweb.HermiT.model.DLClause; | 3 | import org.semanticweb.HermiT.model.DLClause; |
| 10 | 4 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | |
| 5 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 6 | import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType; | ||
| 7 | import uk.ac.ox.cs.JRDFox.store.Parameters; | ||
| 8 | import uk.ac.ox.cs.JRDFox.store.TripleStatus; | ||
| 9 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 11 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | 10 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; |
| 12 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | 11 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; |
| 13 | import uk.ac.ox.cs.pagoda.query.GapByStore4ID; | 12 | import uk.ac.ox.cs.pagoda.query.GapByStore4ID; |
| 14 | import uk.ac.ox.cs.pagoda.rules.DatalogProgram; | 13 | import uk.ac.ox.cs.pagoda.rules.DatalogProgram; |
| 15 | import uk.ac.ox.cs.pagoda.rules.Program; | 14 | import uk.ac.ox.cs.pagoda.rules.Program; |
| 16 | import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; | 15 | import uk.ac.ox.cs.pagoda.util.*; |
| 17 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 18 | import uk.ac.ox.cs.pagoda.util.Timer; | 16 | import uk.ac.ox.cs.pagoda.util.Timer; |
| 19 | import uk.ac.ox.cs.pagoda.util.UFS; | 17 | import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; |
| 20 | import uk.ac.ox.cs.pagoda.util.Utility; | 18 | |
| 21 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | 19 | import java.util.*; |
| 22 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 23 | import uk.ac.ox.cs.JRDFox.store.Parameters; | ||
| 24 | import uk.ac.ox.cs.JRDFox.store.TripleStatus; | ||
| 25 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | ||
| 26 | import uk.ac.ox.cs.JRDFox.store.DataStore.UpdateType; | ||
| 27 | 20 | ||
| 28 | public class BasicQueryEngine extends RDFoxQueryEngine { | 21 | public class BasicQueryEngine extends RDFoxQueryEngine { |
| 29 | 22 | ||
| 30 | protected DataStore store; | 23 | protected DataStore store; |
| 31 | protected Parameters parameters = new Parameters(); | 24 | protected Parameters parameters = new Parameters(); |
| 32 | 25 | Set<DLClause> materialisedRules = new HashSet<DLClause>(); | |
| 33 | public BasicQueryEngine(String name) { | 26 | private UFS<String> equalityGroups = null; |
| 34 | super(name); | 27 | |
| 35 | store = RDFoxQueryEngine.createDataStore(); | 28 | public BasicQueryEngine(String name) { |
| 36 | parameters.m_allAnswersInRoot = true; | 29 | super(name); |
| 37 | parameters.m_useBushy = true; | 30 | store = RDFoxQueryEngine.createDataStore(); |
| 38 | } | 31 | parameters.m_allAnswersInRoot = true; |
| 39 | 32 | parameters.m_useBushy = true; | |
| 40 | public void materialiseFoldedly(DatalogProgram dProgram, GapByStore4ID gap) { | 33 | } |
| 41 | if (gap != null) { | 34 | |
| 42 | materialise("lower program", dProgram.getLower().toString()); | 35 | public void materialiseFoldedly(DatalogProgram dProgram, GapByStore4ID gap) { |
| 43 | String program = dProgram.getUpper().toString(); | 36 | if(isDisposed()) throw new DisposedException(); |
| 44 | try { | 37 | if(gap != null) { |
| 45 | gap.compile(program); | 38 | materialise("lower program", dProgram.getLower().toString()); |
| 46 | gap.addBackTo(); | 39 | String program = dProgram.getUpper().toString(); |
| 47 | getDataStore().clearRulesAndMakeFactsExplicit(); | 40 | try { |
| 48 | } catch (JRDFStoreException e) { | 41 | gap.compile(program); |
| 49 | e.printStackTrace(); | 42 | gap.addBackTo(); |
| 50 | } finally { | 43 | getDataStore().clearRulesAndMakeFactsExplicit(); |
| 51 | gap.clear(); | 44 | } catch(JRDFStoreException e) { |
| 52 | } | 45 | e.printStackTrace(); |
| 53 | } | 46 | } finally { |
| 54 | else | 47 | gap.clear(); |
| 55 | materialise("upper program", dProgram.getUpper().toString()); | 48 | } |
| 56 | } | 49 | } |
| 57 | 50 | else | |
| 58 | public int materialiseRestrictedly(DatalogProgram dProgram, GapByStore4ID gap) { | 51 | materialise("upper program", dProgram.getUpper().toString()); |
| 59 | if (gap != null) { | 52 | } |
| 60 | materialise("lower program", dProgram.getLower().toString()); | 53 | |
| 61 | String program = dProgram.getUpper().toString(); | 54 | public int materialiseRestrictedly(DatalogProgram dProgram, GapByStore4ID gap) { |
| 62 | try { | 55 | if(isDisposed()) throw new DisposedException(); |
| 63 | gap.compile(program); | 56 | if(gap != null) { |
| 64 | gap.addBackTo(); | 57 | materialise("lower program", dProgram.getLower().toString()); |
| 65 | getDataStore().clearRulesAndMakeFactsExplicit(); | 58 | String program = dProgram.getUpper().toString(); |
| 66 | } catch (JRDFStoreException e) { | 59 | try { |
| 67 | e.printStackTrace(); | 60 | gap.compile(program); |
| 68 | } finally { | 61 | gap.addBackTo(); |
| 69 | gap.clear(); | 62 | getDataStore().clearRulesAndMakeFactsExplicit(); |
| 70 | } | 63 | } catch(JRDFStoreException e) { |
| 71 | } | 64 | e.printStackTrace(); |
| 72 | else | 65 | } finally { |
| 73 | materialise("upper program", dProgram.getUpper().toString()); | 66 | gap.clear(); |
| 74 | 67 | } | |
| 75 | return 1; | 68 | } |
| 76 | } | 69 | else |
| 77 | 70 | materialise("upper program", dProgram.getUpper().toString()); | |
| 78 | @Override | 71 | |
| 79 | public AnswerTuples evaluate(String queryText) { | 72 | return 1; |
| 80 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0]); | 73 | } |
| 81 | } | 74 | |
| 82 | 75 | @Override | |
| 83 | @Override | 76 | public AnswerTuples evaluate(String queryText) { |
| 84 | public AnswerTuples evaluate(String queryText, String[] answerVars) { | 77 | if(isDisposed()) throw new DisposedException(); |
| 85 | TupleIterator tupleIterator; | 78 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0]); |
| 86 | try { | 79 | } |
| 87 | tupleIterator = store.compileQuery(queryText.replace("_:", "?"), prefixes, parameters); | 80 | |
| 88 | } catch (JRDFStoreException e) { | 81 | @Override |
| 89 | e.printStackTrace(); | 82 | public AnswerTuples evaluate(String queryText, String[] answerVars) { |
| 90 | return null; | 83 | if(isDisposed()) throw new DisposedException(); |
| 91 | } | 84 | TupleIterator tupleIterator; |
| 92 | return new RDFoxAnswerTuples(answerVars, tupleIterator); | 85 | try { |
| 93 | } | 86 | tupleIterator = store.compileQuery(queryText.replace("_:", "?"), prefixes, parameters); |
| 94 | 87 | } catch(JRDFStoreException e) { | |
| 95 | @Override | 88 | e.printStackTrace(); |
| 96 | public DataStore getDataStore() { | 89 | return null; |
| 97 | return store; | 90 | } |
| 98 | } | 91 | return new RDFoxAnswerTuples(answerVars, tupleIterator); |
| 99 | 92 | } | |
| 100 | @Override | 93 | |
| 101 | public void dispose() { | 94 | @Override |
| 102 | store.dispose(); | 95 | public DataStore getDataStore() { |
| 103 | } | 96 | if(isDisposed()) throw new DisposedException(); |
| 104 | 97 | return store; | |
| 105 | protected void outputClassAssertions(String filename) { | 98 | } |
| 106 | TupleIterator allTuples = null; | 99 | |
| 107 | boolean redirect = false; | 100 | @Override |
| 108 | try { | 101 | public void dispose() { |
| 109 | allTuples = getDataStore().compileQuery("SELECT ?X ?Z WHERE { ?X <" + Namespace.RDF_TYPE + "> ?Z }", prefixes, parameters); | 102 | super.dispose(); |
| 110 | redirect = Utility.redirectCurrentOut(filename); | 103 | store.dispose(); |
| 111 | for (long multi = allTuples.open(); multi != 0; multi = allTuples.getNext()) | 104 | } |
| 112 | System.out.println(RDFoxTripleManager.getQuotedTerm(allTuples.getResource(0)) + " " + RDFoxTripleManager.getQuotedTerm(allTuples.getResource(1))); | 105 | |
| 113 | } catch (JRDFStoreException e) { | 106 | public void outputInstance4BinaryPredicate(String iri, String filename) { |
| 114 | e.printStackTrace(); | 107 | Utility.redirectCurrentOut(filename); |
| 115 | } finally { | 108 | outputInstance4BinaryPredicate(iri); |
| 116 | if (redirect) Utility.closeCurrentOut(); | 109 | Utility.closeCurrentOut(); |
| 117 | if (allTuples != null) allTuples.dispose(); | 110 | } |
| 118 | } | 111 | |
| 119 | } | 112 | public void outputInstance4BinaryPredicate(String iri) { |
| 120 | 113 | outputAnswers("select ?x ?y where { ?x <" + iri + "> ?y . }"); | |
| 121 | public void outputInstance4BinaryPredicate(String iri, String filename) { | 114 | } |
| 122 | Utility.redirectCurrentOut(filename); | 115 | |
| 123 | outputInstance4BinaryPredicate(iri); | 116 | public void outputInstanceNumbers(String filename) { |
| 124 | Utility.closeCurrentOut(); | 117 | TupleIterator predicateTuples = null; |
| 125 | } | 118 | TupleIterator instanceTuples; |
| 126 | 119 | Set<String> number = new HashSet<String>(); | |
| 127 | public void outputInstance4BinaryPredicate(String iri) { | 120 | String predicate; |
| 128 | outputAnswers("select ?x ?y where { ?x <" + iri + "> ?y . }"); | 121 | try { |
| 129 | } | 122 | predicateTuples = |
| 130 | 123 | getDataStore().compileQuery("SELECT DISTINCT ?Y WHERE { ?X <" + Namespace.RDF_TYPE + "> ?Y }", prefixes, parameters); | |
| 131 | public void outputInstanceNumbers(String filename) { | 124 | for(long multi = predicateTuples.open(); multi != 0; multi = predicateTuples.getNext()) { |
| 132 | TupleIterator predicateTuples = null; | 125 | predicate = RDFoxTripleManager.getQuotedTerm(predicateTuples.getResource(0)); |
| 133 | TupleIterator instanceTuples; | 126 | instanceTuples = null; |
| 134 | Set<String> number = new HashSet<String>(); | 127 | try { |
| 135 | String predicate; | 128 | instanceTuples = |
| 136 | try { | 129 | getDataStore().compileQuery("SELECT ?X WHERE { ?X <" + Namespace.RDF_TYPE + "> " + predicate + " }", prefixes, parameters); |
| 137 | predicateTuples = getDataStore().compileQuery("SELECT DISTINCT ?Y WHERE { ?X <" + Namespace.RDF_TYPE + "> ?Y }", prefixes, parameters); | 130 | long totalCount = 0; |
| 138 | for (long multi = predicateTuples.open(); multi != 0; multi = predicateTuples.getNext()) { | 131 | for(long multi1 = instanceTuples.open(); multi1 != 0; multi1 = instanceTuples.getNext()) { |
| 139 | predicate = RDFoxTripleManager.getQuotedTerm(predicateTuples.getResource(0)); | 132 | totalCount += instanceTuples.getMultiplicity(); |
| 140 | instanceTuples = null; | 133 | } |
| 141 | try { | 134 | number.add(predicate + " * " + totalCount); |
| 142 | instanceTuples = getDataStore().compileQuery("SELECT ?X WHERE { ?X <" + Namespace.RDF_TYPE + "> " + predicate + " }", prefixes, parameters); | 135 | } finally { |
| 143 | long totalCount = 0; | 136 | if(instanceTuples != null) instanceTuples.dispose(); |
| 144 | for (long multi1 = instanceTuples.open(); multi1 != 0; multi1 = instanceTuples.getNext()) { | 137 | } |
| 145 | totalCount += instanceTuples.getMultiplicity(); | 138 | } |
| 146 | } | 139 | } catch(JRDFStoreException e) { |
| 147 | number.add(predicate + " * " + totalCount); | 140 | e.printStackTrace(); |
| 148 | } finally { | 141 | } finally { |
| 149 | if (instanceTuples != null) instanceTuples.dispose(); | 142 | if(predicateTuples != null) predicateTuples.dispose(); |
| 150 | } | 143 | predicateTuples = null; |
| 151 | } | 144 | } |
| 152 | } catch (JRDFStoreException e) { | 145 | |
| 153 | e.printStackTrace(); | 146 | try { |
| 154 | } finally { | 147 | predicateTuples = |
| 155 | if (predicateTuples != null) predicateTuples.dispose(); | 148 | getDataStore().compileQuery("SELECT DISTINCT ?Y WHERE { ?X ?Y ?Z }", prefixes, parameters); |
| 156 | predicateTuples = null; | 149 | for(long multi = predicateTuples.open(); multi != 0; multi = predicateTuples.getNext()) { |
| 157 | } | 150 | predicate = RDFoxTripleManager.getQuotedTerm(predicateTuples.getResource(0)); |
| 158 | 151 | instanceTuples = null; | |
| 159 | try { | 152 | try { |
| 160 | predicateTuples = getDataStore().compileQuery("SELECT DISTINCT ?Y WHERE { ?X ?Y ?Z }", prefixes, parameters); | 153 | instanceTuples = |
| 161 | for (long multi = predicateTuples.open(); multi != 0; multi = predicateTuples.getNext()) { | 154 | getDataStore().compileQuery("SELECT ?X ?Z WHERE { ?X " + predicate + " ?Z }", prefixes, parameters); |
| 162 | predicate = RDFoxTripleManager.getQuotedTerm(predicateTuples.getResource(0)); | 155 | long totalCount = 0; |
| 163 | instanceTuples = null; | 156 | for(long multi1 = instanceTuples.open(); multi1 != 0; multi1 = instanceTuples.getNext()) |
| 164 | try { | 157 | totalCount += instanceTuples.getMultiplicity(); |
| 165 | instanceTuples = getDataStore().compileQuery("SELECT ?X ?Z WHERE { ?X " + predicate + " ?Z }", prefixes, parameters); | 158 | number.add(predicate + " * " + totalCount); |
| 166 | ; | 159 | } finally { |
| 167 | long totalCount = 0; | 160 | if(instanceTuples != null) instanceTuples.dispose(); |
| 168 | for (long multi1 = instanceTuples.open(); multi1 != 0; multi1 = instanceTuples.getNext()) | 161 | } |
| 169 | totalCount += instanceTuples.getMultiplicity(); | 162 | } |
| 170 | number.add(predicate + " * " + totalCount); | 163 | |
| 171 | } finally { | 164 | } catch(JRDFStoreException e) { |
| 172 | if (instanceTuples != null) instanceTuples.dispose(); | 165 | e.printStackTrace(); |
| 173 | } | 166 | } finally { |
| 174 | } | 167 | if(predicateTuples != null) predicateTuples.dispose(); |
| 175 | 168 | predicateTuples = null; | |
| 176 | } catch (JRDFStoreException e) { | 169 | } |
| 177 | e.printStackTrace(); | 170 | |
| 178 | } finally { | 171 | Utility.redirectCurrentOut(filename); |
| 179 | if (predicateTuples != null) predicateTuples.dispose(); | 172 | String[] ordered = number.toArray(new String[0]); |
| 180 | predicateTuples = null; | 173 | Arrays.sort(ordered, new DLPredicateComparator()); |
| 181 | } | 174 | for(String line : ordered) System.out.println(line); |
| 182 | 175 | Utility.closeCurrentOut(); | |
| 183 | Utility.redirectCurrentOut(filename); | 176 | |
| 184 | String[] ordered = number.toArray(new String[0]); | 177 | } |
| 185 | Arrays.sort(ordered, new DLPredicateComparator()); | 178 | |
| 186 | for (String line: ordered) System.out.println(line); | 179 | public TupleIterator internal_evaluateAgainstIDBs(String queryText) throws JRDFStoreException { |
| 187 | Utility.closeCurrentOut(); | 180 | TupleIterator iter = |
| 188 | 181 | store.compileQuery(queryText, prefixes, parameters, TripleStatus.TUPLE_STATUS_IDB.union(TripleStatus.TUPLE_STATUS_EDB), TripleStatus.TUPLE_STATUS_IDB); | |
| 189 | } | 182 | // iter.open(); |
| 190 | 183 | return iter; | |
| 191 | public TupleIterator internal_evaluateAgainstIDBs(String queryText) throws JRDFStoreException { | 184 | } |
| 192 | TupleIterator iter = store.compileQuery(queryText, prefixes, parameters, TripleStatus.TUPLE_STATUS_IDB.union(TripleStatus.TUPLE_STATUS_EDB), TripleStatus.TUPLE_STATUS_IDB); | 185 | |
| 186 | public TupleIterator internal_evaluate(String queryText) throws JRDFStoreException { | ||
| 187 | TupleIterator iter = store.compileQuery(queryText, prefixes, parameters); | ||
| 188 | // iter.open(); | ||
| 189 | return iter; | ||
| 190 | } | ||
| 191 | |||
| 192 | public void setExpandEquality(boolean flag) { | ||
| 193 | parameters.m_expandEquality = flag; | ||
| 194 | } | ||
| 195 | |||
| 196 | public TupleIterator internal_evaluateNotExpanded(String queryText) throws JRDFStoreException { | ||
| 197 | parameters.m_expandEquality = false; | ||
| 198 | TupleIterator iter = store.compileQuery(queryText, prefixes, parameters); | ||
| 193 | // iter.open(); | 199 | // iter.open(); |
| 194 | return iter; | 200 | parameters.m_expandEquality = true; |
| 195 | } | 201 | return iter; |
| 196 | 202 | } | |
| 197 | public TupleIterator internal_evaluate(String queryText) throws JRDFStoreException { | 203 | |
| 198 | TupleIterator iter = store.compileQuery(queryText, prefixes, parameters); | 204 | public TupleIterator internal_evaluate(String queryText, boolean incrementally) throws JRDFStoreException { |
| 199 | // iter.open(); | 205 | return incrementally ? internal_evaluateAgainstIDBs(queryText) : internal_evaluate(queryText); |
| 200 | return iter; | 206 | } |
| 201 | } | 207 | |
| 202 | 208 | public String getUnusedRules(Collection<DLClause> clauses, boolean toUpdate) { | |
| 203 | public void setExpandEquality(boolean flag) { | 209 | DLClause clause; |
| 204 | parameters.m_expandEquality = flag; | 210 | for(Iterator<DLClause> iter = clauses.iterator(); iter.hasNext(); ) { |
| 205 | } | 211 | if(materialisedRules.contains(clause = iter.next())) |
| 206 | 212 | iter.remove(); | |
| 207 | public TupleIterator internal_evaluateNotExpanded(String queryText) throws JRDFStoreException { | 213 | else if(toUpdate) materialisedRules.add(clause); |
| 208 | parameters.m_expandEquality = false; | 214 | } |
| 209 | TupleIterator iter = store.compileQuery(queryText, prefixes, parameters); | 215 | |
| 210 | // iter.open(); | 216 | if(clauses.isEmpty()) return null; |
| 211 | parameters.m_expandEquality = true; | 217 | |
| 212 | return iter; | 218 | return Program.toString(clauses); |
| 213 | } | 219 | } |
| 214 | 220 | ||
| 215 | 221 | public void outputMaterialisedRules() { | |
| 216 | public TupleIterator internal_evaluate(String queryText, boolean incrementally) throws JRDFStoreException { | 222 | System.out.println(DLClauseHelper.toString(materialisedRules)); |
| 217 | return incrementally ? internal_evaluateAgainstIDBs(queryText) : internal_evaluate(queryText); | 223 | } |
| 218 | } | 224 | |
| 219 | 225 | public void outputAnswers(String query) { | |
| 220 | Set<DLClause> materialisedRules = new HashSet<DLClause>(); | 226 | TupleIterator iter = null; |
| 221 | 227 | try { | |
| 222 | public String getUnusedRules(Collection<DLClause> clauses, boolean toUpdate) { | 228 | iter = internal_evaluate(query); |
| 223 | DLClause clause; | 229 | System.out.println(query); |
| 224 | for (Iterator<DLClause> iter = clauses.iterator(); iter.hasNext(); ) { | 230 | int arity = iter.getArity(); |
| 225 | if (materialisedRules.contains(clause = iter.next())) | 231 | for(long multi = iter.open(); multi != 0; multi = iter.getNext()) { |
| 226 | iter.remove(); | 232 | for(int i = 0; i < arity; ++i) |
| 227 | else if (toUpdate) materialisedRules.add(clause); | 233 | System.out.print(RDFoxTripleManager.getQuotedTerm(iter.getResource(i)) + "\t"); |
| 228 | } | 234 | System.out.println(); |
| 229 | 235 | } | |
| 230 | if (clauses.isEmpty()) return null; | 236 | } catch(JRDFStoreException e) { |
| 231 | 237 | e.printStackTrace(); | |
| 232 | return Program.toString(clauses); | 238 | } finally { |
| 233 | } | 239 | if(iter != null) iter.dispose(); |
| 234 | 240 | } | |
| 235 | public void outputMaterialisedRules() { | 241 | } |
| 236 | System.out.println(DLClauseHelper.toString(materialisedRules)); | 242 | |
| 237 | } | 243 | public void outputInstance4UnaryPredicate(String iri) { |
| 238 | 244 | outputAnswers("select ?x where { ?x " | |
| 239 | public void outputAnswers(String query) { | 245 | + "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <" |
| 240 | TupleIterator iter = null; | 246 | + iri |
| 241 | try { | 247 | + "> .}"); |
| 242 | iter = internal_evaluate(query); | 248 | } |
| 243 | System.out.println(query); | 249 | |
| 244 | int arity = iter.getArity(); | 250 | public void outputSubjects(String p, String o) { |
| 245 | for (long multi = iter.open(); multi != 0; multi = iter.getNext()) { | 251 | outputAnswers("select x where { ?x <" + p + "> <" + o + "> . }"); |
| 246 | for (int i = 0; i < arity; ++i) | 252 | } |
| 247 | System.out.print(RDFoxTripleManager.getQuotedTerm(iter.getResource(i)) + "\t"); | 253 | |
| 248 | System.out.println(); | 254 | public void outputObjects(String s, String p) { |
| 249 | } | 255 | outputAnswers("select ?x where { <" + s + "> <" + p + "> ?x . }"); |
| 250 | } catch (JRDFStoreException e) { | 256 | } |
| 251 | e.printStackTrace(); | 257 | |
| 252 | } finally { | 258 | public void outputIDBFacts() { |
| 253 | if (iter != null) iter.dispose(); | 259 | TupleIterator iter = null; |
| 254 | } | 260 | try { |
| 255 | } | 261 | iter = internal_evaluateAgainstIDBs("select distict ?x ?y ?z where { ?x ?y ?z }"); |
| 256 | 262 | for(long multi = iter.open(); multi != 0; multi = iter.getNext()) { | |
| 257 | public void outputInstance4UnaryPredicate(String iri) { | 263 | for(int i = 0; i < 3; ++i) |
| 258 | outputAnswers("select ?x where { ?x " | 264 | System.out.print(RDFoxTripleManager.getQuotedTerm(iter.getResource(i)) + "\t"); |
| 259 | + "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <" | 265 | System.out.println(); |
| 260 | + iri | 266 | } |
| 261 | + "> .}"); | 267 | } catch(JRDFStoreException e) { |
| 262 | } | 268 | // TODO Auto-generated catch block |
| 263 | 269 | e.printStackTrace(); | |
| 264 | public void outputSubjects(String p, String o) { | 270 | } finally { |
| 265 | outputAnswers("select x where { ?x <" + p + "> <" + o + "> . }"); | 271 | if(iter != null) iter.dispose(); |
| 266 | } | 272 | } |
| 267 | 273 | ||
| 268 | public void outputObjects(String s, String p) { | 274 | } |
| 269 | outputAnswers("select ?x where { <" + s + "> <" + p + "> ?x . }"); | 275 | |
| 270 | } | 276 | public void outputType4Individual(String iri) { |
| 271 | 277 | outputAnswers("select ?z where { <" + iri + "> " + Namespace.RDF_TYPE_QUOTED + " ?z }"); | |
| 272 | public void outputIDBFacts() { | 278 | } |
| 273 | TupleIterator iter = null; | 279 | |
| 274 | try { | 280 | public int getSameAsNumber() { |
| 275 | iter = internal_evaluateAgainstIDBs("select distict ?x ?y ?z where { ?x ?y ?z }"); | 281 | TupleIterator iter = null; |
| 276 | for (long multi = iter.open(); multi != 0; multi = iter.getNext()) { | 282 | int counter = 0; |
| 277 | for (int i = 0; i < 3; ++i) | 283 | try { |
| 278 | System.out.print(RDFoxTripleManager.getQuotedTerm(iter.getResource(i)) + "\t"); | 284 | iter = internal_evaluate("select ?x ?y where {?x " + Namespace.EQUALITY_QUOTED + " ?y . }"); |
| 279 | System.out.println(); | 285 | for(long multi = iter.open(); multi != 0; multi = iter.getNext()) |
| 280 | } | 286 | if(iter.getResourceID(0) != iter.getResourceID(1)) |
| 281 | } catch (JRDFStoreException e) { | 287 | ++counter; |
| 282 | // TODO Auto-generated catch block | 288 | } catch(JRDFStoreException e) { |
| 283 | e.printStackTrace(); | 289 | e.printStackTrace(); |
| 284 | } finally { | 290 | } finally { |
| 285 | if (iter != null) iter.dispose(); | 291 | if(iter != null) iter.dispose(); |
| 286 | } | 292 | } |
| 287 | 293 | return counter; | |
| 288 | } | 294 | } |
| 289 | 295 | ||
| 290 | public void outputType4Individual(String iri) { | 296 | public UFS<String> getEqualityGroups(boolean reuse) { |
| 291 | outputAnswers("select ?z where { <" + iri + "> " + Namespace.RDF_TYPE_QUOTED + " ?z }"); | 297 | if(reuse && equalityGroups != null) return equalityGroups; |
| 292 | } | 298 | |
| 293 | 299 | equalityGroups = new UFS<String>(); | |
| 294 | public int getSameAsNumber() { | 300 | |
| 295 | TupleIterator iter = null; | 301 | TupleIterator answers = null; |
| 296 | int counter = 0; | 302 | try { |
| 297 | try { | 303 | Timer t = new Timer(); |
| 298 | iter = internal_evaluate("select ?x ?y where {?x " + Namespace.EQUALITY_QUOTED + " ?y . }"); | 304 | answers = internal_evaluate("select ?x ?z where {?x " + Namespace.EQUALITY_QUOTED + "?z . }"); |
| 299 | for (long multi = iter.open(); multi != 0; multi = iter.getNext()) | 305 | for(long multi = answers.open(); multi != 0; multi = answers.getNext()) { |
| 300 | if (iter.getResourceID(0) != iter.getResourceID(1)) | 306 | if(answers.getResourceID(0) != answers.getResourceID(1)) |
| 301 | ++counter; | 307 | equalityGroups.merge(answers.getResource(0).m_lexicalForm, answers.getResource(1).m_lexicalForm); |
| 302 | } catch (JRDFStoreException e) { | 308 | } |
| 303 | e.printStackTrace(); | 309 | Utility.logInfo("@Time to group individuals by equality: " + t.duration()); |
| 304 | } finally { | 310 | } catch(JRDFStoreException e) { |
| 305 | if (iter != null) iter.dispose(); | 311 | e.printStackTrace(); |
| 306 | } | 312 | } finally { |
| 307 | return counter; | 313 | if(answers != null) answers.dispose(); |
| 308 | } | 314 | } |
| 309 | 315 | ||
| 310 | private UFS<String> equalityGroups = null; | 316 | return equalityGroups; |
| 311 | 317 | } | |
| 312 | public UFS<String> getEqualityGroups(boolean reuse) { | 318 | |
| 313 | if (reuse && equalityGroups != null) return equalityGroups; | 319 | public void clearRulesAndIDBFacts(Collection<int[]> collection) { |
| 314 | 320 | // performDeletion(collection); | |
| 315 | equalityGroups = new UFS<String>(); | 321 | collection.clear(); |
| 316 | 322 | try { | |
| 317 | TupleIterator answers = null; | 323 | store.clearRulesAndMakeFactsExplicit(); |
| 318 | try { | 324 | } catch(JRDFStoreException e) { |
| 319 | Timer t = new Timer(); | 325 | e.printStackTrace(); |
| 320 | answers = internal_evaluate("select ?x ?z where {?x " + Namespace.EQUALITY_QUOTED + "?z . }"); | 326 | } |
| 321 | for (long multi = answers.open(); multi != 0; multi = answers.getNext()) { | 327 | } |
| 322 | if (answers.getResourceID(0) != answers.getResourceID(1)) | 328 | |
| 323 | equalityGroups.merge(answers.getResource(0).m_lexicalForm, answers.getResource(1).m_lexicalForm); | 329 | protected void outputClassAssertions(String filename) { |
| 324 | } | 330 | TupleIterator allTuples = null; |
| 325 | Utility.logInfo("@Time to group individuals by equality: " + t.duration()); | 331 | boolean redirect = false; |
| 326 | } catch (JRDFStoreException e) { | 332 | try { |
| 327 | e.printStackTrace(); | 333 | allTuples = |
| 328 | } finally { | 334 | getDataStore().compileQuery("SELECT ?X ?Z WHERE { ?X <" + Namespace.RDF_TYPE + "> ?Z }", prefixes, parameters); |
| 329 | if (answers != null) answers.dispose(); | 335 | redirect = Utility.redirectCurrentOut(filename); |
| 330 | } | 336 | for(long multi = allTuples.open(); multi != 0; multi = allTuples.getNext()) |
| 331 | 337 | System.out.println(RDFoxTripleManager.getQuotedTerm(allTuples.getResource(0)) + " " + RDFoxTripleManager | |
| 332 | return equalityGroups; | 338 | .getQuotedTerm(allTuples.getResource(1))); |
| 333 | } | 339 | } catch(JRDFStoreException e) { |
| 334 | 340 | e.printStackTrace(); | |
| 335 | public void clearRulesAndIDBFacts(Collection<int[]> collection) { | 341 | } finally { |
| 336 | // performDeletion(collection); | 342 | if(redirect) Utility.closeCurrentOut(); |
| 337 | collection.clear(); | 343 | if(allTuples != null) allTuples.dispose(); |
| 338 | try { | 344 | } |
| 339 | store.clearRulesAndMakeFactsExplicit(); | 345 | } |
| 340 | } catch (JRDFStoreException e) { | 346 | |
| 341 | e.printStackTrace(); | 347 | @SuppressWarnings("unused") |
| 342 | } | 348 | private void performDeletion(Collection<int[]> collection) { |
| 343 | } | 349 | Utility.logInfo("Remove all rules, idb facts and added staff..."); |
| 344 | 350 | Timer timer = new Timer(); | |
| 345 | @SuppressWarnings("unused") | 351 | TupleIterator iter = null; |
| 346 | private void performDeletion(Collection<int[]> collection) { | 352 | try { |
| 347 | Utility.logInfo("Remove all rules, idb facts and added staff..."); | 353 | UpdateType ut = UpdateType.ScheduleForDeletion; |
| 348 | Timer timer = new Timer(); | 354 | for(int[] t : collection) |
| 349 | TupleIterator iter = null; | 355 | store.addTriplesByResourceIDs(t, ut); |
| 350 | try { | 356 | |
| 351 | UpdateType ut = UpdateType.ScheduleForDeletion; | 357 | try { |
| 352 | for (int[] t: collection) | 358 | iter = internal_evaluateAgainstIDBs("select ?x ?y ?z where { ?x ?y ?z . }"); |
| 353 | store.addTriplesByResourceIDs(t, ut); | 359 | for(long multi = iter.open(); multi != 0; multi = iter.getNext()) { |
| 354 | 360 | int[] triple = new int[3]; | |
| 355 | try { | 361 | for(int i = 0; i < 3; ++i) |
| 356 | iter = internal_evaluateAgainstIDBs("select ?x ?y ?z where { ?x ?y ?z . }"); | 362 | triple[i] = iter.getResourceID(i); |
| 357 | for (long multi = iter.open(); multi != 0; multi = iter.getNext()) { | 363 | store.addTriplesByResourceIDs(triple, ut); |
| 358 | int[] triple = new int[3]; | 364 | } |
| 359 | for (int i = 0; i < 3; ++i) | 365 | } finally { |
| 360 | triple[i] = iter.getResourceID(i); | 366 | if(iter != null) iter.dispose(); |
| 361 | store.addTriplesByResourceIDs(triple, ut); | 367 | iter = null; |
| 362 | } | 368 | } |
| 363 | } finally { | 369 | store.applyReasoning(true); |
| 364 | if (iter != null) iter.dispose(); | 370 | } catch(JRDFStoreException e) { |
| 365 | iter = null; | 371 | e.printStackTrace(); |
| 366 | } | 372 | } |
| 367 | store.applyReasoning(true); | 373 | Utility.logInfo("Time for deletion: " + timer.duration()); |
| 368 | } catch (JRDFStoreException e) { | 374 | } |
| 369 | e.printStackTrace(); | ||
| 370 | } | ||
| 371 | Utility.logInfo("Time for deletion: " + timer.duration()); | ||
| 372 | } | ||
| 373 | 375 | ||
| 374 | 376 | ||
| 375 | } | 377 | } |
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 f068164..98f0c35 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java | |||
| @@ -12,6 +12,7 @@ import uk.ac.ox.cs.pagoda.query.AnswerTuples; | |||
| 12 | import uk.ac.ox.cs.pagoda.query.AnswerTuplesImp; | 12 | import uk.ac.ox.cs.pagoda.query.AnswerTuplesImp; |
| 13 | import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; | 13 | import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; |
| 14 | import uk.ac.ox.cs.pagoda.util.Utility; | 14 | import uk.ac.ox.cs.pagoda.util.Utility; |
| 15 | import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; | ||
| 15 | 16 | ||
| 16 | import java.io.File; | 17 | import java.io.File; |
| 17 | import java.io.FileNotFoundException; | 18 | import java.io.FileNotFoundException; |
| @@ -19,82 +20,90 @@ import java.nio.file.Paths; | |||
| 19 | import java.util.Set; | 20 | import java.util.Set; |
| 20 | 21 | ||
| 21 | public class KarmaQueryEngine extends RDFoxQueryEngine { | 22 | public class KarmaQueryEngine extends RDFoxQueryEngine { |
| 22 | 23 | ||
| 23 | private MyKarma reasoner = null; | 24 | String karmaDataFile = null, karmaRuleFile = null; |
| 24 | 25 | private MyKarma reasoner = null; | |
| 25 | String karmaDataFile = null, karmaRuleFile = null; | 26 | |
| 26 | 27 | public KarmaQueryEngine(String name) { | |
| 27 | public KarmaQueryEngine(String name) { | 28 | super(name); |
| 28 | super(name); | 29 | |
| 29 | |||
| 30 | // int Base = 1 << 6; | 30 | // int Base = 1 << 6; |
| 31 | // int index = (new Random().nextInt() % Base + Base) % Base; | 31 | // int index = (new Random().nextInt() % Base + Base) % Base; |
| 32 | // karmaDataFile = "karma_data" + index + ".ttl"; | 32 | // karmaDataFile = "karma_data" + index + ".ttl"; |
| 33 | // karmaRuleFile = "karma_rule" + index + ".dlog"; | 33 | // karmaRuleFile = "karma_rule" + index + ".dlog"; |
| 34 | karmaDataFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_data.ttl").toString(); | 34 | karmaDataFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_data.ttl").toString(); |
| 35 | karmaRuleFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_rule.dlog").toString(); | 35 | karmaRuleFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_rule.dlog").toString(); |
| 36 | 36 | ||
| 37 | reasoner = new MyKarma(); | 37 | reasoner = new MyKarma(); |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | public MyKarma getReasoner() { | 40 | public MyKarma getReasoner() { |
| 41 | return reasoner; | 41 | if(isDisposed()) throw new DisposedException(); |
| 42 | } | 42 | return reasoner; |
| 43 | 43 | } | |
| 44 | public void processOntology(OWLOntology elhoOntology) { | 44 | |
| 45 | try { | 45 | public void processOntology(OWLOntology elhoOntology) { |
| 46 | OntologyProcesser.transformOntology(elhoOntology, new File(karmaDataFile), new File(karmaRuleFile)); | 46 | if(isDisposed()) throw new DisposedException(); |
| 47 | } catch (IllegalInputOntologyException e) { | 47 | try { |
| 48 | e.printStackTrace(); | 48 | OntologyProcesser.transformOntology(elhoOntology, new File(karmaDataFile), new File(karmaRuleFile)); |
| 49 | } | 49 | } catch(IllegalInputOntologyException e) { |
| 50 | } | 50 | e.printStackTrace(); |
| 51 | 51 | } | |
| 52 | @Override | 52 | } |
| 53 | public void dispose() { | 53 | |
| 54 | reasoner.dispose(); | 54 | @Override |
| 55 | } | 55 | public void dispose() { |
| 56 | 56 | super.dispose(); | |
| 57 | @Override | 57 | reasoner.dispose(); |
| 58 | public AnswerTuples evaluate(String queryText) { | 58 | } |
| 59 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], null); | 59 | |
| 60 | } | 60 | @Override |
| 61 | 61 | public AnswerTuples evaluate(String queryText) { | |
| 62 | @Override | 62 | if(isDisposed()) throw new DisposedException(); |
| 63 | public AnswerTuples evaluate(String queryText, String[] answerVars) { | 63 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], null); |
| 64 | return evaluate(queryText, answerVars, null); | 64 | } |
| 65 | } | 65 | |
| 66 | 66 | @Override | |
| 67 | public AnswerTuples evaluate(String queryText, AnswerTuples soundAnswerTuples) { | 67 | public AnswerTuples evaluate(String queryText, String[] answerVars) { |
| 68 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], soundAnswerTuples); | 68 | if(isDisposed()) throw new DisposedException(); |
| 69 | } | 69 | return evaluate(queryText, answerVars, null); |
| 70 | 70 | } | |
| 71 | public AnswerTuples evaluate(String queryText, String[] answerVars, AnswerTuples soundAnswerTuples) { | 71 | |
| 72 | KarmaQuery karmaQuery = new KarmaQuery(queryText.replace("_:", "?")); | 72 | public AnswerTuples evaluate(String queryText, AnswerTuples soundAnswerTuples) { |
| 73 | reasoner.setConcurrence(false); | 73 | if(isDisposed()) throw new DisposedException(); |
| 74 | ConjunctiveQuery cq = karmaQuery.getConjunctiveQuery(); | 74 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], soundAnswerTuples); |
| 75 | if (cq == null) return null; | 75 | } |
| 76 | Set<AnswerTuple> answers = reasoner.answerCQ(cq, soundAnswerTuples, !queryText.contains("_:")); | 76 | |
| 77 | return new AnswerTuplesImp(answerVars, answers); | 77 | public AnswerTuples evaluate(String queryText, String[] answerVars, AnswerTuples soundAnswerTuples) { |
| 78 | } | 78 | if(isDisposed()) throw new DisposedException(); |
| 79 | 79 | KarmaQuery karmaQuery = new KarmaQuery(queryText.replace("_:", "?")); | |
| 80 | @Override | 80 | reasoner.setConcurrence(false); |
| 81 | public DataStore getDataStore() { | 81 | ConjunctiveQuery cq = karmaQuery.getConjunctiveQuery(); |
| 82 | return reasoner.getStore(); | 82 | if(cq == null) return null; |
| 83 | } | 83 | Set<AnswerTuple> answers = reasoner.answerCQ(cq, soundAnswerTuples, !queryText.contains("_:")); |
| 84 | 84 | return new AnswerTuplesImp(answerVars, answers); | |
| 85 | public void initialiseKarma() { | 85 | } |
| 86 | try { | 86 | |
| 87 | reasoner.initializeData(new File(karmaDataFile)); | 87 | @Override |
| 88 | reasoner.materialise(new File(karmaRuleFile)); | 88 | public DataStore getDataStore() { |
| 89 | 89 | if(isDisposed()) throw new DisposedException(); | |
| 90 | File tmp; | 90 | return reasoner.getStore(); |
| 91 | if (karmaDataFile != null && ((tmp = new File(karmaDataFile)).exists())) tmp.delete(); | 91 | } |
| 92 | if (karmaRuleFile != null && ((tmp = new File(karmaRuleFile)).exists())) tmp.delete(); | 92 | |
| 93 | } catch (FileNotFoundException e) { | 93 | public void initialiseKarma() { |
| 94 | e.printStackTrace(); | 94 | if(isDisposed()) throw new DisposedException(); |
| 95 | } catch (JRDFStoreException e) { | 95 | try { |
| 96 | e.printStackTrace(); | 96 | reasoner.initializeData(new File(karmaDataFile)); |
| 97 | } | 97 | reasoner.materialise(new File(karmaRuleFile)); |
| 98 | } | 98 | |
| 99 | File tmp; | ||
| 100 | if(karmaDataFile != null && ((tmp = new File(karmaDataFile)).exists())) tmp.delete(); | ||
| 101 | if(karmaRuleFile != null && ((tmp = new File(karmaRuleFile)).exists())) tmp.delete(); | ||
| 102 | } catch(FileNotFoundException e) { | ||
| 103 | e.printStackTrace(); | ||
| 104 | } catch(JRDFStoreException e) { | ||
| 105 | e.printStackTrace(); | ||
| 106 | } | ||
| 107 | } | ||
| 99 | 108 | ||
| 100 | } | 109 | } |
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxAnswerTuples.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxAnswerTuples.java index dd71809..1e8181f 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxAnswerTuples.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxAnswerTuples.java | |||
| @@ -3,15 +3,14 @@ package uk.ac.ox.cs.pagoda.reasoner.light; | |||
| 3 | import org.semanticweb.HermiT.model.Constant; | 3 | import org.semanticweb.HermiT.model.Constant; |
| 4 | import org.semanticweb.HermiT.model.Individual; | 4 | import org.semanticweb.HermiT.model.Individual; |
| 5 | import org.semanticweb.HermiT.model.Term; | 5 | import org.semanticweb.HermiT.model.Term; |
| 6 | |||
| 7 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 8 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 9 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 10 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | 6 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; |
| 11 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | 7 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; |
| 12 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; | 8 | import uk.ac.ox.cs.JRDFox.store.TupleIterator; |
| 9 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 10 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 11 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 13 | 12 | ||
| 14 | public class RDFoxAnswerTuples implements AnswerTuples { | 13 | public class RDFoxAnswerTuples extends AnswerTuples { |
| 15 | 14 | ||
| 16 | long multi; | 15 | long multi; |
| 17 | TupleIterator m_iter; | 16 | TupleIterator m_iter; |
| @@ -22,7 +21,18 @@ public class RDFoxAnswerTuples implements AnswerTuples { | |||
| 22 | m_iter = iter; | 21 | m_iter = iter; |
| 23 | reset(); | 22 | reset(); |
| 24 | } | 23 | } |
| 25 | 24 | ||
| 25 | public static Term getHermitTerm(GroundTerm t) { | ||
| 26 | if(t instanceof uk.ac.ox.cs.JRDFox.model.Individual) { | ||
| 27 | uk.ac.ox.cs.JRDFox.model.Individual individual = (uk.ac.ox.cs.JRDFox.model.Individual) t; | ||
| 28 | return Individual.create(individual.getIRI()); | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | uk.ac.ox.cs.JRDFox.model.Literal literal = ((uk.ac.ox.cs.JRDFox.model.Literal) t); | ||
| 32 | return Constant.create(literal.getLexicalForm(), literal.getDatatype().getIRI()); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 26 | @Override | 36 | @Override |
| 27 | public boolean isValid() { | 37 | public boolean isValid() { |
| 28 | return multi != 0; | 38 | return multi != 0; |
| @@ -34,7 +44,7 @@ public class RDFoxAnswerTuples implements AnswerTuples { | |||
| 34 | return m_iter.getArity(); | 44 | return m_iter.getArity(); |
| 35 | } catch (JRDFStoreException e) { | 45 | } catch (JRDFStoreException e) { |
| 36 | e.printStackTrace(); | 46 | e.printStackTrace(); |
| 37 | return -1; | 47 | return -1; |
| 38 | } | 48 | } |
| 39 | } | 49 | } |
| 40 | 50 | ||
| @@ -44,15 +54,12 @@ public class RDFoxAnswerTuples implements AnswerTuples { | |||
| 44 | multi = m_iter.getNext(); | 54 | multi = m_iter.getNext(); |
| 45 | } catch (JRDFStoreException e) { | 55 | } catch (JRDFStoreException e) { |
| 46 | e.printStackTrace(); | 56 | e.printStackTrace(); |
| 47 | } | 57 | } |
| 48 | } | 58 | } |
| 49 | 59 | ||
| 50 | @Override | 60 | @Override |
| 51 | public void dispose() { | 61 | public void dispose() { |
| 52 | m_iter.dispose(); | 62 | super.dispose(); |
| 53 | } | ||
| 54 | |||
| 55 | protected void finalize() { | ||
| 56 | m_iter.dispose(); | 63 | m_iter.dispose(); |
| 57 | } | 64 | } |
| 58 | 65 | ||
| @@ -85,16 +92,9 @@ public class RDFoxAnswerTuples implements AnswerTuples { | |||
| 85 | public String[] getAnswerVariables() { | 92 | public String[] getAnswerVariables() { |
| 86 | return m_answerVars; | 93 | return m_answerVars; |
| 87 | } | 94 | } |
| 88 | 95 | ||
| 89 | public static Term getHermitTerm(GroundTerm t) { | 96 | protected void finalize() { |
| 90 | if (t instanceof uk.ac.ox.cs.JRDFox.model.Individual) { | 97 | m_iter.dispose(); |
| 91 | uk.ac.ox.cs.JRDFox.model.Individual individual = (uk.ac.ox.cs.JRDFox.model.Individual) t; | ||
| 92 | return Individual.create(individual.getIRI()); | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | uk.ac.ox.cs.JRDFox.model.Literal literal = ((uk.ac.ox.cs.JRDFox.model.Literal) t); | ||
| 96 | return Constant.create(literal.getLexicalForm(), literal.getDatatype().getIRI()); | ||
| 97 | } | ||
| 98 | } | 98 | } |
| 99 | 99 | ||
| 100 | } | 100 | } |
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 61500f5..f835ba9 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java | |||
| @@ -13,119 +13,126 @@ import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner; | |||
| 13 | import uk.ac.ox.cs.pagoda.tracking.AnswerTuplesWriter; | 13 | import uk.ac.ox.cs.pagoda.tracking.AnswerTuplesWriter; |
| 14 | import uk.ac.ox.cs.pagoda.util.Timer; | 14 | import uk.ac.ox.cs.pagoda.util.Timer; |
| 15 | import uk.ac.ox.cs.pagoda.util.Utility; | 15 | import uk.ac.ox.cs.pagoda.util.Utility; |
| 16 | import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; | ||
| 16 | 17 | ||
| 17 | import java.io.File; | 18 | import java.io.File; |
| 18 | import java.util.Collection; | 19 | import java.util.Collection; |
| 19 | 20 | ||
| 20 | public abstract class RDFoxQueryEngine implements QueryEngine { | 21 | public abstract class RDFoxQueryEngine extends QueryEngine { |
| 21 | |||
| 22 | public static final int matNoOfThreads = Runtime.getRuntime().availableProcessors() * 2; | ||
| 23 | protected String name; | ||
| 24 | protected Prefixes prefixes = MyPrefixes.PAGOdAPrefixes.getRDFoxPrefixes(); | ||
| 25 | 22 | ||
| 26 | public RDFoxQueryEngine(String name) { | 23 | public static final int matNoOfThreads = Runtime.getRuntime().availableProcessors() * 2; |
| 27 | this.name = name; | 24 | protected String name; |
| 28 | } | 25 | protected Prefixes prefixes = MyPrefixes.PAGOdAPrefixes.getRDFoxPrefixes(); |
| 29 | 26 | ||
| 30 | public static DataStore createDataStore() { | 27 | public RDFoxQueryEngine(String name) { |
| 31 | DataStore instance = null; | 28 | this.name = name; |
| 32 | try { | 29 | } |
| 30 | |||
| 31 | public static DataStore createDataStore() { | ||
| 32 | DataStore instance = null; | ||
| 33 | try { | ||
| 33 | // instance = new DataStore("par-head-n"); | 34 | // instance = new DataStore("par-head-n"); |
| 34 | instance = new DataStore(StoreType.NarrowParallelHead); | 35 | instance = new DataStore(StoreType.NarrowParallelHead); |
| 35 | instance.setNumberOfThreads(matNoOfThreads); | 36 | instance.setNumberOfThreads(matNoOfThreads); |
| 36 | instance.initialize(); | 37 | instance.initialize(); |
| 37 | } catch(JRDFStoreException e) { | 38 | } catch(JRDFStoreException e) { |
| 38 | e.printStackTrace(); | 39 | e.printStackTrace(); |
| 39 | } | 40 | } |
| 40 | return instance; | 41 | return instance; |
| 41 | } | 42 | } |
| 42 | 43 | ||
| 43 | public String getName() { | 44 | public String getName() { |
| 44 | return name; | 45 | if(isDisposed()) throw new DisposedException(); |
| 45 | } | 46 | return name; |
| 46 | 47 | } | |
| 47 | public abstract DataStore getDataStore(); | 48 | |
| 48 | 49 | public abstract DataStore getDataStore(); | |
| 49 | public abstract void dispose(); | 50 | |
| 50 | 51 | public void importRDFData(String fileName, String importedFile) { | |
| 51 | public void importRDFData(String fileName, String importedFile) { | 52 | if(isDisposed()) throw new DisposedException(); |
| 52 | if(importedFile == null || importedFile.isEmpty()) return; | 53 | if(importedFile == null || importedFile.isEmpty()) return; |
| 53 | Timer t = new Timer(); | 54 | Timer t = new Timer(); |
| 54 | DataStore store = getDataStore(); | 55 | DataStore store = getDataStore(); |
| 55 | try { | 56 | try { |
| 56 | long oldTripleCount = store.getTriplesCount(), tripleCount; | 57 | long oldTripleCount = store.getTriplesCount(), tripleCount; |
| 57 | for (String file: importedFile.split(QueryReasoner.ImportDataFileSeparator)) { | 58 | for(String file : importedFile.split(QueryReasoner.ImportDataFileSeparator)) { |
| 58 | store.importTurtleFile(new File(file), prefixes); | 59 | store.importTurtleFile(new File(file), prefixes); |
| 59 | } | 60 | } |
| 60 | tripleCount = store.getTriplesCount(); | 61 | tripleCount = store.getTriplesCount(); |
| 61 | Utility.logDebug(name + " store after importing " + fileName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)"); | 62 | Utility.logDebug(name + " store after importing " + fileName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)"); |
| 62 | store.clearRulesAndMakeFactsExplicit(); | 63 | store.clearRulesAndMakeFactsExplicit(); |
| 63 | } catch (JRDFStoreException e) { | 64 | } catch(JRDFStoreException e) { |
| 64 | e.printStackTrace(); | 65 | e.printStackTrace(); |
| 65 | } | 66 | } |
| 66 | Utility.logDebug(name + " store finished importing " + fileName + " in " + t.duration() + " seconds."); | 67 | Utility.logDebug(name + " store finished importing " + fileName + " in " + t.duration() + " seconds."); |
| 67 | } | 68 | } |
| 68 | 69 | ||
| 69 | public void importDataFromABoxOf(OWLOntology ontology) { | 70 | public void importDataFromABoxOf(OWLOntology ontology) { |
| 70 | DataStore store = getDataStore(); | 71 | if(isDisposed()) throw new DisposedException(); |
| 71 | try { | 72 | DataStore store = getDataStore(); |
| 72 | long prevTriplesCount = store.getTriplesCount(); | 73 | try { |
| 73 | store.importOntology(ontology.getOWLOntologyManager().createOntology(ontology.getABoxAxioms(true))); | 74 | long prevTriplesCount = store.getTriplesCount(); |
| 74 | long loadedTriples = store.getTriplesCount() - prevTriplesCount; | 75 | store.importOntology(ontology.getOWLOntologyManager().createOntology(ontology.getABoxAxioms(true))); |
| 75 | Utility.logInfo(name + ": loaded " + loadedTriples + " triples from " + ontology.getABoxAxioms(true) | 76 | long loadedTriples = store.getTriplesCount() - prevTriplesCount; |
| 76 | .size() + " ABox axioms"); | 77 | Utility.logInfo(name + ": loaded " + loadedTriples + " triples from " + ontology.getABoxAxioms(true) |
| 77 | } catch(JRDFStoreException | OWLOntologyCreationException e) { | 78 | .size() + " ABox axioms"); |
| 78 | e.printStackTrace(); | 79 | } catch(JRDFStoreException | OWLOntologyCreationException e) { |
| 79 | System.exit(1); | 80 | e.printStackTrace(); |
| 80 | } | 81 | System.exit(1); |
| 81 | 82 | } | |
| 82 | } | 83 | |
| 83 | 84 | } | |
| 84 | public void materialise(String programName, String programText) { | 85 | |
| 85 | if(programText == null) return; | 86 | public void materialise(String programName, String programText) { |
| 86 | Timer t = new Timer(); | 87 | if(isDisposed()) throw new DisposedException(); |
| 87 | DataStore store = getDataStore(); | 88 | if(programText == null) return; |
| 88 | try { | 89 | Timer t = new Timer(); |
| 89 | long oldTripleCount = store.getTriplesCount(), tripleCount; | 90 | DataStore store = getDataStore(); |
| 91 | try { | ||
| 92 | long oldTripleCount = store.getTriplesCount(), tripleCount; | ||
| 90 | // store.addRules(new String[] {programText}); | 93 | // store.addRules(new String[] {programText}); |
| 91 | store.importRules(programText); | 94 | store.importRules(programText); |
| 92 | store.applyReasoning(); | 95 | store.applyReasoning(); |
| 93 | tripleCount = store.getTriplesCount(); | 96 | tripleCount = store.getTriplesCount(); |
| 94 | Utility.logDebug(name + " store after materialising " + programName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)"); | 97 | Utility.logDebug(name + " store after materialising " + programName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)"); |
| 95 | store.clearRulesAndMakeFactsExplicit(); | 98 | store.clearRulesAndMakeFactsExplicit(); |
| 96 | } catch (JRDFStoreException e) { | 99 | } catch(JRDFStoreException e) { |
| 97 | e.printStackTrace(); | 100 | e.printStackTrace(); |
| 98 | } | 101 | } |
| 99 | Utility.logDebug(name + " store finished the materialisation of " + programName + " in " + t.duration() + " seconds."); | 102 | Utility.logDebug(name + " store finished the materialisation of " + programName + " in " + t.duration() + " seconds."); |
| 100 | } | 103 | } |
| 101 | 104 | ||
| 102 | @Override | 105 | @Override |
| 103 | public void evaluate(Collection<String> queryTexts, String answerFile) { | 106 | public void evaluate(Collection<String> queryTexts, String answerFile) { |
| 104 | if (queryTexts == null) | 107 | if(isDisposed()) throw new DisposedException(); |
| 105 | return ; | 108 | if(queryTexts == null) |
| 106 | 109 | return; | |
| 107 | int queryID = 0; | 110 | |
| 108 | AnswerTuplesWriter answerWriter = new AnswerTuplesWriter(answerFile); | 111 | int queryID = 0; |
| 109 | AnswerTuples answerTuples; | 112 | AnswerTuplesWriter answerWriter = new AnswerTuplesWriter(answerFile); |
| 110 | Timer t = new Timer(); | 113 | AnswerTuples answerTuples; |
| 111 | try { | 114 | Timer t = new Timer(); |
| 112 | for (String query: queryTexts) { | 115 | try { |
| 113 | t.reset(); | 116 | for(String query : queryTexts) { |
| 114 | answerTuples = null; | 117 | t.reset(); |
| 115 | try { | 118 | answerTuples = null; |
| 116 | answerTuples = evaluate(query); | 119 | try { |
| 117 | Utility.logDebug("time to answer Query " + ++queryID + ": " + t.duration()); | 120 | answerTuples = evaluate(query); |
| 118 | answerWriter.write(answerTuples.getAnswerVariables(), answerTuples); | 121 | Utility.logDebug("time to answer Query " + ++queryID + ": " + t.duration()); |
| 119 | } finally { | 122 | answerWriter.write(answerTuples.getAnswerVariables(), answerTuples); |
| 120 | if (answerTuples != null) answerTuples.dispose(); | 123 | } finally { |
| 121 | } | 124 | if(answerTuples != null) answerTuples.dispose(); |
| 122 | } | 125 | } |
| 123 | } finally { | 126 | } |
| 124 | answerWriter.close(); | 127 | } finally { |
| 125 | } | 128 | answerWriter.close(); |
| 126 | 129 | } | |
| 127 | Utility.logDebug("done computing query answers by RDFox."); | 130 | |
| 128 | 131 | Utility.logDebug("done computing query answers by RDFox."); | |
| 129 | } | 132 | } |
| 130 | 133 | ||
| 134 | @Override | ||
| 135 | public void dispose() { | ||
| 136 | super.dispose(); | ||
| 137 | } | ||
| 131 | } | 138 | } |
