use of org.apache.jena.reasoner.Reasoner in project jena by apache.
the class TestReasoners method testTransitiveEngineSeparation.
/**
* Test that two transitive engines are independent.
* See JENA-1260
*/
public void testTransitiveEngineSeparation() throws InterruptedException {
String NS = "http://example.com/test#";
Property sp = ResourceFactory.createProperty(NS, "sp");
Property p = ResourceFactory.createProperty(NS, "p");
Property s = ResourceFactory.createProperty(NS, "s");
Resource q = ResourceFactory.createProperty(NS, "q");
Reasoner reasoner = ReasonerRegistry.getTransitiveReasoner();
InfModel simple = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel());
simple.add(s, sp, p);
assertFalse(simple.contains(s, RDFS.subPropertyOf, p));
InfModel withSP = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel());
withSP.add(sp, RDFS.subPropertyOf, RDFS.subPropertyOf);
withSP.add(s, sp, p);
assertTrue(withSP.contains(s, RDFS.subPropertyOf, p));
simple.add(q, sp, p);
assertFalse(simple.contains(q, RDFS.subPropertyOf, p));
}
use of org.apache.jena.reasoner.Reasoner in project jena by apache.
the class TestQueryEngineMultiThreaded method createForwardChainingModel.
private Model createForwardChainingModel() {
Model baseModel = ModelFactory.createDefaultModel();
Model schemaModel = ModelFactory.createDefaultModel();
Model configurationRuleReasoner = ModelFactory.createDefaultModel();
org.apache.jena.rdf.model.Resource configuration = configurationRuleReasoner.createResource();
configuration.addProperty(ReasonerVocabulary.PROPruleMode, "forward");
configuration.addProperty(ReasonerVocabulary.PROPsetRDFSLevel, ReasonerVocabulary.RDFS_SIMPLE);
Reasoner ruleReasoner = RDFSRuleReasonerFactory.theInstance().create(configuration);
InfModel inf = ModelFactory.createInfModel(ruleReasoner, schemaModel, baseModel);
inf.read(new ByteArrayInputStream(TURTLE_RDF.getBytes()), "", "TURTLE");
return inf;
}
use of org.apache.jena.reasoner.Reasoner in project jena by apache.
the class TestOntReasoning method testRDFSAbox.
/**
* Problem reported by Andy Seaborne - combine abox and tbox in RDFS with
* ontmodel
*/
public void testRDFSAbox() {
String sourceT = "<rdf:RDF " + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'" + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#'" + " xmlns:owl=\"http://www.w3.org/2002/07/owl#\">" + " <owl:Class rdf:about='http://example.org/foo#A'>" + " </owl:Class>" + "</rdf:RDF>";
String sourceA = "<rdf:RDF " + " xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'" + " xmlns:rdfs='http://www.w3.org/2000/01/rdf-schema#' " + " xmlns:owl=\"http://www.w3.org/2002/07/owl#\">" + " <rdf:Description rdf:about='http://example.org/foo#x'>" + " <rdf:type rdf:resource='http://example.org/foo#A' />" + " </rdf:Description>" + "</rdf:RDF>";
Model tBox = ModelFactory.createDefaultModel();
tBox.read(new ByteArrayInputStream(sourceT.getBytes()), "http://example.org/foo");
Model aBox = ModelFactory.createDefaultModel();
aBox.read(new ByteArrayInputStream(sourceA.getBytes()), "http://example.org/foo");
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(tBox);
OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM_RULE_INF);
spec.setReasoner(reasoner);
OntModel m = ModelFactory.createOntologyModel(spec, aBox);
List<Individual> inds = new ArrayList<>();
for (Iterator<Individual> i = m.listIndividuals(); i.hasNext(); ) {
inds.add(i.next());
}
assertTrue("x should be an individual", inds.contains(m.getResource("http://example.org/foo#x")));
}
use of org.apache.jena.reasoner.Reasoner in project jena by apache.
the class ModelFactory method createRDFSModel.
/**
* Return a Model through which all the RDFS entailments
* derivable from the given model are accessible. Some work is done
* when the inferenced model is created but each query will also trigger some
* additional inference work.
*
* @param model the Model containing both instance data and schema assertions to be inferenced over
*/
public static InfModel createRDFSModel(Model model) {
Reasoner reasoner = ReasonerRegistry.getRDFSReasoner();
InfGraph graph = reasoner.bind(model.getGraph());
return new InfModelImpl(graph);
}
use of org.apache.jena.reasoner.Reasoner in project jena by apache.
the class WGReasonerTester method runTestDetailedResponse.
/**
* Run a single designated test.
* @param uri the uri of the test, as defined in the manifest file
* @param reasonerF the factory for the reasoner to be tested
* @param testcase the JUnit test case which is requesting this test
* @param configuration optional configuration information
* @return true if the test passes
* @throws IOException if one of the test files can't be found
* @throws JenaException if the test can't be found or fails internally
*/
public int runTestDetailedResponse(String uri, ReasonerFactory reasonerF, TestCase testcase, Resource configuration) throws IOException {
// Find the specification for the named test
Resource test = testManifest.getResource(uri);
testType = (Resource) test.getRequiredProperty(RDF.type).getObject();
if (!(testType.equals(NegativeEntailmentTest) || testType.equals(PositiveEntailmentTest))) {
throw new JenaException("Can't find test: " + uri);
}
Statement descriptionS = test.getProperty(descriptionP);
String description = (descriptionS == null) ? "no description" : descriptionS.getObject().toString();
String status = test.getRequiredProperty(statusP).getObject().toString();
logger.debug("WG test " + test.getURI() + " - " + status);
if (!status.equals("APPROVED")) {
return NOT_APPLICABLE;
}
// Skip the test designed for only non-datatype aware processors
for (String blockedTest : blockedTests) {
if (test.getURI().equals(blockedTest)) {
return NOT_APPLICABLE;
}
}
// Load up the premise documents
Model premises = ModelFactory.createDefaultModel();
for (StmtIterator premisesI = test.listProperties(premiseDocumentP); premisesI.hasNext(); ) {
premises.add(loadFile(premisesI.nextStatement().getObject().toString()));
}
// Load up the conclusions document
Model conclusions = null;
Resource conclusionsRes = (Resource) test.getRequiredProperty(conclusionDocumentP).getObject();
Resource conclusionsType = (Resource) conclusionsRes.getRequiredProperty(RDF.type).getObject();
if (!conclusionsType.equals(FalseDocument)) {
conclusions = loadFile(conclusionsRes.toString());
}
// Construct the inferred graph
Reasoner reasoner = reasonerF.create(configuration);
InfGraph graph = reasoner.bind(premises.getGraph());
Model result = ModelFactory.createModelForGraph(graph);
// Check the results against the official conclusions
boolean correct = true;
int goodResult = PASS;
boolean noisy = !(baseDir.equals(DEFAULT_BASE_DIR) || ARPTests.internet);
if (testType.equals(PositiveEntailmentTest)) {
if (conclusions == null) {
// Check that the result is flagged as semantically invalid
correct = !graph.validate().isValid();
if (noisy) {
System.out.println("PositiveEntailmentTest of FalseDoc " + test.getURI() + (correct ? " - OK" : " - FAIL"));
}
} else {
correct = testConclusions(conclusions.getGraph(), result.getGraph());
if (!graph.validate().isValid()) {
correct = false;
}
if (noisy) {
System.out.println("PositiveEntailmentTest " + test.getURI() + (correct ? " - OK" : " - FAIL"));
}
}
} else {
goodResult = INCOMPLETE;
// A negative entailment check
if (conclusions == null) {
// Check the result is not flagged as invalid
correct = graph.validate().isValid();
if (noisy) {
System.out.println("NegativentailmentTest of FalseDoc " + test.getURI() + (correct ? " - OK" : " - FAIL"));
}
} else {
correct = !testConclusions(conclusions.getGraph(), result.getGraph());
if (noisy) {
System.out.println("NegativeEntailmentTest " + test.getURI() + (correct ? " - OK" : " - FAIL"));
}
}
}
// Debug output on failure
if (!correct) {
logger.debug("Premises: ");
for (StmtIterator i = premises.listStatements(); i.hasNext(); ) {
logger.debug(" - " + i.nextStatement());
}
logger.debug("Conclusions: ");
if (conclusions != null) {
for (StmtIterator i = conclusions.listStatements(); i.hasNext(); ) {
logger.debug(" - " + i.nextStatement());
}
}
}
// Signal the results
if (testcase != null) {
// if ( !correct )
// {
// boolean b = testConclusions(conclusions.getGraph(), result.getGraph());
// System.out.println("**** actual") ;
// result.write(System.out, "TTL") ;
// System.out.println("**** expected") ;
// conclusions.write(System.out, "TTL") ;
// }
Assert.assertTrue("Test: " + test + "\n" + description, correct);
}
return correct ? goodResult : FAIL;
}
Aggregations