use of org.apache.jena.reasoner.Reasoner in project jena by apache.
the class TestBackchainer method testListData.
/**
* Check that a reasoner over an empty rule set accesses
* the raw data successfully.
*/
public void testListData() {
Graph data = Factory.createGraphMem();
for (Triple dataElt : dataElts) {
data.add(dataElt);
}
Graph schema = Factory.createGraphMem();
schema.add(new Triple(c, p, c));
// Case of schema and data but no rule axioms
Reasoner reasoner = createReasoner(new ArrayList<Rule>());
InfGraph infgraph = reasoner.bindSchema(schema).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Object[] { new Triple(p, sP, q), new Triple(q, sP, r), new Triple(a, p, b), new Triple(c, p, c) });
// Case of data and rule axioms but no schema
List<Rule> rules = Rule.parseRules("-> (d p d).");
reasoner = createReasoner(rules);
infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Object[] { new Triple(p, sP, q), new Triple(q, sP, r), new Triple(a, p, b), new Triple(d, p, d) });
// Case of data and rule axioms and schema
infgraph = reasoner.bindSchema(schema).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Object[] { new Triple(p, sP, q), new Triple(q, sP, r), new Triple(a, p, b), new Triple(c, p, c), new Triple(d, p, d) });
}
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;
}
use of org.apache.jena.reasoner.Reasoner in project jena by apache.
the class TestReasoners method doTestFindWithPremises.
/**
* Test a reasoner's ability to implement find with premises.
* Assumes the reasoner can at least implement RDFS subClassOf.
*/
public void doTestFindWithPremises(ReasonerFactory rf) {
Node c1 = NodeFactory.createURI("C1");
Node c2 = NodeFactory.createURI("C2");
Node c3 = NodeFactory.createURI("C3");
Node sC = RDFS.subClassOf.asNode();
Graph data = Factory.createGraphMem();
data.add(new Triple(c2, sC, c3));
Graph premise = Factory.createGraphMem();
premise.add(new Triple(c1, sC, c2));
Reasoner reasoner = rf.create(null);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] {});
TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null, premise), new Object[] { new Triple(c1, sC, c2), new Triple(c1, sC, c3), new Triple(c1, sC, c1) });
TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] {});
}
use of org.apache.jena.reasoner.Reasoner in project jena by apache.
the class TestModelFactory method testCreateInfModel.
public void testCreateInfModel() {
final String rule = "-> (eg:r eg:p eg:v).";
final Reasoner r = new GenericRuleReasoner(Rule.parseRules(rule));
final InfGraph ig = r.bind(ModelFactory.createDefaultModel().getGraph());
final InfModel im = ModelFactory.createInfModel(ig);
JenaTestBase.assertInstanceOf(InfModel.class, im);
Assert.assertEquals(1, im.size());
}
Aggregations