Search in sources :

Example 56 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class BLangPackageBuilder method createTernaryExpr.

public void createTernaryExpr(DiagnosticPos pos, Set<Whitespace> ws) {
    BLangTernaryExpr ternaryExpr = (BLangTernaryExpr) TreeBuilder.createTernaryExpressionNode();
    ternaryExpr.pos = pos;
    ternaryExpr.addWS(ws);
    ternaryExpr.elseExpr = (BLangExpression) exprNodeStack.pop();
    ternaryExpr.thenExpr = (BLangExpression) exprNodeStack.pop();
    ternaryExpr.expr = (BLangExpression) exprNodeStack.pop();
    if (ternaryExpr.expr.getKind() == NodeKind.TERNARY_EXPR) {
        // Re-organizing ternary expression tree if there nested ternary expressions.
        BLangTernaryExpr root = (BLangTernaryExpr) ternaryExpr.expr;
        BLangTernaryExpr parent = root;
        while (parent.elseExpr.getKind() == NodeKind.TERNARY_EXPR) {
            parent = (BLangTernaryExpr) parent.elseExpr;
        }
        ternaryExpr.expr = parent.elseExpr;
        parent.elseExpr = ternaryExpr;
        ternaryExpr = root;
    }
    addExpressionNode(ternaryExpr);
}
Also used : BLangTernaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr)

Example 57 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class ServiceProtoUtils method getInvocationExpression.

private static BLangInvocation getInvocationExpression(BlockNode body) {
    if (body == null) {
        return null;
    }
    for (StatementNode statementNode : body.getStatements()) {
        BLangExpression expression = null;
        // example : conn.send inside while block.
        if (statementNode instanceof BLangWhile) {
            BLangWhile langWhile = (BLangWhile) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langWhile.getBody());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : conn.send inside for block.
        if (statementNode instanceof BLangForeach) {
            BLangForeach langForeach = (BLangForeach) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langForeach.getBody());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : conn.send inside if block.
        if (statementNode instanceof BLangIf) {
            BLangIf langIf = (BLangIf) statementNode;
            BLangInvocation invocExp = getInvocationExpression(langIf.getBody());
            if (invocExp != null) {
                return invocExp;
            }
            invocExp = getInvocationExpression((BLangBlockStmt) langIf.getElseStatement());
            if (invocExp != null) {
                return invocExp;
            }
        }
        // example : _ = conn.send(msg);
        if (statementNode instanceof BLangAssignment) {
            BLangAssignment assignment = (BLangAssignment) statementNode;
            expression = assignment.getExpression();
        }
        // example : grpc:HttpConnectorError err = conn.send(msg);
        if (statementNode instanceof BLangVariableDef) {
            BLangVariableDef variableDef = (BLangVariableDef) statementNode;
            BLangVariable variable = variableDef.getVariable();
            expression = variable.getInitialExpression();
        }
        if (expression != null && expression instanceof BLangInvocation) {
            BLangInvocation invocation = (BLangInvocation) expression;
            if ("send".equals(invocation.getName().getValue())) {
                return invocation;
            }
        }
    }
    return null;
}
Also used : BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 58 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project siddhi by wso2.

the class SetUpdateInMemoryTableTestCase method updateFromTableTest5.

@Test
public void updateFromTableTest5() throws InterruptedException, SQLException {
    log.info("SET-RDBMS-update test case 5: assignment expression containing an output attribute " + "with a basic arithmatic operation.");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long); " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream " + "select price + 100 as newPrice , symbol " + "update StockTable " + "set StockTable.price = newPrice + 100 " + "   on StockTable.symbol == symbol ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 100L });
    updateStockStream.send(new Object[] { "IBM", 100f, 100L });
    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 59 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project siddhi by wso2.

the class AggregationRuntime method compileExpression.

