aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/tracking/TrackingRuleEncoder.java
blob: d05731a1e8b5e1e1897bd8b5545be478a24a568b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package uk.ac.ox.cs.pagoda.tracking;

import org.semanticweb.HermiT.model.*;
import org.semanticweb.owlapi.model.OWLOntology;
import uk.ac.ox.cs.JRDFox.model.Datatype;
import uk.ac.ox.cs.JRDFox.model.GroundTerm;
import uk.ac.ox.cs.JRDFox.model.Literal;
import uk.ac.ox.cs.pagoda.MyPrefixes;
import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper;
import uk.ac.ox.cs.pagoda.query.AnswerTuple;
import uk.ac.ox.cs.pagoda.query.AnswerTuples;
import uk.ac.ox.cs.pagoda.query.GapTupleIterator;
import uk.ac.ox.cs.pagoda.query.QueryRecord;
import uk.ac.ox.cs.pagoda.reasoner.light.BasicQueryEngine;
import uk.ac.ox.cs.pagoda.reasoner.light.RDFoxTripleManager;
import uk.ac.ox.cs.pagoda.rules.UpperDatalogProgram;
import uk.ac.ox.cs.pagoda.util.Namespace;
import uk.ac.ox.cs.pagoda.util.Utility;
import uk.ac.ox.cs.pagoda.util.disposable.Disposable;
import uk.ac.ox.cs.pagoda.util.disposable.DisposedException;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;

public abstract class TrackingRuleEncoder extends Disposable {
    public static final String trackingPredicateRelation = Namespace.PAGODA_AUX + "isTrackingPredicateFor";
    public static final String QueryPredicate = Namespace.PAGODA_AUX + "Query";
    protected BasicQueryEngine store;
    protected QueryRecord currentQuery;
    protected Set<String> unaryPredicates = new HashSet<String>();
    protected Set<String> binaryPredicates = new HashSet<String>();
    UpperDatalogProgram program;
    Collection<DLClause> trackingClauses = new HashSet<DLClause>();
    Collection<DLClause> queryClauses = new LinkedList<DLClause>();
    Map<Integer, DLClause> index2clause = new HashMap<Integer, DLClause>();
    Map<DLClause, Integer> clause2index = new HashMap<DLClause, Integer>();
    String equalityRelatedRuleText = null;
    boolean ruleEncoded = false;
    DLPredicate selected;
    private boolean queryEncoded = false;
    private LinkedList<int[]> addedData = new LinkedList<int[]>();
    private String trackingSuffix;

    public TrackingRuleEncoder(UpperDatalogProgram program, BasicQueryEngine store) {
        this.program = program;
        this.store = store;
    }

    public static String getRawTerm(GroundTerm r) {
        if(r instanceof uk.ac.ox.cs.JRDFox.model.Individual)
            return ((uk.ac.ox.cs.JRDFox.model.Individual) r).getIRI();
        else {
            Literal l = (Literal) r;
            if(l.getDatatype().equals(Datatype.XSD_STRING) && l.getDatatype().equals(Datatype.RDF_PLAIN_LITERAL))
                return "\"" + l.getLexicalForm() + "\"";
            else
                return "\"" + l.getLexicalForm() + "\"^^<" + l.getDatatype().getIRI() + ">";
        }
    }

    protected static String getTrackingSuffix(String queryID) {
        return "_AUXt" + queryID;
    }

    public boolean encodingRules() {
        if(isDisposed()) throw new DisposedException();
        if(ruleEncoded) return false;
        ruleEncoded = true;

//		for (DLClause clause: program.getClauses(currentQuery.getClause())) {
        for(DLClause clause : program.getClauses()) {
            encodingRule(clause);
        }
        return true;
    }

    public Collection<int[]> getAddedData() {
        if(isDisposed()) throw new DisposedException();
        return addedData;
    }

    public String getTrackingPredicate(String predicateIRI) {
        if(isDisposed()) throw new DisposedException();
        if(predicateIRI.startsWith("<"))
            return predicateIRI.replace(">", getTrackingSuffix(currentQuery.getQueryID()) + ">");
        else
            return predicateIRI + getTrackingSuffix(currentQuery.getQueryID());
    }

    public void setCurrentQuery(QueryRecord record) {
        if(isDisposed()) throw new DisposedException();
        deprecateTrackingAndQueryRules();
        currentQuery = record;
        selected = AtomicConcept.create(getSelectedPredicate());
        trackingSuffix = "_AUXt" + currentQuery.getQueryID();
    }

    @Override
    public void dispose() {
        super.dispose();
        deprecateTrackingAndQueryRules();
    }

    public String getTrackingProgram() {
        if(isDisposed()) throw new DisposedException();
        StringBuilder sb = getTrackingProgramBody();
        sb.insert(0, MyPrefixes.PAGOdAPrefixes.prefixesText());
        return sb.toString();
    }

