diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-05-29 18:35:51 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-05-29 18:35:51 +0100 |
| commit | 4b7253559c290b6fdd1c4122830f153fda85dd62 (patch) | |
| tree | 37c1487e4682be719dec532ca3d7e1af353fb9a1 /src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java | |
| parent | 6a559a415b3bdb3048021839e5bcf5bbf5aa4bbd (diff) | |
| download | ACQuA-4b7253559c290b6fdd1c4122830f153fda85dd62.tar.gz ACQuA-4b7253559c290b6fdd1c4122830f153fda85dd62.zip | |
Disposable.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java | 155 |
1 files changed, 82 insertions, 73 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java b/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java index f068164..98f0c35 100644 --- a/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java | |||
| @@ -12,6 +12,7 @@ import uk.ac.ox.cs.pagoda.query.AnswerTuples; | |||
| 12 | import uk.ac.ox.cs.pagoda.query.AnswerTuplesImp; | 12 | import uk.ac.ox.cs.pagoda.query.AnswerTuplesImp; |
| 13 | import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; | 13 | import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; |
| 14 | import uk.ac.ox.cs.pagoda.util.Utility; | 14 | import uk.ac.ox.cs.pagoda.util.Utility; |
| 15 | import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; | ||
| 15 | 16 | ||
| 16 | import java.io.File; | 17 | import java.io.File; |
| 17 | import java.io.FileNotFoundException; | 18 | import java.io.FileNotFoundException; |
| @@ -19,82 +20,90 @@ import java.nio.file.Paths; | |||
| 19 | import java.util.Set; | 20 | import java.util.Set; |
| 20 | 21 | ||
| 21 | public class KarmaQueryEngine extends RDFoxQueryEngine { | 22 | public class KarmaQueryEngine extends RDFoxQueryEngine { |
| 22 | 23 | ||
| 23 | private MyKarma reasoner = null; | 24 | String karmaDataFile = null, karmaRuleFile = null; |
| 24 | 25 | private MyKarma reasoner = null; | |
| 25 | String karmaDataFile = null, karmaRuleFile = null; | 26 | |
| 26 | 27 | public KarmaQueryEngine(String name) { | |
| 27 | public KarmaQueryEngine(String name) { | 28 | super(name); |
| 28 | super(name); | 29 | |
| 29 | |||
| 30 | // int Base = 1 << 6; | 30 | // int Base = 1 << 6; |
| 31 | // int index = (new Random().nextInt() % Base + Base) % Base; | 31 | // int index = (new Random().nextInt() % Base + Base) % Base; |
| 32 | // karmaDataFile = "karma_data" + index + ".ttl"; | 32 | // karmaDataFile = "karma_data" + index + ".ttl"; |
| 33 | // karmaRuleFile = "karma_rule" + index + ".dlog"; | 33 | // karmaRuleFile = "karma_rule" + index + ".dlog"; |
| 34 | karmaDataFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_data.ttl").toString(); | 34 | karmaDataFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_data.ttl").toString(); |
| 35 | karmaRuleFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_rule.dlog").toString(); | 35 | karmaRuleFile = Paths.get(Utility.getGlobalTempDirAbsolutePath(), "karma_rule.dlog").toString(); |
| 36 | 36 | ||
| 37 | reasoner = new MyKarma(); | 37 | reasoner = new MyKarma(); |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | public MyKarma getReasoner() { | 40 | public MyKarma getReasoner() { |
| 41 | return reasoner; | 41 | if(isDisposed()) throw new DisposedException(); |
| 42 | } | 42 | return reasoner; |
| 43 | 43 | } | |
| 44 | public void processOntology(OWLOntology elhoOntology) { | 44 | |
| 45 | try { | 45 | public void processOntology(OWLOntology elhoOntology) { |
| 46 | OntologyProcesser.transformOntology(elhoOntology, new File(karmaDataFile), new File(karmaRuleFile)); | 46 | if(isDisposed()) throw new DisposedException(); |
| 47 | } catch (IllegalInputOntologyException e) { | 47 | try { |
| 48 | e.printStackTrace(); | 48 | OntologyProcesser.transformOntology(elhoOntology, new File(karmaDataFile), new File(karmaRuleFile)); |
| 49 | } | 49 | } catch(IllegalInputOntologyException e) { |
| 50 | } | 50 | e.printStackTrace(); |
| 51 | 51 | } | |
| 52 | @Override | 52 | } |
| 53 | public void dispose() { | 53 | |
| 54 | reasoner.dispose(); | 54 | @Override |
| 55 | } | 55 | public void dispose() { |
| 56 | 56 | super.dispose(); | |
| 57 | @Override | 57 | reasoner.dispose(); |
| 58 | public AnswerTuples evaluate(String queryText) { | 58 | } |
| 59 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], null); | 59 | |
| 60 | } | 60 | @Override |
| 61 | 61 | public AnswerTuples evaluate(String queryText) { | |
| 62 | @Override | 62 | if(isDisposed()) throw new DisposedException(); |
| 63 | public AnswerTuples evaluate(String queryText, String[] answerVars) { | 63 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], null); |
| 64 | return evaluate(queryText, answerVars, null); | 64 | } |
| 65 | } | 65 | |
| 66 | 66 | @Override | |
| 67 | public AnswerTuples evaluate(String queryText, AnswerTuples soundAnswerTuples) { | 67 | public AnswerTuples evaluate(String queryText, String[] answerVars) { |
| 68 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], soundAnswerTuples); | 68 | if(isDisposed()) throw new DisposedException(); |
| 69 | } | 69 | return evaluate(queryText, answerVars, null); |
| 70 | 70 | } | |
| 71 | public AnswerTuples evaluate(String queryText, String[] answerVars, AnswerTuples soundAnswerTuples) { | 71 | |
| 72 | KarmaQuery karmaQuery = new KarmaQuery(queryText.replace("_:", "?")); | 72 | public AnswerTuples evaluate(String queryText, AnswerTuples soundAnswerTuples) { |
| 73 | reasoner.setConcurrence(false); | 73 | if(isDisposed()) throw new DisposedException(); |
| 74 | ConjunctiveQuery cq = karmaQuery.getConjunctiveQuery(); | 74 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], soundAnswerTuples); |
| 75 | if (cq == null) return null; | 75 | } |
| 76 | Set<AnswerTuple> answers = reasoner.answerCQ(cq, soundAnswerTuples, !queryText.contains("_:")); | 76 | |
| 77 | return new AnswerTuplesImp(answerVars, answers); | 77 | public AnswerTuples evaluate(String queryText, String[] answerVars, AnswerTuples soundAnswerTuples) { |
| 78 | } | 78 | if(isDisposed()) throw new DisposedException(); |
| 79 | 79 | KarmaQuery karmaQuery = new KarmaQuery(queryText.replace("_:", "?")); | |
| 80 | @Override | 80 | reasoner.setConcurrence(false); |
| 81 | public DataStore getDataStore() { | 81 | ConjunctiveQuery cq = karmaQuery.getConjunctiveQuery(); |
| 82 | return reasoner.getStore(); | 82 | if(cq == null) return null; |
| 83 | } | 83 | Set<AnswerTuple> answers = reasoner.answerCQ(cq, soundAnswerTuples, !queryText.contains("_:")); |
| 84 | 84 | return new AnswerTuplesImp(answerVars, answers); | |
| 85 | public void initialiseKarma() { | 85 | } |
| 86 | try { | 86 | |
| 87 | reasoner.initializeData(new File(karmaDataFile)); | 87 | @Override |
| 88 | reasoner.materialise(new File(karmaRuleFile)); | 88 | public DataStore getDataStore() { |
| 89 | 89 | if(isDisposed()) throw new DisposedException(); | |
| 90 | File tmp; | 90 | return reasoner.getStore(); |
| 91 | if (karmaDataFile != null && ((tmp = new File(karmaDataFile)).exists())) tmp.delete(); | 91 | } |
| 92 | if (karmaRuleFile != null && ((tmp = new File(karmaRuleFile)).exists())) tmp.delete(); | 92 | |
| 93 | } catch (FileNotFoundException e) { | 93 | public void initialiseKarma() { |
| 94 | e.printStackTrace(); | 94 | if(isDisposed()) throw new DisposedException(); |
| 95 | } catch (JRDFStoreException e) { | 95 | try { |
| 96 | e.printStackTrace(); | 96 | reasoner.initializeData(new File(karmaDataFile)); |
| 97 | } | 97 | reasoner.materialise(new File(karmaRuleFile)); |
| 98 | } | 98 | |
| 99 | File tmp; | ||
| 100 | if(karmaDataFile != null && ((tmp = new File(karmaDataFile)).exists())) tmp.delete(); | ||
| 101 | if(karmaRuleFile != null && ((tmp = new File(karmaRuleFile)).exists())) tmp.delete(); | ||
| 102 | } catch(FileNotFoundException e) { | ||
| 103 | e.printStackTrace(); | ||
| 104 | } catch(JRDFStoreException e) { | ||
| 105 | e.printStackTrace(); | ||
| 106 | } | ||
| 107 | } | ||
| 99 | 108 | ||
| 100 | } | 109 | } |
