Search in sources :

Example 21 with VariableExpressionExecutor

use of org.wso2.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.

the class EventTestCase method testUpdateMetaEvent.

@Test
public void testUpdateMetaEvent() {
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    Attribute price = new Attribute("price", Attribute.Type.FLOAT);
    Attribute volume = new Attribute("volume", Attribute.Type.INT);
    Attribute symbol = new Attribute("symbol", Attribute.Type.STRING);
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addData(volume);
    metaStreamEvent.addData(price);
    metaStreamEvent.addData(symbol);
    metaStreamEvent.initializeAfterWindowData();
    metaStreamEvent.addData(price);
    metaStreamEvent.addOutputData(symbol);
    metaStreamEvent.addOutputData(null);
    MetaStateEvent metaStateEvent = new MetaStateEvent(1);
    metaStateEvent.addEvent(metaStreamEvent);
    VariableExpressionExecutor priceVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("price", Attribute.Type.FLOAT), 0, 0);
    VariableExpressionExecutor volumeVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("volume", Attribute.Type.INT), 0, 0);
    VariableExpressionExecutor symbolVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("symbol", Attribute.Type.STRING), 0, 0);
    QueryParserHelper.reduceMetaComplexEvent(metaStateEvent);
    QueryParserHelper.updateVariablePosition(metaStateEvent, Arrays.asList(priceVariableExpressionExecutor, volumeVariableExpressionExecutor, symbolVariableExpressionExecutor));
    AssertJUnit.assertEquals(1, metaStreamEvent.getBeforeWindowData().size());
    AssertJUnit.assertEquals(1, metaStreamEvent.getOnAfterWindowData().size());
    AssertJUnit.assertEquals(2, metaStreamEvent.getOutputData().size());
    AssertJUnit.assertArrayEquals(new int[] { 0, 0, 1, 0 }, priceVariableExpressionExecutor.getPosition());
    AssertJUnit.assertArrayEquals(new int[] { 0, 0, 0, 0 }, volumeVariableExpressionExecutor.getPosition());
    AssertJUnit.assertArrayEquals(new int[] { 0, 0, 2, 0 }, symbolVariableExpressionExecutor.getPosition());
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Attribute(org.wso2.siddhi.query.api.definition.Attribute) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) Test(org.testng.annotations.Test)

Example 22 with VariableExpressionExecutor

use of org.wso2.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.

the class EventTestCase method testExpressionExecutors.

@Test
public void testExpressionExecutors() {
    // StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute
    // .Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    VariableExpressionExecutor priceVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("price", Attribute.Type.FLOAT), 0, 0);
    priceVariableExpressionExecutor.setPosition(new int[] { 0, SiddhiConstants.UNKNOWN_STATE, SiddhiConstants.OUTPUT_DATA_INDEX, 1 });
    ExpressionExecutor addExecutor = new AddExpressionExecutorFloat(new ConstantExpressionExecutor(10f, Attribute.Type.FLOAT), priceVariableExpressionExecutor);
    StreamEvent event = new StreamEvent(0, 0, 3);
    event.setOutputData(new Object[] { "WSO2", 10f, 5 });
    AssertJUnit.assertEquals("Result of adding should be 20.0", 20f, addExecutor.execute(event));
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) AndConditionExpressionExecutor(org.wso2.siddhi.core.executor.condition.AndConditionExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) Attribute(org.wso2.siddhi.query.api.definition.Attribute) AddExpressionExecutorFloat(org.wso2.siddhi.core.executor.math.add.AddExpressionExecutorFloat) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) Test(org.testng.annotations.Test)

Example 23 with VariableExpressionExecutor

use of org.wso2.siddhi.core.executor.VariableExpressionExecutor 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 24 with VariableExpressionExecutor

use of org.wso2.siddhi.core.executor.VariableExpressionExecutor 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)

Example 25 with VariableExpressionExecutor

use of org.wso2.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.

the class AggregationParser method populateIncomingAggregatorsAndExecutors.

