public static void usageWithDefaultModel() { System.out.println("Results with plain RDF Model"); System.out.println("----------------------------"); System.out.println(); // ontology that will be used final String ont = "http://protege.cim3.net/file/pub/ontologies/koala/koala.owl#"; final String ns = "http://protege.stanford.edu/plugins/owl/owl-library/koala.owl#"; // create Pellet reasoner final Reasoner reasoner = PelletReasonerFactory.theInstance().create(); // create an empty model final Model emptyModel = ModelFactory.createDefaultModel(); // create an inferencing model using Pellet reasoner final InfModel model = ModelFactory.createInfModel(reasoner, emptyModel); // read the file model.read(ont); // print validation report final ValidityReport report = model.validate(); printIterator(report.getReports(), "Validation Results"); // print superclasses final Resource c = model.getResource(ns + "MaleStudentWith3Daughters"); printIterator(model.listObjectsOfProperty(c, RDFS.subClassOf), "All super classes of " + c.getLocalName()); System.out.println(); }
/** * RETE incremental processing bug. */ public void testRETEDec() { String rule = "(?x ?p ?y) -> (?p rdf:type rdf:Property) ."; Reasoner r = new GenericRuleReasoner(Rule.parseRules(rule)); InfModel m = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel()); Resource source = m.createResource("urn:alfie:testResource"); Property prop = m.createProperty("urn:alfie:testProperty"); Statement s1=m.createStatement(source, prop, "value1"); m.createStatement(source, prop, "value2"); m.add(prop, RDF.type, RDF.Property); m.add(s1); m.prepare(); m.remove(s1); assertIsProperty(m, prop); }
/** * The reasoner contract for bind(data) is not quite precise. It allows for * reasoners which have state so that reusing the same reasoner on a second data * model might lead to interference. This in fact used to happen with the transitive * reasoner. This is a test to check that the transitive reasoner state reuse has been fixed at source. */ public void testTransitiveBindReuse() { Reasoner r = ReasonerRegistry.getTransitiveReasoner(); InfModel om1 = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel()); Resource c1 = om1.createResource(PrintUtil.egNS + "Class1"); Resource c2 = om1.createResource(PrintUtil.egNS + "Class2"); Resource c3 = om1.createResource(PrintUtil.egNS + "Class3"); om1.add(c1, RDFS.subClassOf, c2); om1.add(c2, RDFS.subClassOf, c3); om1.prepare(); assertFalse(om1.isEmpty()); InfModel om2 = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel()); StmtIterator si = om2.listStatements(); boolean ok = ! si.hasNext(); si.close(); assertTrue("Transitive reasoner state leak", ok); }
private Set<String> getObjectsOfProperty(final Model model, final String subject, final String property) { final InfModel infModel = createInfModel(getOWLMicroReasoner(), createDefaultModel()); infModel.add(model.listStatements()); infModel.add(pcdmModel.listStatements()); return asStream(infModel.listObjectsOfProperty(createResource(subject), createProperty(property))) .filter(RDFNode::isURIResource).map(RDFNode::asResource).map(Resource::getURI).collect(toSet()); } }
/** * Check that the use of typed literals in the configuration also works */ public void testTypedConfigParameters() { Model m = ModelFactory.createDefaultModel(); Resource configuration= m.createResource(GenericRuleReasonerFactory.URI); configuration.addProperty(ReasonerVocabulary.PROPenableTGCCaching, m.createTypedLiteral(Boolean.TRUE)); GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(configuration); InfModel im = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel()); Resource Ac = im.createResource(PrintUtil.egNS + "A"); Resource Bc = im.createResource(PrintUtil.egNS + "B"); Resource Cc = im.createResource(PrintUtil.egNS + "C"); im.add(Ac, RDFS.subClassOf, Bc); im.add(Bc, RDFS.subClassOf, Cc); assertTrue("TGC enabled correctly", im.contains(Ac, RDFS.subClassOf, Cc)); }
/** * Test the @prefix and @include extensions to the rule parser */ public void testExtendedRuleParser() { List<Rule> rules = Rule.rulesFromURL("file:testing/reasoners/ruleParserTest1.rules"); GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); reasoner.setTransitiveClosureCaching(true); Model base = ModelFactory.createDefaultModel(); InfModel m = ModelFactory.createInfModel(reasoner, base); // Check prefix case String NS1 = "http://jena.hpl.hp.com/newprefix#"; String NS2 = "http://jena.hpl.hp.com/newprefix2#"; String NS3 = "http://jena.hpl.hp.com/newprefix3#"; Resource A = m.getResource(NS1 + "A"); Resource C = m.getResource(NS1 + "C"); Property p = m.getProperty(NS2 + "p"); Property a = m.getProperty(NS3 + "a"); Resource foo = m.getResource(NS1 + "foo"); assertTrue("@prefix test", m.contains(A, p, foo)); // Check RDFS rule inclusion assertTrue("@include RDFS test", m.contains(A, RDFS.subClassOf, C)); assertTrue("@include test", m.contains(a,a,a)); }
Model data = FileManager.get().loadModel("file:testing/reasoners/bugs/loop.owl"); InfModel infmodel = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), data); ((FBRuleInfGraph)infmodel.getGraph()).setTraceOn(true); String baseURI = "http://jena.hpl.hp.com/eg#"; Resource C = infmodel.getResource(baseURI + "C"); Resource I = infmodel.getResource(baseURI + "i"); Property R = infmodel.getProperty(baseURI, "R"); System.out.println("List all instances of C"); int count = 0; for (Iterator<Statement> i = infmodel.listStatements(null, RDF.type, C); i.hasNext(); ) { Statement st = i.next(); System.out.println(" - " + st);
public static void main(String[] args) { FileManager.get().addLocatorClassLoader(ExampleONT_02.class.getClassLoader()); Model tbox = FileManager.get().loadModel("data/inference/tbox.owl", null, "RDF/XML"); // http://en.wikipedia.org/wiki/Tbox Reasoner reasoner = ReasonerRegistry.getOWLReasoner().bindSchema(tbox.getGraph()); Model abox = FileManager.get().loadModel("data/inference/abox.owl", null, "RDF/XML"); // http://en.wikipedia.org/wiki/Abox InfModel inf = ModelFactory.createInfModel(reasoner, abox); ValidityReport validityReport = inf.validate(); if ( !validityReport.isValid() ) { System.out.println("Inconsistent"); Iterator<Report> iter = validityReport.getReports(); while ( iter.hasNext() ) { Report report = iter.next(); System.out.println(report); } } else { System.out.println("Valid"); } }
/** * Test problems with inferring equivalence of some simple class definitions, * reported by Jeffrey Hau. */ public void testEquivalentClass1() { Model base = ModelFactory.createDefaultModel(); base.read("file:testing/reasoners/bugs/equivalentClassTest.owl"); InfModel test = ModelFactory.createInfModel(ReasonerRegistry.getOWLReasoner(), base); String NAMESPACE = "urn:foo#"; Resource A = test.getResource(NAMESPACE + "A"); Resource B = test.getResource(NAMESPACE + "B"); assertTrue("hasValue equiv deduction", test.contains(A, OWL.equivalentClass, B)); }
final Model emptyModel = ModelFactory.createDefaultModel(); final InfModel model = ModelFactory.createInfModel(reasoner, emptyModel); model.read(stream, null, "TURTLE");
/** * Test that a transitive reduction is complete. * Assumes test graph has no cycles (other than the trivial * identity ones). */ public void doTestTransitiveReduction(Model model, Property dp) { InfModel im = ModelFactory.createInfModel(ReasonerRegistry.getTransitiveReasoner(), model); for (ResIterator i = im.listSubjects(); i.hasNext();) { Resource base = i.nextResource(); List<RDFNode> directLinks = new ArrayList<>(); for (NodeIterator j = im.listObjectsOfProperty(base, dp); j.hasNext(); ) { directLinks.add(j.next()); } for (int n = 0; n < directLinks.size(); n++) { Resource d1 = (Resource)directLinks.get(n); for (int m = n+1; m < directLinks.size(); m++) { Resource d2 = (Resource)directLinks.get(m); if (im.contains(d1, dp, d2) && ! base.equals(d1) && !base.equals(d2)) { assertTrue("Triangle discovered in transitive reduction", false); } } } } }
Bag bag1 = aBox.createBag(); Bag bag2 = aBox.createBag(); bag1.addProperty(OWL.differentFrom, bag2); Resource molecule = aBox.createResource(); molecule.addProperty(molecule2atomOntProperty, bag1); molecule.addProperty(molecule2atomOntProperty, bag2); assertTrue(aBox.contains(molecule, RDF.type, moleculeClass)); assertFalse(aBox.validate().isValid()); // fails: why?
Resource i = conclusions.getResource("http://www.w3.org/2002/03owlt/someValuesFrom/premises001#i"); Property p = conclusions.getProperty("http://www.w3.org/2002/03owlt/someValuesFrom/premises001#p"); Resource c = conclusions.getResource("http://www.w3.org/2002/03owlt/someValuesFrom/premises001#c"); Resource r = conclusions.getResource("http://www.w3.org/2002/03owlt/someValuesFrom/premises001#r"); Resource v = (Resource)i.getRequiredProperty(p).getObject(); System.out.println("Value of i.p = " + v); System.out.println("Types of v are: "); for (StmtIterator it2 = conclusions.listStatements(v, RDF.type, (RDFNode)null); it2.hasNext(); ) { System.out.println(" - " + it2.next());
/** * Test bug with leaking variables which results in an incorrect "range = Nothing" deduction. */ public void testRangeBug() { Model model = FileManager.get().loadModel("file:testing/reasoners/bugs/rangeBug.owl"); // Model m = ModelFactory.createDefaultModel(); Reasoner r = ReasonerRegistry.getOWLReasoner(); InfModel omodel = ModelFactory.createInfModel(r, model); String baseuri = "http://decsai.ugr.es/~ontoserver/bacarex2.owl#"; // Resource js = omodel.getResource(baseuri + "JS"); Resource surname = omodel.getResource(baseuri + "surname"); Statement s = omodel.createStatement(surname, RDFS.range, OWL.Nothing); assertTrue(! omodel.contains(s)); }
/** * test remove operator in case with empty data. */ public void testEmptyRemove() { List<Rule> rules = Rule.parseRules( "-> (eg:i eg:prop eg:foo) ." + "(?X eg:prop ?V) -> (?X eg:prop2 ?V) ." + "(?X eg:prop eg:foo) noValue(?X eg:guard 'done') -> remove(0) (?X eg:guard 'done') ." ); GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); InfModel im = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel()); Resource i = im.createResource(PrintUtil.egNS + "i"); Property guard = im.createProperty(PrintUtil.egNS + "guard"); TestUtil.assertIteratorValues(this, im.listStatements(), new Object[] {im.createStatement(i, guard, "done")}); }
@Test public void test253() { final String aOnt = "file:" + PelletTestSuite.base + "misc/longitude2.ttl"; final PelletReasoner aReasoner = PelletReasonerFactory.theInstance().create(); final InfModel aModel = ModelFactory.createInfModel(aReasoner, ModelFactory.createDefaultModel()); aModel.read(aOnt, "N3"); final String aQuery = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n" + "PREFIX mon: <http://www.semwebtech.org/mondial/10/meta#>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "PREFIX : <foo://bla/>\n" + "SELECT ?v0 WHERE\n" + "{ ?v0 rdf:type :EasternHemispherePlace. }\n"; final QueryExecution qe = SparqlDLExecutionFactory.create(QueryFactory.create(aQuery), aModel); final Resource berlin = aModel.getResource("foo://bla/Berlin"); final ResultSet results = qe.execSelect(); assertTrue(results.hasNext()); assertTrue(berlin.equals(results.next().getResource("v0"))); assertFalse(results.hasNext()); }
/** * Listeners on deductions graph should be preserved across rebind operations */ public void testDeductionListener() { final String NS = PrintUtil.egNS; // Data: (eg:i eg:p 'foo') Model base = ModelFactory.createDefaultModel(); Resource i = base.createResource(NS + "i"); Property p = base.createProperty(NS + "p"); i.addProperty(p, "foo"); // Inf model List<Rule> rules = Rule.parseRules( "(?x eg:p ?y) -> (?x eg:q ?y). " ); GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); InfModel infModel = ModelFactory.createInfModel(reasoner, base); TestListener listener = new TestListener(); infModel.getDeductionsModel().register(listener); infModel.rebind(); infModel.prepare(); assertEquals("foo", listener.getLastValue()); i.removeAll(p); i.addProperty(p, "bar"); infModel.rebind(); infModel.prepare(); assertEquals("bar", listener.getLastValue()); }
infModel.prepare(); infModel.setNsPrefixes(prefixes); Model deductions = infModel.getDeductionsModel(); deductions.setNsPrefixes(prefixes); deductions.setNsPrefixes(inModel); deductions.write(writer, outLang); } else { infModel.write(writer, outLang);
/** * Bug that exposed prototypes of owl:Thing despite hiding being switched on. */ public void testHideOnOWLThing() { Reasoner r = ReasonerRegistry.getOWLReasoner(); Model data = ModelFactory.createDefaultModel(); InfModel inf = ModelFactory.createInfModel(r, data); StmtIterator things = inf.listStatements(null, RDF.type, OWL.Thing); TestUtil.assertIteratorLength(things, 0); }
/** * Fact rules with non-empty bodyies failed to fire. */ public void testFactRules() { Model facts = ModelFactory.createDefaultModel(); String NS = PrintUtil.egNS; Property p = facts.createProperty(NS + "p"); List<Rule> rules = Rule.parseRules("makeTemp(?x) -> (?x, eg:p, eg:z). " + "makeTemp(?x) makeTemp(?y) -> (?x, eg:p, ?y) . " + "(?x, eg:p, eg:z) -> (?a, eg:p, eg:b). " + "-> [ (eg:a eg:p eg:y) <- ]." ); GenericRuleReasoner reasoner = new GenericRuleReasoner(rules); InfModel inf = ModelFactory.createInfModel(reasoner, facts); inf.prepare(); TestUtil.assertIteratorLength(inf.listStatements(null, p, (RDFNode)null), 4); }