Search in sources :

Example 36 with InfGraph

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

the class GenericRuleReasoner method bindSchema.

//  =======================================================================
//  Implementation methods
/**
     * Precompute the implications of a schema graph. The statements in the graph
     * will be combined with the data when the final InfGraph is created.
     */
@Override
public Reasoner bindSchema(Graph tbox) throws ReasonerException {
    if (schemaGraph != null) {
        throw new ReasonerException("Can only bind one schema at a time to a GenericRuleReasoner");
    }
    Graph graph = null;
    if (mode == FORWARD) {
        graph = new BasicForwardRuleInfGraph(this, rules, null, tbox);
        ((InfGraph) graph).prepare();
    } else if (mode == FORWARD_RETE) {
        graph = new RETERuleInfGraph(this, rules, null, tbox);
        ((InfGraph) graph).prepare();
    } else if (mode == BACKWARD) {
        graph = tbox;
    } else {
        List<Rule> ruleSet = rules;
        graph = new FBRuleInfGraph(this, ruleSet, getPreload(), tbox);
        if (enableTGCCaching)
            ((FBRuleInfGraph) graph).setUseTGCCache();
        ((FBRuleInfGraph) graph).prepare();
    }
    GenericRuleReasoner grr = new GenericRuleReasoner(rules, graph, factory, mode);
    grr.setDerivationLogging(recordDerivations);
    grr.setTraceOn(traceOn);
    grr.setTransitiveClosureCaching(enableTGCCaching);
    grr.setFunctorFiltering(filterFunctors);
    if (preprocessorHooks != null) {
        for (RulePreprocessHook preprocessorHook : preprocessorHooks) {
            grr.addPreprocessingHook(preprocessorHook);
        }
    }
    return grr;
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph) ReasonerException(org.apache.jena.reasoner.ReasonerException)

Example 37 with InfGraph

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

the class TDB method sync.

/** Sync a TDB-backed Graph. Do nothing if not TDB-backed. */
public static void sync(Graph graph) {
    if (graph == null)
        return;
    if (graph instanceof InfGraph) {
        InfGraph infGraph = (InfGraph) graph;
        sync(infGraph.getRawGraph());
        return;
    }
    syncObject(graph);
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph)

Example 38 with InfGraph

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

the class OWLWGTester method runTest.

/**
     * Run a single designated test.
     * @param test the root node descibing the test
     * @param log set to true to enable derivation logging
     * @param stats set to true to log performance statistics
     * @return true if the test passes
     * @throws IOException if one of the test files can't be found
     */
