io.siddhi.query.compiler.SiddhiCompiler Java Examples

The following examples show how to use io.siddhi.query.compiler.SiddhiCompiler. 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: PolicyExecutionPlannerImpl.java    From eagle with Apache License 2.0 6 votes vote down vote up
private String validateAndGet(String executionPlan) {
    try {
        SiddhiCompiler.parse(executionPlan);
        return executionPlan;
    } catch (SiddhiParserException e) {
        // There should be at least 1 stream definition for compiler to parse the execution plan.
        // Therefore, try prepending a IgnoreStream definition to the execution plan.
        String ignoreStreamDef = "define stream IgnoreStream (ignored bool); ";
        try {
            String epWithStreamDef = ignoreStreamDef + executionPlan;
            SiddhiCompiler.parse(epWithStreamDef);
            return epWithStreamDef;
        } catch (Exception ex) {
            return executionPlan;
        }
    }
}
 
Example #2
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 #3
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 #4
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void test2() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  StockStream [price >= 20]#window.lengthBatch(50) " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having avgPrice>50 " +
            "insert into StockQuote; "
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query().from(InputStream.stream("StockStream").
            filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN_EQUAL,
                    Expression.value(20))).
            window("lengthBatch", 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("avgPrice"),
                            Compare.Operator.GREATER_THAN,
                            Expression.value(50)))).
            insertInto("StockQuote");
    AssertJUnit.assertEquals(api, query);
}
 
Example #5
Source File: ExpressionWindowProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private MetaStateEvent constructExpression(MetaStreamEvent metaStreamEvent,
                                           SiddhiQueryContext siddhiQueryContext) {
    Expression expression = SiddhiCompiler.parseExpression(expressionString);
    MetaStreamEvent metaStreamEventFirst = new MetaStreamEventWrapper(metaStreamEvent);
    metaStreamEventFirst.setInputReferenceId("first");
    MetaStreamEvent metaStreamEventLast = new MetaStreamEventWrapper(metaStreamEvent);
    metaStreamEventLast.setInputReferenceId("last");
    MetaStateEvent metaStateEvent = new MetaStateEvent(
            new MetaStreamEvent[]{metaStreamEvent, metaStreamEventFirst, metaStreamEventLast});
    variableExpressionExecutors = new ArrayList<>();
    SiddhiQueryContext exprQueryContext = new SiddhiOnDemandQueryContext(
            siddhiQueryContext.getSiddhiAppContext(), siddhiQueryContext.getName(),
            expressionString);
    expressionExecutor = ExpressionParser.parseExpression(expression, metaStateEvent,
            0, new HashMap<>(), variableExpressionExecutors, false,
            0, ProcessingMode.SLIDE, true, exprQueryContext);
    if (expressionExecutor.getReturnType() != Attribute.Type.BOOL) {
        throw new SiddhiAppRuntimeException("Expression ('" + expressionString + "') does not return Bool");
    }
    return metaStateEvent;
}
 
Example #6
Source File: ExpressionBatchWindowProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private MetaStateEvent constructExpression(MetaStreamEvent metaStreamEvent,
                                           SiddhiQueryContext siddhiQueryContext) {
    Expression expression = SiddhiCompiler.parseExpression(expressionString);
    MetaStreamEvent metaStreamEventFirst = new MetaStreamEventWrapper(metaStreamEvent);
    metaStreamEventFirst.setInputReferenceId("first");
    MetaStreamEvent metaStreamEventLast = new MetaStreamEventWrapper(metaStreamEvent);
    metaStreamEventLast.setInputReferenceId("last");
    MetaStateEvent metaStateEvent = new MetaStateEvent(
            new MetaStreamEvent[]{metaStreamEvent, metaStreamEventFirst, metaStreamEventLast});
    variableExpressionExecutors = new ArrayList<>();
    SiddhiQueryContext exprQueryContext = new SiddhiOnDemandQueryContext(
            siddhiQueryContext.getSiddhiAppContext(), siddhiQueryContext.getName(),
            expressionString);
    expressionExecutor = ExpressionParser.parseExpression(expression, metaStateEvent,
            0, new HashMap<>(), variableExpressionExecutors, false,
            0, ProcessingMode.SLIDE, true, exprQueryContext);
    if (expressionExecutor.getReturnType() != Attribute.Type.BOOL) {
        throw new SiddhiAppRuntimeException("Expression ('" + expressionString + "') does not return Bool");
    }
    return metaStateEvent;
}
 
