io.siddhi.query.api.execution.query.input.stream.InputStream Java Examples

The following examples show how to use io.siddhi.query.api.execution.query.input.stream.InputStream. 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 testCreatingReturnFilterQueryLimitOffsetAndSort() {
    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)).offset(Expression.value(2))
    );

}
 
Example #2
Source File: DefineAggregationTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void test2() throws SiddhiParserException {

    AggregationDefinition aggregationDefinitionQuery = SiddhiCompiler
            .parseAggregationDefinition("define aggregation StockAggregationDefinition " + "from StockStream "
                    + "select StockStream.timestamp, StockStream.symbol as symbol, "
                    + "       StockStream.price as price " + "   group by StockStream.symbol "
                    + "aggregate by timestamp " + "every seconds, minutes, hours ;");

    AggregationDefinition aggregationDefinition = AggregationDefinition.id("StockAggregationDefinition")
            .from(InputStream.stream("StockStream"))
            .select(Selector.basicSelector().select(Expression.variable("timestamp").ofStream("StockStream"))
                    .select("symbol", Expression.variable("symbol").ofStream("StockStream"))
                    .select("price", Expression.variable("price").ofStream("StockStream"))
                    .groupBy(Expression.variable("symbol").ofStream("StockStream")))
            .aggregateBy(Expression.variable("timestamp")).every(TimePeriod.interval(TimePeriod.Duration.SECONDS,
                    TimePeriod.Duration.MINUTES, TimePeriod.Duration.HOURS));

    AssertJUnit.assertEquals(aggregationDefinition, aggregationDefinitionQuery);

}
 
Example #3
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingNestedFilterQuery2() {
    Query query = Query.query();
    query.from(InputStream.stream(
            Query.query().
                    from(InputStream.stream("StockStream").
                            filter(
                                    Expression.compare(
                                            Expression.variable("price").ofStream("StockStream"),
                                            Compare.Operator.GREATER_THAN_EQUAL,
                                            Expression.value(20))
                            ).filter(Expression.isNull(Expression.variable("price").ofStream("StockStream")))).
                    select(
                            Selector.selector().
                                    select("symbol", Expression.variable("symbol")).
                                    select("avgPrice", Expression.function("avg", Expression.variable("price")))
                    ).
                    returns(OutputStream.OutputEventType.CURRENT_EVENTS))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.variable("avgPrice"))
    );
    query.insertInto("IBMOutStockStream");
}
 
Example #4
Source File: SequenceQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSequenceQuery() {
    Query query = Query.query();
    query.from(
            InputStream.sequenceStream(
                    State.next(State.stream(InputStream.stream("e1", "Stream1")),
                            State.stream(InputStream.stream("e2", "Stream1"))
                    )));
    query.insertInto("OutputStream");
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2", 0))).
                    groupBy(Expression.variable("symbol").ofStream("e1")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN,
                            Expression.value(50)))


    );

}
 
Example #5
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingReturnFilterQueryWithExtension2() {
    Query query = Query.query();
    Window window1 = new Window("ext", "Foo");
    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(window1)
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("ext", "avg", Expression.variable("price")))
    );
}
 
Example #6
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingReturnFilterQueryWithExtension3() {
    Query query = Query.query();
    Window window1 = new Window("Foo");
    AssertJUnit.assertFalse(window1.equals("falsewindow"));
    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(window1)
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("ext", "avg", Expression.variable("price")))
    );
}
 
Example #7
Source File: TableQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingUpdateByQuery() {
    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.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.variable("price"))
    );
    query.updateBy("StockQuote", Expression.compare(
            Expression.variable("symbol"),
            Compare.Operator.EQUAL,
            Expression.variable("symbol").ofStream("StockQuote")));
}
 
Example #8
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingReturnFilterQueryWithFunction() {
    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")))
    );

}
 
Example #9
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 #10
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery6() {
    Query query = Query.query();
    query.from(
            InputStream.faultStream("StockStream").
                    filter(
                            Expression.isNullFaultStream("StockStream")
                    )
    );
    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");

    SiddhiApp.siddhiApp("test").addQuery(query);
}
 
