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);
}
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;
}
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();
}
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);
}
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 };
}
Aggregations