Package org.apache.marmotta.kiwi.reasoner.persistence

Examples of org.apache.marmotta.kiwi.reasoner.persistence.KiWiReasoningConnection


     * @throws Exception
     */
    @Test
    public void testIncrementalReasoningMemory() throws Exception {
        RepositoryConnection con = repository.getConnection();
        KiWiReasoningConnection rcon = rpersistence.getConnection();
        try {
            con.begin();
            // create a triple (ex:a ex:symmetric ex:b); this should trigger rule 2 of the reasoner and add the
            // inverse relationship
            Resource subject  = con.getValueFactory().createURI(NS+"a");
            URI      property = con.getValueFactory().createURI(NS+"symmetric");
            Resource object   = con.getValueFactory().createURI(NS+"b");

            con.add(subject,property,object);
            con.commit();

            // load the statement from the connection so we can add it to the reasoner
            List<Statement> statements = Iterations.asList(con.getStatements(subject,property,object, false));
            Assert.assertEquals(1,statements.size());

            // add triple to engine
            TransactionData data = new TransactionData();
            data.getAddedTriples().add(statements.get(0));
            engine.afterCommit(data);

            // wait for reasoning to complete
            while(engine.isRunning()) {
                log.debug("sleeping for 100ms to let engine finish processing ... ");
                Thread.sleep(100);
            }
            con.begin();

            // after the engine completes, we check whether
            // 1) the expected inferred triple exists
            // 2) the inferred triple is properly justified (based on rule2 and on the triple contained in the transaction data)
            List<Statement> inferred = Iterations.asList(con.getStatements(object,property,subject, true));
            Assert.assertEquals("number of inferred triples differs from expected result",1,inferred.size());

            KiWiTriple triple = (KiWiTriple)inferred.get(0);
            List<Justification> justifications = Iterations.asList(rcon.listJustificationsForTriple(triple));
            Assert.assertEquals("number of justifications for triple differs from expected result",1,justifications.size());

            Justification j = justifications.get(0);
            Assert.assertEquals("number of supporting triples differs from expected result",1,j.getSupportingTriples().size());
            Assert.assertEquals("number of supporting rules differs from expected result",1,j.getSupportingRules().size());

            Assert.assertThat("supporting triple does not match expectation", j.getSupportingTriples(), hasItem((KiWiTriple)statements.get(0)));

            con.commit();


            // now remove again the base triple and inform the reasoner about it, as a consequence, the inferred
            // triple should also be removed
            con.remove(subject,property,object);
            con.commit();
            TransactionData data2 = new TransactionData();
            data2.getRemovedTriples().add(statements.get(0));
            engine.afterCommit(data2);

            // wait for reasoning to complete
            while(engine.isRunning()) {
                log.debug("sleeping for 100ms to let engine finish processing ... ");
                Thread.sleep(100);
            }
            con.begin();

            List<Statement> inferred2 = Iterations.asList(con.getStatements(object,property,subject, true));
            Assert.assertEquals("number of inferred triples differs from expected result", 0, inferred2.size());

            con.commit();
            rcon.commit();
        } finally {
            con.close();
            rcon.close();
        }
    }
