Search in sources :

Example 6 with AbstractDefinition

use of org.ballerinalang.siddhi.query.api.definition.AbstractDefinition in project ballerina by ballerina-lang.

the class SiddhiAppRuntimeBuilder method defineTable.

public void defineTable(TableDefinition tableDefinition) {
    DefinitionParserHelper.validateDefinition(tableDefinition, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap);
    AbstractDefinition currentDefinition = tableDefinitionMap.putIfAbsent(tableDefinition.getId(), tableDefinition);
    if (currentDefinition != null) {
        tableDefinition = (TableDefinition) currentDefinition;
    }
    DefinitionParserHelper.addTable(tableDefinition, tableMap, siddhiAppContext);
}
Also used : AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)

Example 7 with AbstractDefinition

use of org.ballerinalang.siddhi.query.api.definition.AbstractDefinition in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.core.table.Table) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) ArrayList(java.util.ArrayList) IncrementalAggregateCompileCondition(org.ballerinalang.siddhi.core.util.collection.operator.IncrementalAggregateCompileCondition) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) AttributeFunction(org.ballerinalang.siddhi.query.api.expression.AttributeFunction) CompiledCondition(org.ballerinalang.siddhi.core.util.collection.operator.CompiledCondition) Expression(org.ballerinalang.siddhi.query.api.expression.Expression) MatchingMetaInfoHolder(org.ballerinalang.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) HashMap(java.util.HashMap) Map(java.util.Map) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Example 8 with AbstractDefinition

use of org.ballerinalang.siddhi.query.api.definition.AbstractDefinition in project ballerina by ballerina-lang.

the class StreamEventConverterFactory method getConversionElements.

private static List<StreamEventConverter.ConversionMapping> getConversionElements(MetaStreamEvent metaStreamEvent, int size) {
    AbstractDefinition inputDefinition = metaStreamEvent.getInputDefinitions().get(0);
    List<StreamEventConverter.ConversionMapping> conversionMappings = new ArrayList<StreamEventConverter.ConversionMapping>(size);
    for (int j = 0; j < 3; j++) {
        List<Attribute> currentDataList = null;
        if (j == 0) {
            currentDataList = metaStreamEvent.getBeforeWindowData();
        } else if (j == 1) {
            currentDataList = metaStreamEvent.getOnAfterWindowData();
        } else if (j == 2) {
            currentDataList = metaStreamEvent.getOutputData();
        }
        if (currentDataList != null) {
            int i = 0;
            for (Attribute attribute : currentDataList) {
                // Only variable slots will be filled.
                if (attribute == null) {
                    i++;
                } else if (!inputDefinition.getAttributeList().contains(attribute)) {
                    i++;
                } else {
                    int fromPosition = inputDefinition.getAttributePosition(attribute.getName());
                    StreamEventConverter.ConversionMapping conversionMapping = new StreamEventConverter.ConversionMapping();
                    conversionMapping.setFromPosition(fromPosition);
                    int[] toPosition = new int[2];
                    toPosition[0] = j;
                    toPosition[1] = i;
                    conversionMapping.setToPosition(toPosition);
                    conversionMappings.add(conversionMapping);
                    i++;
                }
            }
        }
    }
    return conversionMappings;
}
Also used : Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) ArrayList(java.util.ArrayList) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)

Example 9 with AbstractDefinition

use of org.ballerinalang.siddhi.query.api.definition.AbstractDefinition in project ballerina by ballerina-lang.

the class EventTestCase method testQueryParser.

