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