Search in sources :

Example 16 with ExpressionExecutor

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

the class AggregationParser method processAggregationSelectors.

private static void processAggregationSelectors(AggregationDefinition aggregationDefinition, SiddhiQueryContext siddhiQueryContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> incomingVariableExpressionExecutors, MetaStreamEvent incomingMetaStreamEvent, List<ExpressionExecutor> incomingExpressionExecutors, List<Expression> outputExpressions, OutputAttribute outputAttribute, Expression expression) {
    ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
    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 : ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) Attribute(io.siddhi.query.api.definition.Attribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute)

Example 17 with ExpressionExecutor

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

the class InputParameterValidator method validateExpressionExecutors.

/**
 * The method which validates the extension specific parameters of siddhi App with the pattern specified in the
 * {@link ParameterOverload} annotation in the extension class
 *
 * @param objectHavingAnnotation       the object which has Extension annotation
 * @param attributeExpressionExecutors the executors of each function parameters
 * @throws SiddhiAppValidationException SiddhiAppValidation exception
 */
public static void validateExpressionExecutors(Object objectHavingAnnotation, ExpressionExecutor[] attributeExpressionExecutors) throws SiddhiAppValidationException {
    Extension annotation = objectHavingAnnotation.getClass().getAnnotation(Extension.class);
    if (annotation == null) {
        return;
    }
    ParameterOverload[] parameterOverloads = annotation.parameterOverloads();
    Parameter[] parameters = annotation.parameters();
    String key = AnnotationHelper.createAnnotationKey(annotation);
    // Count the mandatory number of parameters specified in @Extension
    int mandatoryCount = 0;
    Map<String, Parameter> parameterMap = new HashMap<>();
    for (Parameter parameter : parameters) {
        if (!parameter.optional()) {
            mandatoryCount++;
        }
        parameterMap.put(parameter.name(), parameter);
    }
    // Find the parameterOverLoad
    ParameterOverload parameterOverload = null;
    for (ParameterOverload aParameterOverload : parameterOverloads) {
        String[] overloadParameterNames = aParameterOverload.parameterNames();
        if (overloadParameterNames.length == attributeExpressionExecutors.length && (overloadParameterNames.length == 0 || !overloadParameterNames[overloadParameterNames.length - 1].equals(REPETITIVE_PARAMETER_NOTATION))) {
            boolean isExpectedParameterOverload = true;
            for (int i = 0; i < overloadParameterNames.length; i++) {
                String overloadParameterName = overloadParameterNames[i];
                Parameter parameter = parameterMap.get(overloadParameterName);
                boolean supportedReturnType = false;
                for (DataType type : parameter.type()) {
                    if (attributeExpressionExecutors[i].getReturnType().toString().equalsIgnoreCase(type.toString())) {
                        supportedReturnType = true;
                        break;
                    }
                }
                if (!supportedReturnType) {
                    isExpectedParameterOverload = false;
                    break;
                }
            }
            if (isExpectedParameterOverload) {
                parameterOverload = aParameterOverload;
                break;
            }
        } else if (overloadParameterNames.length - 1 <= attributeExpressionExecutors.length && overloadParameterNames.length > 0 && overloadParameterNames[overloadParameterNames.length - 1].equals(REPETITIVE_PARAMETER_NOTATION)) {
            if (attributeExpressionExecutors.length > 0) {
                boolean isExpectedParameterOverload = true;
                for (int i = 0; i < attributeExpressionExecutors.length; i++) {
                    Parameter parameter = null;
                    String overloadParameterName = null;
                    if (i < overloadParameterNames.length - 1) {
                        overloadParameterName = overloadParameterNames[i];
                    } else {
                        overloadParameterName = overloadParameterNames[overloadParameterNames.length - 2];
                    }
                    parameter = parameterMap.get(overloadParameterName);
                    boolean supportedReturnType = false;
                    for (DataType type : parameter.type()) {
                        if (attributeExpressionExecutors[i].getReturnType().toString().equalsIgnoreCase(type.toString())) {
                            supportedReturnType = true;
                            break;
                        }
                    }
                    if (!supportedReturnType) {
                        isExpectedParameterOverload = false;
                        break;
                    }
                }
                if (isExpectedParameterOverload) {
                    parameterOverload = aParameterOverload;
                    break;
                }
            }
        }
    }
    if (parameterOverload == null) {
        if (parameterOverloads.length > 0) {
            List<Attribute.Type> returnTypes = new ArrayList<>();
            for (ExpressionExecutor expressionExecutor : attributeExpressionExecutors) {
                returnTypes.add(expressionExecutor.getReturnType());
            }
            String formattedParamOverloadString = getSupportedParamOverloads(parameterMap, parameterOverloads);
            throw new SiddhiAppValidationException("There is no parameterOverload for '" + key + "' that matches attribute types '" + returnTypes.stream().map(String::valueOf).collect(Collectors.joining(", ", "<", ">")) + "'. Supported parameter overloads are " + formattedParamOverloadString + ".");
        } else {
            if (mandatoryCount > attributeExpressionExecutors.length) {
                throw new SiddhiAppValidationException("The '" + key + "' expects at least " + mandatoryCount + " parameters, but found only " + attributeExpressionExecutors.length + " input parameters.");
            }
        }
    } else {
        String[] overloadParameterNames = parameterOverload.parameterNames();
        for (int i = 0; i < overloadParameterNames.length; i++) {
            String overloadParameterName = overloadParameterNames[i];
            Parameter parameter = parameterMap.get(overloadParameterName);
            if (parameter != null && !parameter.dynamic() && !(attributeExpressionExecutors[i] instanceof ConstantExpressionExecutor)) {
                throw new SiddhiAppValidationException("The '" + key + "' expects input parameter '" + parameter.name() + "' at position '" + i + "' to be static," + " but found a dynamic attribute.");
            }
        }
    }
}
Also used : ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) Extension(io.siddhi.annotation.Extension) DataType(io.siddhi.annotation.util.DataType) Parameter(io.siddhi.annotation.Parameter) DataType(io.siddhi.annotation.util.DataType) ParameterOverload(io.siddhi.annotation.ParameterOverload)