@Test
public void testQueryParser() {
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    StreamDefinition outStreamDefinition = StreamDefinition.id("outputStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT);
    Query query = new Query();
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"), Compare.Operator.NOT_EQUAL, Expression.value(50))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("outputStream");
    Map<String, AbstractDefinition> tableDefinitionMap = new HashMap<>();
    Map<String, AbstractDefinition> windowDefinitionMap = new HashMap<>();
    Map<String, AbstractDefinition> aggregationDefinitionMap = new HashMap<>();
    Map<String, Table> tableMap = new HashMap<String, Table>();
    Map<String, Window> eventWindowMap = new HashMap<String, Window>();
    Map<String, AggregationRuntime> aggregationMap = new HashMap<String, AggregationRuntime>();
    Map<String, List<Source>> eventSourceMap = new HashMap<String, List<Source>>();
    Map<String, List<Sink>> eventSinkMap = new HashMap<String, List<Sink>>();
    Map<String, AbstractDefinition> streamDefinitionMap = new HashMap<String, AbstractDefinition>();
    LockSynchronizer lockSynchronizer = new LockSynchronizer();
    streamDefinitionMap.put("cseEventStream", streamDefinition);
    streamDefinitionMap.put("outputStream", outStreamDefinition);
    SiddhiContext siddhicontext = new SiddhiContext();
    SiddhiAppContext context = new SiddhiAppContext();
    context.setSiddhiContext(siddhicontext);
    context.setElementIdGenerator(new ElementIdGenerator(context.getName()));
    context.setSnapshotService(new SnapshotService(context));
    QueryRuntime runtime = QueryParser.parse(query, context, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, aggregationMap, eventWindowMap, lockSynchronizer, "1");
    AssertJUnit.assertNotNull(runtime);
    AssertJUnit.assertTrue(runtime.getStreamRuntime() instanceof SingleStreamRuntime);
    AssertJUnit.assertNotNull(runtime.getSelector());
    AssertJUnit.assertTrue(runtime.getMetaComplexEvent() instanceof MetaStreamEvent);
}
Also used : Query(org.ballerinalang.siddhi.query.api.execution.query.Query) HashMap(java.util.HashMap) ElementIdGenerator(org.ballerinalang.siddhi.core.util.ElementIdGenerator) Source(org.ballerinalang.siddhi.core.stream.input.source.Source) SiddhiContext(org.ballerinalang.siddhi.core.config.SiddhiContext) Sink(org.ballerinalang.siddhi.core.stream.output.sink.Sink) QueryRuntime(org.ballerinalang.siddhi.core.query.QueryRuntime) LockSynchronizer(org.ballerinalang.siddhi.core.util.lock.LockSynchronizer) List(java.util.List) AggregationRuntime(org.ballerinalang.siddhi.core.aggregation.AggregationRuntime) Window(org.ballerinalang.siddhi.core.window.Window) SnapshotService(org.ballerinalang.siddhi.core.util.snapshot.SnapshotService) Table(org.ballerinalang.siddhi.core.table.Table) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) SingleStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) SiddhiAppContext(org.ballerinalang.siddhi.core.config.SiddhiAppContext) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) Test(org.testng.annotations.Test)

Example 10 with AbstractDefinition

use of org.ballerinalang.siddhi.query.api.definition.AbstractDefinition in project ballerina by ballerina-lang.

the class PartitionParser method createMetaEventForPartitioner.

/**
 * Create metaEvent to be used by StreamPartitioner with output attributes.
 *
 * @param stateEvent metaStateEvent of the queryRuntime
 * @return metaStateEvent
 */
private static MetaStateEvent createMetaEventForPartitioner(MetaComplexEvent stateEvent) {
    MetaStateEvent metaStateEvent;
    if (stateEvent instanceof MetaStateEvent) {
        metaStateEvent = new MetaStateEvent(((MetaStateEvent) stateEvent).getStreamEventCount());
        for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) stateEvent).getMetaStreamEvents()) {
            AbstractDefinition definition = metaStreamEvent.getLastInputDefinition();
            MetaStreamEvent newMetaStreamEvent = new MetaStreamEvent();
            for (Attribute attribute : definition.getAttributeList()) {
                newMetaStreamEvent.addOutputData(attribute);
            }
            newMetaStreamEvent.addInputDefinition(definition);
            newMetaStreamEvent.setEventType(metaStreamEvent.getEventType());
            metaStateEvent.addEvent(newMetaStreamEvent);
        }
    } else {
        metaStateEvent = new MetaStateEvent(1);
        AbstractDefinition definition = ((MetaStreamEvent) stateEvent).getLastInputDefinition();
        MetaStreamEvent newMetaStreamEvent = new MetaStreamEvent();
        for (Attribute attribute : definition.getAttributeList()) {
            newMetaStreamEvent.addOutputData(attribute);
        }
        newMetaStreamEvent.addInputDefinition(definition);
        newMetaStreamEvent.setEventType(((MetaStreamEvent) stateEvent).getEventType());
        metaStateEvent.addEvent(newMetaStreamEvent);
    }
    return metaStateEvent;
}
Also used : Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent)

Aggregations

AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)17 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)9 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)8 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)7 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)7 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)6 ArrayList (java.util.ArrayList)5 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)5 AttributeFunction (org.ballerinalang.siddhi.query.api.expression.AttributeFunction)4 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)4 HashMap (java.util.HashMap)3 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)3 ConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor)3 Table (org.ballerinalang.siddhi.core.table.Table)3 Expression (org.ballerinalang.siddhi.query.api.expression.Expression)3 Map (java.util.Map)2 SiddhiAppContext (org.ballerinalang.siddhi.core.config.SiddhiAppContext)2 OperationNotSupportedException (org.ballerinalang.siddhi.core.exception.OperationNotSupportedException)2 AndConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor)2 BoolConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.BoolConditionExpressionExecutor)2