Search in sources :

Example 56 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class LossyFrequentWindowTestCase method frequentUniqueWindowTest3.

@Test
public void frequentUniqueWindowTest3() throws InterruptedException {
    log.info("LossyFrequentWindow test3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream purchase (cardNo string, price float); " + "define window purchaseWindow (cardNo string, price float) lossyFrequent(0.3,0.05,cardNo); ";
    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();
    inputHandler.send(new Object[] { "3224-3244-2432-4124", 73.36f });
    for (int i = 0; i < 25; i++) {
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 73.36f });
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 78.36f });
        inputHandler.send(new Object[] { "1234-3244-2432-123", 86.36f });
        // this event will be included because we
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 48.36f });
    // only consider cardNo
    }
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("In Event count", 101, inEventCount);
    AssertJUnit.assertEquals("Out Event count", 1, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 57 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class LossyFrequentWindowTestCase method testLossyFrequentUniqueWindow1.

@Test
public void testLossyFrequentUniqueWindow1() throws InterruptedException {
    log.info("LossyFrequentWindow test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream purchase (cardNo string, price float); " + "define window purchaseWindow (cardNo string, price float) lossyFrequent(0.1,0.01); ";
    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 < 25; 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 });
    }
    // these events will not be picked
    inputHandler.send(new Object[] { "1124-3244-2432-4126", 78.36f });
    inputHandler.send(new Object[] { "1124-3244-2432-4126", 78.36f });
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("In Event count", 100, inEventCount);
    AssertJUnit.assertEquals("Out Event count", 0, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 58 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class SortWindowTestCase method testSortWindow1.

@Test
public void testSortWindow1() throws InterruptedException {
    log.info("SortWindow test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume long); " + "define window cseEventWindow (symbol string, price float, volume long) sort(2,volume, 'asc'); ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from cseEventWindow " + "select 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) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.6f, 100L });
    inputHandler.send(new Object[] { "IBM", 75.6f, 300L });
    inputHandler.send(new Object[] { "WSO2", 57.6f, 200L });
    inputHandler.send(new Object[] { "WSO2", 55.6f, 20L });
    inputHandler.send(new Object[] { "WSO2", 57.6f, 40L });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(5, inEventCount);
    AssertJUnit.assertEquals(3, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 59 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class SortWindowTestCase method testSortWindow2.

@Test
public void testSortWindow2() throws InterruptedException {
    log.info("SortWindow test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume long); " + "define window cseEventWindow (symbol string, price float, volume long) sort(2,volume, 'asc', price, " + "'desc'); ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from cseEventWindow " + "select 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) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 50, 100L });
    inputHandler.send(new Object[] { "IBM", 20, 100L });
    inputHandler.send(new Object[] { "WSO2", 40, 50L });
    inputHandler.send(new Object[] { "WSO2", 100, 20L });
    inputHandler.send(new Object[] { "WSO2", 50, 50L });
    Thread.sleep(1000);
    AssertJUnit.assertEquals(5, inEventCount);
    AssertJUnit.assertEquals(3, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 60 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeBatchWindowTestCase method testTimeWindowBatch2.

@Test
public void testTimeWindowBatch2() throws InterruptedException {
    log.info("TimeWindowBatch Test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseEventWindow (symbol string, price float, volume int) timeBatch(1 sec); ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') " + "from cseEventWindow " + "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.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)161 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)130 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)130 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)118 Event (org.wso2.siddhi.core.event.Event)117 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)82 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)35 Query (org.wso2.siddhi.query.api.execution.query.Query)32 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)14 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)12 ArrayList (java.util.ArrayList)10 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)10 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)10 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)9 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)8 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)8 Window (org.wso2.siddhi.core.window.Window)7 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)6 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)6