View Full Code Here


     * @throws Exception
     */
    @Test
    public void testIncrementalReasoningConjunction() throws Exception {
        RepositoryConnection con = repository.getConnection();
        KiWiReasoningConnection rcon = rpersistence.getConnection();
        try {
            con.begin();
            // create a triple (ex:a ex:symmetric ex:b); this should trigger rule 2 of the reasoner and add the
            // inverse relationship
            Resource a  = con.getValueFactory().createURI(NS+"a");
            URI      property = con.getValueFactory().createURI(NS+"transitive");
            Resource b   = con.getValueFactory().createURI(NS+"b");
            Resource c   = con.getValueFactory().createURI(NS+"c");
            Resource d   = con.getValueFactory().createURI(NS+"d");

            con.add(a,property,b);
            con.add(b,property,c);
            con.commit();

            // load the statement from the connection so we can add it to the reasoner
            List<Statement> statements = Iterations.asList(con.getStatements(null,property,null, false));
            Assert.assertEquals(2,statements.size());

            // add triples to engine
            TransactionData data = new TransactionData();
            data.getAddedTriples().addAll(statements);
            engine.afterCommit(data);

            // wait for reasoning to complete
            while(engine.isRunning()) {
                log.debug("sleeping for 100ms to let engine finish processing ... ");
                Thread.sleep(100);
            }
            con.begin();

            // after the engine completes, we check whether
            // 1) the expected inferred triple exists (must be (ex:a ex:transitive ex:c) )
            // 2) the inferred triple is properly justified (based on rule2 and on the triple contained in the transaction data)
            List<Statement> inferred = Iterations.asList(con.getStatements(a,property,c, true));
            Assert.assertEquals("number of inferred triples differs from expected result",1,inferred.size());

            KiWiTriple triple = (KiWiTriple)inferred.get(0);
            List<Justification> justifications = Iterations.asList(rcon.listJustificationsForTriple(triple));
            Assert.assertEquals("number of justifications for triple differs from expected result",1,justifications.size());

            Justification j = justifications.get(0);
            Assert.assertEquals("number of supporting triples differs from expected result",2,j.getSupportingTriples().size());
            Assert.assertEquals("number of supporting rules differs from expected result",1,j.getSupportingRules().size());

            Assert.assertThat("supporting triple does not match expectation", j.getSupportingTriples(), hasItem((KiWiTriple)statements.get(0)));
            Assert.assertThat("supporting triple does not match expectation", j.getSupportingTriples(), hasItem((KiWiTriple)statements.get(1)));

            con.commit();


            // add another triple and check if the incremental reasoning works
            con.add(c,property,d);
            con.commit();

            // load the statement from the connection so we can add it to the reasoner
            List<Statement> statements2 = Iterations.asList(con.getStatements(c,property,d, false));
            Assert.assertEquals(1, statements2.size());

            // add triples to engine
            TransactionData data2 = new TransactionData();
            data2.getAddedTriples().addAll(statements2);
            engine.afterCommit(data2);

            // wait for reasoning to complete
            while(engine.isRunning()) {
                log.debug("sleeping for 100ms to let engine finish processing ... ");
                Thread.sleep(100);
            }
            con.begin();


            // after the engine completes, we check whether the expected inferred triples exist
            // (must be (ex:a ex:transitive ex:d) and (ex:b ex:transitive ex:d) )
            List<Statement> inferred2 = Iterations.asList(con.getStatements(a,property,d, true));
            Assert.assertEquals("number of inferred triples differs from expected result", 1, inferred2.size());

            List<Statement> inferred3 = Iterations.asList(con.getStatements(b,property,d, true));
            Assert.assertEquals("number of inferred triples differs from expected result", 1, inferred3.size());


            // now remove again the base triple and inform the reasoner about it, as a consequence, the inferred
            // triple should also be removed
            con.remove(c, property, d);
            con.commit();
            TransactionData data3 = new TransactionData();
            data3.getRemovedTriples().add(statements2.get(0));
            engine.afterCommit(data3);

            // wait for reasoning to complete
            while(engine.isRunning()) {
                log.debug("sleeping for 100ms to let engine finish processing ... ");
                Thread.sleep(100);
            }
            con.begin();

            List<Statement> inferred4 = Iterations.asList(con.getStatements(a,property,d, true));
            Assert.assertEquals("number of inferred triples differs from expected result", 0, inferred4.size());

            List<Statement> inferred5 = Iterations.asList(con.getStatements(b,property,d, true));
            Assert.assertEquals("number of inferred triples differs from expected result", 0, inferred5.size());

            con.commit();
            rcon.commit();
        } finally {
            con.close();
            rcon.close();
        }
    }
View Full Code Here

     * @throws Exception
     */
    @Test
    public void testFullReasoning() throws Exception {
        RepositoryConnection con = repository.getConnection();
        KiWiReasoningConnection rcon = rpersistence.getConnection();
        try {
            // add some triples
            con.begin();

            Resource a   = con.getValueFactory().createURI(NS+"a");
            Resource b   = con.getValueFactory().createURI(NS+"b");
            Resource c   = con.getValueFactory().createURI(NS+"c");
            Resource d   = con.getValueFactory().createURI(NS+"d");
            URI      t   = con.getValueFactory().createURI(NS+"transitive");
            URI      s   = con.getValueFactory().createURI(NS+"symmetric");

            con.add(this.getClass().getResourceAsStream("simple.ttl"),"http://localhost/resource/", RDFFormat.TURTLE);
            con.commit();

            // run the full reasoner
            engine.reRunPrograms();

            // wait for reasoning to complete
            while(engine.isRunning()) {
                log.debug("sleeping for 100ms to let engine finish processing ... ");
                Thread.sleep(100);
            }
            con.begin();

            // after reasoning is finished, we expect to find the following triples:

            Assert.assertTrue("expected inferred triple not found", con.hasStatement(a,t,c,true));
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(b,t,d,true));
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(b,s,a,true));

            con.commit();
        } finally {
            con.close();
            rcon.close();
        }

    }
