Package org.apache.marmotta.kiwi.reasoner.model.program

Examples of org.apache.marmotta.kiwi.reasoner.model.program.Program


     */
    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();
            }
View Full Code Here


        //    2a) check if description has been updated and store it if necessary in the database
        //    2b) check if namespaces have been removed, and remove them if necessary from the database
        //    2c) check if namespaces have been added, and add them if necessary to the database
        //    2d) check if rules have been removed, and remove them if necessary from the database
        //    2e) check if rules have been added, and add them if necessary to the database
        Program old = loadProgram(program.getName());
        if(old == null) {
            storeProgram(program);
        } else {
            //    2a) check if description has been updated and store it if necessary in the database
            if( (old.getDescription() != null && !old.getDescription().equals(program.getDescription())) ||
                    (old.getDescription() == null && program.getDescription() != null)) {
                PreparedStatement updateProgramDescription = getPreparedStatement("programs.update_desc");
                synchronized (updateProgramDescription) {
                    updateProgramDescription.setString(1, program.getDescription());
                    updateProgramDescription.setLong(2, old.getId());
                    updateProgramDescription.executeUpdate();
                }
            }

            //    2b) check if namespaces have been removed, and remove them if necessary from the database
            PreparedStatement deleteProgramNS = getPreparedStatement("programs.delete_ns");
            synchronized (deleteProgramNS) {
                deleteProgramNS.clearBatch();
                for(Map.Entry<String,String> oldNS : old.getNamespaces().entrySet()) {
                    if(!program.getNamespaces().entrySet().contains(oldNS)) {
                        deleteProgramNS.setLong(1,old.getId());
                        deleteProgramNS.setString(2,oldNS.getKey());
                        deleteProgramNS.setString(3,oldNS.getValue());
                        deleteProgramNS.addBatch();
                    }
                }
                deleteProgramNS.executeBatch();
            }

            //    2c) check if namespaces have been added, and add them if necessary to the database
            PreparedStatement addProgramNS = getPreparedStatement("programs.add_ns");
            synchronized (addProgramNS) {
                addProgramNS.clearBatch();
                for(Map.Entry<String,String> newNS : program.getNamespaces().entrySet()) {
                    if(!old.getNamespaces().entrySet().contains(newNS)) {
                        addProgramNS.setLong(1,old.getId());
                        addProgramNS.setString(2, newNS.getKey());
                        addProgramNS.setString(3, newNS.getValue());
                        addProgramNS.addBatch();
                    }
                }
                addProgramNS.executeBatch();
            }

            //    2d) check if rules have been removed, and remove them if necessary from the database
            PreparedStatement deleteProgramRule = getPreparedStatement("programs.delete_rule");
            PreparedStatement deleteRule        = getPreparedStatement("rules.delete_by_id");
            synchronized (deleteProgramRule) {
                deleteProgramRule.clearBatch();
                deleteRule.clearBatch();
                for(Rule oldRule : old.getRules()) {
                    if(!program.getRules().contains(oldRule)) {
                        deleteProgramRule.setLong(1,old.getId());
                        deleteProgramRule.setLong(2,oldRule.getId());
                        deleteProgramRule.addBatch();

                        deleteRule.setLong(1,oldRule.getId());
                        deleteRule.addBatch();

                        deleteJustifications(oldRule);

                        oldRule.setId(null);
                    }
                }
                deleteProgramRule.executeBatch();
                deleteRule.executeBatch();
            }

            //    2e) check if rules have been added, and add them if necessary to the database
            // first create a new entry in the rules table
            PreparedStatement insertRule = getPreparedStatement("rules.insert");
            PreparedStatement addProgramRule = getPreparedStatement("programs.add_rule");
            synchronized (insertRule) {
                insertRule.clearBatch();
                addProgramRule.clearBatch();

                for(Rule rule : program.getRules()) {
                    if(!old.getRules().contains(rule)) {
                        rule.setId(getNextSequence("seq.rules"));

                        insertRule.setLong(1, rule.getId());
                        insertRule.setString(2, rule.getName());
                        insertRule.setString(3, rule.getDescription());
                        insertRule.setString(4, rule.toString(program.getNamespaces()));
                        insertRule.addBatch();

                        addProgramRule.setLong(1,old.getId());
                        addProgramRule.setLong(2,rule.getId());
                        addProgramRule.addBatch();
                    }
                }
                insertRule.executeBatch();
View Full Code Here

        }

    }

    protected Program constructProgramFromDatabase(ResultSet row) throws SQLException, ParseException {
        Program program = new Program();
        program.setId(row.getLong("id"));
        program.setName(row.getString("name"));
        program.setDescription(row.getString("description"));

        // load namespaces
        PreparedStatement loadProgramNS = getPreparedStatement("namespaces.load_by_program");
        synchronized (loadProgramNS) {
            loadProgramNS.setLong(1, program.getId());
            ResultSet nsResult = loadProgramNS.executeQuery();
            while(nsResult.next()) {
                program.addNamespace(nsResult.getString("ns_prefix"), nsResult.getString("ns_uri"));
            }
            nsResult.close();
        }

        // load rules
        PreparedStatement loadRule = getPreparedStatement("rules.load_by_program");
        synchronized (loadRule) {
            loadRule.setLong(1,program.getId());
            ResultSet ruleResult = loadRule.executeQuery();
            while(ruleResult.next()) {
                program.addRule(constructRuleFromDatabase(ruleResult,program.getNamespaces()));
            }
            ruleResult.close();
        }

        return program;
View Full Code Here


        // first, load a sample program (it does not really matter what it actually contains, since we are not really
        // running the reasoner)
        KWRLProgramParserBase parser = new KWRLProgramParser(v, this.getClass().getResourceAsStream("test-001.kwrl"));
        Program p = parser.parseProgram();
        p.setName("test-001");

        KiWiReasoningConnection connection = rpersistence.getConnection();
        try {
            // should not throw an exception and the program should have a database ID afterwards
            connection.storeProgram(p);
            connection.commit();
        } finally {
            connection.close();
        }

        // then get a connection to the repository and create a number of triples, some inferred and some base
        RepositoryConnection con = repository.getConnection();
        try {
            con.add(s1,p1,o1);
            con.add(s2,p1,o2);
            con.add(s3,p1,o3);

            con.add(s1,p2,o1,ctxi);
            con.add(s2,p2,o2,ctxi);
            con.add(s3,p2,o3,ctxi);

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

        connection = rpersistence.getConnection();
        try {
            // retrieve the persisted triples and put them into two sets to build justifications
            List<Statement> baseTriples = asList(connection.listTriples(null,null,null,v.convert(ctxb),false, true));
            List<Statement> infTriples = asList(connection.listTriples(null,null,null,v.convert(ctxi),true, true));

            Assert.assertEquals("number of base triples was not 3", 3, baseTriples.size());
            Assert.assertEquals("number of inferred triples was not 3", 3, infTriples.size());

            // we manually update the "inferred" flag for all inferred triples, since this is not possible through the
            // repository API
            PreparedStatement updateInferred = connection.getJDBCConnection().prepareStatement("UPDATE triples SET inferred = true WHERE id = ?");
            for(Statement stmt : infTriples) {
                KiWiTriple triple = (KiWiTriple)stmt;
                updateInferred.setLong(1,triple.getId());
                updateInferred.addBatch();
            }
            updateInferred.executeBatch();
            updateInferred.close();

            // now we create some justifications for the inferred triples and store them
            Set<Justification> justifications = new HashSet<Justification>();
            Justification j1 = new Justification();
            j1.getSupportingRules().add(p.getRules().get(0));
            j1.getSupportingRules().add(p.getRules().get(1));
            j1.getSupportingTriples().add((KiWiTriple) baseTriples.get(0));
            j1.getSupportingTriples().add((KiWiTriple) baseTriples.get(1));
            j1.setTriple((KiWiTriple) infTriples.get(0));
            justifications.add(j1);

            Justification j2 = new Justification();
            j2.getSupportingRules().add(p.getRules().get(1));
            j2.getSupportingTriples().add((KiWiTriple) baseTriples.get(1));
            j2.getSupportingTriples().add((KiWiTriple) baseTriples.get(2));
            j2.setTriple((KiWiTriple) infTriples.get(1));
            justifications.add(j2);

            connection.storeJustifications(justifications);
            connection.commit();

            // we should now have two justifications in the database
            PreparedStatement listJustifications = connection.getJDBCConnection().prepareStatement("SELECT count(*) AS count FROM reasoner_justifications");
            ResultSet resultListJustifications = listJustifications.executeQuery();

            Assert.assertTrue(resultListJustifications.next());
            Assert.assertEquals(2, resultListJustifications.getInt("count"));
            resultListJustifications.close();
            connection.commit();

            PreparedStatement listSupportingTriples = connection.getJDBCConnection().prepareStatement("SELECT count(*) AS count FROM reasoner_just_supp_triples");
            ResultSet resultListSupportingTriples = listSupportingTriples.executeQuery();

            Assert.assertTrue(resultListSupportingTriples.next());
            Assert.assertEquals(4, resultListSupportingTriples.getInt("count"));
            resultListSupportingTriples.close();
            connection.commit();

            PreparedStatement listSupportingRules = connection.getJDBCConnection().prepareStatement("SELECT count(*) AS count FROM reasoner_just_supp_rules");
            ResultSet resultListSupportingRules = listSupportingRules.executeQuery();

            Assert.assertTrue(resultListSupportingRules.next());
            Assert.assertEquals(3, resultListSupportingRules.getInt("count"));
            resultListSupportingRules.close();
            connection.commit();



            // *** check listing justifications by base triple (supporting triple)

            // there should now be two justifications based on triple baseTriples.get(1))
            List<Justification> supported1 = asList(connection.listJustificationsBySupporting((KiWiTriple) baseTriples.get(1)));
            Assert.assertEquals("number of justifications is wrong",2,supported1.size());
            Assert.assertThat("justifications differ", supported1, hasItems(j1,j2));

            // only j1 should be supported by triple baseTriples.get(0))
            List<Justification> supported2 = asList(connection.listJustificationsBySupporting((KiWiTriple) baseTriples.get(0)));
            Assert.assertEquals("number of justifications is wrong", 1, supported2.size());
            Assert.assertThat("justifications differ", supported2, allOf(hasItem(j1), not(hasItem(j2))));

            // only j2 should be supported by triple baseTriples.get(2))
            List<Justification> supported3 = asList(connection.listJustificationsBySupporting((KiWiTriple) baseTriples.get(2)));
            Assert.assertEquals("number of justifications is wrong", 1, supported3.size());
            Assert.assertThat("justifications differ", supported3, allOf(hasItem(j2), not(hasItem(j1))));

            // *** check listing justificatoins by supporting rule

            // there should now be two justifications based on triple p.getRules().get(1)
            List<Justification> supported4 = asList(connection.listJustificationsBySupporting(p.getRules().get(1)));
            Assert.assertEquals("number of justifications is wrong", 2, supported4.size());
            Assert.assertThat("justifications differ", supported4, hasItems(j1,j2));

            // only j1 should be supported by triple p.getRules().get(0)
            List<Justification> supported5 = asList(connection.listJustificationsBySupporting(p.getRules().get(0)));
            Assert.assertEquals("number of justifications is wrong", 1, supported5.size());
            Assert.assertThat("justifications differ", supported5, allOf(hasItem(j1), not(hasItem(j2))));


            // *** check listing justifications by supported (inferred) triple
View Full Code Here

    @GET
    @Path("/{name}")
    @Produces("text/plain")
    public Response getProgram(@PathParam("name") String name) {
        try {
            Program program = provider.getProgram(name);
            if (program == null) return Response.status(404).entity("Could not find program with name '" + name + "'").build();
            return Response.ok(program.toString()).build();
        } catch (Exception e) {
            return Response.serverError().entity(e.getMessage()).build();
        }
    }
View Full Code Here

    }

    @Test
    public void testStoreLoadProgram() throws Exception {
        KWRLProgramParserBase parser = new KWRLProgramParser(repository.getValueFactory(), this.getClass().getResourceAsStream("test-001.kwrl"));
        Program p = parser.parseProgram();
        p.setName("test-001");

        KiWiReasoningConnection connection = rpersistence.getConnection();
        try {
            // should not throw an exception and the program should have a database ID afterwards
            connection.storeProgram(p);
            connection.commit();

            Assert.assertTrue("program did not get a database ID", p.getId() >= 0);

            // load the program by name and check if it is equal to the original program
            Program p1 = connection.loadProgram("test-001");
            connection.commit();

            Assert.assertNotNull("load program by name: loaded program is null",p1);
            Assert.assertEquals("load program by name: loaded program differs from original",p,p1);

            // load the program by name and check if it is equal to the original program
            Program p2 = connection.loadProgram(p.getId());
            connection.commit();

            Assert.assertNotNull("load program by ID: loaded program is null",p2);
            Assert.assertEquals("load program by ID: loaded program differs from original",p,p2);
View Full Code Here

     * Test storing and then updating a program (by removing two rules)
     */
    @Test
    public void testUpdateProgram() throws Exception {
        KWRLProgramParserBase parser = new KWRLProgramParser(repository.getValueFactory(), this.getClass().getResourceAsStream("test-001.kwrl"));
        Program p = parser.parseProgram();
        p.setName("test-001");

        KiWiReasoningConnection connection = rpersistence.getConnection();
        try {
            // should not throw an exception and the program should have a database ID afterwards
            connection.storeProgram(p);
            connection.commit();

            Assert.assertTrue("program did not get a database ID", p.getId() >= 0);


            // load the program by name and check if it is equal to the original program
            Program p1 = connection.loadProgram("test-001");
            connection.commit();

            Assert.assertNotNull("load program by name: loaded program is null",p1);
            Assert.assertEquals("load program by name: loaded program differs from original",p,p1);


            PreparedStatement listRules1 = connection.getJDBCConnection().prepareStatement("SELECT count(*) AS count FROM reasoner_rules");
            ResultSet resultListRules1 = listRules1.executeQuery();

            Assert.assertTrue(resultListRules1.next());
            Assert.assertEquals(5, resultListRules1.getInt("count"));
            resultListRules1.close();
            connection.commit();



            // now remove two rules from the original and update the existing program
            p.getRules().remove(p.getRules().size()-1);
            p.getRules().remove(p.getRules().size()-1);
            p.addNamespace("myns","http://example.com/myns");

            connection.updateProgram(p);

            // load the program by name and check if it is equal to the original program
            Program p2 = connection.loadProgram(p.getName());
            connection.commit();

            Assert.assertNotNull("load program by name: loaded program is null",p2);
            Assert.assertEquals("load program by name: loaded program differs from original",p,p2);

View Full Code Here

        KiWiReasoningConnection connection = rpersistence.getConnection();
        try {
            List<Program> programs = new ArrayList<Program>();
            for(String name : new String[] {"test-001", "test-002", "test-003", "test-004"}) {
                KWRLProgramParserBase parser = new KWRLProgramParser(repository.getValueFactory(), this.getClass().getResourceAsStream(name+".kwrl"));
                Program p = parser.parseProgram();
                p.setName(name);
                connection.storeProgram(p);
                connection.commit();

                programs.add(p);
            }
View Full Code Here


    @Test
    public void testParseProgram() throws Exception {
        KWRLProgramParserBase parser = new KWRLProgramParser(repository.getValueFactory(), this.getClass().getResourceAsStream(filename+".kwrl"));
        Program p = parser.parseProgram();

        Assert.assertNotNull(p);
        Assert.assertFalse(p.getRules().isEmpty());

    }
View Full Code Here

        // store a program, very simple transitive and symmetric rules:
        // ($1 ex:property $2), ($2 ex:property $3) -> ($1 ex:property $3)
        // ($1 ex:symmetric $2) -> ($2 ex:symmetric $1)
        KWRLProgramParserBase parser = new KWRLProgramParser(repository.getValueFactory(), this.getClass().getResourceAsStream("simple.kwrl"));
        Program p = parser.parseProgram();
        p.setName("simple");

        KiWiReasoningConnection connection = rpersistence.getConnection();
        try {
            // should not throw an exception and the program should have a database ID afterwards
            connection.storeProgram(p);
View Full Code Here

TOP

Related Classes of org.apache.marmotta.kiwi.reasoner.model.program.Program

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.