private static void populateIncomingAggregatorsAndExecutors(AggregationDefinition aggregationDefinition, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> incomingVariableExpressionExecutors, String aggregatorName, MetaStreamEvent incomingMetaStreamEvent, List<ExpressionExecutor> incomingExpressionExecutors, List<IncrementalAttributeAggregator> incrementalAttributeAggregators, List<Variable> groupByVariableList, List<Expression> outputExpressions) {
    ExpressionExecutor[] timeStampTimeZoneExecutors = setTimeStampTimeZoneExecutors(aggregationDefinition, siddhiAppContext, tableMap, incomingVariableExpressionExecutors, aggregatorName, incomingMetaStreamEvent);
    ExpressionExecutor timestampExecutor = timeStampTimeZoneExecutors[0];
    ExpressionExecutor timeZoneExecutor = timeStampTimeZoneExecutors[1];
    Attribute timestampAttribute = new Attribute("AGG_TIMESTAMP", Attribute.Type.LONG);
    incomingMetaStreamEvent.addOutputData(timestampAttribute);
    incomingExpressionExecutors.add(timestampExecutor);
    incomingMetaStreamEvent.addOutputData(new Attribute("AGG_TIMEZONE", Attribute.Type.STRING));
    incomingExpressionExecutors.add(timeZoneExecutor);
    AbstractDefinition incomingLastInputStreamDefinition = incomingMetaStreamEvent.getLastInputDefinition();
    for (Variable groupByVariable : groupByVariableList) {
        incomingMetaStreamEvent.addOutputData(incomingLastInputStreamDefinition.getAttributeList().get(incomingLastInputStreamDefinition.getAttributePosition(groupByVariable.getAttributeName())));
        incomingExpressionExecutors.add(ExpressionParser.parseExpression(groupByVariable, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName));
    }
    // Add AGG_TIMESTAMP to output as well
    outputExpressions.add(Expression.variable("AGG_TIMESTAMP"));
    aggregationDefinition.getAttributeList().add(timestampAttribute);
    for (OutputAttribute outputAttribute : aggregationDefinition.getSelector().getSelectionList()) {
        Expression expression = outputAttribute.getExpression();
        if (expression instanceof AttributeFunction) {
            IncrementalAttributeAggregator incrementalAggregator = null;
            try {
                incrementalAggregator = (IncrementalAttributeAggregator) SiddhiClassLoader.loadExtensionImplementation(new AttributeFunction("incrementalAggregator", ((AttributeFunction) expression).getName(), ((AttributeFunction) expression).getParameters()), IncrementalAttributeAggregatorExtensionHolder.getInstance(siddhiAppContext));
            } catch (SiddhiAppCreationException ex) {
                try {
                    SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiAppContext));
                    ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
                    incomingExpressionExecutors.add(expressionExecutor);
                    incomingMetaStreamEvent.addOutputData(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
                    aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
                    outputExpressions.add(Expression.variable(outputAttribute.getRename()));
                } catch (SiddhiAppCreationException e) {
                    throw new SiddhiAppCreationException("'" + ((AttributeFunction) expression).getName() + "' is neither a incremental attribute aggregator extension or a function" + " extension", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                }
            }
            if (incrementalAggregator != null) {
                initIncrementalAttributeAggregator(incomingLastInputStreamDefinition, (AttributeFunction) expression, incrementalAggregator);
                incrementalAttributeAggregators.add(incrementalAggregator);
                aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), incrementalAggregator.getReturnType()));
                outputExpressions.add(incrementalAggregator.aggregate());
            }
        } else {
            if (expression instanceof Variable && groupByVariableList.contains(expression)) {
                Attribute groupByAttribute = null;
                for (Attribute attribute : incomingMetaStreamEvent.getOutputData()) {
                    if (attribute.getName().equals(((Variable) expression).getAttributeName())) {
                        groupByAttribute = attribute;
                        break;
                    }
                }
                if (groupByAttribute == null) {
                    throw new SiddhiAppCreationException("Expected GroupBy attribute '" + ((Variable) expression).getAttributeName() + "' not used in aggregation '" + aggregatorName + "' processing.", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                }
                aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), groupByAttribute.getType()));
                outputExpressions.add(Expression.variable(groupByAttribute.getName()));
            } else {
                ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
                incomingExpressionExecutors.add(expressionExecutor);
                incomingMetaStreamEvent.addOutputData(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
                aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
                outputExpressions.add(Expression.variable(outputAttribute.getRename()));
            }
        }
    }
}
Also used : Variable(org.wso2.siddhi.query.api.expression.Variable) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) Attribute(org.wso2.siddhi.query.api.definition.Attribute) Expression(org.wso2.siddhi.query.api.expression.Expression) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) IncrementalAttributeAggregator(org.wso2.siddhi.core.query.selector.attribute.aggregator.incremental.IncrementalAttributeAggregator) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) AttributeFunction(org.wso2.siddhi.query.api.expression.AttributeFunction)

Aggregations

VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)32 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)23 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)18 Attribute (org.wso2.siddhi.query.api.definition.Attribute)18 ArrayList (java.util.ArrayList)14 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)13 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)12 Expression (org.wso2.siddhi.query.api.expression.Expression)10 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)8 Variable (org.wso2.siddhi.query.api.expression.Variable)8 HashMap (java.util.HashMap)7 CompiledCondition (org.wso2.siddhi.core.util.collection.operator.CompiledCondition)7 AbstractDefinition (org.wso2.siddhi.query.api.definition.AbstractDefinition)7 SingleStreamRuntime (org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime)6 MatchingMetaInfoHolder (org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)6 AndConditionExpressionExecutor (org.wso2.siddhi.core.executor.condition.AndConditionExpressionExecutor)5 ConditionExpressionExecutor (org.wso2.siddhi.core.executor.condition.ConditionExpressionExecutor)5 Map (java.util.Map)4 Test (org.testng.annotations.Test)4 Table (org.wso2.siddhi.core.table.Table)4