diff options
| author | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
|---|---|---|
| committer | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
| commit | 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch) | |
| tree | 47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java new file mode 100644 index 0000000..30771ab --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner.light; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.util.Collection; | ||
| 5 | |||
| 6 | import uk.ac.ox.cs.pagoda.MyPrefixes; | ||
| 7 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 8 | import uk.ac.ox.cs.pagoda.reasoner.QueryEngine; | ||
| 9 | import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner; | ||
| 10 | import uk.ac.ox.cs.pagoda.tracking.AnswerTuplesWriter; | ||
| 11 | import uk.ac.ox.cs.pagoda.util.Timer; | ||
| 12 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 13 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 14 | import uk.ac.ox.cs.JRDFox.Prefixes; | ||
| 15 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 16 | import uk.ac.ox.cs.JRDFox.store.DataStore.StoreType; | ||
| 17 | |||
| 18 | public abstract class RDFoxQueryEngine implements QueryEngine { | ||
| 19 | |||
| 20 | public static final int matNoOfThreads = Runtime.getRuntime().availableProcessors() * 2; | ||
| 21 | |||
| 22 | protected String name; | ||
| 23 | protected Prefixes prefixes = MyPrefixes.PAGOdAPrefixes.getRDFoxPrefixes(); | ||
| 24 | |||
| 25 | public RDFoxQueryEngine(String name) { | ||
| 26 | this.name = name; | ||
| 27 | } | ||
| 28 | |||
| 29 | public abstract DataStore getDataStore(); | ||
| 30 | |||
| 31 | public abstract void dispose(); | ||
| 32 | |||
| 33 | public void importRDFData(String fileName, String importedFile) { | ||
| 34 | if (importedFile == null || importedFile.isEmpty()) return ; | ||
| 35 | Timer t = new Timer(); | ||
| 36 | DataStore store = getDataStore(); | ||
| 37 | try { | ||
| 38 | long oldTripleCount = store.getTriplesCount(), tripleCount; | ||
| 39 | for (String file: importedFile.split(QueryReasoner.ImportDataFileSeparator)) | ||
| 40 | store.importTurtleFile(new File(file), prefixes); | ||
| 41 | tripleCount = store.getTriplesCount(); | ||
| 42 | Utility.logDebug(name + " store after importing " + fileName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)"); | ||
| 43 | store.clearRulesAndMakeFactsExplicit(); | ||
| 44 | } catch (JRDFStoreException e) { | ||
| 45 | e.printStackTrace(); | ||
| 46 | } | ||
| 47 | Utility.logDebug(name + " store finished importing " + fileName + " in " + t.duration() + " seconds."); | ||
| 48 | } | ||
| 49 | |||
| 50 | public void materialise(String programName, String programText) { | ||
| 51 | if (programText == null) return ; | ||
| 52 | Timer t = new Timer(); | ||
| 53 | DataStore store = getDataStore(); | ||
| 54 | try { | ||
| 55 | long oldTripleCount = store.getTriplesCount(), tripleCount; | ||
| 56 | // store.addRules(new String[] {programText}); | ||
| 57 | store.importRules(programText); | ||
| 58 | store.applyReasoning(); | ||
| 59 | tripleCount = store.getTriplesCount(); | ||
| 60 | Utility.logDebug(name + " store after materialising " + programName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)"); | ||
| 61 | store.clearRulesAndMakeFactsExplicit(); | ||
| 62 | } catch (JRDFStoreException e) { | ||
| 63 | e.printStackTrace(); | ||
| 64 | } | ||
| 65 | Utility.logDebug(name + " store finished the materialisation of " + programName + " in " + t.duration() + " seconds."); | ||
| 66 | } | ||
| 67 | |||
| 68 | @Override | ||
| 69 | public void evaluate(Collection<String> queryTexts, String answerFile) { | ||
| 70 | if (queryTexts == null) | ||
| 71 | return ; | ||
| 72 | |||
| 73 | int queryID = 0; | ||
| 74 | AnswerTuplesWriter answerWriter = new AnswerTuplesWriter(answerFile); | ||
| 75 | AnswerTuples answerTuples; | ||
| 76 | Timer t = new Timer(); | ||
| 77 | try { | ||
| 78 | for (String query: queryTexts) { | ||
| 79 | t.reset(); | ||
| 80 | answerTuples = null; | ||
| 81 | try { | ||
| 82 | answerTuples = evaluate(query); | ||
| 83 | Utility.logDebug("time to answer Query " + ++queryID + ": " + t.duration()); | ||
| 84 | answerWriter.write(answerTuples.getAnswerVariables(), answerTuples); | ||
| 85 | } finally { | ||
| 86 | if (answerTuples != null) answerTuples.dispose(); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } finally { | ||
| 90 | answerWriter.close(); | ||
| 91 | } | ||
| 92 | |||
| 93 | Utility.logDebug("done computing query answers by RDFox."); | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | public static DataStore createDataStore() { | ||
| 98 | DataStore instance = null; | ||
| 99 | try { | ||
| 100 | // instance = new DataStore("par-head-n"); | ||
| 101 | instance = new DataStore(StoreType.NarrowParallelHead); | ||
| 102 | instance.setNumberOfThreads(matNoOfThreads); | ||
| 103 | instance.initialize(); | ||
| 104 | } catch (JRDFStoreException e) { | ||
| 105 | e.printStackTrace(); | ||
| 106 | } | ||
| 107 | return instance; | ||
| 108 | } | ||
| 109 | |||
| 110 | } | ||