View Full Code Here

     * @throws Exception
     */
    //@Test
    public void testAddRemoveRule() throws Exception {
        RepositoryConnection con = repository.getConnection();
        KiWiReasoningConnection rcon = rpersistence.getConnection();
        try {
            // add some triples
            con.begin();

            Resource a   = con.getValueFactory().createURI(NS+"a");
            Resource b   = con.getValueFactory().createURI(NS+"b");
            Resource c   = con.getValueFactory().createURI(NS+"c");
            Resource d   = con.getValueFactory().createURI(NS+"d");
            URI      t   = con.getValueFactory().createURI(NS+"transitive");
            URI      s   = con.getValueFactory().createURI(NS+"symmetric");

            con.add(this.getClass().getResourceAsStream("simple.ttl"),"http://localhost/resource/", RDFFormat.TURTLE);
            con.commit();

            // run the full reasoner
            engine.reRunPrograms();

            // wait for reasoning to complete
            while(engine.isRunning()) {
                log.debug("sleeping for 100ms to let engine finish processing ... ");
                Thread.sleep(100);
            }
            con.begin();

            // after reasoning is finished, we expect to find the following triples:

            Assert.assertTrue("expected inferred triple not found", con.hasStatement(a,t,c,true));
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(b,t,d,true));
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(b,s,a,true));

            con.commit();


            // now we remove the rule2 (symmetric rule) from the program, update the program in the database and then
            // inform the reasoning engine about the removed rule
            Program p = rcon.loadProgram("simple");
            Rule removed = null;
            Iterator<Rule> it = p.getRules().iterator();
            while(it.hasNext()) {
                Rule r = it.next();
                if(r.getName().equals("rule2")) {
                    it.remove();
                    removed = r;
                }
            }
            Assert.assertNotNull("rule 2 not found in program", removed);
            rcon.updateProgram(p);
            rcon.commit();

            engine.notifyRemoveRules();

            // after removing, the inferred symmetric triple should be gone, but the others should still exist
            con.begin();
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(a,t,c,true));
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(b, t, d, true));
            Assert.assertFalse("unexpected inferred triple found", con.hasStatement(b, s, a, true));
            con.commit();


            // let's add the rule again to the program, update the database, and inform the engine
            p.getRules().add(removed);
            rcon.updateProgram(p);
            rcon.commit();

            engine.notifyAddRule(removed);

            // after adding, the inferred symmetric triple should again be present
            con.begin();
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(a,t,c,true));
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(b,t,d,true));
            Assert.assertTrue("expected inferred triple not found", con.hasStatement(b, s, a, true));
            con.commit();

        } finally {
            con.close();
            rcon.close();
        }

    }
View Full Code Here

    public void loadPrograms() {
        log.info("program configuration changed, reloading ...");
        patternRuleMap = new HashMap<Pattern, Rule>();

        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            try {
                programs       = Iterations.asList(connection.listPrograms());

                for(Program p : programs) {
                    for(Rule rule : p.getRules()) {
                        for(Pattern pattern : rule.getBody()) {
                            patternRuleMap.put(pattern,rule);
                        }
                    }
                }
            } finally {
                connection.close();
            }
        } catch (SQLException ex) {
            programs = Collections.emptyList();
            log.warn("cannot load reasoning programs, reasoning disabled (error message: {})", ex.getMessage());
        }
