diff options
Diffstat (limited to 'test/uk/ac/ox/cs/pagoda/global_tests/BugTests.java')
| -rw-r--r-- | test/uk/ac/ox/cs/pagoda/global_tests/BugTests.java | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/test/uk/ac/ox/cs/pagoda/global_tests/BugTests.java b/test/uk/ac/ox/cs/pagoda/global_tests/BugTests.java new file mode 100644 index 0000000..19e0b2a --- /dev/null +++ b/test/uk/ac/ox/cs/pagoda/global_tests/BugTests.java | |||
| @@ -0,0 +1,184 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.global_tests; | ||
| 2 | |||
| 3 | import org.semanticweb.owlapi.apibinding.OWLManager; | ||
| 4 | import org.semanticweb.owlapi.model.*; | ||
| 5 | import org.testng.Assert; | ||
| 6 | import org.testng.annotations.Test; | ||
| 7 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 8 | import uk.ac.ox.cs.pagoda.query.AnswerTuples; | ||
| 9 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 10 | import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner; | ||
| 11 | import uk.ac.ox.cs.pagoda.util.TestUtil; | ||
| 12 | |||
| 13 | import java.io.IOException; | ||
| 14 | import java.nio.file.Files; | ||
| 15 | import java.nio.file.Paths; | ||
| 16 | |||
| 17 | public class BugTests { | ||
| 18 | |||
| 19 | public static final String NS = "http://example.org/test#%s"; | ||
| 20 | |||
| 21 | private IRI getEntityIRI(String name) { | ||
| 22 | return IRI.create(String.format(NS, name)); | ||
| 23 | } | ||
| 24 | |||
| 25 | @Test | ||
| 26 | public void minimumCardinalityAxiom() throws OWLOntologyCreationException, IOException, OWLOntologyStorageException { | ||
| 27 | |||
| 28 | /* | ||
| 29 | * Build test ontology | ||
| 30 | * */ | ||
| 31 | |||
| 32 | OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); | ||
| 33 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 34 | OWLOntology ontology = manager.createOntology(); | ||
| 35 | |||
| 36 | OWLClass student = factory.getOWLClass(getEntityIRI("Student")); | ||
| 37 | manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(student)); | ||
| 38 | OWLClass course = factory.getOWLClass(getEntityIRI("Course")); | ||
| 39 | manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(course)); | ||
| 40 | OWLClass hardWorkingStudent = factory.getOWLClass(getEntityIRI("HardWorkingStudent")); | ||
| 41 | manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(hardWorkingStudent)); | ||
| 42 | OWLNamedIndividual a = factory.getOWLNamedIndividual(getEntityIRI("a")); | ||
| 43 | OWLNamedIndividual b = factory.getOWLNamedIndividual(getEntityIRI("b")); | ||
| 44 | OWLNamedIndividual c1 = factory.getOWLNamedIndividual(getEntityIRI("c1")); | ||
| 45 | OWLNamedIndividual c2 = factory.getOWLNamedIndividual(getEntityIRI("c2")); | ||
| 46 | OWLNamedIndividual c3 = factory.getOWLNamedIndividual(getEntityIRI("c3")); | ||
| 47 | OWLNamedIndividual d1 = factory.getOWLNamedIndividual(getEntityIRI("d1")); | ||
| 48 | OWLNamedIndividual d2 = factory.getOWLNamedIndividual(getEntityIRI("d2")); | ||
| 49 | OWLNamedIndividual d3 = factory.getOWLNamedIndividual(getEntityIRI("d3")); | ||
| 50 | OWLObjectProperty takesCourse = factory.getOWLObjectProperty(IRI.create(String.format(NS, "takesCourse"))); | ||
| 51 | manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(takesCourse)); | ||
| 52 | |||
| 53 | // Class assertions | ||
| 54 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(student, a)); // Student(a) | ||
| 55 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(student, b)); // Student(b) | ||
| 56 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(course, c1)); // Course(c1) | ||
| 57 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(course, c2)); // Course(c2) | ||
| 58 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(course, c3)); // Course(c3) | ||
| 59 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(course, d1)); // Course(d1) | ||
| 60 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(course, d2)); // Course(d2) | ||
| 61 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(course, d3)); // Course(d3) | ||
| 62 | |||
| 63 | // Role assertions | ||
| 64 | manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(takesCourse, a, c1)); // takesCourse(a,c1) | ||
| 65 | manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(takesCourse, a, c2)); // takesCourse(a,c2) | ||
| 66 | manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(takesCourse, a, c3)); // takesCourse(a,c3) | ||
| 67 | manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(takesCourse, b, d1)); // takesCourse(b,d1) | ||
| 68 | manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(takesCourse, b, d2)); // takesCourse(b,d2) | ||
| 69 | manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(takesCourse, b, d3)); // takesCourse(b,d3) | ||
| 70 | |||
| 71 | // Minimum cardinality axiom | ||
| 72 | manager.addAxiom(ontology, | ||
| 73 | factory.getOWLEquivalentClassesAxiom(hardWorkingStudent, | ||
| 74 | factory.getOWLObjectMinCardinality(3, | ||
| 75 | takesCourse))); | ||
| 76 | |||
| 77 | manager.saveOntology(ontology, Files.newOutputStream(Paths.get("/home/alessandro/Desktop/test-ontology.owl"))); | ||
| 78 | |||
| 79 | /* | ||
| 80 | * Test one query | ||
| 81 | * */ | ||
| 82 | |||
| 83 | QueryReasoner pagoda = QueryReasoner.getInstance(ontology); | ||
| 84 | pagoda.loadOntology(ontology); | ||
| 85 | if (pagoda.preprocess()) { | ||
| 86 | String query = "select distinct ?x ?y " + | ||
| 87 | " where { " | ||
| 88 | + " ?x <" + takesCourse.toStringID() + "> _:z . " | ||
| 89 | + " ?y <" + takesCourse.toStringID() + "> _:z " + | ||
| 90 | " }"; | ||
| 91 | AnswerTuples answers = pagoda.evaluate(query); | ||
| 92 | int count = 0; | ||
| 93 | for (AnswerTuple ans; answers.isValid(); answers.moveNext()) { | ||
| 94 | ans = answers.getTuple(); | ||
| 95 | TestUtil.logInfo(ans); | ||
| 96 | count++; | ||
| 97 | } | ||
| 98 | Assert.assertEquals(count, 2); | ||
| 99 | } | ||
| 100 | pagoda.dispose(); | ||
| 101 | } | ||
| 102 | |||
| 103 | /** | ||
| 104 | * Bug: the relevant ontology is not a subset of the original one. | ||
| 105 | * | ||
| 106 | * @throws OWLOntologyCreationException | ||
| 107 | * @throws IOException | ||
| 108 | * @throws OWLOntologyStorageException | ||
| 109 | */ | ||
| 110 | @Test | ||
| 111 | public void rTest() throws OWLOntologyCreationException, IOException, OWLOntologyStorageException { | ||
| 112 | |||
| 113 | /* | ||
| 114 | * Build test ontology | ||
| 115 | * */ | ||
| 116 | |||
| 117 | OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); | ||
| 118 | OWLDataFactory factory = manager.getOWLDataFactory(); | ||
| 119 | OWLOntology ontology = manager.createOntology(); | ||
| 120 | |||
| 121 | OWLClass classA = factory.getOWLClass(getEntityIRI("A")); | ||
| 122 | manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(classA)); | ||
| 123 | OWLClass classB = factory.getOWLClass(getEntityIRI("B")); | ||
| 124 | manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(classB)); | ||
| 125 | OWLNamedIndividual a = factory.getOWLNamedIndividual(getEntityIRI("a")); | ||
| 126 | OWLNamedIndividual b = factory.getOWLNamedIndividual(getEntityIRI("b")); | ||
| 127 | OWLNamedIndividual c = factory.getOWLNamedIndividual(getEntityIRI("c")); | ||
| 128 | OWLObjectProperty roleR = factory.getOWLObjectProperty(IRI.create(String.format(NS, "R"))); | ||
| 129 | manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(roleR)); | ||
| 130 | OWLObjectProperty roleP = factory.getOWLObjectProperty(IRI.create(String.format(NS, "P"))); | ||
| 131 | manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(roleP)); | ||
| 132 | |||
| 133 | // Class assertions | ||
| 134 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(classA, a)); // A(a) | ||
| 135 | manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(classA, b)); // A(b) | ||
| 136 | manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(roleP, c, a)); // P(c,a) | ||
| 137 | |||
| 138 | // Axioms | ||
| 139 | // subsetOf(A someValuesFrom(R owl:Thing)) | ||
| 140 | manager.addAxiom(ontology, | ||
| 141 | factory.getOWLSubClassOfAxiom(classA, | ||
| 142 | factory.getOWLObjectSomeValuesFrom(roleR, | ||
| 143 | factory.getOWLThing()))); | ||
| 144 | |||
| 145 | // inverseFunctional(R) | ||
| 146 | manager.addAxiom(ontology, | ||
| 147 | factory.getOWLInverseFunctionalObjectPropertyAxiom(roleR)); | ||
| 148 | |||
| 149 | // subsetOf(someValuesFrom(inverseOf(P) owl:thing) B) | ||
| 150 | manager.addAxiom(ontology, | ||
| 151 | factory.getOWLSubClassOfAxiom(factory.getOWLObjectSomeValuesFrom(roleP.getInverseProperty(), | ||
| 152 | factory.getOWLThing()), | ||
| 153 | classB)); | ||
| 154 | /* | ||
| 155 | * Save the ontology | ||
| 156 | * */ | ||
| 157 | |||
| 158 | // manager.saveOntology(ontology, Files.newOutputStream(Paths.get("/home/alessandro/Desktop/test-ontology.owl"))); | ||
| 159 | |||
| 160 | /* | ||
| 161 | * Test one query | ||
| 162 | * */ | ||
| 163 | |||
| 164 | QueryReasoner pagoda = QueryReasoner.getInstance(ontology); | ||
| 165 | pagoda.loadOntology(ontology); | ||
| 166 | if(pagoda.preprocess()) { | ||
| 167 | String queryStr = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + | ||
| 168 | "select distinct ?x" + | ||
| 169 | " where { " | ||
| 170 | + " ?x rdf:type " + classB + | ||
| 171 | " }"; | ||
| 172 | QueryRecord queryRecord = pagoda.getQueryManager().create(queryStr); | ||
| 173 | System.out.println(queryRecord); | ||
| 174 | pagoda.evaluate(queryRecord); | ||
| 175 | AnswerTuples answers = queryRecord.getAnswers(); | ||
| 176 | System.out.println("Difficulty: " + queryRecord.getDifficulty()); | ||
| 177 | for(AnswerTuple ans; answers.isValid(); answers.moveNext()) { | ||
| 178 | ans = answers.getTuple(); | ||
| 179 | TestUtil.logInfo(ans); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | pagoda.dispose(); | ||
| 183 | } | ||
| 184 | } | ||
