Search in sources :

Example 61 with InfGraph

use of org.apache.jena.reasoner.InfGraph 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;
}
Also used : JenaException(org.apache.jena.shared.JenaException) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Example 62 with InfGraph

use of org.apache.jena.reasoner.InfGraph in project jena by apache.

the class TestLPDerivation method doTest.

/**
     * Run a single derivation test. Only tests the first derivation found.
     * @param ruleSrc source for a set of rules
     * @param tabled  array of predicate nodes which should be tabled by the rules
     * @param triples inital array of triple data
     * @param query   the query to be tested the first result will be checked
     * @param matches the set of triple matches which should occur in the derivation
     * @param rulenumber the index of the rule in the rule list which should occur in the derivation
     */
private void doTest(String ruleSrc, Node[] tabled, Triple[] triples, Triple query, Triple[] matches, int rulenumber) {
    List<Rule> rules = Rule.parseRules(ruleSrc);
    Graph data = Factory.createGraphMem();
    for (Triple triple : triples) {
        data.add(triple);
    }
    InfGraph infgraph = makeInfGraph(rules, data, tabled);
    ExtendedIterator<Triple> results = infgraph.find(query);
    assertTrue(results.hasNext());
    Triple result = results.next();
    results.close();
    Rule rule = rules.get(rulenumber);
    List<Triple> matchList = Arrays.asList(matches);
    Iterator<Derivation> derivations = infgraph.getDerivation(result);
    assertTrue(derivations.hasNext());
    RuleDerivation derivation = (RuleDerivation) derivations.next();
    //        PrintWriter pw = new PrintWriter(System.out);
    //        derivation.printTrace(pw, true);
    //        pw.close();
    assertEquals(result, derivation.getConclusion());
    assertEquals(matchList, derivation.getMatches());
    assertEquals(rule, derivation.getRule());
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) Derivation(org.apache.jena.reasoner.Derivation) RuleDerivation(org.apache.jena.reasoner.rulesys.RuleDerivation) Rule(org.apache.jena.reasoner.rulesys.Rule) RuleDerivation(org.apache.jena.reasoner.rulesys.RuleDerivation)

Example 63 with InfGraph

use of org.apache.jena.reasoner.InfGraph in project jena by apache.

the class WebOntTestHarness method doRunTest.

/**
     * Run a single test of any sort, return true if the test succeeds.
     */
public boolean doRunTest(Resource test) throws IOException {
    if (test.hasProperty(RDF.type, OWLTest.PositiveEntailmentTest) || test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest) || test.hasProperty(RDF.type, OWLTest.OWLforOWLTest) || test.hasProperty(RDF.type, OWLTest.ImportEntailmentTest) || test.hasProperty(RDF.type, OWLTest.TrueTest)) {
        // Entailment tests
        boolean processImports = test.hasProperty(RDF.type, OWLTest.ImportEntailmentTest);
        Model premises = getDoc(test, RDFTest.premiseDocument, processImports);
        Model conclusions = getDoc(test, RDFTest.conclusionDocument);
        comprehensionAxioms(premises, conclusions);
        long t1 = System.currentTimeMillis();
        InfGraph graph = reasoner.bind(premises.getGraph());
        if (printProfile) {
            ((FBRuleInfGraph) graph).resetLPProfile(true);
        }
        Model result = ModelFactory.createModelForGraph(graph);
        boolean correct = WGReasonerTester.testConclusions(conclusions.getGraph(), result.getGraph());
        long t2 = System.currentTimeMillis();
        lastTestDuration = t2 - t1;
        if (printProfile) {
            ((FBRuleInfGraph) graph).printLPProfile();
        }
        if (test.hasProperty(RDF.type, OWLTest.NegativeEntailmentTest)) {
            correct = !correct;
        }
        return correct;
    } else if (test.hasProperty(RDF.type, OWLTest.InconsistencyTest)) {
        //            System.out.println("Starting: " + test);
        Model input = getDoc(test, RDFTest.inputDocument);
        long t1 = System.currentTimeMillis();
        InfGraph graph = reasoner.bind(input.getGraph());
        boolean correct = !graph.validate().isValid();
        long t2 = System.currentTimeMillis();
        lastTestDuration = t2 - t1;
        return correct;
    } else if (test.hasProperty(RDF.type, OWLTest.ConsistencyTest)) {
        // Not used normally becase we are not complete enough to prove consistency
        //            System.out.println("Starting: " + test);
        Model input = getDoc(test, RDFTest.inputDocument);
        long t1 = System.currentTimeMillis();
        InfGraph graph = reasoner.bind(input.getGraph());
        boolean correct = graph.validate().isValid();
        long t2 = System.currentTimeMillis();
        lastTestDuration = t2 - t1;
        return correct;
    } else {
        for (StmtIterator i = test.listProperties(RDF.type); i.hasNext(); ) {
            System.out.println("Test type = " + i.nextStatement().getObject());
        }
        throw new ReasonerException("Unknown test type");
    }
}
Also used : FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) ReasonerException(org.apache.jena.reasoner.ReasonerException)

Example 64 with InfGraph

use of org.apache.jena.reasoner.InfGraph 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[] {});
}
Also used : Triple(org.apache.jena.graph.Triple) InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph) TransitiveReasoner(org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node)

Example 65 with InfGraph

use of org.apache.jena.reasoner.InfGraph in project jena by apache.

the class TestInfGraph method testInfGraph.

public void testInfGraph() {
    InfGraph ig = getInfGraph();
    assertSame(ig.getPrefixMapping(), ig.getRawGraph().getPrefixMapping());
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph)

Aggregations

InfGraph (org.apache.jena.reasoner.InfGraph)70 Reasoner (org.apache.jena.reasoner.Reasoner)36 Graph (org.apache.jena.graph.Graph)11 Triple (org.apache.jena.graph.Triple)10 Node (org.apache.jena.graph.Node)7 RDFNode (org.apache.jena.rdf.model.RDFNode)5 TransitiveReasoner (org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner)5 FBRuleInfGraph (org.apache.jena.reasoner.rulesys.FBRuleInfGraph)3 JenaException (org.apache.jena.shared.JenaException)3 Derivation (org.apache.jena.reasoner.Derivation)2 ReasonerException (org.apache.jena.reasoner.ReasonerException)2 TriplePattern (org.apache.jena.reasoner.TriplePattern)2 XSDDatatype (org.apache.jena.datatypes.xsd.XSDDatatype)1 XSDDateTime (org.apache.jena.datatypes.xsd.XSDDateTime)1 InfModel (org.apache.jena.rdf.model.InfModel)1 InfModelImpl (org.apache.jena.rdf.model.impl.InfModelImpl)1 BasicForwardRuleReasoner (org.apache.jena.reasoner.rulesys.BasicForwardRuleReasoner)1 BuiltinException (org.apache.jena.reasoner.rulesys.BuiltinException)1 GenericRuleReasoner (org.apache.jena.reasoner.rulesys.GenericRuleReasoner)1 Rule (org.apache.jena.reasoner.rulesys.Rule)1