public CompiledCondition compileExpression(Expression expression, Within within, Expression per, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, String queryName, SiddhiAppContext siddhiAppContext) {
    Map<TimePeriod.Duration, CompiledCondition> withinTableCompiledConditions = new HashMap<>();
    CompiledCondition withinInMemoryCompileCondition;
    CompiledCondition onCompiledCondition;
    List<Attribute> additionalAttributes = new ArrayList<>();
    // Define additional attribute list
    additionalAttributes.add(new Attribute("_START", Attribute.Type.LONG));
    additionalAttributes.add(new Attribute("_END", Attribute.Type.LONG));
    // Get table definition. Table definitions for all the tables used to persist aggregates are similar.
    // Therefore it's enough to get the definition from one table.
    AbstractDefinition tableDefinition = ((Table) aggregationTables.values().toArray()[0]).getTableDefinition();
    // Alter existing meta stream event or create new one if a meta stream doesn't exist
    // After calling this method the original MatchingMetaInfoHolder's meta stream event would be altered
    MetaStreamEvent newMetaStreamEventWithStartEnd = createNewMetaStreamEventWithStartEnd(matchingMetaInfoHolder, additionalAttributes);
    MatchingMetaInfoHolder alteredMatchingMetaInfoHolder = null;
    // Alter meta info holder to contain stream event and aggregate both when it's a store query
    if (matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length == 1) {
        matchingMetaInfoHolder = alterMetaInfoHolderForStoreQuery(newMetaStreamEventWithStartEnd, matchingMetaInfoHolder);
        alteredMatchingMetaInfoHolder = matchingMetaInfoHolder;
    }
    // Create new MatchingMetaInfoHolder containing newMetaStreamEventWithStartEnd and table meta event
    MatchingMetaInfoHolder streamTableMetaInfoHolderWithStartEnd = createNewStreamTableMetaInfoHolder(newMetaStreamEventWithStartEnd, tableDefinition);
    // Create per expression executor
    ExpressionExecutor perExpressionExecutor = ExpressionParser.parseExpression(per, matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
    if (perExpressionExecutor.getReturnType() != Attribute.Type.STRING) {
        throw new SiddhiAppCreationException("Query " + queryName + "'s per value expected a string but found " + perExpressionExecutor.getReturnType(), per.getQueryContextStartIndex(), per.getQueryContextEndIndex());
    }
    // Create within expression
    Expression withinExpression;
    Expression start = Expression.variable(additionalAttributes.get(0).getName());
    Expression end = Expression.variable(additionalAttributes.get(1).getName());
    Expression compareWithStartTime = Compare.compare(start, Compare.Operator.LESS_THAN_EQUAL, Expression.variable("AGG_TIMESTAMP"));
    Expression compareWithEndTime = Compare.compare(Expression.variable("AGG_TIMESTAMP"), Compare.Operator.LESS_THAN, end);
    withinExpression = Expression.and(compareWithStartTime, compareWithEndTime);
    // Create start and end time expression
    Expression startEndTimeExpression;
    if (within.getTimeRange().size() == 1) {
        startEndTimeExpression = new AttributeFunction("incrementalAggregator", "startTimeEndTime", within.getTimeRange().get(0));
    } else {
        // within.getTimeRange().size() == 2
        startEndTimeExpression = new AttributeFunction("incrementalAggregator", "startTimeEndTime", within.getTimeRange().get(0), within.getTimeRange().get(1));
    }
    ExpressionExecutor startTimeEndTimeExpressionExecutor = ExpressionParser.parseExpression(startEndTimeExpression, matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
    // These compile conditions are used to check whether the aggregates in tables are within the given duration.
    for (Map.Entry<TimePeriod.Duration, Table> entry : aggregationTables.entrySet()) {
        CompiledCondition withinTableCompileCondition = entry.getValue().compileCondition(withinExpression, streamTableMetaInfoHolderWithStartEnd, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
        withinTableCompiledConditions.put(entry.getKey(), withinTableCompileCondition);
    }
    // Create compile condition for in-memory data.
    // This compile condition is used to check whether the running aggregates (in-memory data)
    // are within given duration
    withinInMemoryCompileCondition = OperatorParser.constructOperator(new ComplexEventChunk<>(true), withinExpression, streamTableMetaInfoHolderWithStartEnd, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    // On compile condition.
    // After finding all the aggregates belonging to within duration, the final on condition (given as
    // "on stream1.name == aggregator.nickName ..." in the join query) must be executed on that data.
    // This condition is used for that purpose.
    onCompiledCondition = OperatorParser.constructOperator(new ComplexEventChunk<>(true), expression, matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    return new IncrementalAggregateCompileCondition(withinTableCompiledConditions, withinInMemoryCompileCondition, onCompiledCondition, tableMetaStreamEvent, aggregateMetaSteamEvent, additionalAttributes, alteredMatchingMetaInfoHolder, perExpressionExecutor, startTimeEndTimeExpressionExecutor);
}
Also used : Table(org.wso2.siddhi.core.table.Table) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) Attribute(org.wso2.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) ArrayList(java.util.ArrayList) IncrementalAggregateCompileCondition(org.wso2.siddhi.core.util.collection.operator.IncrementalAggregateCompileCondition) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) AttributeFunction(org.wso2.siddhi.query.api.expression.AttributeFunction) CompiledCondition(org.wso2.siddhi.core.util.collection.operator.CompiledCondition) Expression(org.wso2.siddhi.query.api.expression.Expression) MatchingMetaInfoHolder(org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) HashMap(java.util.HashMap) Map(java.util.Map) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 60 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project siddhi by wso2.

the class AggregationParser method setTimeStampTimeZoneExecutors.

private static ExpressionExecutor[] setTimeStampTimeZoneExecutors(AggregationDefinition aggregationDefinition, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, String aggregatorName, MetaStreamEvent metaStreamEvent) {
    Expression timestampExpression = aggregationDefinition.getAggregateAttribute();
    Expression timeZoneExpression;
    ExpressionExecutor timestampExecutor;
    ExpressionExecutor timeZoneExecutor;
    boolean isSystemTimeBased = false;
    // When execution is based on system time, the system's time zone would be used.
    if (timestampExpression == null) {
        isSystemTimeBased = true;
        timestampExpression = AttributeFunction.function("currentTimeMillis", null);
    }
    timestampExecutor = ExpressionParser.parseExpression(timestampExpression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
    if (timestampExecutor.getReturnType() == Attribute.Type.STRING) {
        Expression expression = AttributeFunction.function("incrementalAggregator", "timestampInMilliseconds", timestampExpression);
        timestampExecutor = ExpressionParser.parseExpression(expression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
        timeZoneExpression = AttributeFunction.function("incrementalAggregator", "getTimeZone", timestampExpression);
        timeZoneExecutor = ExpressionParser.parseExpression(timeZoneExpression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
    } else if (timestampExecutor.getReturnType() == Attribute.Type.LONG) {
        if (isSystemTimeBased) {
            timeZoneExpression = AttributeFunction.function("incrementalAggregator", "getTimeZone", null);
            timeZoneExecutor = ExpressionParser.parseExpression(timeZoneExpression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
        } else {
            // If long value is given, it's assumed that the
            timeZoneExpression = Expression.value("+00:00");
            // time zone is GMT
            timeZoneExecutor = ExpressionParser.parseExpression(timeZoneExpression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
        }
    } else {
        throw new SiddhiAppCreationException("AggregationDefinition '" + aggregationDefinition.getId() + "'s aggregateAttribute expects " + "long or string, but found " + timestampExecutor.getReturnType() + ". " + "Hence, can't create the siddhi app '" + siddhiAppContext.getName() + "'", timestampExpression.getQueryContextStartIndex(), timestampExpression.getQueryContextEndIndex());
    }
    return new ExpressionExecutor[] { timestampExecutor, timeZoneExecutor };
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) Expression(org.wso2.siddhi.query.api.expression.Expression) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException)

Aggregations

Expression (org.wso2.siddhi.query.api.expression.Expression)32 ArrayList (java.util.ArrayList)20 Attribute (org.wso2.siddhi.query.api.definition.Attribute)16 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)15 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)15 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)13 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)13 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)13 Variable (org.wso2.siddhi.query.api.expression.Variable)11 Test (org.testng.annotations.Test)10 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)10 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)10 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)10 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)9 OutputAttribute (org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute)8 CompiledCondition (org.wso2.siddhi.core.util.collection.operator.CompiledCondition)7 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)6 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)6 MatchingMetaInfoHolder (org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)6 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)6