Search in sources :

Example 51 with ExpressionExecutor

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

the class OrderByEventComparator method compare.

@Override
public int compare(ComplexEvent complexEvent1, ComplexEvent complexEvent2) {
    if (groupByExecutors != null) {
        for (int i = 0, groupByExecutorsLength = groupByExecutors.length; i < groupByExecutorsLength; i++) {
            ExpressionExecutor executor = groupByExecutors[i];
            boolean isAscending = isAscendingArray[i];
            Object value1 = executor.execute(complexEvent1);
            Object value2 = executor.execute(complexEvent2);
            if (value1 != null && value2 != null) {
                int results = 0;
                Attribute.Type type = executor.getReturnType();
                switch(type) {
                    case STRING:
                        results = ((String) value1).compareTo(((String) value2));
                        break;
                    case INT:
                        results = ((Integer) value1).compareTo(((Integer) value2));
                        break;
                    case LONG:
                        results = ((Long) value1).compareTo(((Long) value2));
                        break;
                    case FLOAT:
                        results = ((Float) value1).compareTo(((Float) value2));
                        break;
                    case DOUBLE:
                        results = ((Double) value1).compareTo(((Double) value2));
                        break;
                    case BOOL:
                        results = ((Boolean) value1).compareTo(((Boolean) value2));
                        break;
                    case OBJECT:
                        int hashDiff = value1.hashCode() - value2.hashCode();
                        if (hashDiff < 0) {
                            results = -1;
                        } else if (hashDiff > 0) {
                            results = 1;
                        }
                }
                if (!isAscending) {
                    results = results * -1;
                }
                if (results != 0) {
                    return results;
                }
            } else if (value1 != null) {
                return -1;
            } else if (value2 != null) {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) OrderByAttribute(org.wso2.siddhi.query.api.execution.query.selection.OrderByAttribute) Attribute(org.wso2.siddhi.query.api.definition.Attribute)

Example 52 with ExpressionExecutor

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

the class InMemoryTable method compileUpdateSet.

@Override
public CompiledUpdateSet compileUpdateSet(UpdateSet updateSet, MatchingMetaInfoHolder matchingMetaInfoHolder, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, String queryName) {
    Map<Integer, ExpressionExecutor> expressionExecutorMap = new HashMap<>();
    for (UpdateSet.SetAttribute setAttribute : updateSet.getSetAttributeList()) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        int attributePosition = tableDefinition.getAttributePosition(setAttribute.getTableVariable().getAttributeName());
        expressionExecutorMap.put(attributePosition, expressionExecutor);
    }
    return new InMemoryCompiledUpdateSet(expressionExecutorMap);
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) UpdateSet(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateSet)

Example 53 with ExpressionExecutor

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

the class AbstractQueryableRecordTable method query.

@Override
public StreamEvent query(StateEvent matchingEvent, CompiledCondition compiledCondition, CompiledSelection compiledSelection) throws ConnectionUnavailableException {
    RecordStoreCompiledSelection recordStoreCompiledSelection = ((RecordStoreCompiledSelection) compiledSelection);
    RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    Map<String, Object> parameterMap = new HashMap<>();
    for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
        parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
    }
    for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledSelection.variableExpressionExecutorMap.entrySet()) {
        parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
    }
    Iterator<Object[]> records;
    if (recordTableHandler != null) {
        records = recordTableHandler.query(matchingEvent.getTimestamp(), parameterMap, recordStoreCompiledCondition.compiledCondition, recordStoreCompiledSelection.compiledSelection);
    } else {
        records = query(parameterMap, recordStoreCompiledCondition.compiledCondition, recordStoreCompiledSelection.compiledSelection);
    }
    ComplexEventChunk<StreamEvent> streamEventComplexEventChunk = new ComplexEventChunk<>(true);
    if (records != null) {
        while (records.hasNext()) {
            Object[] record = records.next();
            StreamEvent streamEvent = storeEventPool.borrowEvent();
            System.arraycopy(record, 0, streamEvent.getOutputData(), 0, record.length);
            streamEventComplexEventChunk.add(streamEvent);
        }
    }
    return streamEventComplexEventChunk.getFirst();
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 54 with ExpressionExecutor

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

the class AbstractRecordTable method compileUpdateSet.

public CompiledUpdateSet compileUpdateSet(UpdateSet updateSet, MatchingMetaInfoHolder matchingMetaInfoHolder, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, String queryName) {
    RecordTableCompiledUpdateSet recordTableCompiledUpdateSet = new RecordTableCompiledUpdateSet();
    Map<String, ExpressionExecutor> parentExecutorMap = new HashMap<>();
    for (UpdateSet.SetAttribute setAttribute : updateSet.getSetAttributeList()) {
        ExpressionBuilder expressionBuilder = new ExpressionBuilder(setAttribute.getAssignmentExpression(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
        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(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) UpdateSet(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateSet) CompiledUpdateSet(org.wso2.siddhi.core.table.CompiledUpdateSet) CompiledExpression(org.wso2.siddhi.core.util.collection.operator.CompiledExpression)

Example 55 with ExpressionExecutor

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

the class AbstractRecordTable method update.

@Override
public void update(ComplexEventChunk<StateEvent> updatingEventChunk, CompiledCondition compiledCondition, CompiledUpdateSet compiledUpdateSet) throws ConnectionUnavailableException {
    RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    RecordTableCompiledUpdateSet recordTableCompiledUpdateSet = (RecordTableCompiledUpdateSet) compiledUpdateSet;
    List<Map<String, Object>> updateConditionParameterMaps = new ArrayList<>();
    List<Map<String, Object>> updateSetParameterMaps = new ArrayList<>();
    updatingEventChunk.reset();
    long timestamp = 0L;
    while (updatingEventChunk.hasNext()) {
        StateEvent stateEvent = updatingEventChunk.next();
        Map<String, Object> variableMap = new HashMap<>();
        for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
            variableMap.put(entry.getKey(), entry.getValue().execute(stateEvent));
        }
        updateConditionParameterMaps.add(variableMap);
        Map<String, Object> variableMapForUpdateSet = new HashMap<>();
        for (Map.Entry<String, ExpressionExecutor> entry : recordTableCompiledUpdateSet.getExpressionExecutorMap().entrySet()) {
            variableMapForUpdateSet.put(entry.getKey(), entry.getValue().execute(stateEvent));
        }
        updateSetParameterMaps.add(variableMapForUpdateSet);
        timestamp = stateEvent.getTimestamp();
    }
    if (recordTableHandler != null) {
        recordTableHandler.update(timestamp, recordStoreCompiledCondition.compiledCondition, updateConditionParameterMaps, recordTableCompiledUpdateSet.getUpdateSetMap(), updateSetParameterMaps);
    } else {
        update(recordStoreCompiledCondition.compiledCondition, updateConditionParameterMaps, recordTableCompiledUpdateSet.getUpdateSetMap(), updateSetParameterMaps);
    }
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StateEvent(org.wso2.siddhi.core.event.state.StateEvent) HashMap(java.util.HashMap) Map(java.util.Map)

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