Search in sources :

Example 31 with InfGraph

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

the class TestFBRules method testRegexNulls.

/**
     * Test regex handling of null groups
     */
public void testRegexNulls() {
    String rules = "[r2: (?x p ?y) regex(?y, '((Boys)|(Girls))(.*)', ?m1, ?m2, ?m3, ?m4) ->  (?x q ?m2) (?x r ?m3) (?x s ?m4) ] \n" + "";
    Graph data = Factory.createGraphMem();
    data.add(new Triple(n1, p, NodeFactory.createLiteral("Girls44")));
    InfGraph infgraph = createInfGraph(rules, data);
    infgraph.prepare();
    TestUtil.assertIteratorValues(this, infgraph.getDeductionsGraph().find(null, null, null), new Triple[] { new Triple(n1, q, NodeFactory.createLiteral("")), new Triple(n1, r, NodeFactory.createLiteral("Girls")), new Triple(n1, s, NodeFactory.createLiteral("44")) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph)

Example 32 with InfGraph

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

the class TestFBRules method testRemoveBuiltin.

/**
     * The the "remove" builtin
     */
public void testRemoveBuiltin() {
    String rules = "[rule1: (?x p ?y), (?x q ?y) -> remove(0)]" + "";
    InfGraph infgraph = createInfGraph(rules);
    infgraph.add(new Triple(n1, p, Util.makeIntNode(1)));
    infgraph.add(new Triple(n1, p, Util.makeIntNode(2)));
    infgraph.add(new Triple(n1, q, Util.makeIntNode(2)));
    TestUtil.assertIteratorValues(this, infgraph.find(n1, null, null), new Triple[] { new Triple(n1, p, Util.makeIntNode(1)), new Triple(n1, q, Util.makeIntNode(2)) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph)

Example 33 with InfGraph

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

the class TestFBRules method testHybridRDFS2.

/**
     * Test example hybrid rules for rdfs.
     */
public void testHybridRDFS2() {
    Graph data = Factory.createGraphMem();
    data.add(new Triple(a, p, b));
    data.add(new Triple(p, sP, r));
    data.add(new Triple(r, RDFS.range.asNode(), C1));
    String rules = "[rdfs3:  (?p rdfs:range ?c)  -> [(?y rdf:type ?c) <- (?x ?p ?y)] ]" + "[rdfs6:  (?p rdfs:subPropertyOf ?q) -> [ (?a ?q ?b) <- (?a ?p ?b)] ]";
    InfGraph infgraph = createInfGraph(rules, data);
    //        ((FBRuleInfGraph)infgraph).setTraceOn(true);
    TestUtil.assertIteratorValues(this, infgraph.find(b, ty, C1), new Object[] { new Triple(b, ty, C1) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph)

Example 34 with InfGraph

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

the class TestFBRules method testSchemaBinding.

/**
     * Test schmea partial binding machinery, forward subset.
     */
public void testSchemaBinding() {
    String rules = "[testRule1: (n1 p ?a) -> (n2, p, ?a)]" + "[testRule2: (n1 q ?a) -> (n2, q, ?a)]" + "[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]" + "[testBRule4: (n3 p ?a) <- (n1, p, ?a)]";
    List<Rule> ruleList = Rule.parseRules(rules);
    Graph schema = Factory.createGraphMem();
    schema.add(new Triple(n1, p, n3));
    Graph data = Factory.createGraphMem();
    data.add(new Triple(n1, q, n4));
    data.add(new Triple(n1, q, n3));
    Reasoner reasoner = createReasoner(ruleList);
    Reasoner boundReasoner = reasoner.bindSchema(schema);
    InfGraph infgraph = boundReasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Triple[] { new Triple(n1, p, n3), new Triple(n2, p, n3), new Triple(n3, p, n3), new Triple(n1, q, n4), new Triple(n2, q, n4), new Triple(n1, q, n3), new Triple(n2, q, n3), new Triple(res, p, n3) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Example 35 with InfGraph

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

the class GenericRuleReasoner method bind.

/**
     * Attach the reasoner to a set of RDF data to process.
     * The reasoner may already have been bound to specific rules or ontology
     * axioms (encoded in RDF) through earlier bindRuleset calls.
     * 
     * @param data the RDF data to be processed, some reasoners may restrict
     * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
     * @return an inference graph through which the data+reasoner can be queried.
     * @throws ReasonerException if the data is ill-formed according to the
     * constraints imposed by this reasoner.
     */
@Override
public InfGraph bind(Graph data) throws ReasonerException {
    Graph schemaArg = schemaGraph == null ? getPreload() : schemaGraph;
    InfGraph graph = null;
    if (mode == FORWARD) {
        graph = new BasicForwardRuleInfGraph(this, rules, schemaArg);
        ((BasicForwardRuleInfGraph) graph).setTraceOn(traceOn);
    } else if (mode == FORWARD_RETE) {
        graph = new RETERuleInfGraph(this, rules, schemaArg);
        ((BasicForwardRuleInfGraph) graph).setTraceOn(traceOn);
        ((BasicForwardRuleInfGraph) graph).setFunctorFiltering(filterFunctors);
    } else if (mode == BACKWARD) {
        graph = new LPBackwardRuleInfGraph(this, getBruleStore(), data, schemaArg);
        ((LPBackwardRuleInfGraph) graph).setTraceOn(traceOn);
    } else {
        List<Rule> ruleSet = ((FBRuleInfGraph) schemaArg).getRules();
        FBRuleInfGraph fbgraph = new FBRuleInfGraph(this, ruleSet, schemaArg);
        graph = fbgraph;
        if (enableTGCCaching)
            fbgraph.setUseTGCCache();
        fbgraph.setTraceOn(traceOn);
        fbgraph.setFunctorFiltering(filterFunctors);
        if (preprocessorHooks != null) {
            for (RulePreprocessHook preprocessorHook : preprocessorHooks) {
                fbgraph.addPreprocessingHook(preprocessorHook);
            }
        }
    }
    graph.setDerivationLogging(recordDerivations);
    graph.rebind(data);
    return graph;
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph)

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