Example #11
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery7() {
    Query query = Query.query();
    query.from(
            InputStream.faultStream("StockStream").
                    filter(
                            Expression.isNullFaultStream("StockStream", 1)
                    )
    );
    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");

    SiddhiApp.siddhiApp("test").addQuery(query);
}
 
Example #12
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery9() {
    Query query = Query.query();
    query.from(
            InputStream.faultStream("StockStream").
                    filter(
                            Expression.isNullInnerStream("StockStream")
                    )
    );
    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");

    SiddhiApp.siddhiApp("test").addQuery(query);
}
 
Example #13
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 #14
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = SiddhiAppValidationException.class)
public void testQuery11() {
    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.addQuery(query);
    siddhiApp.addQuery(query);
}
 
Example #15
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void test5() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  AllStockQuotes[price==Foo.price and Foo.try<5]  " +
            "select symbol, avg(price) as avgPrice " +
            "return ;"
    );
    AssertJUnit.assertNotNull(query);
    Query api = Query.query().from(InputStream.stream("AllStockQuotes").
            filter(Expression.and(Expression.compare(Expression.variable("price"), Compare.Operator.EQUAL,
                    Expression.variable("price").ofStream("Foo")), Expression.compare(Expression.variable("try")
                    .ofStream("Foo"), Compare.Operator.LESS_THAN, Expression.value(5))))).
            select(Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))));
    AssertJUnit.assertEquals(api, query);
}
 
Example #16
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void test4() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  AllStockQuotes#window.lengthBatch(50)  " +
            "select symbol, avg(price) as avgPrice " +
            "return ;"
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query().from(InputStream.stream("AllStockQuotes").
            window("lengthBatch", Expression.value(50))).
            select(Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price")))).
            returns();
    AssertJUnit.assertEquals(api, query);
}
 
Example #17
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingNestedFilterQuery() {
    Query query = Query.query();
    query.from(InputStream.stream(
            Query.query().
                    from(InputStream.stream("StockStream").
                            filter(
                                    Expression.compare(
                                            Expression.variable("price").ofStream("StockStream"),
                                            Compare.Operator.GREATER_THAN_EQUAL,
                                            Expression.value(20))
                            ).filter(Expression.isNull(Expression.variable("price").ofStream("StockStream")))).
                    select(
                            Selector.selector().
                                    select("symbol", Expression.variable("symbol")).
                                    select("avgPrice", Expression.function("avg", Expression.variable("price")))
                    ).
                    returns())
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.variable("avgPrice"))
    );
    query.insertInto("IBMOutStockStream");
}
 
Example #18
Source File: TableQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingDeleteQuery() {
    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.compare(Expression.value(100),
                                            Compare.Operator.GREATER_THAN_EQUAL,
                                            Expression.variable("volume")
                                    )
                            )
                    ).window("lengthBatch", Expression.value(50))
    );
    query.deleteBy("StockQuote", Expression.compare(
            Expression.variable("symbol"),
            Compare.Operator.EQUAL,
            Expression.variable("symbol").ofStream("StockQuote")));

}
 
Example #19
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void test1() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  StockStream[price>3]#window.length(50) " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having (price >= 20) " +
            "insert all events into StockQuote; "
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query().from(InputStream.stream("StockStream").
            filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression
                    .value(3))).
            window("length", Expression.value(50))).
            select(Selector.selector().select(Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(
                            Expression.variable("price"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(20)))).
            insertInto("StockQuote", OutputStream.OutputEventType.ALL_EVENTS);
    AssertJUnit.assertEquals(api, query);

}
 
Example #20
Source File: DefineAggregationTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefineAggregationWithExactTimeSpecifier2() {
    AggregationDefinition aggregationDefinition = AggregationDefinition.id("StockAggregation")
            .from(InputStream.stream("StockStream"))
            .select(Selector.basicSelector()
                    .select("timestamp", Expression.variable("timestamp").ofStream("StockStream"))
                    .select("symbol", Expression.variable("symbol").ofStream("StockStream"))
                    .select("price", Expression.variable("price").ofStream("StockStream"))
                    .groupBy(Expression.variable("price").ofStream("StockStream")))
            .aggregateBy(Expression.variable("timestamp"))
            .every(TimePeriod.interval(TimePeriod.Duration.DAYS,
                    TimePeriod.Duration.MONTHS,
                    TimePeriod.Duration.YEARS));
}
 
