Search in sources :

Example 1 with NotifyingSail

use of org.openrdf.sail.NotifyingSail in project blueprints by tinkerpop.

the class SailTest method testSailChangedListeners.

@Test
public void testSailChangedListeners() throws Exception {
    if (sail instanceof NotifyingSail) {
        final Collection<SailChangedEvent> events = new LinkedList<SailChangedEvent>();
        SailChangedListener listener = new SailChangedListener() {

            public void sailChanged(final SailChangedEvent event) {
                events.add(event);
            }
        };
        ((NotifyingSail) sail).addSailChangedListener(listener);
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
        SailConnection sc = sail.getConnection();
        try {
            sc.begin();
            sc.clear();
            sc.commit();
            sc.begin();
            events.clear();
            assertEquals(0, events.size());
            sc.addStatement(uriA, uriB, uriC, uriA);
            sc.addStatement(uriB, uriC, uriA, uriA);
            // Events are buffered until the commit
            assertEquals(0, events.size());
            sc.commit();
            sc.begin();
            // Only one SailChangedEvent per commit
            assertEquals(1, events.size());
            SailChangedEvent event = events.iterator().next();
            assertTrue(event.statementsAdded());
            assertFalse(event.statementsRemoved());
            events.clear();
            assertEquals(0, events.size());
            sc.removeStatements(uriA, uriB, uriC, uriA);
            sc.commit();
            sc.begin();
            assertEquals(1, events.size());
            event = events.iterator().next();
            assertFalse(event.statementsAdded());
            assertTrue(event.statementsRemoved());
            events.clear();
            assertEquals(0, events.size());
            sc.clear();
            sc.commit();
            sc.begin();
            assertEquals(1, events.size());
            event = events.iterator().next();
            assertFalse(event.statementsAdded());
            assertTrue(event.statementsRemoved());
        } finally {
            sc.rollback();
            sc.close();
        }
    }
}
Also used : NotifyingSail(org.openrdf.sail.NotifyingSail) SailChangedEvent(org.openrdf.sail.SailChangedEvent) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) SailChangedListener(org.openrdf.sail.SailChangedListener) URI(org.openrdf.model.URI) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with NotifyingSail

use of org.openrdf.sail.NotifyingSail in project blueprints by tinkerpop.

the class SailTest method setUp.

@Before
public final void setUp() throws Exception {
    before();
    this.sail = createSail();
    sail.initialize();
    if (sail instanceof NotifyingSail) {
        SailConnection sc = sail.getConnection();
        try {
            if (sc instanceof InferencerConnection) {
                inferencer = new ForwardChainingRDFSInferencer((NotifyingSail) sail);
            }
        } finally {
            sc.rollback();
            sc.close();
        }
    }
    addFile(SailTest.class.getResourceAsStream("graph-example-sail-test.trig"), RDFFormat.TRIG);
}
Also used : NotifyingSail(org.openrdf.sail.NotifyingSail) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) InferencerConnection(org.openrdf.sail.inferencer.InferencerConnection) ForwardChainingRDFSInferencer(org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer) Before(org.junit.Before)

Example 3 with NotifyingSail

use of org.openrdf.sail.NotifyingSail in project blueprints by tinkerpop.

the class SailTest method testSailConnectionListeners.

// listeners ///////////////////////////////////////////////////////////////
// (disabled for Sails which do not implement NotifyingSail)
@Test
public void testSailConnectionListeners() throws Exception {
    if (sail instanceof NotifyingSail) {
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
        TestListener listener1 = new TestListener(), listener2 = new TestListener();
        NotifyingSailConnection sc = ((NotifyingSail) sail).getConnection();
        try {
            sc.begin();
            sc.clear();
            sc.commit();
            sc.begin();
            // Add a listener and add statements
            sc.addConnectionListener(listener1);
            sc.addStatement(uriA, uriB, uriC, uriA);
            sc.addStatement(uriB, uriC, uriA, uriA);
            sc.commit();
            sc.begin();
            // Add another listener and remove a statement
            sc.addConnectionListener(listener2);
            sc.removeStatements(uriA, null, null);
            sc.commit();
            sc.begin();
            assertEquals(2, listener1.getAdded());
            assertEquals(0, listener2.getAdded());
            assertEquals(1, listener1.getRemoved());
            assertEquals(1, listener2.getRemoved());
            // Remove a listener and clear
            sc.removeConnectionListener(listener1);
            sc.clear();
            sc.commit();
            sc.begin();
            assertEquals(1, listener1.getRemoved());
            assertEquals(2, listener2.getRemoved());
        } finally {
            sc.rollback();
            sc.close();
        }
    }
}
Also used : NotifyingSail(org.openrdf.sail.NotifyingSail) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) URI(org.openrdf.model.URI) Test(org.junit.Test)

Aggregations

NotifyingSail (org.openrdf.sail.NotifyingSail)3 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)3 Test (org.junit.Test)2 URI (org.openrdf.model.URI)2 SailConnection (org.openrdf.sail.SailConnection)2 LinkedList (java.util.LinkedList)1 Before (org.junit.Before)1 SailChangedEvent (org.openrdf.sail.SailChangedEvent)1 SailChangedListener (org.openrdf.sail.SailChangedListener)1 InferencerConnection (org.openrdf.sail.inferencer.InferencerConnection)1 ForwardChainingRDFSInferencer (org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer)1