diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/light/BasicQueryEngine.java | 720 |
1 files changed, 361 insertions, 359 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 | } |
