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/KarmaQueryEngine.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/KarmaQueryEngine.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java | 98 |
1 files changed, 98 insertions, 0 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 new file mode 100644 index 0000000..f70dde9 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/reasoner/light/KarmaQueryEngine.java | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.reasoner.light; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.io.FileNotFoundException; | ||
| 5 | import java.util.*; | ||
| 6 | |||
| 7 | import org.semanticweb.karma2.*; | ||
| 8 | import org.semanticweb.karma2.clausifier.OntologyProcesser; | ||
| 9 | import org.semanticweb.karma2.exception.IllegalInputOntologyException; | ||
| 10 | import org.semanticweb.karma2.model.ConjunctiveQuery; | ||
| 11 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 12 | |||
| 13 | import uk.ac.ox.cs.pagoda.query.*; | ||
| 14 | import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; | ||
| 15 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 16 | import uk.ac.ox.cs.JRDFox.JRDFStoreException; | ||
| 17 | import uk.ac.ox.cs.JRDFox.store.DataStore; | ||
| 18 | |||
| 19 | public class KarmaQueryEngine extends RDFoxQueryEngine { | ||
| 20 | |||
| 21 | private MyKarma reasoner = null; | ||
| 22 | |||
| 23 | String karmaDataFile = null, karmaRuleFile = null; | ||
| 24 | |||
| 25 | public KarmaQueryEngine(String name) { | ||
| 26 | super(name); | ||
| 27 | |||
| 28 | // int Base = 1 << 6; | ||
| 29 | // int index = (new Random().nextInt() % Base + Base) % Base; | ||
| 30 | // karmaDataFile = "karma_data" + index + ".ttl"; | ||
| 31 | // karmaRuleFile = "karma_rule" + index + ".dlog"; | ||
| 32 | karmaDataFile = Utility.TempDirectory + "karma_data.ttl"; | ||
| 33 | karmaRuleFile = Utility.TempDirectory + "karma_rule.dlog"; | ||
| 34 | |||
| 35 | reasoner = new MyKarma(); | ||
| 36 | } | ||
| 37 | |||
| 38 | public MyKarma getReasoner() { | ||
| 39 | return reasoner; | ||
| 40 | } | ||
| 41 | |||
| 42 | public void processOntology(OWLOntology elhoOntology) { | ||
| 43 | try { | ||
| 44 | OntologyProcesser.transformOntology(elhoOntology, new File(karmaDataFile), new File(karmaRuleFile)); | ||
| 45 | } catch (IllegalInputOntologyException e) { | ||
| 46 | e.printStackTrace(); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | @Override | ||
| 51 | public void dispose() { | ||
| 52 | reasoner.dispose(); | ||
| 53 | } | ||
| 54 | |||
| 55 | @Override | ||
| 56 | public AnswerTuples evaluate(String queryText) { | ||
| 57 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], null); | ||
| 58 | } | ||
| 59 | |||
| 60 | @Override | ||
| 61 | public AnswerTuples evaluate(String queryText, String[] answerVars) { | ||
| 62 | return evaluate(queryText, answerVars, null); | ||
| 63 | } | ||
| 64 | |||
| 65 | public AnswerTuples evaluate(String queryText, AnswerTuples soundAnswerTuples) { | ||
| 66 | return evaluate(queryText, ConjunctiveQueryHelper.getAnswerVariables(queryText)[0], soundAnswerTuples); | ||
| 67 | } | ||
| 68 | |||
| 69 | public AnswerTuples evaluate(String queryText, String[] answerVars, AnswerTuples soundAnswerTuples) { | ||
| 70 | KarmaQuery karmaQuery = new KarmaQuery(queryText.replace("_:", "?")); | ||
| 71 | reasoner.setConcurrence(false); | ||
| 72 | ConjunctiveQuery cq = karmaQuery.getConjunctiveQuery(); | ||
| 73 | if (cq == null) return null; | ||
| 74 | Set<AnswerTuple> answers = reasoner.answerCQ(cq, soundAnswerTuples, !queryText.contains("_:")); | ||
| 75 | return new AnswerTuplesImp(answerVars, answers); | ||
| 76 | } | ||
| 77 | |||
| 78 | @Override | ||
| 79 | public DataStore getDataStore() { | ||
| 80 | return reasoner.getStore(); | ||
| 81 | } | ||
| 82 | |||
| 83 | public void initialiseKarma() { | ||
| 84 | try { | ||
| 85 | reasoner.initializeData(new File(karmaDataFile)); | ||
| 86 | reasoner.materialise(new File(karmaRuleFile)); | ||
| 87 | |||
| 88 | File tmp; | ||
| 89 | if (karmaDataFile != null && ((tmp = new File(karmaDataFile)).exists())) tmp.delete(); | ||
| 90 | if (karmaRuleFile != null && ((tmp = new File(karmaRuleFile)).exists())) tmp.delete(); | ||
| 91 | } catch (FileNotFoundException e) { | ||
| 92 | e.printStackTrace(); | ||
| 93 | } catch (JRDFStoreException e) { | ||
| 94 | e.printStackTrace(); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | } | ||
