Java Code Examples for io.siddhi.query.api.execution.query.Query#from()

The following examples show how to use io.siddhi.query.api.execution.query.Query#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingReturnFilterQuery() {
    Query query = Query.query();
    query.from(
            InputStream.stream("StockStream").
                    filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value
                                    (9.5)),
                            Compare.Operator.GREATER_THAN,
                            Expression.variable("price")),
                            Expression.compare(Expression.value(100),
                                    Compare.Operator.GREATER_THAN_EQUAL,
                                    Expression.variable("volume")
                            )
                            )
                    ).window("lengthBatch", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price")))
    );
    query.returns();

}
 
Example 2
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingReturnFilterQueryWithExtension() {
    Query query = Query.query();
    query.from(
            InputStream.stream("StockStream").
                    filter(Expression.and(Expression.compare(Expression.function("ext", "FooBarCond", Expression
                                    .value(7), Expression.value(9.5)),
                            Compare.Operator.GREATER_THAN,
                            Expression.variable("price")),
                            Expression.function("ext", "BarCond", Expression.value(100),
                                    Expression.variable("volume")
                            )
                            )
                    ).function("ext", "Foo", Expression.value(67), Expression.value(89)).window("ext",
                    "lengthFirst10", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("ext", "avg", Expression.variable("price")))
    );

}
 
Example 3
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery10() {
    TimeConstant timeConstant = new TimeConstant(1000);

    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.stream(InputStream.stream("e1", "Stream1")
                                    .filter(Expression.compare(Expression.variable("price"),
                                            Compare.Operator.GREATER_THAN_EQUAL,
                                            Expression.value(30)))),
                            State.next(State.logicalNot(State.stream(InputStream.stream("Stream1")
                                            .filter(Expression.compare(Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(20)))), timeConstant.week(1)),
                                    State.stream(InputStream.stream("e3", "Stream2").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("price").ofStream("e1")))))
                    )
            )
    );
}
 
Example 4
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingReturnFilterQueryLimitAndSort() {
    Query query = Query.query();
    query.from(
            InputStream.stream("StockStream").
                    filter(Expression.and(Expression.compare(Expression.function("FooBarCond", Expression.value
                                    (7), Expression.value(9.5)),
                            Compare.Operator.GREATER_THAN,
                            Expression.variable("price")),
                            Expression.function("BarCond", Expression.value(100),
                                    Expression.variable("volume")
                            )
                            )
                    ).function("ext", "Foo", Expression.value(67), Expression.value(89)).window("ext",
                    "lengthFirst10", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("ext", "avg", Expression.variable("symbol"))).
                    orderBy(Expression.variable("avgPrice"), OrderByAttribute.Order.DESC).
                    limit(Expression.value(5))
    );

}
 
Example 5
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery82() throws InterruptedException {
    log.info("Filter test82");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.DOUBLE);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"),
            Compare.Operator.LESS_THAN, Expression.value(50))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")));
    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);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50d, 60d});
    inputHandler.send(new Object[]{"WSO2", 70d, 40d});
    inputHandler.send(new Object[]{"WSO2", 44d, 200d});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();
}
 
Example 6
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery58() throws InterruptedException {
    log.info("Filter test58");

    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);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("quantity"),
            Compare.Operator.EQUAL, Expression.value(5d))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    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);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50f, 60d, 5});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 200d, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();

}
 
Example 7
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery81() throws InterruptedException {
    log.info("Filter test81");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_THAN, Expression.value(70f))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")));
    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);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50f, 60d});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();
}
 
Example 8
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingFilterQuery() {
    Query query = Query.query();
    query.from(
            InputStream.stream("StockStream").
                    filter(
                            Expression.and(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price")),
                                    Expression.compare(
                                            Expression.value(100),
                                            Compare.Operator.GREATER_THAN_EQUAL,
                                            Expression.variable("volume")
                                    )
                            )
                    )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    query.insertInto("OutStockStream");

    SiddhiApp.siddhiApp("test").addQuery(query);

}
 
Example 9
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery93() throws InterruptedException {
    log.info("Filter test93");

    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.LONG).attribute
            ("quantity", Attribute.Type.INT);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_THAN, Expression.value(100f))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression
            .variable("price")).select("quantity", Expression.variable("quantity")));
    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);
            count.addAndGet(inEvents.length);
        }
    });

    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 50f, 60L, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 40L, 10});
    inputHandler.send(new Object[]{"WSO2", 44f, 200L, 56});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    AssertJUnit.assertEquals(2, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 10
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = DuplicateAttributeException.class)
public void testCreatingFilterQueryWithDuplicateOutputAttribute() {
    Query query = Query.query();
    query.from(
            InputStream.stream("StockStream").
                    filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value
                                    (9.5)),
                            Compare.Operator.GREATER_THAN,
                            Expression.variable("price")),
                            Expression.compare(Expression.value(100),
                                    Compare.Operator.GREATER_THAN_EQUAL,
                                    Expression.variable("volume")
                            )
                            )
                    ).window("lengthBatch", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.function("avg", Expression.variable("price"))).
                    select("price", Expression.variable("price")).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    query.insertInto("OutStockStream");

}
 