public boolean runTest(Resource test, boolean log, boolean stats) throws IOException {
    // Find the specification for the named test
    RDFNode testType = test.getRequiredProperty(RDF.type).getObject();
    if (!(testType.equals(NegativeEntailmentTest) || testType.equals(PositiveEntailmentTest))) {
        throw new JenaException("Can't find test: " + test);
    }
    String description = test.getRequiredProperty(descriptionP).getObject().toString();
    String status = test.getRequiredProperty(statusP).getObject().toString();
    logger.debug("WG test " + test.getURI() + " - " + status);
    // Load up the premise documents
    Model premises = ModelFactory.createDefaultModel();
    for (StmtIterator premisesI = test.listProperties(premiseDocumentP); premisesI.hasNext(); ) {
        premises.add(loadFile(premisesI.nextStatement().getObject().toString() + ".rdf"));
    }
    // Load up the conclusions document
    Resource conclusionsRes = (Resource) test.getRequiredProperty(conclusionDocumentP).getObject();
    Model conclusions = loadFile(conclusionsRes.toString() + ".rdf");
    // Optional logging
    if (log) {
        if (configuration == null) {
            Model m = ModelFactory.createDefaultModel();
            configuration = m.createResource();
        }
        configuration.addProperty(ReasonerVocabulary.PROPtraceOn, "true").addProperty(ReasonerVocabulary.PROPderivationLogging, "true");
    }
    Reasoner reasoner = reasonerF.create(configuration);
    long t1 = System.currentTimeMillis();
    InfGraph graph = reasoner.bind(premises.getGraph());
    Model result = ModelFactory.createModelForGraph(graph);
    if (stats && graph instanceof FBRuleInfGraph) {
    //            ((FBRuleInfGraph)graph).resetLPProfile(true);
    }
    // Check the results against the official conclusions
    boolean correct = true;
    if (testType.equals(PositiveEntailmentTest)) {
        correct = WGReasonerTester.testConclusions(conclusions.getGraph(), result.getGraph());
    } else {
        // A negative entailment check
        correct = !WGReasonerTester.testConclusions(conclusions.getGraph(), result.getGraph());
    }
    long t2 = System.currentTimeMillis();
    timeCost += (t2 - t1);
    numTests++;
    if (stats) {
        logger.info("Time=" + (t2 - t1) + "ms for " + test.getURI());
        printStats();
        if (graph instanceof FBRuleInfGraph) {
            ((FBRuleInfGraph) graph).printLPProfile();
        }
    }
    if (!correct) {
    // List all the forward deductions for debugging
    //            if (graph instanceof FBRuleInfGraph) {
    //                System.out.println("Error: deductions graph was ...");
    //                FBRuleInfGraph fbGraph = (FBRuleInfGraph)graph;
    //                Graph deductions = fbGraph.getDeductionsGraph();
    //                org.apache.jena.PrintUtil.printOut(deductions.find(null,null,null));
    //            }
    }
    // Signal the results        
    if (testcase != null) {
        Assert.assertTrue("Test: " + test + "\n" + reasonerF.getURI() + "\n" + description, correct);
    }
    return correct;
}
Also used : JenaException(org.apache.jena.shared.JenaException) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Example 39 with InfGraph

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

the class TestBackchainer method testRestriction3.

/**
     * Test restriction example
     */
public void testRestriction3() {
    Graph data = Factory.createGraphMem();
    data.add(new Triple(a, ty, r));
    data.add(new Triple(r, sC, C1));
    data.add(new Triple(C1, ty, OWL.Restriction.asNode()));
    data.add(new Triple(C1, OWL.onProperty.asNode(), p));
    data.add(new Triple(C1, OWL.allValuesFrom.asNode(), c));
    List<Rule> rules = Rule.parseRules("[-> (rdfs:subClassOf rdfs:range rdfs:Class)]" + //        "[-> (owl:Class rdfs:subClassOf rdfs:Class)]" +
    "[rdfs3:  bound(?c)   (?p rdfs:range ?c) (?x ?p ?y) -> (?y rdf:type ?c)]" + "[rdfs3:  unbound(?c) (?x ?p ?y), (?p rdfs:range ?c) -> (?y rdf:type ?c)]" + "[rdfs7:  (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]" + "[rdfs8:  (?a rdfs:subClassOf ?b) (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" + "[restrictionProc4b: bound(?Y) (?X ?P ?Y), notEqual(?P, rdf:type), (?X rdf:type all(?P, ?C)),-> (?Y rdf:type ?C)]" + "[restrictionProc4b: unbound(?Y), (?X rdf:type all(?P, ?C)), (?X ?P ?Y), notEqual(?P, rdf:type),-> (?Y rdf:type ?C)]" + "");
    Reasoner reasoner = createReasoner(rules);
    InfGraph infgraph = reasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(null, ty, c), new Object[] {});
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Example 40 with InfGraph

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

the class TestBackchainer method testBuiltin2.

/**
     * Test basic builtin usage.
     */
public void testBuiltin2() {
    Graph data = Factory.createGraphMem();
    data.add(new Triple(a, p, b));
    data.add(new Triple(a, q, c));
    List<Rule> rules = Rule.parseRules("[r1: (?x r ?y ) <- bound(?x), (?x p ?y) ]" + "[r2: (?x r ?y) <- unbound(?x), (?x q ?y)]");
    Reasoner reasoner = createReasoner(rules);
    InfGraph infgraph = reasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(a, r, null), new Object[] { new Triple(a, r, b) });
    TestUtil.assertIteratorValues(this, infgraph.find(null, r, null), new Object[] { new Triple(a, r, c) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

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