View Full Code Here

        // clean up justifications depending on the rule
        updateTaskStatus("cleaning up unsupported triples");


        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            try {

                // this is done automatically now when updating or deleting a program:
                // removeJustificationsByQuery("reasoner.listJustificationsByRule", ImmutableMap.of("rule", (Object) rule));

                // then remove all inferred triples that are no longer supported
                cleanupUnsupported(connection);

                // and finally garbage collect those triples that are inferred and deleted
                // garbage collection is now carried out by a thread in the triple store
                //garbageCollectTriples();
            } catch (SQLException ex) {
                connection.rollback();
                throw ex;
            } finally {
                connection.close();
            }
        } catch (SailException ex) {
            log.error("REPOSITORY ERROR: could not clean up unsupported triples, database state will be inconsistent! Message: {}", ex.getMessage());
            log.debug("Exception details:", ex);
        } catch (SQLException ex) {
View Full Code Here

        }

        if(data.getRemovedTriples().size() > 0) {
            log.debug("cleaning up justifications and inferences for {} triples",data.getRemovedTriples().size());
            try {
                KiWiReasoningConnection connection = persistence.getConnection();
                try {
                    // first clean up justifications that are no longer supported
                    cleanupJustifications(connection, data.getRemovedTriples());


                    // then remove all inferred triples that are no longer supported
                    cleanupUnsupported(connection);

                    // and finally garbage collect those triples that are inferred and deleted
                    // garbage collection is now carried out by a thread in the triple store
                    //garbageCollectTriples();
                    connection.commit();
                } catch (SQLException ex) {
                    connection.rollback();
                    throw ex;
                } finally {
                    connection.close();
                }
            } catch (SailException ex) {
                log.error("REPOSITORY ERROR: could not clean up unsupported triples, database state will be inconsistent! Message: {}", ex.getMessage());
                log.debug("Exception details:", ex);
            } catch (SQLException ex) {
View Full Code Here

        // clean up all justifications
        updateTaskStatus("removing old justifications");


        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            try {

                // remove all justifications from the database
                connection.deleteJustifications();

                // clean up inferred triples by removing them from the triple store; the transaction system should take care of the rest
                cleanupUnsupported(connection);

                // and finally garbage collect those triples that are inferred and deleted
                // garbage collection is now carried out by a thread in the triple store
                //garbageCollectTriples();
                connection.commit();
            } catch (SQLException ex) {
                connection.rollback();
                throw ex;
            } finally {
                connection.close();
            }
        } catch (SailException ex) {
            log.error("REPOSITORY ERROR: could not clean up unsupported triples, database state will be inconsistent! Message: {}", ex.getMessage());
            log.debug("Exception details:", ex);
        } catch (SQLException ex) {
View Full Code Here

        // persist the justifications that have been created in the rule processing
        long counter = 0;
        updateTaskStatus("storing new justifications ...");

        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            try {
                connection.storeJustifications(justifications);
                connection.commit();
            } catch (SQLException ex) {
                connection.rollback();
                throw ex;
            } finally {
                connection.close();
            }
        } catch (SQLException ex) {
            log.error("DATABASE ERROR: could not add new justifications for triples, database state will be inconsistent! Message: {}",ex.getMessage());
            log.debug("Exception details:",ex);
        }
View Full Code Here

        }

        CloseableIteration<QueryResult, SQLException> bodyResult;

        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            SailConnection     sail = store.getConnection();
            KiWiSailConnection isail = getWrappedConnection(sail);
            try {

                // if there are further patterns, evaluate them; if the matched pattern was the only pattern, then
                // simply take the match as binding
                if(body.size() > 0) {
                    bodyResult = connection.query(body,match,null,null,true);
                } else if(match != null) {
                    bodyResult = new SingletonIteration<QueryResult, SQLException>(match);
                } else {
                    bodyResult = new EmptyIteration<QueryResult, SQLException>();
                }

                // construct triples out of the bindings and the rule heads
                long counter = 0;

                // initialise a new set of justifications
                Set<Justification> justifications = new HashSet<Justification>();

                sail.begin();
                while(bodyResult.hasNext()) {
                    QueryResult row = bodyResult.next();
                    Map<VariableField,KiWiNode> binding = row.getBindings();

                    Resource subject = null;
                    URI property = null;
                    Value object;

                    if(rule.getHead().getSubject() != null && rule.getHead().getSubject().isVariableField()) {
                        if(!binding.get(rule.getHead().getSubject()).isUriResource() && !binding.get(rule.getHead().getSubject()).isAnonymousResource()) {
                            log.info("cannot use value {} as subject, because it is not a resource",binding.get(rule.getHead().getSubject()));
                            continue;
                        }
                        subject = (KiWiResource)binding.get(rule.getHead().getSubject());
                    } else if(rule.getHead().getSubject() != null && rule.getHead().getSubject().isResourceField()) {
                        subject = ((ResourceField)rule.getHead().getSubject()).getResource();
                    } else
                        throw new IllegalArgumentException("Subject of rule head may only be a variable or a resource; rule: "+rule);

                    if(rule.getHead().getProperty() != null && rule.getHead().getProperty().isVariableField()) {
                        if(!binding.get(rule.getHead().getProperty()).isUriResource()) {
                            log.info("cannot use value {} as property, because it is not a URI resource",binding.get(rule.getHead().getProperty()));
                            continue;
                        }
                        property = (KiWiUriResource)binding.get(rule.getHead().getProperty());
                    } else if(rule.getHead().getProperty() != null && rule.getHead().getProperty().isResourceField()) {
                        property = (KiWiUriResource)((ResourceField)rule.getHead().getProperty()).getResource();
                    } else
                        throw new IllegalArgumentException("Property of rule head may only be a variable or a resource; rule: "+rule);

                    if(rule.getHead().getObject() != null && rule.getHead().getObject().isVariableField()) {
                        object = binding.get(rule.getHead().getObject());
                    } else if(rule.getHead().getObject() != null && rule.getHead().getObject().isResourceField()) {
                        object = ((ResourceField)rule.getHead().getObject()).getResource();
                    } else if(rule.getHead().getObject() != null && rule.getHead().getObject().isLiteralField()) {
                        object = ((LiteralField)rule.getHead().getObject()).getLiteral();
                    } else
                        throw new IllegalArgumentException("Object of rule head may only be a variable, a literal, or a resource; rule: "+rule);


                    KiWiTriple triple = isail.addInferredStatement(subject, property, object);

                    Justification justification = new Justification();
                    justification.setTriple(triple);
                    justification.getSupportingRules().add(rule);
                    justification.getSupportingTriples().addAll(row.getJustifications());
                    justifications.add(justification);

                    // when the batch size is reached, commit the transaction, save the justifications, and start a new
                    // transaction and new justification set
                    if(++counter % config.getBatchSize() == 0) {
                        persistenceLock.lock();

                        try {

                            sail.commit();

                            log.debug("adding {} justifications",justifications.size());

                            updateTaskStatus("storing justifications ...");
                            Set<Justification> baseJustifications = getBaseJustifications(connection,justifications);

                            if(config.isRemoveDuplicateJustifications()) {
                                removeDuplicateJustifications(connection,baseJustifications);
                            }

                            log.debug("{} justifications added after resolving inferred triples", baseJustifications.size());

                            // persist the justifications that have been created in the rule processing
                            if(baseJustifications.size() > 0) {
                                connection.storeJustifications(baseJustifications);
                            }
                            connection.commit();
                            sail.begin();
                        } finally {
                            persistenceLock.unlock();
                        }
                        justifications.clear();
                    }
                }

                persistenceLock.lock();
                try {
                    sail.commit();

                    log.debug("adding {} justifications",justifications.size());
                    updateTaskStatus("storing justifications ...");
                    Set<Justification> baseJustifications = getBaseJustifications(connection,justifications);

                    if(config.isRemoveDuplicateJustifications()) {
                        removeDuplicateJustifications(connection,baseJustifications);
                    }

                    // persist the justifications that have been created in the rule processing
                    if(baseJustifications.size() > 0) {
                        connection.storeJustifications(baseJustifications);
                    }

                    log.debug("{} justifications added after resolving inferred triples", baseJustifications.size());

                    Iterations.closeCloseable(bodyResult);
                    connection.commit();
                } finally {
                    persistenceLock.unlock();
                }
            } catch(SailException ex) {
                connection.rollback();
                sail.rollback();
                throw ex;
            } catch(SQLException ex) {
                sail.rollback();
                connection.rollback();
                throw ex;
            } finally {
                connection.close();
                sail.close();
            }

        } catch(SQLException ex) {
            log.error("DATABASE ERROR: could not process rule, database state will be inconsistent! Message: {}",ex.getMessage());
View Full Code Here

TOP

Related Classes of org.apache.marmotta.kiwi.reasoner.persistence.KiWiReasoningConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.