Example 11
Source File: TableQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingInQuery() {
    Query query = Query.query();
    query.from(
            InputStream.stream("cseEventStream").
                    filter(
                            Expression.and(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price")),
                                    Expression.in(
                                            Expression.compare(Expression.value(9.5),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.variable("price")),
                                            "table"
                                    )
                            )
                    ).window("lengthBatch", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    query.insertInto("StockQuote");

}
 
Example 12
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery12() {
    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare(Expression
                                            .variable("price"),
                                    Compare.Operator.GREATER_THAN_EQUAL,
                                    Expression.value(30)))),
                            State.next(
                                    State.stream(InputStream.stream("e2", "Stream1").filter(Expression.compare
                                            (Expression.variable("price").ofFunction("e1", 1),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(20)))),
                                    State.stream(InputStream.stream("e3", "Stream2").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("price").ofInnerStream("e1"))))
                            )
                    )
            )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofInnerStream("e1")).
                    select("avgPrice", Expression.function("avg",
                            Expression.variable("price").ofInnerStream("e2"))).
                    groupBy(Expression.variable("symbol").ofInnerStream("e1")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN,
                            Expression.value(50)))
    );
    query.insertInto("OutputStream");
}
 
Example 13
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingFilterQueryWithFaultStream3() {
    Query query = Query.query();
    query.from(
            InputStream.faultStream("StockStream").
                    filter(
                            Expression.and(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price")),
                                    Expression.compare(
                                            Expression.value(100),
                                            Compare.Operator.GREATER_THAN_EQUAL,
                                            Expression.variable("volume")
                                    )
                            )
                    )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    query.insertIntoFault("OutStockStream", OutputStream.OutputEventType.CURRENT_EVENTS);
    SiddhiApp.siddhiApp("test").addQuery(query);
}
 
Example 14
Source File: PartitionTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPartitionQuery3() throws InterruptedException {
    log.info("Partition test3");


    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiApp siddhiApp = new SiddhiApp("plan3");

    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);

    siddhiApp.defineStream(streamDefinition);

    Partition partition = Partition.partition().
            with("cseEventStream", Expression.variable("symbol"));

    Query query = Query.query();
    query.from(InputStream.stream("cseEventStream"));
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.variable("price")).
                    select("volume", Expression.variable("volume"))

    );
    query.insertIntoInner("StockStream");


    Query query1 = Query.query();
    query1.from(InputStream.innerStream("StockStream"));
    query1.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.variable("price")).
                    select("volume", Expression.variable("volume"))

    );
    query1.insertInto("OutStockStream");

    partition.addQuery(query);
    partition.addQuery(query1);

    siddhiApp.addPartition(partition);


    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});
    inputHandler.send(new Object[]{"WSO2", 75.6f, 100});
    inputHandler.send(new Object[]{"IBM", 75.6f, 100});
    inputHandler.send(new Object[]{"ORACLE", 75.6f, 100});
    SiddhiTestHelper.waitForEvents(100, 4, count, 60000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(4, count.get());

}
 
Example 15
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterQuery31() throws InterruptedException {
    log.info("Filter test31");

    SiddhiManager siddhiManager = new SiddhiManager();

    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.FLOAT).attribute("available", Attribute.Type.BOOL);

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").
            filter(Expression.compare(Expression.variable("available"),
                    Compare.Operator.NOT_EQUAL,
                    Expression.value(true))
            )
    );
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.variable("price")).
                    select("available", Expression.variable("available"))
    );
    query.insertInto("StockQuote");

    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.assertEquals("WSO2", inEvents[0].getData(0).toString());
            count.addAndGet(inEvents.length);
        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();

    inputHandler.send(new Object[]{"IBM", 55.6f, true});
    inputHandler.send(new Object[]{"WSO2", 57.6f, false});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();


}
 
