diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java | 387 |
1 files changed, 0 insertions, 387 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java b/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java deleted file mode 100644 index d05731a..0000000 --- a/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java +++ /dev/null | |||
| @@ -1,387 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.tracking; | ||
| 2 | |||
| 3 | import org.semanticweb.HermiT.model.*; | ||
| 4 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 5 | import uk.ac.ox.cs.JRDFox.model.Datatype; | ||
| 6 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | ||
| 7 | import uk.ac.ox.cs.JRDFox.model.Literal; | ||
| 8 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 9 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | ||
| 10 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 11 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 12 | import uk.ac.ox.cs.pagoda.query.GapTupleIterator; | ||
| 13 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 14 | import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine; | ||
| 15 | import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager; | ||
| 16 | import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram; | ||
| 17 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 18 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 19 | import uk.ac.ox.cs.pagoda.util.disposable.Disposable; | ||
| 20 | import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; | ||
| 21 | |||
| 22 | import java.io.BufferedWriter; | ||
| 23 | import java.io.FileOutputStream; | ||
| 24 | import java.io.IOException; | ||
| 25 | import java.io.OutputStreamWriter; | ||
| 26 | import java.util.*; | ||
| 27 | |||
| 28 | public abstract class TrackingRuleEncoder extends Disposable { | ||
| 29 | public static final String trackingPredicateRelation = Namespace.PAGODA_AUX + "isTrackingPredicateFor"; | ||
| 30 | public static final String QueryPredicate = Namespace.PAGODA_AUX + "Query"; | ||
| 31 | protected BasicQueryEngine store; | ||
| 32 | protected QueryRecord currentQuery; | ||
| 33 | protected Set<String> unaryPredicates = new HashSet<String>(); | ||
| 34 | protected Set<String> binaryPredicates = new HashSet<String>(); | ||
| 35 | UpperDatalogProgram program; | ||
| 36 | Collection<DLClause> trackingClauses = new HashSet<DLClause>(); | ||
| 37 | Collection<DLClause> queryClauses = new LinkedList<DLClause>(); | ||
| 38 | Map<Integer, DLClause> index2clause = new HashMap<Integer, DLClause>(); | ||
| 39 | Map<DLClause, Integer> clause2index = new HashMap<DLClause, Integer>(); | ||
| 40 | String equalityRelatedRuleText = null; | ||
| 41 | boolean ruleEncoded = false; | ||
| 42 | DLPredicate selected; | ||
| 43 | private boolean queryEncoded = false; | ||
| 44 | private LinkedList<int[]> addedData = new LinkedList<int[]>(); | ||
| 45 | private String trackingSuffix; | ||
| 46 | |||
| 47 | public TrackingRuleEncoder(UpperDatalogProgram program, BasicQueryEngine store) { | ||
| 48 | this.program = program; | ||
| 49 | this.store = store; | ||
| 50 | } | ||
| 51 | |||
| 52 | public static String getRawTerm(GroundTerm r) { | ||
| 53 | if(r instanceof uk.ac.ox.cs.JRDFox.model.Individual) | ||
| 54 | return ((uk.ac.ox.cs.JRDFox.model.Individual) r).getIRI(); | ||
| 55 | else { | ||
| 56 | Literal l = (Literal) r; | ||
| 57 | if(l.getDatatype().equals(Datatype.XSD_STRING) && l.getDatatype().equals(Datatype.RDF_PLAIN_LITERAL)) | ||
| 58 | return "\"" + l.getLexicalForm() + "\""; | ||
| 59 | else | ||
| 60 | return "\"" + l.getLexicalForm() + "\"^^<" + l.getDatatype().getIRI() + ">"; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | protected static String getTrackingSuffix(String queryID) { | ||
| 65 | return "_AUXt" + queryID; | ||
| 66 | } | ||
| 67 | |||
| 68 | public boolean encodingRules() { | ||
| 69 | if(isDisposed()) throw new DisposedException(); | ||
| 70 | if(ruleEncoded) return false; | ||
| 71 | ruleEncoded = true; | ||
| 72 | |||
| 73 | // for (DLClause clause: program.getClauses(currentQuery.getClause())) { | ||
| 74 | for(DLClause clause : program.getClauses()) { | ||
| 75 | encodingRule(clause); | ||
| 76 | } | ||
| 77 | return true; | ||
| 78 | } | ||
| 79 | |||
| 80 | public Collection<int[]> getAddedData() { | ||
| 81 | if(isDisposed()) throw new DisposedException(); | ||
| 82 | return addedData; | ||
| 83 | } | ||
| 84 | |||
| 85 | public String getTrackingPredicate(String predicateIRI) { | ||
| 86 | if(isDisposed()) throw new DisposedException(); | ||
| 87 | if(predicateIRI.startsWith("<")) | ||
| 88 | return predicateIRI.replace(">", getTrackingSuffix(currentQuery.getQueryID()) + ">"); | ||
| 89 | else | ||
| 90 | return predicateIRI + getTrackingSuffix(currentQuery.getQueryID()); | ||
| 91 | } | ||
| 92 | |||
| 93 | public void setCurrentQuery(QueryRecord record) { | ||
| 94 | if(isDisposed()) throw new DisposedException(); | ||
| 95 | deprecateTrackingAndQueryRules(); | ||
| 96 | currentQuery = record; | ||
| 97 | selected = AtomicConcept.create(getSelectedPredicate()); | ||
| 98 | trackingSuffix = "_AUXt" + currentQuery.getQueryID(); | ||
| 99 | } | ||
| 100 | |||
| 101 | @Override | ||
| 102 | public void dispose() { | ||
| 103 | super.dispose(); | ||
| 104 | deprecateTrackingAndQueryRules(); | ||
| 105 | } | ||
| 106 | |||
| 107 | public String getTrackingProgram() { | ||
| 108 | if(isDisposed()) throw new DisposedException(); | ||
| 109 | StringBuilder sb = getTrackingProgramBody(); | ||
| 110 | sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText()); | ||
| 111 | return sb.toString(); | ||
| 112 | } | ||
| 113 | |||
| 114 | public void saveTrackingRules(String fileName) { | ||
| 115 | if(isDisposed()) throw new DisposedException(); | ||
| 116 | BufferedWriter writer = null; | ||
| 117 | try { | ||
| 118 | writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))); | ||
| 119 | writer.write(getTrackingProgram()); | ||
| 120 | writer.close(); | ||
| 121 | } catch(IOException e) { | ||
| 122 | e.printStackTrace(); | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | Utility.logDebug("The tracking rules are saved in " + fileName + "."); | ||
| 126 | } | ||
| 127 | |||
| 128 | public String getSelectedPredicate() { | ||
| 129 | if(isDisposed()) throw new DisposedException(); | ||
| 130 | return getIRI("_selected" + currentQuery.getQueryID()); | ||
| 131 | } | ||
| 132 | |||
| 133 | public DLClause getSelectedClause(String iri) { | ||
| 134 | if(isDisposed()) throw new DisposedException(); | ||
| 135 | int index = iri.lastIndexOf("_r") + 2; | ||
| 136 | int ruleIndex = Integer.parseInt(iri.substring(index)); | ||
| 137 | return index2clause.get(ruleIndex); | ||
| 138 | } | ||
| 139 | |||
| 140 | /** | ||
| 141 | * SELECT ?X | ||
| 142 | * WHERE { | ||
| 143 | * ?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#:type> :_selected? | ||
| 144 | * } | ||
| 145 | */ | ||
| 146 | public String getSelectedSPARQLQuery() { | ||
| 147 | if(isDisposed()) throw new DisposedException(); | ||
| 148 | StringBuilder builder = new StringBuilder(); | ||
| 149 | builder.append("SELECT ?X\nWHERE {\n?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "); | ||
| 150 | builder.append(selected.toString()).append("\n}"); | ||
| 151 | return builder.toString(); | ||
| 152 | } | ||
| 153 | |||
| 154 | public OWLOntology getOntology() { | ||
| 155 | if(isDisposed()) throw new DisposedException(); | ||
| 156 | return program.getOntology(); | ||
| 157 | } | ||
| 158 | |||
| 159 | public UpperDatalogProgram getProgram() { | ||
| 160 | if(isDisposed()) throw new DisposedException(); | ||
| 161 | return program; | ||
| 162 | } | ||
| 163 | |||
| 164 | public String getOriginalPredicate(String p) { | ||
| 165 | if(isDisposed()) throw new DisposedException(); | ||
| 166 | if(p.startsWith("<")) { | ||
| 167 | if(!p.endsWith(trackingSuffix + ">")) return null; | ||
| 168 | } | ||
| 169 | else if(!p.endsWith(trackingSuffix)) return null; | ||
| 170 | |||
| 171 | return p.replace(trackingSuffix, ""); | ||
| 172 | } | ||
| 173 | |||
| 174 | public boolean isAuxPredicate(String p) { | ||
| 175 | if(isDisposed()) throw new DisposedException(); | ||
| 176 | return false; | ||
| 177 | } | ||
| 178 | |||
| 179 | protected abstract String getEqualityRelatedRuleText(); | ||
| 180 | |||
| 181 | protected String getIRI(String name) { | ||
| 182 | return program.getOntology().getOntologyID().getOntologyIRI().toString() + "#" + name; | ||
| 183 | } | ||
| 184 | |||
| 185 | protected abstract void encodingRule(DLClause clause); | ||
| 186 | |||
| 187 | protected Individual getIndividual4GeneralRule(DLClause clause) { | ||
| 188 | clause = program.getCorrespondingClause(clause); | ||
| 189 | // if (clause == null) | ||
| 190 | // return Individual.create(getIRI("_r0")); | ||
| 191 | |||
| 192 | int index; | ||
| 193 | if(clause2index.containsKey(clause)) | ||
| 194 | index = clause2index.get(clause); | ||
| 195 | else { | ||
| 196 | index = clause2index.size() + 1; | ||
| 197 | index2clause.put(index, clause); | ||
| 198 | clause2index.put(clause, index); | ||
| 199 | } | ||
| 200 | |||
| 201 | return Individual.create(getIRI("_r" + index)); | ||
| 202 | } | ||
| 203 | |||
| 204 | private void encodingQuery(QueryRecord[] botQuerRecords) { | ||
| 205 | if(queryEncoded) return; | ||
| 206 | queryEncoded = true; | ||
| 207 | |||
| 208 | if(currentQuery.getArity() > 0 && currentQuery.getArity() < 3) { | ||
| 209 | encodingAtomicQuery(botQuerRecords); | ||
| 210 | |||
| 211 | } | ||
| 212 | else { | ||
| 213 | DLClause queryClause = currentQuery.getClause(); | ||
| 214 | Atom[] bodyAtoms = queryClause.getBodyAtoms(); | ||
| 215 | for(Atom bodyAtom : bodyAtoms) | ||
| 216 | addQueryRule(bodyAtom, bodyAtoms); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | private void addQueryRule(Atom atom, Atom[] atoms) { | ||
| 221 | DLClause newClause; | ||
| 222 | Atom headAtom; | ||
| 223 | |||
| 224 | headAtom = Atom.create( | ||
| 225 | getTrackingDLPredicate(atom.getDLPredicate()), | ||
| 226 | DLClauseHelper.getArguments(atom)); | ||
| 227 | newClause = DLClause.create(new Atom[]{headAtom}, atoms); | ||
| 228 | queryClauses.add(newClause); | ||
| 229 | } | ||
| 230 | |||
| 231 | protected String getCurrentQueryPredicate() { | ||
| 232 | return QueryPredicate + currentQuery.getQueryID(); | ||
| 233 | } | ||
| 234 | |||
| 235 | protected void encodingAtomicQuery(QueryRecord[] botQuerRecords) { | ||
| 236 | encodingAtomicQuery(botQuerRecords, false); | ||
| 237 | } | ||
| 238 | |||
| 239 | protected void encodingAtomicQuery(QueryRecord[] botQuerRecords, boolean includingBottom) { | ||
| 240 | DLClause queryClause = currentQuery.getClause(); | ||
| 241 | AnswerTuples answerTuples = currentQuery.getGapAnswers(); | ||
| 242 | String[] answerVariables = currentQuery.getAnswerVariables(); | ||
| 243 | |||
| 244 | String currentQueryPredicate = getCurrentQueryPredicate(); | ||
| 245 | Atom newAtom; | ||
| 246 | if(answerVariables.length == 1) { | ||
| 247 | AtomicConcept queryConcept = AtomicConcept.create(currentQueryPredicate); | ||
| 248 | newAtom = Atom.create(queryConcept, Variable.create(answerVariables[0])); | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | AtomicRole queryRole = AtomicRole.create(currentQueryPredicate); | ||
| 252 | newAtom = Atom.create(queryRole, Variable.create(answerVariables[0]), Variable.create(answerVariables[1])); | ||
| 253 | } | ||
| 254 | |||
| 255 | Atom[] bodyAtoms = queryClause.getBodyAtoms(); | ||
| 256 | Atom[] newBodyAtoms = new Atom[queryClause.getBodyLength() + 1]; | ||
| 257 | for(int i = 0; i < bodyAtoms.length; ++i) | ||
| 258 | newBodyAtoms[i + 1] = bodyAtoms[i]; | ||
| 259 | newBodyAtoms[0] = newAtom; | ||
| 260 | |||
| 261 | for(Atom bodyAtom : bodyAtoms) | ||
| 262 | addQueryRule(bodyAtom, newBodyAtoms); | ||
| 263 | |||
| 264 | RDFoxTripleManager tripleManager = new RDFoxTripleManager(store.getDataStore(), true); | ||
| 265 | // MyPrefixes prefixes = MyPrefixes.PAGOdAPrefixes; | ||
| 266 | int[] triple; | ||
| 267 | int predicate = tripleManager.getResourceID(AtomicConcept.create(currentQueryPredicate)); | ||
| 268 | int rdftype = tripleManager.getResourceID(AtomicRole.create(Namespace.RDF_TYPE)); | ||
| 269 | if(answerVariables.length == 1) { | ||
| 270 | for(AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 271 | answer = answerTuples.getTuple(); | ||
| 272 | triple = | ||
| 273 | new int[]{tripleManager.getResourceID(getRawTerm(answer.getGroundTerm(0))), rdftype, predicate}; | ||
| 274 | addedData.add(triple); | ||
| 275 | tripleManager.addTripleByID(triple); | ||
| 276 | // System.out.println("To be removed ... \n" + tripleManager.getRawTerm(tripleManager.getResourceID(prefixes.expandIRI(answer.getRawTerm(0)))) + " " + tripleManager.getRawTerm(rdftype) + " " + tripleManager.getRawTerm(predicate)); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | else { | ||
| 280 | for(AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 281 | answer = answerTuples.getTuple(); | ||
| 282 | triple = | ||
| 283 | new int[]{tripleManager.getResourceID(getRawTerm(answer.getGroundTerm(0))), predicate, tripleManager | ||
| 284 | .getResourceID(getRawTerm(answer.getGroundTerm(1)))}; | ||
| 285 | addedData.add(triple); | ||
| 286 | tripleManager.addTripleByID(triple); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | // answerTuples.dispose(); | ||
| 290 | |||
| 291 | if(includingBottom && botQuerRecords != null) { | ||
| 292 | int index = 0; | ||
| 293 | GroundTerm t; | ||
| 294 | String raw; | ||
| 295 | for(QueryRecord botQueryRecord : botQuerRecords) { | ||
| 296 | answerTuples = botQueryRecord.getGapAnswers(); | ||
| 297 | int subID = 0;//botQueryRecord.getSubID(); | ||
| 298 | String p = subID == 0 ? AtomicConcept.NOTHING.getIRI() : Namespace.OWL_NS + "Nothing_final" + (++index); | ||
| 299 | predicate = tripleManager.getResourceID(AtomicConcept.create(p = getTrackingPredicate(p))); | ||
| 300 | for(AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) { | ||
| 301 | answer = answerTuples.getTuple(); | ||
| 302 | // System.out.println("To be removed ... " + answer.getRawTerm(0)); | ||
| 303 | raw = ((t = | ||
| 304 | answer.getGroundTerm(0)) instanceof uk.ac.ox.cs.JRDFox.model.Individual) ? ((uk.ac.ox.cs.JRDFox.model.Individual) t) | ||
| 305 | .getIRI() : t.toString(); | ||
| 306 | triple = new int[]{tripleManager.getResourceID(raw), rdftype, predicate}; | ||
| 307 | addedData.add(triple); | ||
| 308 | tripleManager.addTripleByID(triple); | ||
| 309 | } | ||
| 310 | // answerTuples.dispose(); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | Utility.logDebug(addedData.size() + " triples are added into the store."); | ||
| 315 | } | ||
| 316 | |||
| 317 | protected DLPredicate getGapDLPredicate(DLPredicate dlPredicate) { | ||
| 318 | return getDLPredicate(dlPredicate, GapTupleIterator.gapPredicateSuffix); | ||
| 319 | } | ||
| 320 | |||
| 321 | DLPredicate getDLPredicate(DLPredicate p, String suffix) { | ||
| 322 | if(isDisposed()) throw new DisposedException(); | ||
| 323 | if(p instanceof AtomicConcept) | ||
| 324 | return AtomicConcept.create(((AtomicConcept) p).getIRI() + suffix); | ||
| 325 | else if(p instanceof DatatypeRestriction) { | ||
| 326 | DatatypeRestriction restriction = (DatatypeRestriction) p; | ||
| 327 | String newURI = restriction.getDatatypeURI() + suffix; | ||
| 328 | return getDatatypeRestriction(restriction, newURI); | ||
| 329 | } | ||
| 330 | else if(p instanceof AtomicRole) | ||
| 331 | return AtomicRole.create(((AtomicRole) p).getIRI() + suffix); | ||
| 332 | else if(p instanceof AnnotatedEquality || p instanceof Equality) | ||
| 333 | return AtomicRole.create(Namespace.EQUALITY + suffix); | ||
| 334 | else if(p instanceof Inequality) | ||
| 335 | return AtomicRole.create(Namespace.INEQUALITY + suffix); | ||
| 336 | else if(p instanceof DatatypeRestriction) | ||
| 337 | return AtomicConcept.create(((DatatypeRestriction) p).getDatatypeURI() + suffix); | ||
| 338 | else { | ||
| 339 | Utility.logDebug("strange DL predicate appeared ... " + p, | ||
| 340 | "the program paused here in TrackingRuleEncoderDisj.java"); | ||
| 341 | return null; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | protected DLPredicate getTrackingDLPredicate(DLPredicate dlPredicate) { | ||
| 346 | return getDLPredicate(dlPredicate, getTrackingSuffix(currentQuery.getQueryID())); | ||
| 347 | } | ||
| 348 | |||
| 349 | protected DLPredicate getDatatypeRestriction(DatatypeRestriction restriction, String newName) { | ||
| 350 | int length = restriction.getNumberOfFacetRestrictions(); | ||
| 351 | String[] facets = new String[length]; | ||
| 352 | Constant[] values = new Constant[length]; | ||
| 353 | for(int i = 0; i < length; ++i) { | ||
| 354 | facets[i] = restriction.getFacetURI(i); | ||
| 355 | values[i] = restriction.getFacetValue(i); | ||
| 356 | } | ||
| 357 | return DatatypeRestriction.create(newName, facets, values); | ||
| 358 | } | ||
| 359 | |||
| 360 | private String getTrackingRuleText() { | ||
| 361 | return DLClauseHelper.toString(trackingClauses); | ||
| 362 | } | ||
| 363 | |||
| 364 | private String getQueryRuleText() { | ||
| 365 | return DLClauseHelper.toString(queryClauses); | ||
| 366 | } | ||
| 367 | |||
| 368 | protected StringBuilder getTrackingProgramBody() { | ||
| 369 | encodingRules(); | ||
| 370 | encodingQuery(new QueryRecord[0]); | ||
| 371 | |||
| 372 | StringBuilder sb = new StringBuilder(); | ||
| 373 | sb.append(getTrackingRuleText()); | ||
| 374 | sb.append(getEqualityRelatedRuleText()); | ||
| 375 | sb.append(getQueryRuleText()); | ||
| 376 | return sb; | ||
| 377 | } | ||
| 378 | |||
| 379 | private void deprecateTrackingAndQueryRules() { | ||
| 380 | trackingClauses.clear(); | ||
| 381 | queryClauses.clear(); | ||
| 382 | addedData.clear(); | ||
| 383 | ruleEncoded = false; | ||
| 384 | queryEncoded = false; | ||
| 385 | } | ||
| 386 | |||
| 387 | } | ||
