Search in sources :

Example 36 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class ExpressionParser method parseInnerExpression.

/**
 * Parse the set of inner expression of AttributeFunctionExtensions and handling all (*) cases.
 *
 * @param innerExpressions        InnerExpressions to be parsed
 * @param metaEvent               Meta Event
 * @param currentState            Current state number
 * @param tableMap                Event Table Map
 * @param executorList            List to hold VariableExpressionExecutors to update after query parsing @return
 * @param siddhiAppContext        SiddhiAppContext
 * @param groupBy                 is for groupBy expression
 * @param defaultStreamEventIndex Default StreamEvent Index
 * @return List of expressionExecutors
 */
private static ExpressionExecutor[] parseInnerExpression(Expression[] innerExpressions, MetaComplexEvent metaEvent, int currentState, Map<String, Table> tableMap, List<VariableExpressionExecutor> executorList, SiddhiAppContext siddhiAppContext, boolean groupBy, int defaultStreamEventIndex, String queryName) {
    ExpressionExecutor[] innerExpressionExecutors;
    if (innerExpressions != null) {
        if (innerExpressions.length > 0) {
            innerExpressionExecutors = new ExpressionExecutor[innerExpressions.length];
            for (int i = 0, innerExpressionsLength = innerExpressions.length; i < innerExpressionsLength; i++) {
                innerExpressionExecutors[i] = parseExpression(innerExpressions[i], metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            }
        } else {
            List<Expression> outputAttributes = new ArrayList<Expression>();
            if (metaEvent instanceof MetaStreamEvent) {
                List<Attribute> attributeList = ((MetaStreamEvent) metaEvent).getLastInputDefinition().getAttributeList();
                for (Attribute attribute : attributeList) {
                    outputAttributes.add(new Variable(attribute.getName()));
                }
            } else {
                for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaEvent).getMetaStreamEvents()) {
                    List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
                    for (Attribute attribute : attributeList) {
                        Expression outputAttribute = new Variable(attribute.getName());
                        if (!outputAttributes.contains(outputAttribute)) {
                            outputAttributes.add(outputAttribute);
                        } else {
                            List<AbstractDefinition> definitions = new ArrayList<AbstractDefinition>();
                            for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaEvent).getMetaStreamEvents()) {
                                definitions.add(aMetaStreamEvent.getLastInputDefinition());
                            }
                            throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, attribute.getQueryContextStartIndex(), attribute.getQueryContextEndIndex());
                        }
                    }
                }
            }
            innerExpressionExecutors = new ExpressionExecutor[outputAttributes.size()];
            for (int i = 0, innerExpressionsLength = outputAttributes.size(); i < innerExpressionsLength; i++) {
                innerExpressionExecutors[i] = parseExpression(outputAttributes.get(i), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            }
        }
    } else {
        innerExpressionExecutors = new ExpressionExecutor[0];
    }
    return innerExpressionExecutors;
}
Also used : Variable(org.ballerinalang.siddhi.query.api.expression.Variable) InConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.InConditionExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) NotConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.NotConditionExpressionExecutor) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) BoolConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.BoolConditionExpressionExecutor) IsNullStreamConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullStreamConditionExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) OrConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.OrConditionExpressionExecutor) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) IsNullConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullConditionExpressionExecutor) ConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) ArrayList(java.util.ArrayList) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) DuplicateAttributeException(org.ballerinalang.siddhi.query.api.exception.DuplicateAttributeException) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) Expression(org.ballerinalang.siddhi.query.api.expression.Expression) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Example 37 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class CollectionOperator method tryUpdate.

@Override
public ComplexEventChunk<StreamEvent> tryUpdate(ComplexEventChunk<StateEvent> updatingOrAddingEventChunk, Object storeEvents, InMemoryCompiledUpdateSet compiledUpdateSet, AddingStreamEventExtractor addingStreamEventExtractor) {
    updatingOrAddingEventChunk.reset();
    ComplexEventChunk<StreamEvent> failedEventChunk = new ComplexEventChunk<StreamEvent>(updatingOrAddingEventChunk.isBatch());
    while (updatingOrAddingEventChunk.hasNext()) {
        StateEvent updateOrAddingEvent = updatingOrAddingEventChunk.next();
        try {
            boolean updated = false;
            if (((Collection<StreamEvent>) storeEvents).size() > 0) {
                for (StreamEvent storeEvent : ((Collection<StreamEvent>) storeEvents)) {
                    updateOrAddingEvent.setEvent(storeEventPosition, storeEvent);
                    if ((Boolean) expressionExecutor.execute(updateOrAddingEvent)) {
                        for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
                            storeEvent.setOutputData(entry.getValue().execute(updateOrAddingEvent), entry.getKey());
                        }
                        updated = true;
                    }
                }
            }
            if (!updated) {
                failedEventChunk.add(addingStreamEventExtractor.getAddingStreamEvent(updateOrAddingEvent));
            }
        } finally {
            updateOrAddingEvent.setEvent(storeEventPosition, null);
        }
    }
    return failedEventChunk;
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent) Map(java.util.Map)

