Search in sources :

Example 31 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery6.

@Test
public void testPartitionQuery6() throws InterruptedException {
    log.info("Partition test6");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "@app:name('PartitionTest6') " + "define stream cseEventStream (symbol string, price float,volume int);" + "define stream cseEventStream1 (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream , symbol of cseEventStream1) begin @info(name = 'query') " + "from cseEventStream select symbol,price,volume insert into #StockStream ;" + "@info(name = 'query1') from #StockStream select symbol,price,volume insert into OutStockStream ;" + "@info(name = 'query2') from cseEventStream1 select symbol,price,volume insert into  #StockStream1 ;" + "@info(name = 'query3') from #StockStream1 select symbol,price,volume insert into OutStockStream ; " + "end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    InputHandler inputHandler2 = siddhiAppRuntime.getInputHandler("cseEventStream1");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 100 });
    inputHandler2.send(new Object[] { "IBM", 75.6f, 100 });
    inputHandler2.send(new Object[] { "WSO21", 75.6f, 100 });
    inputHandler2.send(new Object[] { "IBM1", 75.6f, 100 });
    inputHandler2.send(new Object[] { "ORACLE1", 75.6f, 100 });
    SiddhiTestHelper.waitForEvents(100, 8, count, 60000);
    AssertJUnit.assertEquals(8, count.get());
    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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 32 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery28.

@Test
public void testPartitionQuery28() throws InterruptedException {
    log.info("Partition test24");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "@app:name('PartitionTest24') " + "define stream cseEventStream (symbol string,  price float, volume int, threshold double);" + "partition with (symbol of cseEventStream) begin @info(name = 'query1') from " + "cseEventStream[700>price AND threshold != volume] select symbol, sum(price) as price, " + "volume , threshold" + " insert into " + "OutStockStream ;  end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75.6f, 100, 100.0 });
    inputHandler.send(new Object[] { "WSO2", 75.6f, 100, 52.0 });
    inputHandler.send(new Object[] { "IBM", 75.6f, 100, 50.0 });
    inputHandler.send(new Object[] { "ORACLE", 75.6f, 100, 200.0 });
    SiddhiTestHelper.waitForEvents(100, 3, count, 60000);
    AssertJUnit.assertEquals(3, count.get());
    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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 33 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery9.

@Test
public void testPartitionQuery9() throws InterruptedException {
    log.info("Partition test9");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "@app:name('PartitionTest9') " + "define stream cseEventStream (symbol string, price float,volume int);" + "define stream cseEventStream1 (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream)" + "begin" + "@info(name = 'query') from cseEventStream select symbol,min(price) as min_price,volume insert into" + " OutStockStream ;" + "end ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(75.0f, event.getData()[1]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(705.0f, event.getData()[1]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertEquals(35.0f, event.getData()[1]);
                } else if (count.get() == 4) {
                    AssertJUnit.assertEquals(50.0f, event.getData()[1]);
                }
                eventArrived = true;
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 75f, 100 });
    inputHandler.send(new Object[] { "WSO2", 705f, 100 });
    inputHandler.send(new Object[] { "IBM", 35f, 100 });
    inputHandler.send(new Object[] { "ORACLE", 50.0f, 100 });
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    AssertJUnit.assertEquals(4, count.get());
    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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 34 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class PartitionTestCase1 method testPartitionQuery20.

@Test
public void testPartitionQuery20() throws InterruptedException {
    log.info("Partition test20");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('PartitionTest20') " + "" + "" + "define stream cseEventStream (symbol string, price float,volume int); " + "" + "define stream cseEventStreamOne (symbol string, price float,volume int);" + "" + "@info(name = 'query')" + "from cseEventStreamOne " + "select symbol, price, volume " + "insert into cseEventStream;" + " " + "partition with (price>=100 as 'large' or price<100 as 'medium' or price<50 as 'small' of " + "cseEventStream) " + "   begin" + "   @info(name = 'query1') " + "   from cseEventStream " + "   select symbol, sum(price) as price " + "   insert into #OutStockStream1 ; " + " " + "   @info(name = 'query2') " + "   from #OutStockStream1 " + "   insert into #OutStockStream2 ;" + " " + "   @info(name = 'query3') " + "   from #OutStockStream2 " + "   insert into OutStockStream ;" + " " + "   end ; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
                eventArrived = true;
                if (count.get() == 1) {
                    AssertJUnit.assertEquals(25.0, event.getData()[1]);
                } else if (count.get() == 2) {
                    AssertJUnit.assertEquals(25.0, event.getData()[1]);
                } else if (count.get() == 3) {
                    AssertJUnit.assertEquals(7005.60009765625, event.getData()[1]);
                } else if (count.get() == 4) {
                    AssertJUnit.assertTrue(event.getData()[1].equals(75.0) || event.getData()[1].equals(100.0));
                } else if (count.get() == 5) {
                    AssertJUnit.assertTrue(event.getData()[1].equals(50.0) || event.getData()[1].equals(100.0));
                } else if (count.get() == 6) {
                    AssertJUnit.assertEquals(50.0, event.getData()[1]);
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStreamOne");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 25f, 100 });
    inputHandler.send(new Object[] { "WSO2", 7005.6f, 100 });
    inputHandler.send(new Object[] { "IBM", 50f, 100 });
    inputHandler.send(new Object[] { "ORACLE", 25f, 100 });
    SiddhiTestHelper.waitForEvents(100, 5, count, 60000);
    AssertJUnit.assertTrue(6 >= count.get());
    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) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 35 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class FilterTestCase2 method testFilterQuery112.

// ****************************************************************************************************************
// Expression Multiply
@Test
public void testFilterQuery112() throws InterruptedException {
    log.info("Filter test112");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE).attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream"));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("multipliedQuantity", Expression.multiply(Expression.variable("quantity"), Expression.value(4))).select("multipliedPrice", Expression.multiply(Expression.variable("price"), Expression.value(2))).select("multipliedVolume", Expression.multiply(Expression.variable("volume"), Expression.value(3))).select("multipliedAwards", Expression.multiply(Expression.variable("awards"), Expression.value(5))));
    query.insertInto("OutputStream");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[1].toString()));
            AssertJUnit.assertTrue("111.0".equals(inEvents[0].getData()[2].toString()));
            AssertJUnit.assertTrue("300.0".equals(inEvents[0].getData()[3].toString()));
            AssertJUnit.assertTrue("15".equals(inEvents[0].getData()[4].toString()));
            count = count + inEvents.length;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.5f, 100d, 5, 3L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Query(org.wso2.siddhi.query.api.execution.query.Query) 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

SiddhiManager (org.wso2.siddhi.core.SiddhiManager)273 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)272 Test (org.testng.annotations.Test)261 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)245 Event (org.wso2.siddhi.core.event.Event)241 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)138 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)109 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)88 Query (org.wso2.siddhi.query.api.execution.query.Query)84 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)81 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)13 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)13 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 HashMap (java.util.HashMap)6 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)6 InMemoryConfigManager (org.wso2.siddhi.core.util.config.InMemoryConfigManager)5 ExecutionElement (org.wso2.siddhi.query.api.execution.ExecutionElement)4