io.siddhi.query.api.expression.Expression Java Examples

The following examples show how to use io.siddhi.query.api.expression.Expression. 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: CacheTable.java    From siddhi with Apache License 2.0 6 votes vote down vote up
public CacheCompiledConditionWithRouteToCache generateCacheCompileCondition(
        Expression condition, MatchingMetaInfoHolder storeMatchingMetaInfoHolder,
        SiddhiQueryContext siddhiQueryContext, List<VariableExpressionExecutor> storeVariableExpressionExecutors) {
    boolean routeToCache = checkConditionToRouteToCache(condition, storeMatchingMetaInfoHolder);
    MetaStateEvent metaStateEvent = new MetaStateEvent(storeMatchingMetaInfoHolder.getMetaStateEvent().
            getMetaStreamEvents().length);
    for (MetaStreamEvent referenceMetaStreamEvent : storeMatchingMetaInfoHolder.getMetaStateEvent().
            getMetaStreamEvents()) {
        metaStateEvent.addEvent(referenceMetaStreamEvent);
    }
    MatchingMetaInfoHolder matchingMetaInfoHolder = new MatchingMetaInfoHolder(
            metaStateEvent,
            storeMatchingMetaInfoHolder.getMatchingStreamEventIndex(),
            storeMatchingMetaInfoHolder.getStoreEventIndex(),
            storeMatchingMetaInfoHolder.getMatchingStreamDefinition(),
            this.tableDefinition,
            storeMatchingMetaInfoHolder.getCurrentState());

    Map<String, Table> tableMap = new ConcurrentHashMap<>();
    tableMap.put(this.tableDefinition.getId(), this);

    return new CacheCompiledConditionWithRouteToCache(compileCondition(condition, matchingMetaInfoHolder,
            storeVariableExpressionExecutors, tableMap, siddhiQueryContext, routeToCache), routeToCache);
}
 
Example #2
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 #3
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 #4
Source File: AggregationExpressionVisitor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
public void addVariableExpression(Expression expression) {
    Variable variable = (Variable) expression;
    String streamId = variable.getStreamId();
    if (streamId == null) {
        if (this.allAttributesList.contains(variable.getAttributeName())) {
            this.conditionOperands.push(expression);
        } else {
            this.conditionOperands.push("true");
        }
    } else {
        if (streamId.equals(inputStreamRefId)) {
            this.conditionOperands.push(expression);
        } else if (this.tableAttributesNameList.contains(variable.getAttributeName())) {
            this.conditionOperands.push(expression);
        } else {
            this.conditionOperands.push("true");
        }
    }

}
 
Example #5
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 #6
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 6 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 Expression visitAddition_math_operation(@NotNull SiddhiQLParser.Addition_math_operationContext ctx) {
    Expression expression;
    if (ctx.add != null) {
        expression = Expression.add((Expression) visit(ctx.math_operation(0)),
                (Expression) visit(ctx.math_operation(1)));
    } else if (ctx.substract != null) {
        expression = Expression.subtract((Expression) visit(ctx.math_operation(0)),
                (Expression) visit(ctx.math_operation(1)));
    } else {
        throw newSiddhiParserException(ctx);
    }
    populateQueryContext(expression, ctx);
    return expression;
}
 
Example #7
Source File: OnDemandQueryParser.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private static OnDemandQueryRuntime getOnDemandQueryRuntime(OnDemandQuery onDemandQuery,
                                                            Map<String, Table> tableMap,
                                                            Map<String, Window> windowMap,
                                                            int metaPosition, LockWrapper lockWrapper,
                                                            MetaStreamEvent metaStreamEvent,
                                                            OutputStream outputStream, Expression onCondition,
                                                            SiddhiQueryContext siddhiQueryContext) {
    try {
        List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
        Table table = tableMap.get(outputStream.getId());

        if (table != null) {
            return constructOnDemandQueryRuntime(table, onDemandQuery, tableMap, windowMap,
                    metaPosition, onCondition, metaStreamEvent,
                    variableExpressionExecutors, lockWrapper, siddhiQueryContext);
        } else {
            throw new OnDemandQueryCreationException(outputStream.getId() + " is not a table.");
        }

    } finally {
        SnapshotService.getSkipStateStorageThreadLocal().set(null);
    }
}
 
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: LossyFrequentWindowProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public CompiledCondition compileCondition(Expression condition, MatchingMetaInfoHolder matchingMetaInfoHolder,
                                          List<VariableExpressionExecutor> variableExpressionExecutors,
                                          Map<String, Table> tableMap, WindowState state,
                                          SiddhiQueryContext siddhiQueryContext) {
    return OperatorParser.constructOperator(state.map.values(), condition, matchingMetaInfoHolder,
            variableExpressionExecutors, tableMap, siddhiQueryContext);
}
 