Example 38 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class EventChunkOperator method update.

@Override
public void update(ComplexEventChunk<StateEvent> updatingEventChunk, Object storeEvents, InMemoryCompiledUpdateSet compiledUpdateSet) {
    ComplexEventChunk<StreamEvent> storeEventChunk = (ComplexEventChunk<StreamEvent>) storeEvents;
    updatingEventChunk.reset();
    while (updatingEventChunk.hasNext()) {
        StateEvent updatingEvent = updatingEventChunk.next();
        try {
            storeEventChunk.reset();
            while (storeEventChunk.hasNext()) {
                StreamEvent storeEvent = storeEventChunk.next();
                updatingEvent.setEvent(storeEventPosition, storeEvent);
                if ((Boolean) expressionExecutor.execute(updatingEvent)) {
                    for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
                        storeEvent.setOutputData(entry.getValue().execute(updatingEvent), entry.getKey());
                    }
                }
            }
        } finally {
            updatingEvent.setEvent(storeEventPosition, null);
        }
    }
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent) Map(java.util.Map)

Example 39 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class EventChunkOperator method tryUpdate.

@Override
public ComplexEventChunk<StreamEvent> tryUpdate(ComplexEventChunk<StateEvent> updatingOrAddingEventChunk, Object storeEvents, InMemoryCompiledUpdateSet compiledUpdateSet, AddingStreamEventExtractor addingStreamEventExtractor) {
    ComplexEventChunk<StreamEvent> storeEventChunk = (ComplexEventChunk<StreamEvent>) storeEvents;
    updatingOrAddingEventChunk.reset();
    ComplexEventChunk<StreamEvent> failedEventChunk = new ComplexEventChunk<StreamEvent>(updatingOrAddingEventChunk.isBatch());
    while (updatingOrAddingEventChunk.hasNext()) {
        StateEvent overwritingOrAddingEvent = updatingOrAddingEventChunk.next();
        try {
            boolean updated = false;
            storeEventChunk.reset();
            while (storeEventChunk.hasNext()) {
                StreamEvent storeEvent = storeEventChunk.next();
                overwritingOrAddingEvent.setEvent(storeEventPosition, storeEvent);
                if ((Boolean) expressionExecutor.execute(overwritingOrAddingEvent)) {
                    for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
                        storeEvent.setOutputData(entry.getValue().execute(overwritingOrAddingEvent), entry.getKey());
                    }
                    updated = true;
                }
            }
            if (!updated) {
                failedEventChunk.add(addingStreamEventExtractor.getAddingStreamEvent(overwritingOrAddingEvent));
            }
        } finally {
            overwritingOrAddingEvent.setEvent(storeEventPosition, null);
        }
    }
    return failedEventChunk;
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent) Map(java.util.Map)

Example 40 with ExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ExpressionExecutor in project ballerina by ballerina-lang.

the class IncrementalAggregateCompileCondition method createAggregateSelectionEventChunk.

private ComplexEventChunk<StreamEvent> createAggregateSelectionEventChunk(ComplexEventChunk<StreamEvent> complexEventChunkToHoldMatches, List<ExpressionExecutor> outputExpressionExecutors) {
    ComplexEventChunk<StreamEvent> aggregateSelectionComplexEventChunk = new ComplexEventChunk<>(true);
    StreamEvent resetEvent = streamEventPoolForTableMeta.borrowEvent();
    resetEvent.setType(ComplexEvent.Type.RESET);
    while (complexEventChunkToHoldMatches.hasNext()) {
        StreamEvent streamEvent = complexEventChunkToHoldMatches.next();
        StreamEvent newStreamEvent = streamEventPoolForAggregateMeta.borrowEvent();
        Object[] outputData = new Object[newStreamEvent.getOutputData().length];
        for (int i = 0; i < outputExpressionExecutors.size(); i++) {
            outputData[i] = outputExpressionExecutors.get(i).execute(streamEvent);
        }
        newStreamEvent.setTimestamp(streamEvent.getTimestamp());
        newStreamEvent.setOutputData(outputData);
        aggregateSelectionComplexEventChunk.add(newStreamEvent);
    }
    for (ExpressionExecutor expressionExecutor : outputExpressionExecutors) {
        expressionExecutor.execute(resetEvent);
    }
    return aggregateSelectionComplexEventChunk;
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent)

Aggregations

ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)46 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)33 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)15 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)14 Map (java.util.Map)13 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)13 HashMap (java.util.HashMap)11 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)11 ArrayList (java.util.ArrayList)10 ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)10 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)10 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)8 StateEvent (org.ballerinalang.siddhi.core.event.state.StateEvent)7 SiddhiAppRuntimeException (org.ballerinalang.siddhi.core.exception.SiddhiAppRuntimeException)7 Expression (org.ballerinalang.siddhi.query.api.expression.Expression)7 AndConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor)5 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)5 OutputAttribute (org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute)5 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)4 ConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor)4