Search in sources :

Example 21 with ExpressionExecutor

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

the class SelectorParser method getAttributeProcessors.

/**
 * Method to construct AttributeProcessor list for the selector.
 *
 * @param selector                    Selector
 * @param id                          stream id
 * @param siddhiAppContext            siddhi app context
 * @param metaComplexEvent            meta ComplexEvent
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors list of VariableExpressionExecutors
 * @param outputStream
 * @return list of AttributeProcessors
 */
private static List<AttributeProcessor> getAttributeProcessors(Selector selector, String id, SiddhiAppContext siddhiAppContext, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, OutputStream outputStream, String queryName, int metaPosition) {
    List<AttributeProcessor> attributeProcessorList = new ArrayList<>();
    StreamDefinition outputDefinition = StreamDefinition.id(id);
    outputDefinition.setQueryContextStartIndex(outputStream.getQueryContextStartIndex());
    outputDefinition.setQueryContextEndIndex(outputStream.getQueryContextEndIndex());
    List<OutputAttribute> outputAttributes = selector.getSelectionList();
    if (selector.getSelectionList().size() == 0) {
        if (metaComplexEvent instanceof MetaStreamEvent) {
            List<Attribute> attributeList = ((MetaStreamEvent) metaComplexEvent).getLastInputDefinition().getAttributeList();
            for (Attribute attribute : attributeList) {
                outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
            }
        } else {
            int position = 0;
            for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                if (metaPosition == SiddhiConstants.UNKNOWN_STATE || metaPosition == position) {
                    List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
                    for (Attribute attribute : attributeList) {
                        OutputAttribute outputAttribute = new OutputAttribute(new Variable(attribute.getName()));
                        if (!outputAttributes.contains(outputAttribute)) {
                            outputAttributes.add(outputAttribute);
                        } else {
                            List<AbstractDefinition> definitions = new ArrayList<>();
                            for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                                definitions.add(aMetaStreamEvent.getLastInputDefinition());
                            }
                            throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, outputStream.getQueryContextStartIndex(), outputStream.getQueryContextEndIndex());
                        }
                    }
                }
                ++position;
            }
        }
    }
    int i = 0;
    for (OutputAttribute outputAttribute : outputAttributes) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(outputAttribute.getExpression(), metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, !(selector.getGroupByList().isEmpty()), 0, queryName);
        if (expressionExecutor instanceof VariableExpressionExecutor) {
            // for variables we will directly put
            // value at conversion stage
            VariableExpressionExecutor executor = ((VariableExpressionExecutor) expressionExecutor);
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(new MetaStateEventAttribute(executor.getAttribute(), executor.getPosition()));
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(executor.getAttribute());
            }
            outputDefinition.attribute(outputAttribute.getRename(), ((VariableExpressionExecutor) expressionExecutor).getAttribute().getType());
        } else {
            // To maintain output variable positions
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            }
            AttributeProcessor attributeProcessor = new AttributeProcessor(expressionExecutor);
            attributeProcessor.setOutputPosition(i);
            attributeProcessorList.add(attributeProcessor);
            outputDefinition.attribute(outputAttribute.getRename(), attributeProcessor.getOutputType());
        }
        i++;
    }
    metaComplexEvent.setOutputDefinition(outputDefinition);
    return attributeProcessorList;
}
Also used : Variable(org.wso2.siddhi.query.api.expression.Variable) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) ConditionExpressionExecutor(org.wso2.siddhi.core.executor.condition.ConditionExpressionExecutor) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) Attribute(org.wso2.siddhi.query.api.definition.Attribute) MetaStateEventAttribute(org.wso2.siddhi.core.event.state.MetaStateEventAttribute) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) DuplicateAttributeException(org.wso2.siddhi.query.api.exception.DuplicateAttributeException) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) MetaStateEventAttribute(org.wso2.siddhi.core.event.state.MetaStateEventAttribute) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 22 with ExpressionExecutor

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

the class SingleInputStreamParser method generateProcessor.

