* @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();
}
}