Example #7
Source File: DefinePartitionTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void test1() {
    Partition partition = SiddhiCompiler.parsePartition("partition with (200>volume as 'LessValue' or 200<=volume" +
            " as 'HighValue' of cseEventStream) begin from cseEventStream select sum(volume) as sumvolume insert " +
            "into StockStream ;  end ");

    AssertJUnit.assertEquals(Partition.partition().
                    with("cseEventStream",
                            Partition.range("LessValue",
                                    Expression.compare(
                                            Expression.value(200),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("volume"))
                            ),
                            Partition.range("HighValue",
                                    Expression.compare(
                                            Expression.value(200),
                                            Compare.Operator.LESS_THAN_EQUAL,
                                            Expression.variable("volume"))
                            )).toString().split("queryList")[0],
            partition.toString().split("queryList")[0]);
}
 
Example #8
Source File: DefineAggregationTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void test1() throws SiddhiParserException {

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

    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("symbol").ofStream("StockStream")))
            .aggregateBy(Expression.variable("timestamp"))
            .every(TimePeriod.range(TimePeriod.Duration.SECONDS, TimePeriod.Duration.DAYS));

    AssertJUnit.assertEquals(aggregationDefinition, aggregationDefinitionQuery);
}
 
Example #9
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 #10
Source File: DefineStreamTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultilevelNestedAnnotations1() throws SiddhiParserException {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition(
            "@sink(url='http://foo.com/test/{{data}}', " +
                    "   @map(type='xml', " +
                    "@payload('<test><time>{{time}}</time></test>')" +
                    "   )" +
                    ") " +
                    "define stream fooStream (id int, name string);"
    );

    AssertJUnit.assertEquals(
            StreamDefinition
                    .id("fooStream")
                    .attribute("id", Attribute.Type.INT)
                    .attribute("name", Attribute.Type.STRING)
                    .annotation(Annotation.annotation("sink")
                            .element("url", "http://foo.com/test/{{data}}")
                            .annotation(Annotation.annotation("map")
                                    .element("type", "xml")
                                    .annotation(Annotation.annotation("payload")
                                            .element("<test><time>{{time}}</time></test>")))),
            streamDefinition);
}
 
Example #11
Source File: DefineStreamTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultilevelNestedAnnotations3() throws SiddhiParserException {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition(
            "@sink(url='http://foo.com/test/{{data}}', " +
                    "   @map(type=\"\"\"xml\"\"\", " +
                    "@payload(\"\"\"{ \"time\":{{time}}}\"\"\") " +
                    "   )" +
                    ") " +
                    "define stream fooStream (id int, name string);"
    );

    AssertJUnit.assertEquals(
            StreamDefinition
                    .id("fooStream")
                    .attribute("id", Attribute.Type.INT)
                    .attribute("name", Attribute.Type.STRING)
                    .annotation(Annotation.annotation("sink")
                            .element("url", "http://foo.com/test/{{data}}")
                            .annotation(Annotation.annotation("map")
                                    .element("type", "xml")
                                    .annotation(Annotation.annotation("payload")
                                            .element("{ \"time\":{{time}}}")))),
            streamDefinition);
}
 