Example #10
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 #11
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 #12
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery76() throws InterruptedException {
    log.info("Filter test76");

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.LESS_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")));
    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});
    inputHandler.send(new Object[]{"WSO2", 70f, 40L});
    inputHandler.send(new Object[]{"WSO2", 44f, 200L});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();
}
 
Example #13
Source File: AggregationParser.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private static void validateBaseAggregators(List<IncrementalAttributeAggregator> incrementalAttributeAggregators,
                                            IncrementalAttributeAggregator incrementalAttributeAggregator,
                                            Attribute[] baseAttributes, Expression[] baseAttributeInitialValues,
                                            Expression[] baseAggregators, int i) {
    for (int i1 = i; i1 < incrementalAttributeAggregators.size(); i1++) {
        IncrementalAttributeAggregator otherAttributeAggregator = incrementalAttributeAggregators.get(i1);
        if (otherAttributeAggregator != incrementalAttributeAggregator) {
            Attribute[] otherBaseAttributes = otherAttributeAggregator.getBaseAttributes();
            Expression[] otherBaseAttributeInitialValues = otherAttributeAggregator
                    .getBaseAttributeInitialValues();
            Expression[] otherBaseAggregators = otherAttributeAggregator.getBaseAggregators();
            for (int j = 0; j < otherBaseAttributes.length; j++) {
                if (baseAttributes[i].equals(otherBaseAttributes[j])) {
                    if (!baseAttributeInitialValues[i].equals(otherBaseAttributeInitialValues[j])) {
                        throw new SiddhiAppCreationException("BaseAttributes having same name should " +
                                "be defined with same initial values, but baseAttribute '" +
                                baseAttributes[i] + "' is defined in '" +
                                incrementalAttributeAggregator.getClass().getName() + "' and '" +
                                otherAttributeAggregator.getClass().getName() +
                                "' with different initial values.");
                    }
                    if (!baseAggregators[i].equals(otherBaseAggregators[j])) {
                        throw new SiddhiAppCreationException("BaseAttributes having same name should " +
                                "be defined with same baseAggregators, but baseAttribute '" +
                                baseAttributes[i] + "' is defined in '" +
                                incrementalAttributeAggregator.getClass().getName() + "' and '" +
                                otherAttributeAggregator.getClass().getName() +
                                "' with different baseAggregators.");
                    }
                }
            }
        }
    }
}
 
Example #14
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery75() throws InterruptedException {
    log.info("Filter test75");

    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.LESS_THAN_EQUAL,
            Expression.value(3L))));
    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", 500f, 60d, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 60f, 300d, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();

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

    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("volume"),
            Compare.Operator.LESS_THAN_EQUAL,
            Expression.value(200L))));
    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, 300d, 4});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();

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

    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(50d))));
    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, 1, count, 100);
    siddhiAppRuntime.shutdown();
}
 
Example #17
Source File: ExpressionWindowProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public CompiledCondition compileCondition(Expression condition, MatchingMetaInfoHolder matchingMetaInfoHolder,
                                          List<VariableExpressionExecutor> variableExpressionExecutors,
                                          Map<String, Table> tableMap, WindowState state,
                                          SiddhiQueryContext siddhiQueryContext) {
    return OperatorParser.constructOperator(state.expiredEventQueue, condition, matchingMetaInfoHolder,
            variableExpressionExecutors, tableMap, siddhiQueryContext);
}
 
Example #18
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 Object visitOr_math_operation(@NotNull SiddhiQLParser.Or_math_operationContext ctx) {
    if (ctx.OR() != null) {
        Expression expression = Expression.or((Expression) visit(ctx.math_operation(0)),
                (Expression) visit(ctx.math_operation(1)));
        populateQueryContext(expression, ctx);
        return expression;
    } else {
        throw newSiddhiParserException(ctx);
    }
}
 
Example #19
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery90() throws InterruptedException {
    log.info("Filter test90");

    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.LESS_THAN, Expression.value(10f))));
    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 #20
Source File: AndCollectionExpression.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public AndCollectionExpression(Expression expression, CollectionScope collectionScope, CollectionExpression
        leftCollectionExpression, CollectionExpression rightCollectionExpression) {
    this.expression = expression;
    this.collectionScope = collectionScope;
    this.leftCollectionExpression = leftCollectionExpression;
    this.rightCollectionExpression = rightCollectionExpression;
    multiPrimaryKeys.addAll(leftCollectionExpression.getMultiPrimaryKeys());
    multiPrimaryKeys.addAll(rightCollectionExpression.getMultiPrimaryKeys());
}
 