Example 18 with ExpressionExecutor

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

the class ExpressionBuilder method buildStreamVariableExecutor.

private void buildStreamVariableExecutor(Variable variable, int streamEventChainIndex, ExpressionVisitor expressionVisitor, Attribute.Type type) {
    String id = variable.getAttributeName();
    if (variable.getStreamId() != null) {
        id = variable.getStreamId() + "." + id;
    }
    expressionVisitor.beginVisitStreamVariable(id, variable.getStreamId(), variable.getAttributeName(), type);
    if (!variableExpressionExecutorMap.containsKey(id)) {
        ExpressionExecutor variableExpressionExecutor = ExpressionParser.parseExpression(variable, matchingMetaInfoHolder.getMetaStateEvent(), streamEventChainIndex, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        variableExpressionExecutorMap.put(id, variableExpressionExecutor);
    }
    expressionVisitor.endVisitStreamVariable(id, variable.getStreamId(), variable.getAttributeName(), type);
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor)

Example 19 with ExpressionExecutor

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

the class UpdateOrInsertReducer method reduceEventsForInsert.

public List<Object[]> reduceEventsForInsert(List<Object[]> failedRecords, Map<String, ExpressionExecutor> inMemorySetExecutors) {
    ComplexEventChunk<StreamEvent> toInsertEventChunk = new ComplexEventChunk<>();
    StateEvent joinEvent = stateEventFactory.newInstance();
    for (Object[] data : failedRecords) {
        StreamEvent failedEvent = streamEventFactory.newInstance();
        failedEvent.setOutputData(data);
        joinEvent.setEvent(streamEventIndex, failedEvent);
        boolean updated = false;
        toInsertEventChunk.reset();
        while (toInsertEventChunk.hasNext()) {
            StreamEvent toInsertEvent = toInsertEventChunk.next();
            joinEvent.setEvent(storeEventIndex, toInsertEvent);
            if ((Boolean) inMemoryCompiledCondition.execute(joinEvent)) {
                for (Map.Entry<String, ExpressionExecutor> entry : inMemorySetExecutors.entrySet()) {
                    toInsertEvent.setOutputData(entry.getValue().execute(failedEvent), attributeMap.get(entry.getKey()));
                }
                updated = true;
            }
        }
        if (!updated) {
            toInsertEventChunk.add(failedEvent);
        }
    }
    List<Object[]> toInsertRecords = new LinkedList<>();
    toInsertEventChunk.reset();
    while (toInsertEventChunk.hasNext()) {
        StreamEvent streamEvent = toInsertEventChunk.next();
        toInsertRecords.add(streamEvent.getOutputData());
    }
    return toInsertRecords;
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) StreamEvent(io.siddhi.core.event.stream.StreamEvent) LinkedList(java.util.LinkedList) StateEvent(io.siddhi.core.event.state.StateEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 20 with ExpressionExecutor

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

the class AbstractRecordTable method compileUpdateSet.

public CompiledUpdateSet compileUpdateSet(UpdateSet updateSet, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiQueryContext siddhiQueryContext) {
    RecordTableCompiledUpdateSet recordTableCompiledUpdateSet = new RecordTableCompiledUpdateSet();
    Map<String, ExpressionExecutor> parentExecutorMap = new HashMap<>();
    for (UpdateSet.SetAttribute setAttribute : updateSet.getSetAttributeList()) {
        ExpressionExecutor inMemoryAssignmentExecutor = ExpressionParser.parseExpression(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        ExpressionBuilder expressionBuilder = new ExpressionBuilder(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, null, inMemoryAssignmentExecutor, siddhiQueryContext);
        CompiledExpression compiledExpression = compileSetAttribute(expressionBuilder);
        recordTableCompiledUpdateSet.put(setAttribute.getTableVariable().getAttributeName(), compiledExpression);
        Map<String, ExpressionExecutor> expressionExecutorMap = expressionBuilder.getVariableExpressionExecutorMap();
        parentExecutorMap.putAll(expressionExecutorMap);
    }
    recordTableCompiledUpdateSet.setExpressionExecutorMap(parentExecutorMap);
    return recordTableCompiledUpdateSet;
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet) UpdateSet(io.siddhi.query.api.execution.query.output.stream.UpdateSet) CompiledExpression(io.siddhi.core.util.collection.operator.CompiledExpression)

Aggregations

ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)53 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)43 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)26 Map (java.util.Map)22 Attribute (io.siddhi.query.api.definition.Attribute)21 StreamEvent (io.siddhi.core.event.stream.StreamEvent)20 HashMap (java.util.HashMap)20 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)19 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)14 ArrayList (java.util.ArrayList)13 StateEvent (io.siddhi.core.event.state.StateEvent)12 Variable (io.siddhi.query.api.expression.Variable)11 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)10 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)10 Expression (io.siddhi.query.api.expression.Expression)10 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)8 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)8 AndConditionExpressionExecutor (io.siddhi.core.executor.condition.AndConditionExpressionExecutor)5 ConditionExpressionExecutor (io.siddhi.core.executor.condition.ConditionExpressionExecutor)5 QueryableProcessor (io.siddhi.core.query.processor.stream.window.QueryableProcessor)4