Search in sources :

Example 6 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class TimeBatchWindowTestCase method timeWindowBatchTest6.

@Test
public void timeWindowBatchTest6() throws InterruptedException {
    log.info("timeWindowBatch Test6");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) join twitterStream#window.timeBatch(1 sec) " + "on cseEventStream.symbol== twitterStream.company " + "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " + "insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[] { "WSO2", 55.6f, 100 });
        twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
        cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
        Thread.sleep(1500);
        cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
        Thread.sleep(1000);
        AssertJUnit.assertTrue("In Events can be 1 or 2 ", inEventCount == 1 || inEventCount == 2);
        AssertJUnit.assertEquals(0, removeEventCount);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 7 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class TimeBatchWindowTestCase method timeWindowBatchTest5.

@Test
public void timeWindowBatchTest5() throws InterruptedException {
    log.info("timeWindowBatch Test5");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define stream twitterStream (user string, tweet string, company string); ";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) join twitterStream#window.timeBatch(1 sec) " + "on cseEventStream.symbol== twitterStream.company " + "select cseEventStream.symbol as symbol, twitterStream.tweet, cseEventStream.price " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += (inEvents.length);
                }
                if (removeEvents != null) {
                    removeEventCount += (removeEvents.length);
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        InputHandler twitterStreamHandler = siddhiAppRuntime.getInputHandler("twitterStream");
        siddhiAppRuntime.start();
        cseEventStreamHandler.send(new Object[] { "WSO2", 55.6f, 100 });
        twitterStreamHandler.send(new Object[] { "User1", "Hello World", "WSO2" });
        cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
        Thread.sleep(1100);
        cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
        Thread.sleep(1000);
        AssertJUnit.assertTrue("In Events can be 1 or 2 ", inEventCount == 1 || inEventCount == 2);
        AssertJUnit.assertTrue("Removed Events can be 1 or 2 ", removeEventCount == 1 || removeEventCount == 2);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 8 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class TimeBatchWindowTestCase method timeWindowBatchTest2.

@Test
public void timeWindowBatchTest2() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.timeBatch(1 sec) " + "select symbol, sum(price) as price " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { "IBM", 700f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    Thread.sleep(2000);
    AssertJUnit.assertEquals(3, inEventCount);
    AssertJUnit.assertEquals(1, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 9 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class TimeLengthWindowTestCase method timeLengthWindowTest6.

/*
        Time period > Window time
        Number of events > length
    */
@Test
public void timeLengthWindowTest6() throws InterruptedException {
    log.info("Testing timeLength window with no of events greater than window length and time period greater than" + " window time");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorStream = "define stream sensorStream (id string, sensorValue int);";
    String query = "@info(name = 'query1') from sensorStream#window.timeLength(3 sec,6)" + " select id,sum(sensorValue) as sum" + " insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                if (inEvents[0].getData(0).toString().equals("id6")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                if (inEvents[0].getData(0).toString().equals("id7")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                if (inEvents[0].getData(0).toString().equals("id8")) {
                    AssertJUnit.assertEquals("6", inEvents[0].getData(1).toString());
                }
                inEventCount++;
            }
            if (removeEvents != null) {
                if (removeEvents[0].getData(0).toString().equals("id1")) {
                    AssertJUnit.assertEquals("5", removeEvents[0].getData(1).toString());
                }
                if (removeEvents[0].getData(0).toString().equals("id2")) {
                    AssertJUnit.assertEquals("5", removeEvents[0].getData(1).toString());
                }
                if (removeEvents[0].getData(0).toString().equals("id3")) {
                    AssertJUnit.assertEquals("3", removeEvents[0].getData(1).toString());
                }
                removeEventCount++;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "id1", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id2", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id3", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id4", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id5", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id6", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id7", 1 });
    Thread.sleep(520);
    inputHandler.send(new Object[] { "id8", 1 });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(8, inEventCount);
    AssertJUnit.assertEquals(2, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 10 with QueryCallback

use of org.ballerinalang.siddhi.core.query.output.callback.QueryCallback in project ballerina by ballerina-lang.

the class TimeLengthWindowTestCase method timeLengthWindowTest1.

/*
        Time Period < Window time
        Number of Events < Window length
    */
@Test
public void timeLengthWindowTest1() throws InterruptedException {
    log.info("Testing timeLength window with no of events less than window length and time period less than " + "window time");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1') from cseEventStream#window.timeLength(4 sec,10) select symbol,price," + "volume insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    Thread.sleep(5000);
    AssertJUnit.assertEquals(4, inEventCount);
    AssertJUnit.assertEquals(4, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)575 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)574 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)574 Test (org.testng.annotations.Test)574 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)572 Event (org.ballerinalang.siddhi.core.event.Event)569 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)73 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)73 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)73 ArrayList (java.util.ArrayList)25 InMemoryPersistenceStore (org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore)11 PersistenceStore (org.ballerinalang.siddhi.core.util.persistence.PersistenceStore)11 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)10 StringConcatAggregatorString (org.ballerinalang.siddhi.core.query.extension.util.StringConcatAggregatorString)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)4 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)1