public static Processor generateProcessor(StreamHandler streamHandler, MetaComplexEvent metaEvent, List<VariableExpressionExecutor> variableExpressionExecutors, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, String queryName) {
    Expression[] parameters = streamHandler.getParameters();
    MetaStreamEvent metaStreamEvent;
    int stateIndex = SiddhiConstants.UNKNOWN_STATE;
    if (metaEvent instanceof MetaStateEvent) {
        stateIndex = ((MetaStateEvent) metaEvent).getStreamEventCount() - 1;
        metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(stateIndex);
    } else {
        metaStreamEvent = (MetaStreamEvent) metaEvent;
    }
    ExpressionExecutor[] attributeExpressionExecutors;
    if (parameters != null) {
        if (parameters.length > 0) {
            attributeExpressionExecutors = new ExpressionExecutor[parameters.length];
            for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
                attributeExpressionExecutors[i] = ExpressionParser.parseExpression(parameters[i], metaEvent, stateIndex, tableMap, variableExpressionExecutors, siddhiAppContext, false, SiddhiConstants.CURRENT, queryName);
            }
        } else {
            List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
            int parameterSize = attributeList.size();
            attributeExpressionExecutors = new ExpressionExecutor[parameterSize];
            for (int i = 0; i < parameterSize; i++) {
                attributeExpressionExecutors[i] = ExpressionParser.parseExpression(new Variable(attributeList.get(i).getName()), metaEvent, stateIndex, tableMap, variableExpressionExecutors, siddhiAppContext, false, SiddhiConstants.CURRENT, queryName);
            }
        }
    } else {
        attributeExpressionExecutors = new ExpressionExecutor[0];
    }
    ConfigReader configReader;
    if (streamHandler instanceof Filter) {
        return new FilterProcessor(attributeExpressionExecutors[0]);
    } else if (streamHandler instanceof Window) {
        WindowProcessor windowProcessor = (WindowProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, WindowProcessorExtensionHolder.getInstance(siddhiAppContext));
        configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((Window) streamHandler).getNamespace(), ((Window) streamHandler).getName());
        windowProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler);
        return windowProcessor;
    } else if (streamHandler instanceof StreamFunction) {
        AbstractStreamProcessor abstractStreamProcessor;
        configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((StreamFunction) streamHandler).getNamespace(), ((StreamFunction) streamHandler).getName());
        if (supportsBatchProcessing) {
            try {
                abstractStreamProcessor = (StreamProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamProcessorExtensionHolder.getInstance(siddhiAppContext));
                metaStreamEvent.addInputDefinition(abstractStreamProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler));
                return abstractStreamProcessor;
            } catch (SiddhiAppCreationException e) {
                if (!e.isClassLoadingIssue()) {
                    ExceptionUtil.populateQueryContext(e, streamHandler, siddhiAppContext);
                    throw e;
                }
            }
        }
        abstractStreamProcessor = (StreamFunctionProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamFunctionProcessorExtensionHolder.getInstance(siddhiAppContext));
        metaStreamEvent.addInputDefinition(abstractStreamProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler));
        return abstractStreamProcessor;
    } else {
        throw new SiddhiAppCreationException(streamHandler.getClass().getName() + " is not supported", streamHandler.getQueryContextStartIndex(), streamHandler.getQueryContextEndIndex());
    }
}
Also used : Window(org.wso2.siddhi.query.api.execution.query.input.handler.Window) Variable(org.wso2.siddhi.query.api.expression.Variable) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) AbstractStreamProcessor(org.wso2.siddhi.core.query.processor.stream.AbstractStreamProcessor) Attribute(org.wso2.siddhi.query.api.definition.Attribute) FilterProcessor(org.wso2.siddhi.core.query.processor.filter.FilterProcessor) StreamFunction(org.wso2.siddhi.query.api.execution.query.input.handler.StreamFunction) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) Expression(org.wso2.siddhi.query.api.expression.Expression) Filter(org.wso2.siddhi.query.api.execution.query.input.handler.Filter) WindowProcessor(org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 23 with ExpressionExecutor

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

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.wso2.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StateEvent(org.wso2.siddhi.core.event.state.StateEvent) Map(java.util.Map)

Example 24 with ExpressionExecutor

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

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.wso2.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StateEvent(org.wso2.siddhi.core.event.state.StateEvent) Map(java.util.Map)

Example 25 with ExpressionExecutor

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

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.wso2.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Aggregations

ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)46 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)35 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)21 Attribute (org.wso2.siddhi.query.api.definition.Attribute)19 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)15 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)15 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)14 ArrayList (java.util.ArrayList)13 Map (java.util.Map)13 HashMap (java.util.HashMap)12 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)11 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)9 Expression (org.wso2.siddhi.query.api.expression.Expression)9 Variable (org.wso2.siddhi.query.api.expression.Variable)9 SiddhiAppRuntimeException (org.wso2.siddhi.core.exception.SiddhiAppRuntimeException)8 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)7 AbstractDefinition (org.wso2.siddhi.query.api.definition.AbstractDefinition)6 OutputAttribute (org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute)6 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)5 AndConditionExpressionExecutor (org.wso2.siddhi.core.executor.condition.AndConditionExpressionExecutor)5