Search in sources :

Example 96 with SiddhiManager

use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow6.

@Test
public void testExternalTimeBatchWindow6() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test6");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream LoginEvents (timestamp long, ip string); " + "define window LoginWindow (timestamp long, ip string) externalTimeBatch(timestamp, 1 sec, 0, 3 sec) " + "output all events; ";
    String query = "" + "@info(name = 'query0') " + "from LoginEvents " + "insert into LoginWindow; " + "" + "@info(name = 'query1') " + "from LoginWindow " + "select timestamp, ip, count() as total  " + "insert all events into uniqueIps ;";
    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) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("LoginEvents");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { 1366335804341L, "192.10.1.3" });
    inputHandler.send(new Object[] { 1366335804599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335804600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335804607L, "192.10.1.6" });
    inputHandler.send(new Object[] { 1366335805599L, "192.10.1.4" });
    inputHandler.send(new Object[] { 1366335805600L, "192.10.1.5" });
    inputHandler.send(new Object[] { 1366335805607L, "192.10.1.6" });
    Thread.sleep(5000);
    assertEquals("Event arrived", true, eventArrived);
    assertEquals("In Events ", 2, inEventCount);
    assertEquals("Remove Events ", 0, removeEventCount);
    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 97 with SiddhiManager

use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.

the class ExternalTimeBatchWindowTestCase method testExternalTimeBatchWindow13.

@Test
public void testExternalTimeBatchWindow13() throws InterruptedException {
    log.info("ExternalTimeBatchWindow test13");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (timestamp long, symbol string, price float, volume int); " + "define stream twitterStream (timestamp long, user string, tweet string, company string); " + "define window cseEventWindow (timestamp long, symbol string, price float, volume int) " + "externalTimeBatch(timestamp, 1 sec, 0); " + "define window twitterWindow (timestamp long, user string, tweet string, company string) " + "externalTimeBatch(timestamp, 1 sec, 0); ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from twitterStream " + "insert into twitterWindow; " + "" + "@info(name = 'query2') " + "from cseEventWindow join twitterWindow " + "on cseEventWindow.symbol== twitterWindow.company " + "select cseEventWindow.symbol as symbol, twitterWindow.tweet, cseEventWindow.price " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query2", 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[] { 1366335804341L, "WSO2", 55.6f, 100 });
        twitterStreamHandler.send(new Object[] { 1366335804341L, "User1", "Hello World", "WSO2" });
        twitterStreamHandler.send(new Object[] { 1366335805301L, "User2", "Hello World2", "WSO2" });
        cseEventStreamHandler.send(new Object[] { 1366335805341L, "WSO2", 75.6f, 100 });
        cseEventStreamHandler.send(new Object[] { 1366335806541L, "WSO2", 57.6f, 100 });
        Thread.sleep(1000);
        assertEquals(2, inEventCount);
        assertEquals(1, removeEventCount);
        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 98 with SiddhiManager

use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.

the class FrequentWindowEventTbaleTestCase method testFrequentUniqueWindow1.

@Test
public void testFrequentUniqueWindow1() throws InterruptedException {
    log.info("FrequentWindow test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream purchase (cardNo string, price float); " + "define window purchaseWindow (cardNo string, price float) frequent(2); ";
    String query = "" + "@info(name = 'query0') " + "from purchase[price >= 30] " + "insert into purchaseWindow; " + "" + "@info(name = 'query1') " + "from purchaseWindow " + "select cardNo, price " + "insert all events into PotentialFraud ;";
    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 += inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount += removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchase");
    siddhiAppRuntime.start();
    for (int i = 0; i < 2; i++) {
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 73.36f });
        inputHandler.send(new Object[] { "1234-3244-2432-123", 46.36f });
        inputHandler.send(new Object[] { "5768-3244-2432-5646", 48.36f });
        inputHandler.send(new Object[] { "9853-3244-2432-4125", 78.36f });
    }
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("In Event count", 8, inEventCount);
    AssertJUnit.assertEquals("Out Event count", 6, removeEventCount);
    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 99 with SiddhiManager

use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.

the class LenghtBatchWindowTestCase method testLengthBatchWindow1.

@Test
public void testLengthBatchWindow1() throws InterruptedException {
    log.info("Testing length batch window with no of events smaller than window size");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseWindow (symbol string, price float, volume int) lengthBatch(4); ";
    String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow ;" + "@info(name = 'query2') from cseWindow insert into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query2", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.fail("No events should arrive");
            inEventCount = inEventCount + inEvents.length;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 0 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
    Thread.sleep(500);
    AssertJUnit.assertEquals(0, inEventCount);
    AssertJUnit.assertFalse(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 100 with SiddhiManager

use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.

the class LenghtBatchWindowTestCase method testLengthBatchWindow5.

@Test
public void testLengthBatchWindow5() throws InterruptedException {
    final int length = 2;
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseWindow (symbol string, price float, volume int) lengthBatch(" + length + ") output " + "expired events; ";
    String query = "@info(name = 'query1') from cseEventStream select symbol,price,volume insert into cseWindow ;" + "@info(name = 'query2') from cseWindow " + "select symbol,price,volume " + "insert expired events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count++;
                AssertJUnit.assertEquals("Remove event order", count, event.getData(2));
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    inputHandler.send(new Object[] { "IBM", 700f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    Thread.sleep(500);
    AssertJUnit.assertEquals("Remove event count", 4, count);
    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) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)1321 Test (org.testng.annotations.Test)1319 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)1231 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)1120 Event (org.ballerinalang.siddhi.core.event.Event)798 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)574 TestUtil (org.ballerinalang.siddhi.core.TestUtil)300 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)201 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)85 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)82 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)80 ArrayList (java.util.ArrayList)25 InMemoryBroker (org.ballerinalang.siddhi.core.util.transport.InMemoryBroker)16 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)12 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)12 InMemoryPersistenceStore (org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore)12 PersistenceStore (org.ballerinalang.siddhi.core.util.persistence.PersistenceStore)12 HashMap (java.util.HashMap)10 InMemoryConfigManager (org.ballerinalang.siddhi.core.util.config.InMemoryConfigManager)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6