Search in sources :

Example 1 with ReasonerException

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

the class RETEConflictSet method execute.

/**
     * Execute a single rule firing. 
     */
public static void execute(RETERuleContext context, boolean isAdd) {
    Rule rule = context.getRule();
    BindingEnvironment env = context.getEnv();
    ForwardRuleInfGraphI infGraph = (ForwardRuleInfGraphI) context.getGraph();
    if (infGraph.shouldTrace()) {
        logger.info("Fired rule: " + rule.toShortString());
    }
    RETEEngine engine = context.getEngine();
    engine.incRuleCount();
    List<Triple> matchList = null;
    if (infGraph.shouldLogDerivations() && isAdd) {
        // Create derivation record
        matchList = new ArrayList<>(rule.bodyLength());
        for (int i = 0; i < rule.bodyLength(); i++) {
            Object clause = rule.getBodyElement(i);
            if (clause instanceof TriplePattern) {
                matchList.add(env.instantiate((TriplePattern) clause));
            }
        }
    }
    for (int i = 0; i < rule.headLength(); i++) {
        Object hClause = rule.getHeadElement(i);
        if (hClause instanceof TriplePattern) {
            Triple t = env.instantiate((TriplePattern) hClause);
            // that we can't record in RDF
            if (isAdd) {
                if (!context.contains(t)) {
                    engine.addTriple(t, true);
                    if (infGraph.shouldLogDerivations()) {
                        infGraph.logDerivation(t, new RuleDerivation(rule, t, matchList, infGraph));
                    }
                }
            } else {
                if (context.contains(t)) {
                    // Remove the generated triple
                    engine.deleteTriple(t, true);
                }
            }
        // }
        } else if (hClause instanceof Functor && isAdd) {
            Functor f = (Functor) hClause;
            Builtin imp = f.getImplementor();
            if (imp != null) {
                imp.headAction(f.getBoundArgs(env), f.getArgLength(), context);
            } else {
                throw new ReasonerException("Invoking undefined Functor " + f.getName() + " in " + rule.toShortString());
            }
        } else if (hClause instanceof Rule) {
            Rule r = (Rule) hClause;
            if (r.isBackward()) {
                if (isAdd) {
                    infGraph.addBRule(r.instantiate(env));
                } else {
                    infGraph.deleteBRule(r.instantiate(env));
                }
            } else {
                throw new ReasonerException("Found non-backward subrule : " + r);
            }
        }
    }
}
Also used : ReasonerException(org.apache.jena.reasoner.ReasonerException) Triple(org.apache.jena.graph.Triple) TriplePattern(org.apache.jena.reasoner.TriplePattern)

Example 2 with ReasonerException

use of org.apache.jena.reasoner.ReasonerException 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 3 with ReasonerException

use of org.apache.jena.reasoner.ReasonerException 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)

Aggregations

ReasonerException (org.apache.jena.reasoner.ReasonerException)3 InfGraph (org.apache.jena.reasoner.InfGraph)2 Graph (org.apache.jena.graph.Graph)1 Triple (org.apache.jena.graph.Triple)1 TriplePattern (org.apache.jena.reasoner.TriplePattern)1 FBRuleInfGraph (org.apache.jena.reasoner.rulesys.FBRuleInfGraph)1