aboutsummaryrefslogtreecommitdiff
path: root/test/uk/ac/ox/cs/hermit
diff options
context:
space:
mode:
authoryzhou <yujiao.zhou@gmail.com>2015-04-21 10:34:27 +0100
committeryzhou <yujiao.zhou@gmail.com>2015-04-21 10:34:27 +0100
commit9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch)
tree47511c0fb89dccff0db4b5990522e04f294d795b /test/uk/ac/ox/cs/hermit
parentb1ac207612ee8b045244253fb94b866104bc34f2 (diff)
downloadACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz
ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip
initial version
Diffstat (limited to 'test/uk/ac/ox/cs/hermit')
-rw-r--r--test/uk/ac/ox/cs/hermit/HermitQueryReasoner.java219
-rw-r--r--test/uk/ac/ox/cs/hermit/HermitTester.java55
-rw-r--r--test/uk/ac/ox/cs/hermit/JAIR_HermiT.java120
3 files changed, 394 insertions, 0 deletions
diff --git a/test/uk/ac/ox/cs/hermit/HermitQueryReasoner.java b/test/uk/ac/ox/cs/hermit/HermitQueryReasoner.java
new file mode 100644
index 0000000..6e9446d
--- /dev/null
+++ b/test/uk/ac/ox/cs/hermit/HermitQueryReasoner.java
@@ -0,0 +1,219 @@
1package uk.ac.ox.cs.hermit;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.PrintStream;
8import java.util.HashSet;
9import java.util.Set;
10import java.util.concurrent.Callable;
11import java.util.concurrent.ExecutionException;
12import java.util.concurrent.ExecutorService;
13import java.util.concurrent.Executors;
14import java.util.concurrent.Future;
15import java.util.concurrent.TimeUnit;
16import java.util.concurrent.TimeoutException;
17
18import org.semanticweb.HermiT.Reasoner;
19import org.semanticweb.HermiT.model.Atom;
20import org.semanticweb.HermiT.model.AtomicRole;
21import org.semanticweb.owlapi.model.IRI;
22import org.semanticweb.owlapi.model.OWLAxiom;
23import org.semanticweb.owlapi.model.OWLClassExpression;
24import org.semanticweb.owlapi.model.OWLDataFactory;
25import org.semanticweb.owlapi.model.OWLDatatype;
26import org.semanticweb.owlapi.model.OWLIndividual;
27import org.semanticweb.owlapi.model.OWLNamedIndividual;
28import org.semanticweb.owlapi.model.OWLObjectProperty;
29import org.semanticweb.owlapi.model.OWLOntology;
30import org.semanticweb.owlapi.model.OWLOntologyCreationException;
31import org.semanticweb.owlapi.model.OWLOntologyManager;
32import org.semanticweb.owlapi.model.OWLOntologyStorageException;
33import org.semanticweb.owlapi.reasoner.Node;
34
35import uk.ac.ox.cs.pagoda.owl.OWLHelper;
36import uk.ac.ox.cs.pagoda.owl.QueryRoller;
37import uk.ac.ox.cs.pagoda.query.QueryManager;
38import uk.ac.ox.cs.pagoda.query.QueryRecord;
39import uk.ac.ox.cs.pagoda.util.Timer;
40
41public class HermitQueryReasoner {
42
43 public static void main(String... args) throws FileNotFoundException, OWLOntologyCreationException, OWLOntologyStorageException {
44 if (args.length == 0) {
45// args = new String[] {"/media/krr-nas-share/Yujiao/ontologies/lubm/lubm1_merged.owl", null, PagodaTester.lubm_query};
46// args = new String[] {"/users/yzhou/ontologies/uobm/uobm1_merged.owl", null, "/users/yzhou/ontologies/uobm/queries/standard.sparql"};
47// args = new String[] {"/users/yzhou/ontologies/fly/fly_anatomy_XP_with_GJ_FC_individuals.owl", null, "/users/yzhou/ontologies/fly/queries/fly.sparql"};
48// args = new String[] {"/media/krr-nas-share/Yujiao/ontologies/npd/npd-all-minus-datatype.owl", "/media/krr-nas-share/Yujiao/ontologies/npd/data/npd-data-dump-minus-datatype-new.ttl", "/users/yzhou/ontologies/npd/queries/atomic.sparql"};
49// args = new String[] {"/media/krr-nas-share/Yujiao/ontologies/npd/npd-all.owl", "/media/krr-nas-share/Yujiao/ontologies/npd/data/npd-data-dump-processed.ttl", "/users/yzhou/ontologies/npd/queries/atomic.sparql"};
50// args = new String[] {PagodaTester.dbpedia_tbox, PagodaTester.dbpedia_abox, PagodaTester.dbpedia_query};
51// args = new String[] {"/users/yzhou/ontologies/test/unsatisfiable.owl", null, "/users/yzhou/ontologies/test/unsatisfiable_queries.sparql"};
52
53// args = new String[] {"/media/krr-nas-share/Yujiao/ontologies/bio2rdf/chembl/cco-processed-noDPR-noDPD.ttl", "/media/krr-nas-share/Yujiao/ontologies/bio2rdf/chembl/graph sampling/sample_100.nt", "/media/krr-nas-share/Yujiao/ontologies/bio2rdf/chembl/queries/atomic_one_filtered.sparql", "../test-share/results/chembl/hermit_1p"};
54 args = new String[] {"/users/yzhou/temp/uniprot_debug/core-processed-noDis.owl", "/users/yzhou/temp/uniprot_debug/sample_1_removed.nt", "/media/krr-nas-share/Yujiao/ontologies/bio2rdf/uniprot/queries/atomic_one.sparql", "../test-share/results/uniprot/hermit_1p"}; }
55// args = new String[] {"imported.owl", "", "/media/krr-nas-share/Yujiao/ontologies/bio2rdf/uniprot/queries/atomic_one.sparql", "../test-share/results/uniprot/hermit_1p"}; }
56
57
58 PrintStream ps = args.length < 4 ? null : new PrintStream(new File(args[3]));
59 for (int i = 0; i < args.length; ++i) {
60 if (args[i] == null || args[i].equalsIgnoreCase("null")) args[i] = "";
61 System.out.println("Argument " + i + ": " + args[i]);
62 }
63
64// PrintStream ps = null; // new PrintStream(new File("../test-share/results/reactome/ "));
65 if (ps != null) System.setOut(ps);
66
67 Timer t = new Timer();
68 OWLOntology onto = OWLHelper.loadOntology(args[0]);
69 OWLOntologyManager man = onto.getOWLOntologyManager();
70
71 OWLDatatype date = man.getOWLDataFactory().getOWLDatatype(IRI.create("http://www.w3.org/2001/XMLSchema#date"));
72
73 if (onto.containsEntityInSignature(date)) {
74 for (OWLOntology o: onto.getImportsClosure())
75 for (OWLAxiom axiom: o.getAxioms())
76 if (axiom.containsEntityInSignature(date)) {
77 System.out.println("The axiom: " + axiom + " is being ingored. ");
78 man.removeAxiom(onto, axiom);
79 }
80 man.saveOntology(onto, new FileOutputStream(args[0] = "tbox_hermit.owl"));
81 man.removeOntology(onto);
82 onto = OWLHelper.loadOntology(man, args[0]);
83 System.out.println("TBox processed in " + t.duration());
84 }
85
86 try {
87 onto = OWLHelper.getImportedOntology(onto, args[1]);
88 } catch (IOException e) {
89 e.printStackTrace();
90 }
91 System.out.println("Ontology loaded in " + t.duration());
92 System.out.println("ABox axioms:" + onto.getABoxAxioms(true).size());
93// for (OWLOntology o: onto.getImportsClosure())
94// for (OWLAxiom axiom: o.getAxioms())
95// System.out.println(axiom);
96
97 Reasoner hermit = new Reasoner(onto);
98 if (!hermit.isConsistent()) {
99 System.out.println("The ontology is inconsistent.");
100 return ;
101 }
102 System.out.println("Preprocessing DONE in " + t.duration());
103
104// System.out.println(hermit.isConsistent());
105
106 QueryManager queryManager = new QueryManager();
107 QueryRoller roller = new QueryRoller(onto.getOWLOntologyManager().getOWLDataFactory());
108 int failedCounter = 0;
109 Timer total = new Timer();
110 for (QueryRecord record: queryManager.collectQueryRecords(args[2])) {
111 if (Integer.parseInt(record.getQueryID()) < 10) continue;
112 if (total.duration() > 18000) {
113 System.out.println("Time out 5h.");
114 return ;
115 }
116 System.out.println("--------------------- Query " + record.getQueryID() + " -----------------------");
117 System.out.println(record.getQueryText());
118 ExecutorService exec = Executors.newSingleThreadExecutor();
119 try {
120 Future<Boolean> succ = exec.submit(new QueryThread(record, onto, hermit, roller));
121 try {
122 try {
123 if (record.getQueryID().equals("1"))
124 System.out.println(succ.get(60, TimeUnit.MINUTES));
125 else
126 System.out.println(succ.get(20, TimeUnit.MINUTES));
127 } catch (InterruptedException e) {
128// e.printStackTrace();
129 } catch (ExecutionException e) {
130// e.printStackTrace();
131 } catch (TimeoutException e) {
132// e.printStackTrace();
133 }
134 } finally {
135 if (succ.cancel(true)) {
136 System.out.println("Trying to cancel the current query thread " + (++failedCounter));
137 }
138 }
139 } finally {
140 exec.shutdownNow();
141 }
142 }
143
144 if (ps != null) ps.close();
145 System.exit(0);
146 }
147
148}
149
150class QueryThread implements Callable<Boolean> {
151
152 QueryRecord record;
153 OWLOntology onto;
154 Reasoner hermit;
155 QueryRoller roller;
156
157 public QueryThread(QueryRecord record2, OWLOntology onto2, Reasoner hermit2, QueryRoller roller2) {
158 record = record2; onto = onto2; hermit = hermit2; roller = roller2;
159 }
160
161 @Override
162 public Boolean call() throws Exception {
163 Set<String> answers = new HashSet<String>();
164 Timer t = new Timer();
165 if (record.getDistinguishedVariables().length > 1) {
166 if (record.getDistinguishedVariables().length == 2 && record.getClause().getBodyLength() == 1) {
167 dealWithAtomicBinaryQuery(record.getClause().getBodyAtom(0), answers);
168 System.out.println("Query " + record.getQueryID() + " The number of answers: " + answers.size());
169 System.out.println("Query " + record.getQueryID() + " Total time: " + t.duration());
170 }
171 else {
172 System.out.println("Query " + record.getQueryID() + " The number of answers: Query cannot be processsed.");
173 System.out.println("Query " + record.getQueryID() + " Total time: Query cannot be processsed.");
174 }
175 return false;
176 }
177
178 OWLClassExpression exp = null;
179
180 try {
181 exp = roller.rollUp(record.getClause(), record.getAnswerVariables()[0]);
182 } catch (Exception e) {
183 System.out.println("Query " + record.getQueryID() + " The number of answers: Query cannot be processsed.");
184 System.out.println("Query " + record.getQueryID() + " Total time: Query cannot be processsed.");
185 return false;
186 }
187 System.out.println(exp);
188 for (Node<OWLNamedIndividual> node: hermit.getInstances(exp, false)) {
189 for (OWLIndividual ind: node.getEntities()) {
190 answers.add(ind.toStringID());
191 }
192 }
193 System.out.println("Query " + record.getQueryID() + " The number of answers: " + answers.size());
194 System.out.println("Query " + record.getQueryID() + " Total time: " + t.duration());
195 return true;
196 }
197
198 private void dealWithAtomicBinaryQuery(Atom bodyAtom, Set<String> answers) {
199 StringBuilder sb = new StringBuilder();
200 OWLDataFactory f = onto.getOWLOntologyManager().getOWLDataFactory();
201 OWLObjectProperty p = f.getOWLObjectProperty(IRI.create(((AtomicRole) bodyAtom.getDLPredicate()).getIRI()));
202 for (Node<OWLNamedIndividual> sub: hermit.getInstances(f.getOWLObjectMinCardinality(1, p), false)) {
203 for (Node<OWLNamedIndividual> obj: hermit.getObjectPropertyValues(sub.getRepresentativeElement(), p)) {
204 for (OWLNamedIndividual subInd: sub.getEntities()) {
205 sb.setLength(0);
206 sb.append(subInd.toString()).append(" ");
207 int len = sb.length();
208 for (OWLNamedIndividual objInd: obj.getEntities()) {
209 sb.setLength(len);
210 sb.append(objInd.toString());
211 answers.add(sb.toString());
212 }
213 }
214 }
215 }
216 }
217
218}
219
diff --git a/test/uk/ac/ox/cs/hermit/HermitTester.java b/test/uk/ac/ox/cs/hermit/HermitTester.java
new file mode 100644
index 0000000..00df658
--- /dev/null
+++ b/test/uk/ac/ox/cs/hermit/HermitTester.java
@@ -0,0 +1,55 @@
1package uk.ac.ox.cs.hermit;
2
3import org.semanticweb.HermiT.Configuration;
4import org.semanticweb.HermiT.Reasoner;
5import org.semanticweb.HermiT.model.Atom;
6import org.semanticweb.HermiT.model.DLClause;
7import org.semanticweb.HermiT.model.DLOntology;
8import org.semanticweb.HermiT.structural.OWLClausification;
9import org.semanticweb.owlapi.model.IRI;
10import org.semanticweb.owlapi.model.OWLAxiom;
11import org.semanticweb.owlapi.model.OWLClass;
12import org.semanticweb.owlapi.model.OWLDataFactory;
13import org.semanticweb.owlapi.model.OWLNamedIndividual;
14import org.semanticweb.owlapi.model.OWLOntology;
15import org.semanticweb.owlapi.reasoner.Node;
16
17import uk.ac.ox.cs.pagoda.owl.OWLHelper;
18
19public class HermitTester {
20
21 public static void main(String[] args) {
22 OWLOntology onto = OWLHelper.loadOntology("imported.owl");
23 Reasoner hermit = new Reasoner(onto);
24 OWLDataFactory f = onto.getOWLOntologyManager().getOWLDataFactory();
25 OWLClass concept = f.getOWLClass(IRI.create("http://semantics.crl.ibm.com/univ-bench-dl.owl#Query12"));
26
27 for (OWLOntology o: onto.getImportsClosure()) {
28 System.out.println(o.containsEntityInSignature(concept));
29 for (OWLAxiom axiom: o.getAxioms())
30 if (axiom.containsEntityInSignature(concept))
31 System.out.println(axiom);
32 }
33
34 for (Node<OWLNamedIndividual> node : hermit.getInstances(concept, false))
35 for (OWLNamedIndividual i: node.getEntities()) {
36 System.out.println(i.toStringID());
37 }
38
39// clausifierTest();
40 }
41
42 @SuppressWarnings("unused")
43 private static void clausifierTest() {
44 OWLOntology onto = OWLHelper.loadOntology("/users/yzhou/ontologies/travel.owl");
45 OWLClausification clausifier = new OWLClausification(new Configuration());
46 DLOntology dlOntology = (DLOntology)clausifier.preprocessAndClausify(onto, null)[1];
47
48 for (DLClause clause: dlOntology.getDLClauses())
49 System.out.println(clause);
50 for (Atom atom : dlOntology.getPositiveFacts())
51 System.out.println(atom);
52
53 }
54
55}
diff --git a/test/uk/ac/ox/cs/hermit/JAIR_HermiT.java b/test/uk/ac/ox/cs/hermit/JAIR_HermiT.java
new file mode 100644
index 0000000..2fc478d
--- /dev/null
+++ b/test/uk/ac/ox/cs/hermit/JAIR_HermiT.java
@@ -0,0 +1,120 @@
1package uk.ac.ox.cs.hermit;
2
3import org.junit.Test;
4
5import uk.ac.ox.cs.pagoda.tester.PagodaTester;
6
7public class JAIR_HermiT {
8
9 @Test
10 public void lubm1() throws Exception {
11 String[] args = new String[] {
12 PagodaTester.onto_dir + "lubm/univ-bench.owl",
13 PagodaTester.onto_dir + "lubm/data/lubm1_owl",
14 PagodaTester.onto_dir + "lubm/queries/test.sparql"
15// , "/home/yzhou/java-workspace/test-share/results_new/lubm1/hermit"
16 };
17 HermitQueryReasoner.main(args);
18 }
19
20 @Test
21 public void lubm1_rolledUp() throws Exception {
22 String[] args = new String[] {
23 "/home/yzhou/backup/20141212/univ-bench-queries.owl",
24 PagodaTester.onto_dir + "lubm/data/lubm1_owl",
25 PagodaTester.onto_dir + "lubm/queries/atomic_lubm.sparql"
26// , "/home/yzhou/java-workspace/test-share/results_new/lubm1/hermit_rolledUp"
27 };
28 HermitQueryReasoner.main(args);
29 }
30
31 @Test
32 public void uobm1() throws Exception {
33 String[] args = new String[] {
34 PagodaTester.onto_dir + "uobm/univ-bench-dl.owl",
35 PagodaTester.onto_dir + "uobm/data/uobm1_owl_withDeclaration",
36 PagodaTester.onto_dir + "uobm/queries/standard.sparql"
37// , "hermit_uobm1.out"
38// , "/home/yzhou/java-workspace/test-share/results_new/uobm1/hermit"
39 };
40 HermitQueryReasoner.main(args);
41 }
42
43 @Test
44 public void uobm1_rolledUp() throws Exception {
45 String[] args = new String[] {
46 "/home/yzhou/backup/20141212/univ-bench-dl-queries.owl",
47 PagodaTester.onto_dir + "uobm/data/uobm1_owl_withDeclaration",
48 PagodaTester.onto_dir + "uobm/queries/atomic_uobm.sparql"
49 , "hermit_uobm1_rolledUp.out"
50// , "/home/yzhou/java-workspace/test-share/results_new/uobm1/hermit_rolledUp"
51 };
52 HermitQueryReasoner.main(args);
53 }
54
55 @Test
56 public void fly_rolledUp() throws Exception {
57 HermitQueryReasoner.main(
58 PagodaTester.onto_dir + "fly/fly-all-in-one_rolledUp.owl",
59// PagodaTester.onto_dir + "fly/fly_anatomy_XP_with_GJ_FC_individuals.owl",
60 null,
61 PagodaTester.onto_dir + "fly/queries/fly_atomic.sparql"
62 , "hermit_fly.out"
63 );
64 }
65
66 @Test
67 public void npd() throws Exception {
68 HermitQueryReasoner.main(
69 PagodaTester.onto_dir + "npd/npd-all-minus-datatype.owl",
70 PagodaTester.onto_dir + "npd/data/npd-data-dump-minus-datatype-new.ttl",
71 PagodaTester.onto_dir + "npd/queries/atomic_ground.sparql"
72 , "hermit_npd.out"
73// , "/home/yzhou/java-workspace/test-share/results_new/npd/hermit"
74 );
75 }
76
77 @Test
78 public void dbpedia() throws Exception {
79 HermitQueryReasoner.main(
80 PagodaTester.onto_dir + "dbpedia/integratedOntology-all-in-one-minus-datatype.owl",
81 PagodaTester.onto_dir + "dbpedia/data/dbpedia-minus-datatype-new.ttl",
82 PagodaTester.onto_dir + "dbpedia/queries/atomic_ground.sparql"
83 , "/home/yzhou/java-workspace/test-share/results_new/dbpedia/hermit"
84 );
85 }
86
87 @Test
88 public void reactome() throws Exception {
89 HermitQueryReasoner.main(
90 PagodaTester.onto_dir + "bio2rdf/reactome/biopax-level3-processed.owl",
91 PagodaTester.onto_dir + "bio2rdf/reactome/graph sampling/reactome_sample_10.ttl",
92 PagodaTester.onto_dir + "bio2rdf/reactome/queries/atomic_ground.sparql"
93 , "/home/yzhou/java-workspace/test-share/results_new/reactome/hermit_10p"
94 );
95 }
96
97 @Test
98 public void chembl() throws Exception {
99 HermitQueryReasoner.main(
100 PagodaTester.onto_dir + "bio2rdf/chembl/cco-noDPR.ttl",
101// null,
102 PagodaTester.onto_dir + "bio2rdf/chembl/graph sampling/sample_1.nt",
103 PagodaTester.onto_dir + "bio2rdf/chembl/queries/atomic_ground.sparql"
104 , "hermit_chembl.out"
105// , "/home/yzhou/java-workspace/test-share/results_new/chembl/hermit_1p"
106 );
107 }
108
109 @Test
110 public void uniprot() throws Exception {
111 HermitQueryReasoner.main(
112 PagodaTester.onto_dir + "bio2rdf/uniprot/core-sat.owl",
113 PagodaTester.onto_dir + "bio2rdf/uniprot/graph sampling/sample_1.nt",
114 PagodaTester.onto_dir + "bio2rdf/uniprot/queries/atomic_ground.sparql"
115 , "hermit_uniprot.out"
116// , "/home/yzhou/java-workspace/test-share/results_new/uniprot/hermit_1p"
117 );
118 }
119
120}