Example #12
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 #13
Source File: AbsentPatternTestCase.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 e1=Stream1[price>20] -> not Stream2[price>e1.price] for 2 sec " +
            "select e1.symbol as symbol1 " +
            "insert into OutputStream ;");
    AssertJUnit.assertNotNull(query);

    Query api = Query.query();
    api.from(
            InputStream.patternStream(State.next(
                    State.stream(InputStream.stream("e1", "Stream1")
                            .filter(Expression.compare(Expression.variable("price"),
                                    Compare.Operator.GREATER_THAN,
                                    Expression.value(20)))),
                    State.logicalNot(State.stream(InputStream.stream("Stream2")
                                    .filter(Expression.compare(Expression.variable("price"),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price").ofStream("e1")))),
                            new TimeConstant(2000)))
            ))
            .select(Selector.selector().select("symbol1", Expression.variable("symbol").ofStream("e1")))
            .insertInto("OutputStream");

    AssertJUnit.assertEquals(api, query);
}
 
Example #14
Source File: AbsentPatternTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = SiddhiParserException.class)
public void test1() throws SiddhiParserException {

    SiddhiCompiler.parseQuery("from e1=Stream1[price>20] -> not Stream2[price>e1.price] " +
            "select e1.symbol as symbol, e1.price as price " +
            "insert into OutputStream ;");
}
 
Example #15
Source File: SimpleQueryValidatorTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = SiddhiAppValidationException.class)
public void testQueryWithNotExistingAttributes() throws InterruptedException {

    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume long);";
    String query = "@info(name = 'query1') from cseEventStream[volume >= 50] select symbol1,price,volume insert " +
            "into outputStream ;";

    SiddhiAppParser.parse(SiddhiCompiler.parse(cseEventStream + query),
            cseEventStream + query, siddhiContext);
}
 
Example #16
Source File: SiddhiAppRuntimeImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public Event[] query(String onDemandQuery) {
    if (this.running) {
        return query(SiddhiCompiler.parseOnDemandQuery(onDemandQuery), onDemandQuery);
    }
    throw new OnDemandQueryCreationException("The siddhi app, '" + this.getName() + "' is currently shut down, " +
            "the on demand query '" + onDemandQuery + "' cannot be executed.");
}
 
Example #17
Source File: QueryAPITestCaseForTableWithCache.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test12() {
    log.info("Test12 - Test output attributes and its types for table");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreDummyForCache\", @Cache(size=\"10\"))\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String storeQuery = "" +
            "from StockTable " +
            "select * ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);

    siddhiAppRuntime.start();
    Attribute[] actualAttributeArray = siddhiAppRuntime.getStoreQueryOutputAttributes(SiddhiCompiler.parseStoreQuery
            (storeQuery));
    Attribute symbolAttribute = new Attribute("symbol", Attribute.Type.STRING);
    Attribute priceAttribute = new Attribute("price", Attribute.Type.FLOAT);
    Attribute volumeAttribute = new Attribute("volume", Attribute.Type.LONG);
    Attribute[] expectedAttributeArray = new Attribute[]{symbolAttribute, priceAttribute, volumeAttribute};
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);

    storeQuery = "" +
            "from StockTable " +
            "select symbol, sum(volume) as totalVolume ;";

    actualAttributeArray = siddhiAppRuntime.getStoreQueryOutputAttributes(SiddhiCompiler.parseStoreQuery
            (storeQuery));
    Attribute totalVolumeAttribute = new Attribute("totalVolume", Attribute.Type.LONG);
    expectedAttributeArray = new Attribute[]{symbolAttribute, totalVolumeAttribute};
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);
}
 
Example #18
Source File: SimpleQueryValidatorTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testInvalidFilterCondition2() throws InterruptedException {
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume long);";
    String query = "@info(name = 'query1') from cseEventStream[not(price)] select symbol,price,volume insert into" +
            " outputStream ;";

    SiddhiAppParser.parse(SiddhiCompiler.parse(cseEventStream + query),
            cseEventStream + query, siddhiContext);
}
 
Example #19
Source File: SimpleQueryValidatorTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplexFilterQuery1() throws InterruptedException {
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume long, available " +
            "bool);";
    String query = "@info(name = 'query1') from cseEventStream[available] select symbol,price,volume insert into " +
            "outputStream ;";

    SiddhiAppParser.parse(SiddhiCompiler.parse(cseEventStream + query),
            cseEventStream + query, siddhiContext);
}
 
