blob: 61500f52ccd44603d1bd4796a346fd916de60fed (
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
|
package uk.ac.ox.cs.pagoda.reasoner.light;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import uk.ac.ox.cs.JRDFox.JRDFStoreException;
import uk.ac.ox.cs.JRDFox.Prefixes;
import uk.ac.ox.cs.JRDFox.store.DataStore;
import uk.ac.ox.cs.JRDFox.store.DataStore.StoreType;
import uk.ac.ox.cs.pagoda.MyPrefixes;
import uk.ac.ox.cs.pagoda.query.AnswerTuples;
import uk.ac.ox.cs.pagoda.reasoner.QueryEngine;
import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner;
import uk.ac.ox.cs.pagoda.tracking.AnswerTuplesWriter;
import uk.ac.ox.cs.pagoda.util.Timer;
import uk.ac.ox.cs.pagoda.util.Utility;
import java.io.File;
import java.util.Collection;
public abstract class RDFoxQueryEngine implements QueryEngine {
public static final int matNoOfThreads = Runtime.getRuntime().availableProcessors() * 2;
protected String name;
protected Prefixes prefixes = MyPrefixes.PAGOdAPrefixes.getRDFoxPrefixes();
public RDFoxQueryEngine(String name) {
this.name = name;
}
public static DataStore createDataStore() {
DataStore instance = null;
try {
// instance = new DataStore("par-head-n");
instance = new DataStore(StoreType.NarrowParallelHead);
instance.setNumberOfThreads(matNoOfThreads);
instance.initialize();
} catch(JRDFStoreException e) {
e.printStackTrace();
}
return instance;
}
public String getName() {
return name;
}
public abstract DataStore getDataStore();
public abstract void dispose();
public void importRDFData(String fileName, String importedFile) {
if(importedFile == null || importedFile.isEmpty()) return;
Timer t = new Timer();
DataStore store = getDataStore();
try {
long oldTripleCount = store.getTriplesCount(), tripleCount;
for (String file: importedFile.split(QueryReasoner.ImportDataFileSeparator)) {
store.importTurtleFile(new File(file), prefixes);
}
tripleCount = store.getTriplesCount();
Utility.logDebug(name + " store after importing " + fileName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)");
store.clearRulesAndMakeFactsExplicit();
} catch (JRDFStoreException e) {
e.printStackTrace();
}
Utility.logDebug(name + " store finished importing " + fileName + " in " + t.duration() + " seconds.");
}
public void importDataFromABoxOf(OWLOntology ontology) {
DataStore store = getDataStore();
try {
long prevTriplesCount = store.getTriplesCount();
store.importOntology(ontology.getOWLOntologyManager().createOntology(ontology.getABoxAxioms(true)));
long loadedTriples = store.getTriplesCount() - prevTriplesCount;
Utility.logInfo(name + ": loaded " + loadedTriples + " triples from " + ontology.getABoxAxioms(true)
.size() + " ABox axioms");
} catch(JRDFStoreException | OWLOntologyCreationException e) {
e.printStackTrace();
System.exit(1);
}
}
public void materialise(String programName, String programText) {
if(programText == null) return;
Timer t = new Timer();
DataStore store = getDataStore();
try {
long oldTripleCount = store.getTriplesCount(), tripleCount;
// store.addRules(new String[] {programText});
store.importRules(programText);
store.applyReasoning();
tripleCount = store.getTriplesCount();
Utility.logDebug(name + " store after materialising " + programName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)");
store.clearRulesAndMakeFactsExplicit();
} catch (JRDFStoreException e) {
e.printStackTrace();
}
Utility.logDebug(name + " store finished the materialisation of " + programName + " in " + t.duration() + " seconds.");
}
@Override
public void evaluate(Collection<String> queryTexts, String answerFile) {
if (queryTexts == null)
return ;
int queryID = 0;
AnswerTuplesWriter answerWriter = new AnswerTuplesWriter(answerFile);
AnswerTuples answerTuples;
Timer t = new Timer();
try {
for (String query: queryTexts) {
t.reset();
answerTuples = null;
try {
answerTuples = evaluate(query);
Utility.logDebug("time to answer Query " + ++queryID + ": " + t.duration());
answerWriter.write(answerTuples.getAnswerVariables(), answerTuples);
} finally {
if (answerTuples != null) answerTuples.dispose();
}
}
} finally {
answerWriter.close();
}
Utility.logDebug("done computing query answers by RDFox.");
}
}
|