Example 16
Source File: PassThroughTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void passThroughTest2() throws InterruptedException {
    log.info("pass through test2");
    SiddhiManager siddhiManager = new SiddhiManager();

    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.INT);
    StreamDefinition cseEventStream1 = StreamDefinition.id("cseEventStream1").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.INT);

    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("price", Expression.variable("price"))
    );
    query.insertInto("StockQuote");


    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.defineStream(cseEventStream1);
    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(inEvents);
            count += inEvents.length;
            eventArrived.set(true);
        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream1");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 100});
    inputHandler.send(new Object[]{"WSO2", 100});
    SiddhiTestHelper.waitForEvents(10, eventArrived, 100);
    AssertJUnit.assertEquals(0, count);
    AssertJUnit.assertFalse(eventArrived.get());
    siddhiAppRuntime.shutdown();
}
 
Example 17
Source File: PassThroughTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void passThroughTest1() throws InterruptedException {
    log.info("pass through test1");
    SiddhiManager siddhiManager = new SiddhiManager();

    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type
            .STRING).attribute("price", Attribute.Type.INT);

    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("price", Expression.variable("price"))
    );
    query.insertInto("StockQuote");


    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(inEvents);
            AssertJUnit.assertTrue("IBM".equals(inEvents[0].getData(0)) || "WSO2".equals(inEvents[0].getData(0)));
            count += inEvents.length;
            eventArrived.set(true);
        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"IBM", 100});
    inputHandler.send(new Object[]{"WSO2", 100});
    SiddhiTestHelper.waitForEvents(10, eventArrived, 100);
    AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived.get());
    siddhiAppRuntime.shutdown();
}
 
Example 18
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void test9() {

    Query query = SiddhiCompiler.parseQuery("from  StockStream[7-9.5 > price and 100 >= volume]#bar(price)#window" +
            ".length(50)#foo(67,89)#[10<=price]  " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having avgPrice  >= 50 " +
            "insert into OutStockStream ;"
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query();
    api.from(
            InputStream.stream("StockStream").
                    filter(
                            Expression.and(
                                    Expression.compare(
                                            Expression.subtract(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price")),
                                    Expression.compare(
                                            Expression.value(100),
                                            Compare.Operator.GREATER_THAN_EQUAL,
                                            Expression.variable("volume")
                                    )
                            )
                    ).
                    function("bar", Expression.variable("price")).
                    window("length", Expression.value(50)).
                    function("foo", Expression.value(67), Expression.value(89)).
                    filter(
                            Expression.compare(
                                    Expression.value(10),
                                    Compare.Operator.LESS_THAN_EQUAL,
                                    Expression.variable("price")
                            )
                    )
    );
    api.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );
    api.insertInto("OutStockStream");

    AssertJUnit.assertEquals(api, query);
}
 
Example 19
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterQuery113() throws InterruptedException {
    log.info("Filter test113");

    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("modPrice", Expression.mod(Expression.variable("price"), Expression.value(2))).
                    select("modVolume", Expression.mod(Expression.variable("volume"), Expression.value(2))).
                    select("modQuantity", Expression.mod(Expression.variable("quantity"), Expression.value(2))).
                    select("modAwards", Expression.mod(Expression.variable("awards"), Expression.value(2)))

    );
    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("1.5".equals(inEvents[0].getData()[1].toString()));
            AssertJUnit.assertTrue("1.0".equals(inEvents[0].getData()[2].toString()));
            AssertJUnit.assertTrue("1".equals(inEvents[0].getData()[3].toString()));
            AssertJUnit.assertTrue("1".equals(inEvents[0].getData()[4].toString()));
            count.addAndGet(inEvents.length);
        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 55.5f, 101d, 5, 7L});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example 20
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFilterQuery111() throws InterruptedException {
    log.info("Filter test111");

    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("dividedPrice", Expression.divide(Expression.variable("price"), Expression.value(2))).
                    select("dividedVolume", Expression.divide(Expression.variable("volume"), Expression.value(2))).
                    select("dividedQuantity", Expression.divide(Expression.variable("quantity"), Expression.value
                            (5))).
                    select("dividedAwards", Expression.divide(Expression.variable("awards"), Expression.value(10)))

    );
    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("30.0".equals(inEvents[0].getData()[1].toString()));
            AssertJUnit.assertTrue("50.0".equals(inEvents[0].getData()[2].toString()));
            AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[3].toString()));
            AssertJUnit.assertTrue("7".equals(inEvents[0].getData()[4].toString()));
            count.addAndGet(inEvents.length);

        }

    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[]{"WSO2", 60f, 100d, 100, 70L});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();

}