Example #21
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery84() throws InterruptedException {
    log.info("Filter test84");

    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("price"),
            Compare.Operator.LESS_THAN,
            Expression.value(60L))));
    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", 500f, 50d, 6});
    inputHandler.send(new Object[]{"WSO2", 70f, 60d, 2});
    inputHandler.send(new Object[]{"WSO2", 50f, 300d, 4});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    AssertJUnit.assertEquals(1, count.get());
    siddhiAppRuntime.shutdown();
}
 
Example #22
Source File: FrequentWindowProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public CompiledCondition compileCondition(Expression condition, MatchingMetaInfoHolder matchingMetaInfoHolder,
                                          List<VariableExpressionExecutor> variableExpressionExecutors,
                                          Map<String, Table> tableMap, WindowState state,
                                          SiddhiQueryContext siddhiQueryContext) {
    return OperatorParser.constructOperator(state.map.values(), condition, matchingMetaInfoHolder,
            variableExpressionExecutors, tableMap, siddhiQueryContext);
}
 
Example #23
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 #24
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery63() throws InterruptedException {
    log.info("Filter test63");

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"),
            Compare.Operator.EQUAL, Expression.value(40d))));
    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, 60L});
    inputHandler.send(new Object[]{"WSO2", 70f, 40L});
    inputHandler.send(new Object[]{"WSO2", 44f, 200L});
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();

}
 
Example #25
Source File: AvgIncrementalAttributeAggregator.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public Expression[] getBaseAggregators() {
    Expression sumAggregator = Expression.function("sum",
            Expression.variable(getBaseAttributes()[0].getName()));
    Expression countAggregator = Expression.function("sum",
            Expression.variable(getBaseAttributes()[1].getName()));
    return new Expression[]{sumAggregator, countAggregator};
}
 
Example #26
Source File: DelayWindowProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public CompiledCondition compileCondition(Expression condition, MatchingMetaInfoHolder matchingMetaInfoHolder,
                                          List<VariableExpressionExecutor> variableExpressionExecutors,
                                          Map<String, Table> tableMap, WindowState state,
                                          SiddhiQueryContext siddhiQueryContext) {
    return OperatorParser.constructOperator(((DelayedWindowState) state).delayedEventQueue,
            condition, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
}
 
Example #27
Source File: FilterTestCase1.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterQuery71() throws InterruptedException {
    log.info("Filter test71");

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

    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"),
            Compare.Operator.LESS_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")));
    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});
    inputHandler.send(new Object[]{"WSO2", 70f, 40L});
    inputHandler.send(new Object[]{"WSO2", 44f, 200L});
    SiddhiTestHelper.waitForEvents(10, 2, count, 100);
    siddhiAppRuntime.shutdown();
}
 
Example #28
Source File: FilterTestCase2.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = SiddhiAppCreationException.class)
public void testFilterQuery108() throws InterruptedException {
    log.info("Filter test108");

    SiddhiManager siddhiManager = new SiddhiManager();

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

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

    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);

}
 
Example #29
Source File: Window.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public CompiledCondition compileCondition(Expression condition, MatchingMetaInfoHolder matchingMetaInfoHolder,
                                          List<VariableExpressionExecutor> variableExpressionExecutors,
                                          Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
    if (this.internalWindowProcessor instanceof FindableProcessor) {
        return ((FindableProcessor) this.internalWindowProcessor).compileCondition(condition,
                matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
    } else {
        throw new OperationNotSupportedException("Cannot construct finder for the window " + this
                .windowDefinition.getWindow());
    }

}
 
Example #30
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test15() {
    Query queryString = SiddhiCompiler.parseQuery("" +
            "from  StockStream[7+9.5 > price and 100 >= volume]#window.length(50) " +
            " " +
            "select symbol as symbol, price as price, volume as volume  " +
            "update StockQuote \n " +
            " on symbol==StockQuote.symbol ;"
    );
    AssertJUnit.assertNotNull(queryString);

    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("price", Expression.variable("price")).
                    select("volume", Expression.variable("volume"))
    );
    query.updateBy("StockQuote", OutputStream.OutputEventType.CURRENT_EVENTS,
            Expression.compare(
                    Expression.variable("symbol"),
                    Compare.Operator.EQUAL,
                    Expression.variable("symbol").ofStream("StockQuote")));

    AssertJUnit.assertEquals(query, queryString);
}