    public void saveTrackingRules(String fileName) {
        if(isDisposed()) throw new DisposedException();
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
            writer.write(getTrackingProgram());
            writer.close();
        } catch(IOException e) {
            e.printStackTrace();
            return;
        }
        Utility.logDebug("The tracking rules are saved in " + fileName + ".");
    }

    public String getSelectedPredicate() {
        if(isDisposed()) throw new DisposedException();
        return getIRI("_selected" + currentQuery.getQueryID());
    }

    public DLClause getSelectedClause(String iri) {
        if(isDisposed()) throw new DisposedException();
        int index = iri.lastIndexOf("_r") + 2;
        int ruleIndex = Integer.parseInt(iri.substring(index));
        return index2clause.get(ruleIndex);
    }

    /**
     * SELECT ?X
     * WHERE {
     * ?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#:type> :_selected?
     * }
     */
    public String getSelectedSPARQLQuery() {
        if(isDisposed()) throw new DisposedException();
        StringBuilder builder = new StringBuilder();
        builder.append("SELECT ?X\nWHERE {\n?X <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ");
        builder.append(selected.toString()).append("\n}");
        return builder.toString();
    }

    public OWLOntology getOntology() {
        if(isDisposed()) throw new DisposedException();
        return program.getOntology();
    }

    public UpperDatalogProgram getProgram() {
        if(isDisposed()) throw new DisposedException();
        return program;
    }

    public String getOriginalPredicate(String p) {
        if(isDisposed()) throw new DisposedException();
        if(p.startsWith("<")) {
            if(!p.endsWith(trackingSuffix + ">")) return null;
        }
        else if(!p.endsWith(trackingSuffix)) return null;

        return p.replace(trackingSuffix, "");
    }

    public boolean isAuxPredicate(String p) {
        if(isDisposed()) throw new DisposedException();
        return false;
    }

    protected abstract String getEqualityRelatedRuleText();

    protected String getIRI(String name) {
        return program.getOntology().getOntologyID().getOntologyIRI().toString() + "#" + name;
    }

    protected abstract void encodingRule(DLClause clause);

    protected Individual getIndividual4GeneralRule(DLClause clause) {
        clause = program.getCorrespondingClause(clause);
//		if (clause == null)
//			return Individual.create(getIRI("_r0"));

        int index;
        if(clause2index.containsKey(clause))
            index = clause2index.get(clause);
        else {
            index = clause2index.size() + 1;
            index2clause.put(index, clause);
            clause2index.put(clause, index);
        }

        return Individual.create(getIRI("_r" + index));
    }

    private void encodingQuery(QueryRecord[] botQuerRecords) {
        if(queryEncoded) return;
        queryEncoded = true;

        if(currentQuery.getArity() > 0 && currentQuery.getArity() < 3) {
            encodingAtomicQuery(botQuerRecords);

        }
        else {
            DLClause queryClause = currentQuery.getClause();
            Atom[] bodyAtoms = queryClause.getBodyAtoms();
            for(Atom bodyAtom : bodyAtoms)
                addQueryRule(bodyAtom, bodyAtoms);
        }
    }

    private void addQueryRule(Atom atom, Atom[] atoms) {
        DLClause newClause;
        Atom headAtom;

        headAtom = Atom.create(
                getTrackingDLPredicate(atom.getDLPredicate()),
                DLClauseHelper.getArguments(atom));
        newClause = DLClause.create(new Atom[]{headAtom}, atoms);
        queryClauses.add(newClause);
    }

    protected String getCurrentQueryPredicate() {
        return QueryPredicate + currentQuery.getQueryID();
    }

    protected void encodingAtomicQuery(QueryRecord[] botQuerRecords) {
        encodingAtomicQuery(botQuerRecords, false);
    }

    protected void encodingAtomicQuery(QueryRecord[] botQuerRecords, boolean includingBottom) {
        DLClause queryClause = currentQuery.getClause();
        AnswerTuples answerTuples = currentQuery.getGapAnswers();
        String[] answerVariables = currentQuery.getAnswerVariables();

        String currentQueryPredicate = getCurrentQueryPredicate();
        Atom newAtom;
        if(answerVariables.length == 1) {
            AtomicConcept queryConcept = AtomicConcept.create(currentQueryPredicate);
            newAtom = Atom.create(queryConcept, Variable.create(answerVariables[0]));
        }
        else {
            AtomicRole queryRole = AtomicRole.create(currentQueryPredicate);
            newAtom = Atom.create(queryRole, Variable.create(answerVariables[0]), Variable.create(answerVariables[1]));
        }

        Atom[] bodyAtoms = queryClause.getBodyAtoms();
        Atom[] newBodyAtoms = new Atom[queryClause.getBodyLength() + 1];
        for(int i = 0; i < bodyAtoms.length; ++i)
            newBodyAtoms[i + 1] = bodyAtoms[i];
        newBodyAtoms[0] = newAtom;

        for(Atom bodyAtom : bodyAtoms)
            addQueryRule(bodyAtom, newBodyAtoms);

        RDFoxTripleManager tripleManager = new RDFoxTripleManager(store.getDataStore(), true);
//		MyPrefixes prefixes = MyPrefixes.PAGOdAPrefixes;
        int[] triple;
        int predicate = tripleManager.getResourceID(AtomicConcept.create(currentQueryPredicate));
        int rdftype = tripleManager.getResourceID(AtomicRole.create(Namespace.RDF_TYPE));
        if(answerVariables.length == 1) {
            for(AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) {
                answer = answerTuples.getTuple();
                triple =
                        new int[]{tripleManager.getResourceID(getRawTerm(answer.getGroundTerm(0))), rdftype, predicate};
                addedData.add(triple);
                tripleManager.addTripleByID(triple);
//				System.out.println("To be removed ... \n" + tripleManager.getRawTerm(tripleManager.getResourceID(prefixes.expandIRI(answer.getRawTerm(0)))) + " " + tripleManager.getRawTerm(rdftype) + " " + tripleManager.getRawTerm(predicate));
            }
        }
        else {
            for(AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) {
                answer = answerTuples.getTuple();
                triple =
                        new int[]{tripleManager.getResourceID(getRawTerm(answer.getGroundTerm(0))), predicate, tripleManager
                                .getResourceID(getRawTerm(answer.getGroundTerm(1)))};
                addedData.add(triple);
                tripleManager.addTripleByID(triple);
            }
        }
//        answerTuples.dispose();

        if(includingBottom && botQuerRecords != null) {
            int index = 0;
            GroundTerm t;
            String raw;
            for(QueryRecord botQueryRecord : botQuerRecords) {
                answerTuples = botQueryRecord.getGapAnswers();
                int subID = 0;//botQueryRecord.getSubID();
                String p = subID == 0 ? AtomicConcept.NOTHING.getIRI() : Namespace.OWL_NS + "Nothing_final" + (++index);
                predicate = tripleManager.getResourceID(AtomicConcept.create(p = getTrackingPredicate(p)));
                for(AnswerTuple answer; answerTuples.isValid(); answerTuples.moveNext()) {
                    answer = answerTuples.getTuple();
//					System.out.println("To be removed ... " + answer.getRawTerm(0));
                    raw = ((t =
                            answer.getGroundTerm(0)) instanceof uk.ac.ox.cs.JRDFox.model.Individual) ? ((uk.ac.ox.cs.JRDFox.model.Individual) t)
                            .getIRI() : t.toString();
                    triple = new int[]{tripleManager.getResourceID(raw), rdftype, predicate};
                    addedData.add(triple);
                    tripleManager.addTripleByID(triple);
                }
//                answerTuples.dispose();
            }
        }

        Utility.logDebug(addedData.size() + " triples are added into the store.");
    }

    protected DLPredicate getGapDLPredicate(DLPredicate dlPredicate) {
        return getDLPredicate(dlPredicate, GapTupleIterator.gapPredicateSuffix);
    }

    DLPredicate getDLPredicate(DLPredicate p, String suffix) {
        if(isDisposed()) throw new DisposedException();
        if(p instanceof AtomicConcept)
            return AtomicConcept.create(((AtomicConcept) p).getIRI() + suffix);
        else if(p instanceof DatatypeRestriction) {
            DatatypeRestriction restriction = (DatatypeRestriction) p;
            String newURI = restriction.getDatatypeURI() + suffix;
            return getDatatypeRestriction(restriction, newURI);
        }
        else if(p instanceof AtomicRole)
            return AtomicRole.create(((AtomicRole) p).getIRI() + suffix);
        else if(p instanceof AnnotatedEquality || p instanceof Equality)
            return AtomicRole.create(Namespace.EQUALITY + suffix);
        else if(p instanceof Inequality)
            return AtomicRole.create(Namespace.INEQUALITY + suffix);
        else if(p instanceof DatatypeRestriction)
            return AtomicConcept.create(((DatatypeRestriction) p).getDatatypeURI() + suffix);
        else {
            Utility.logDebug("strange DL predicate appeared ... " + p,
                             "the program paused here in TrackingRuleEncoderDisj.java");
            return null;
        }
    }

    protected DLPredicate getTrackingDLPredicate(DLPredicate dlPredicate) {
        return getDLPredicate(dlPredicate, getTrackingSuffix(currentQuery.getQueryID()));
    }

    protected DLPredicate getDatatypeRestriction(DatatypeRestriction restriction, String newName) {
        int length = restriction.getNumberOfFacetRestrictions();
        String[] facets = new String[length];
        Constant[] values = new Constant[length];
        for(int i = 0; i < length; ++i) {
            facets[i] = restriction.getFacetURI(i);
            values[i] = restriction.getFacetValue(i);
        }
        return DatatypeRestriction.create(newName, facets, values);
    }

    private String getTrackingRuleText() {
        return DLClauseHelper.toString(trackingClauses);
    }

    private String getQueryRuleText() {
        return DLClauseHelper.toString(queryClauses);
    }

    protected StringBuilder getTrackingProgramBody() {
        encodingRules();
        encodingQuery(new QueryRecord[0]);

        StringBuilder sb = new StringBuilder();
        sb.append(getTrackingRuleText());
        sb.append(getEqualityRelatedRuleText());
        sb.append(getQueryRuleText());
        return sb;
    }

    private void deprecateTrackingAndQueryRules() {
        trackingClauses.clear();
        queryClauses.clear();
        addedData.clear();
        ruleEncoded = false;
        queryEncoded = false;
    }

}