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

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


     * @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


     * @throws SailException  in case the program already exists
     */
    public void addProgram(Program program) throws SailException {
        // store program in the database
        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            try {
                // should not throw an exception and the program should have a database ID afterwards
                connection.storeProgram(program);
                connection.commit();
            } finally {
                connection.close();
            }
        } catch (SQLException ex) {
            throw new SailException("cannot store program in database",ex);
        }

View Full Code Here

     */
    public void updateProgram(Program program) throws SailException {
        Set<Rule> added = new HashSet<Rule>();
        Set<Rule> removed = new HashSet<Rule>();
        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            try {
                // load old version of program and calculate difference
                Program old = connection.loadProgram(program.getName());
                if(old != null) {
                    for(Rule r : old.getRules()) {
                        if(!program.getRules().contains(r)) {
                            removed.add(r);
                        }
                    }
                    for(Rule r : program.getRules()) {
                        if(!old.getRules().contains(r)) {
                            added.add(r);
                        }
                    }

                }

                // store program in the database
                connection.updateProgram(program);
                connection.commit();
            } finally {
                connection.close();
            }
        } catch (SQLException ex) {
            throw new SailException("cannot store program in database",ex);
        }

View Full Code Here

     *
     * @return
     */
    public CloseableIteration<Program,SailException> listPrograms() throws SailException {
        try {
            final KiWiReasoningConnection connection = persistence.getConnection();

            return new ExceptionConvertingIteration<Program, SailException>(connection.listPrograms()) {
                /**
                 * Converts an exception from the underlying iteration to an exception of
                 * type <tt>X</tt>.
                 */
                @Override
                protected SailException convert(Exception e) {
                    return new SailException(e);
                }

                @Override
                protected void handleClose() throws SailException {
                    super.handleClose();

                    try {
                        connection.commit();
                        connection.close();
                    } catch (SQLException ex) {
                        throw new SailException("database error while committing/closing connection");
                    }
                }
            };
View Full Code Here

     * @return the parsed program, or null in case a program with the given name does not exist
     * @throws SailException  in case an error occurs
     */
    public Program getProgram(String name) throws SailException {
        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            try {
                // should not throw an exception and the program should have a database ID afterwards
                Program p = connection.loadProgram(name);
                connection.commit();
                return p;
            } finally {
                connection.close();
            }
        } catch (SQLException ex) {
            throw new SailException("cannot load program from database",ex);
        }
    }
View Full Code Here

     * @param name the unique name of the program to remove
     * @throws SailException
     */
    public void deleteProgram(String name) throws SailException {
        try {
            KiWiReasoningConnection connection = persistence.getConnection();
            try {
                Program p = connection.loadProgram(name);
                connection.deleteProgram(p);
                connection.commit();
            } finally {
                connection.close();
            }
        } catch (SQLException ex) {
            throw new SailException("cannot load program from database",ex);
        }
        engine.loadPrograms();
View Full Code Here

     * @return
     * @throws SailException
     */
    public CloseableIteration<Justification,SailException> justify(long tripleId) throws SailException {
        try {
            final KiWiReasoningConnection connection = persistence.getConnection();

            return new ExceptionConvertingIteration<Justification, SailException>(connection.listJustificationsForTriple(tripleId)) {
                /**
                 * Converts an exception from the underlying iteration to an exception of
                 * type <tt>X</tt>.
                 */
                @Override
                protected SailException convert(Exception e) {
                    return new SailException(e);
                }

                @Override
                protected void handleClose() throws SailException {
                    super.handleClose();

                    try {
                        connection.commit();
                        connection.close();
                    } catch (SQLException ex) {
                        throw new SailException("database error while committing/closing connection");
                    }
                }
            };
View Full Code Here

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

        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 | SQLException ex) {
                    log.error("REASONING ERROR: could not clean up unsupported triples, database state will be inconsistent! Message: {}", ex.getMessage());
                    log.debug("Exception details:", ex);
                    throw ex;
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.