Example #21
Source File: OutputRateTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingQuery1() {
    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.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("symbol"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(50)
                    ))
    );

    query.output(OutputRate.perTimePeriod(Expression.value(1000L)).output(OutputRate.Type.LAST));
    query.insertInto("StockQuote");

}
 
Example #22
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 #23
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
     * {@inheritDoc}
     * <p>The default implementation returns the result of calling
     * {@link #visitChildren} on {@code ctx}.</p>
     *
     * @param ctx
     */
    @Override
    public Query visitQuery(@NotNull SiddhiQLParser.QueryContext ctx) {

//        query
//        : annotation* query_input query_section? output_rate? (query_output | query_publish)
//        ;

        try {
            Query query = Query.query().from((InputStream) visit(ctx.query_input()));

            if (ctx.query_section() != null) {
                query.select((Selector) visit(ctx.query_section()));
            }
            if (ctx.output_rate() != null) {
                query.output((OutputRate) visit(ctx.output_rate()));
            }
            for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
                query.annotation((Annotation) visit(annotationContext));
            }
            if (ctx.query_output() != null) {
                query.outStream((OutputStream) visit(ctx.query_output()));
            }
            populateQueryContext(query, ctx);
            return query;
        } finally {
            activeStreams.clear();
        }
    }
 
Example #24
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuery1() {
    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("length", Expression.value(50))
    );
    query.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)
                    ))
    );
    query.insertInto("OutStockStream");

}
 
Example #25
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 #26
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery103() throws InterruptedException {
    log.info("Filter test103");

    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.GREATER_THAN_EQUAL, Expression.value(50d))));
    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, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 40d, 10});
    inputHandler.send(new Object[]{"WSO2", 44f, 200d, 56});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();

}
 
Example #27
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery15() {
    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.every(
                                    State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(30))))),
                            State.next(
                                    State.zeroOrOne(
                                            State.stream(InputStream.stream("e2", "Stream1").filter(Expression
                                                    .compare(Expression.variable("price").ofStream("e2", Variable
                                                                    .LAST),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.value(20))))
                                    ),
                                    State.next(
                                            State.stream(InputStream.stream("e3", "Stream2").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.variable("price").ofStream("e1")))),
                                            State.stream(InputStream.stream("e4", "Stream3").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN,
                                                            Expression.value(74)))))
                            )
                    )
            )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2")))
    );
    query.insertInto("OutputStream");
}
 
Example #28
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery14() {
    Query query = Query.query();
    query.from(
            InputStream.patternStream(
                    State.next(
                            State.every(
                                    State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.value(30))))),
                            State.next(
                                    State.zeroOrMany(
                                            State.stream(InputStream.stream("e2", "Stream1").filter(Expression
                                                    .compare(Expression.variable("price").ofStream("e2", Variable
                                                                    .LAST),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.value(20))))
                                    ),
                                    State.next(
                                            State.stream(InputStream.stream("e3", "Stream2").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN_EQUAL,
                                                            Expression.variable("price").ofStream("e1")))),
                                            State.stream(InputStream.stream("e4", "Stream3").filter(Expression
                                                    .compare(Expression.variable("price"),
                                                            Compare.Operator.GREATER_THAN,
                                                            Expression.value(74)))))
                            )
                    )
            )
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2")))
    );
    query.insertInto("OutputStream");
}
 
Example #29
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery107() throws InterruptedException {
    log.info("Filter test107");

    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.GREATER_THAN_EQUAL, 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, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();

}
 
Example #30
Source File: PatternQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatternQuery11() {
    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)))), new TimeConstant(1000)),
                                    State.stream(InputStream.stream("e3", "Stream2").filter(Expression.compare
                                            (Expression.variable("price"),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("price").ofStream("e1")))))
                    )
            )

    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol").ofStream("e1")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price").ofStream("e2"))).
                    groupBy(Expression.variable("symbol").ofStream("e1")).
                    having(Expression.compare(Expression.variable("avgPrice"),
                            Compare.Operator.GREATER_THAN,
                            Expression.value(50)))


    );
    query.insertInto("OutputStream");

}