Example #20
Source File: QueryAPITestCaseForTestStore.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test12() {
    log.info("Test12 - Test output attributes and its types for table");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long); " +
            "@Store(type=\"testStoreContainingInMemoryTable\")\n" +
            "define table StockTable (symbol string, price float, volume long); ";
    String storeQuery = "" +
            "from StockTable " +
            "select * ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);

    siddhiAppRuntime.start();
    Attribute[] actualAttributeArray = siddhiAppRuntime.getStoreQueryOutputAttributes(SiddhiCompiler.parseStoreQuery
            (storeQuery));
    Attribute symbolAttribute = new Attribute("symbol", Attribute.Type.STRING);
    Attribute priceAttribute = new Attribute("price", Attribute.Type.FLOAT);
    Attribute volumeAttribute = new Attribute("volume", Attribute.Type.LONG);
    Attribute[] expectedAttributeArray = new Attribute[]{symbolAttribute, priceAttribute, volumeAttribute};
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);

    storeQuery = "" +
            "from StockTable " +
            "select symbol, sum(volume) as totalVolume ;";

    actualAttributeArray = siddhiAppRuntime.getStoreQueryOutputAttributes(SiddhiCompiler.parseStoreQuery
            (storeQuery));
    Attribute totalVolumeAttribute = new Attribute("totalVolume", Attribute.Type.LONG);
    expectedAttributeArray = new Attribute[]{symbolAttribute, totalVolumeAttribute};
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);
}
 
Example #21
Source File: OnDemandQueryTableTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test12() throws InterruptedException {
    log.info("Test12 - Test output attributes and its types for table");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long);" +
            "@PrimaryKey('symbol') " +
            "define table StockTable (symbol string, price float, volume long); ";
    String onDemandQuery = "" +
            "from StockTable " +
            "select * ;";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);

    siddhiAppRuntime.start();
    Attribute[] actualAttributeArray = siddhiAppRuntime.getOnDemandQueryOutputAttributes(
            SiddhiCompiler.parseOnDemandQuery(onDemandQuery));
    Attribute symbolAttribute = new Attribute("symbol", Attribute.Type.STRING);
    Attribute priceAttribute = new Attribute("price", Attribute.Type.FLOAT);
    Attribute volumeAttribute = new Attribute("volume", Attribute.Type.LONG);
    Attribute[] expectedAttributeArray = new Attribute[]{symbolAttribute, priceAttribute, volumeAttribute};
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);

    onDemandQuery = "" +
            "from StockTable " +
            "select symbol, sum(volume) as totalVolume ;";

    actualAttributeArray = siddhiAppRuntime.getOnDemandQueryOutputAttributes(SiddhiCompiler.parseOnDemandQuery
            (onDemandQuery));
    Attribute totalVolumeAttribute = new Attribute("totalVolume", Attribute.Type.LONG);
    expectedAttributeArray = new Attribute[]{symbolAttribute, totalVolumeAttribute};
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);
}
 
Example #22
Source File: AbsentPatternTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = SiddhiParserException.class)
public void test3() throws SiddhiParserException {

    SiddhiCompiler.parseQuery("from not Stream1[price>20] for 1 sec -> not Stream2[price>e1.price] for 1 sec " +
            "select e1.symbol as symbol, e1.price as price " +
            "insert into OutputStream ;");
}
 
Example #23
Source File: AbsentPatternTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = SiddhiParserException.class)
public void test2() throws SiddhiParserException {

    SiddhiCompiler.parseQuery("from e1=Stream1[price>20] -> not e2=Stream2[price>e1.price] for 1 sec " +
            "select e1.symbol as symbol, e1.price as price " +
            "insert into OutputStream ;");
}
 
Example #24
Source File: DefineStreamTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingStreamDefinition() {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("define stream StockStream ( symbol " +
            "string, price int, volume float );");
    StreamDefinition api = StreamDefinition.id("StockStream").attribute("symbol", Attribute.Type.STRING)
            .attribute("price", Attribute.Type.INT).attribute("volume", Attribute.Type.FLOAT);
    AssertJUnit.assertEquals(api, streamDefinition);
}
 
