// 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));
List<Statement> infTriples = asList(connection.listTriples(null,null,null,v.convert(ctxi),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
// there should now be one justification supporting infTriples.get(0)
List<Justification> supported6 = asList(connection.listJustificationsForTriple((KiWiTriple) infTriples.get(0)));
Assert.assertEquals("number of justifications is wrong", 1, supported6.size());
Assert.assertThat("justifications differ", supported6, allOf(hasItem(j1), not(hasItem(j2))));
// there should now be one justification supporting infTriples.get(1)
List<Justification> supported7 = asList(connection.listJustificationsForTriple((KiWiTriple) infTriples.get(1)));
Assert.assertEquals("number of justifications is wrong", 1, supported7.size());
Assert.assertThat("justifications differ", supported7, allOf(hasItem(j2), not(hasItem(j1))));
// there should now be no justification supporting infTriples.get(2)
List<Justification> supported8 = asList(connection.listJustificationsForTriple((KiWiTriple) infTriples.get(2)));
Assert.assertEquals("number of justifications is wrong", 0, supported8.size());
// *** check listing unsupported triples
List<KiWiTriple> unsupported = asList(connection.listUnsupportedTriples());
Assert.assertEquals("number of unsupported triples is wrong",1,unsupported.size());
Assert.assertThat("unsupported triples differ", unsupported, hasItem((KiWiTriple)infTriples.get(2)));
// now we delete justification 2; as a consequence,
// - there should be only once justification left
// - there should be two unsupported triples
connection.deleteJustifications(Collections.singleton(j2));
// we should now have one justifications in the database
resultListJustifications = listJustifications.executeQuery();
Assert.assertTrue(resultListJustifications.next());
Assert.assertEquals(1, resultListJustifications.getInt("count"));
resultListJustifications.close();
connection.commit();
resultListSupportingTriples = listSupportingTriples.executeQuery();
Assert.assertTrue(resultListSupportingTriples.next());
Assert.assertEquals(2, resultListSupportingTriples.getInt("count"));
resultListSupportingTriples.close();
connection.commit();
resultListSupportingRules = listSupportingRules.executeQuery();
Assert.assertTrue(resultListSupportingRules.next());
Assert.assertEquals(2, resultListSupportingRules.getInt("count"));
resultListSupportingRules.close();
connection.commit();
List<KiWiTriple> unsupported2 = asList(connection.listUnsupportedTriples());
Assert.assertEquals("number of unsupported triples is wrong",2,unsupported2.size());
Assert.assertThat("unsupported triples differ", unsupported2, hasItem((KiWiTriple)infTriples.get(1)));
} catch(BatchUpdateException ex) {
if(ex.getNextException() != null) {
ex.printStackTrace();
throw ex.getNextException();
} else {
throw ex;
}
} finally {
connection.close();
}
}