Search in sources :

Example 1 with SiddhiAppRuntime

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

the class StartForever method execute.

@Override
public void execute(Context context) {
    context.setReturnValues();
    String siddhiApp = context.getStringArgument(0);
    SiddhiAppRuntime siddhiAppRuntime = StreamingRuntimeManager.getInstance().createSiddhiAppRuntime(siddhiApp);
    Set<String> streamIds = siddhiAppRuntime.getStreamDefinitionMap().keySet();
    Map<String, InputHandler> streamSpecificInputHandlerMap = new HashMap<>();
    for (String streamId : streamIds) {
        streamSpecificInputHandlerMap.put(streamId, siddhiAppRuntime.getInputHandler(streamId));
    }
    BRefValueArray inputStreamReferenceArray = (BRefValueArray) context.getRefArgument(0);
    BRefValueArray functionPointerArray = (BRefValueArray) context.getRefArgument(4);
    for (int i = 0; i < inputStreamReferenceArray.size(); i++) {
        BStream stream = (BStream) inputStreamReferenceArray.get(i);
        InputHandler inputHandler = streamSpecificInputHandlerMap.get(stream.getStreamId());
        stream.subscribe(inputHandler);
    }
    for (int i = 0; i < functionPointerArray.size(); i++) {
        BFunctionPointer functionPointer = (BFunctionPointer) functionPointerArray.get(i);
        String functionName = functionPointer.value().getFunctionName();
        String streamId = "stream" + functionName.replaceAll("\\$", "_");
        StreamingRuntimeManager.getInstance().addCallback(streamId, functionPointer, siddhiAppRuntime);
    }
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) HashMap(java.util.HashMap) BStream(org.ballerinalang.model.values.BStream) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) BFunctionPointer(org.ballerinalang.model.values.BFunctionPointer)

Example 2 with SiddhiAppRuntime

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

the class SequenceTestCase method testQuery4.

@Test
public void testQuery4() throws InterruptedException {
    log.info("testSequence4 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from every e1=Stream2[price>20]*, e2=Stream1[price>e1[0].price] " + "select e1[0].price as price1, e1[1].price as price2, e2.price as price3 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.6f, 55.7f, 57.6f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 59.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, 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 3 with SiddhiAppRuntime

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

the class SequenceTestCase method testTimeBatchAndSequence.

@Test
public void testTimeBatchAndSequence() throws Exception {
    log.info("testTimeBatchAndSequence  OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream received_reclamations (timestamp long, product_id string, defect_category string);";
    String query = "" + "@info(name = 'query1') " + "from received_reclamations#window.timeBatch(1 sec) " + "select product_id, defect_category, count(product_id) as num " + "group by product_id, defect_category " + "insert into reclamation_averages;" + "" + "@info(name = 'query2') " + "from a=reclamation_averages[num > 1], b=reclamation_averages[num > a.num and product_id == a" + ".product_id and defect_category == a.defect_category] " + "select a.product_id, a.defect_category, a.num as oldNum, b.num as newNum " + "insert into increased_reclamations;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("increased_reclamations", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            if (events != null) {
                for (Event event : events) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            String product = (String) event.getData()[0];
                            String defectCategory = (String) event.getData()[1];
                            long oldNum = (Long) event.getData()[2];
                            long newNum = (Long) event.getData()[3];
                            AssertJUnit.assertTrue(product.equals("abc"));
                            AssertJUnit.assertTrue(defectCategory.equals("123"));
                            AssertJUnit.assertTrue(oldNum < newNum);
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
            }
            eventArrived = true;
        }
    });
    InputHandler i1 = siddhiAppRuntime.getInputHandler("received_reclamations");
    siddhiAppRuntime.start();
    for (int i = 0; i < 5; i++) {
        i1.send(new Object[] { System.currentTimeMillis(), "abc", "123" });
        Thread.sleep(100);
    }
    Thread.sleep(500);
    for (int i = 0; i < 8; i++) {
        i1.send(new Object[] { System.currentTimeMillis(), "abc", "123" });
        Thread.sleep(100);
    }
    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
}
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)

Example 4 with SiddhiAppRuntime

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

the class AbsentSequenceTestCase method testQueryAbsent13.

@Test
public void testQueryAbsent13() throws InterruptedException {
    log.info("Test the query e1, not e2 for 1 sec, e3 with e1, e2(condition failed) e3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); " + "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from e1=Stream1[price>10], not Stream2[price>20] for 1 sec, e3=Stream3[price>30] " + "select e1.symbol as symbol1, e3.symbol as symbol3 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1", new Object[] { "WSO2", "GOOGLE" });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 15.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 8.7f, 100 });
    Thread.sleep(1100);
    stream3.send(new Object[] { "GOOGLE", 55.7f, 100 });
    Thread.sleep(100);
    callback.throwAssertionErrors();
    AssertJUnit.assertEquals("Number of success events", 0, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertFalse("Event arrived", callback.isEventArrived());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) TestUtil(org.ballerinalang.siddhi.core.TestUtil) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 5 with SiddhiAppRuntime

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

the class AbsentSequenceTestCase method testQueryAbsent3.

@Test
public void testQueryAbsent3() throws InterruptedException {
    log.info("Test the query e1, not e2 sending e2 for 1 sec");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from e1=Stream1[price>20], not Stream2[price>e1.price] for 1 sec " + "select e1.symbol as symbol1 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    TestUtil.TestCallback callback = TestUtil.addQueryCallback(siddhiAppRuntime, "query1");
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 58.7f, 100 });
    Thread.sleep(1000);
    callback.throwAssertionErrors();
    AssertJUnit.assertEquals("Number of success events", 0, callback.getInEventCount());
    AssertJUnit.assertEquals("Number of remove events", 0, callback.getRemoveEventCount());
    AssertJUnit.assertFalse("Event arrived", callback.isEventArrived());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) TestUtil(org.ballerinalang.siddhi.core.TestUtil) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)1235 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)1231 Test (org.testng.annotations.Test)1231 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)1123 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)11 InMemoryConfigManager (org.ballerinalang.siddhi.core.util.config.InMemoryConfigManager)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6