Example #25
Source File: OnDemandQueryTableTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test13() throws InterruptedException {
    log.info("Test13 - Test output attributes and its types for aggregation table");

    SiddhiManager siddhiManager = new SiddhiManager();

    String streams = "" +
            "define stream StockStream (symbol string, price float, volume long);" +
            "define aggregation StockTableAg " +
            "from StockStream " +
            "select symbol, price " +
            "group by symbol " +
            "aggregate every minutes ...year;";
    String onDemandQuery = "" +
            "from StockTableAg within '2018-**-** **:**:**' per 'minutes' select symbol, price ";

    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);

    siddhiAppRuntime.start();
    Attribute[] actualAttributeArray = siddhiAppRuntime.getOnDemandQueryOutputAttributes(
            SiddhiCompiler.parseOnDemandQuery(onDemandQuery));
    Attribute symbolAttribute = new Attribute("symbol", Attribute.Type.STRING);
    Attribute priceAttribute = new Attribute("price", Attribute.Type.FLOAT);
    Attribute[] expectedAttributeArray = new Attribute[]{symbolAttribute, priceAttribute};
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);

    onDemandQuery = "" +
            "from StockTableAg within '2018-**-** **:**:**' per 'minutes' select symbol, sum(price) as total";

    actualAttributeArray = siddhiAppRuntime.getOnDemandQueryOutputAttributes(SiddhiCompiler.parseOnDemandQuery
            (onDemandQuery));
    Attribute totalVolumeAttribute = new Attribute("total", Attribute.Type.DOUBLE);
    expectedAttributeArray = new Attribute[]{symbolAttribute, totalVolumeAttribute};
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);
}
 
Example #26
Source File: DefineStreamTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultilevelNestedAnnotations2() throws SiddhiParserException {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("" +
            "@source(" +
            "   type='http', " +
            "   context='/test', " +
            "   transport='http,https', " +
            "   @map(" +
            "       type='xml', " +
            "       namespace = \"h=uri, a=uri\", " +
            "       @attributes(" +
            "           '//h:time', " +
            "           '//h:data'" +
            "       )" +
            "   )" +
            ") " +
            "define stream fooStream (id int, name string);");

    AssertJUnit.assertEquals(
            StreamDefinition
                    .id("fooStream")
                    .attribute("id", Attribute.Type.INT)
                    .attribute("name", Attribute.Type.STRING)
                    .annotation(Annotation.annotation("source")
                            .element("type", "http")
                            .element("context", "/test")
                            .element("transport", "http,https")
                            .annotation(Annotation.annotation("map")
                                    .element("type", "xml")
                                    .element("namespace", "h=uri, a=uri")
                                    .annotation(Annotation.annotation("attributes")
                                            .element("//h:time")
                                            .element("//h:data")
                                    )
                            )
                    ),
            streamDefinition);
}
 
Example #27
Source File: QueryStoreTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test1() throws SiddhiParserException {
    StoreQuery query = SiddhiCompiler.parseStoreQuery("" +
            "from StockTable " +
            "on price>3 " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having (price >= 20) ;"
    );
    AssertJUnit.assertNotNull(query);

    StoreQuery api = StoreQuery.query().
            from(
                    InputStore.store("StockTable").
                            on(Expression.compare(Expression.variable("price"),
                                    Compare.Operator.GREATER_THAN,
                                    Expression.value(3)))).
            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))
                            )
            );

    AssertJUnit.assertEquals(api, query);


}
 
Example #28
Source File: DefineStreamTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testEqualObjects() throws SiddhiParserException {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("@Foo(name='bar','Custom')define " +
            "stream cseStream ( symbol string, price int, volume float )");
    AssertJUnit.assertEquals(StreamDefinition.
                    id("cseStream").
                    attribute("symbol", Attribute.Type.STRING).
                    attribute("price", Attribute.Type.INT).
                    attribute("volume", Attribute.Type.FLOAT).annotation(Annotation.annotation("Foo").element
                    ("name", "bar").element("Custom")),
            streamDefinition);
}
 
Example #29
Source File: DefineStreamTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingStreamDefinition2() {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("define stream StockStream ( symbol " +
            "string, price int, volume double, data Object );");
    StreamDefinition api = StreamDefinition.id("StockStream").attribute("symbol", Attribute.Type.STRING)
            .attribute("price", Attribute.Type.INT).attribute("volume", Attribute.Type.DOUBLE).attribute("data",
                    Attribute.Type.OBJECT);
    AssertJUnit.assertEquals(api, streamDefinition);
}
 
Example #30
Source File: DefineStreamTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = SiddhiParserException.class)
    public void testCreatingStreamWithDuplicateAttribute() {
        StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("define stream StockStream ( symbol " +
                "string, symbol int, volume float );");
//        StreamDefinition.id("StockStream").attribute("symbol", Attribute.Type.STRING).attribute("symbol", Attribute
// .Type.INT).attribute("volume", Attribute.Type.FLOAT);
    }