Search in sources :

Example 31 with Attribute

use of org.ballerinalang.siddhi.query.api.definition.Attribute 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 32 with Attribute

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

the class MatcherParser method constructMatchingMetaStateHolder.

public static MatchingMetaInfoHolder constructMatchingMetaStateHolder(MetaComplexEvent matchingMetaComplexEvent, int defaultStreamEventIndex, AbstractDefinition candsidateDefinition, int currentState) {
    int storeEventIndex = 0;
    MetaStreamEvent tableStreamEvent = new MetaStreamEvent();
    tableStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
    tableStreamEvent.addInputDefinition(candsidateDefinition);
    for (Attribute attribute : candsidateDefinition.getAttributeList()) {
        tableStreamEvent.addOutputData(attribute);
    }
    MetaStateEvent metaStateEvent = null;
    if (matchingMetaComplexEvent instanceof MetaStreamEvent) {
        metaStateEvent = new MetaStateEvent(2);
        metaStateEvent.addEvent(((MetaStreamEvent) matchingMetaComplexEvent));
        metaStateEvent.addEvent(tableStreamEvent);
        storeEventIndex = 1;
        defaultStreamEventIndex = 0;
        if (currentState == UNKNOWN_STATE) {
            currentState = defaultStreamEventIndex;
        }
    } else {
        MetaStreamEvent[] metaStreamEvents = ((MetaStateEvent) matchingMetaComplexEvent).getMetaStreamEvents();
        // for join
        for (; storeEventIndex < metaStreamEvents.length; storeEventIndex++) {
            MetaStreamEvent metaStreamEvent = metaStreamEvents[storeEventIndex];
            if (storeEventIndex != defaultStreamEventIndex && metaStreamEvent.getLastInputDefinition().equalsIgnoreAnnotations(candsidateDefinition)) {
                metaStateEvent = ((MetaStateEvent) matchingMetaComplexEvent);
                break;
            }
        }
        if (metaStateEvent == null) {
            metaStateEvent = new MetaStateEvent(metaStreamEvents.length + 1);
            for (MetaStreamEvent metaStreamEvent : metaStreamEvents) {
                metaStateEvent.addEvent(metaStreamEvent);
            }
            metaStateEvent.addEvent(tableStreamEvent);
            storeEventIndex = metaStreamEvents.length;
        }
    }
    return new MatchingMetaInfoHolder(metaStateEvent, defaultStreamEventIndex, storeEventIndex, metaStateEvent.getMetaStreamEvent(defaultStreamEventIndex).getLastInputDefinition(), candsidateDefinition, currentState);
}
Also used : Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) MatchingMetaInfoHolder(org.ballerinalang.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent)

Example 33 with Attribute

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

the class DefinitionParserHelper method addTable.

public static void addTable(TableDefinition tableDefinition, ConcurrentMap<String, Table> tableMap, SiddhiAppContext siddhiAppContext) {
    if (!tableMap.containsKey(tableDefinition.getId())) {
        MetaStreamEvent tableMetaStreamEvent = new MetaStreamEvent();
        tableMetaStreamEvent.addInputDefinition(tableDefinition);
        for (Attribute attribute : tableDefinition.getAttributeList()) {
            tableMetaStreamEvent.addOutputData(attribute);
        }
        StreamEventPool tableStreamEventPool = new StreamEventPool(tableMetaStreamEvent, 10);
        StreamEventCloner tableStreamEventCloner = new StreamEventCloner(tableMetaStreamEvent, tableStreamEventPool);
        Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STORE, tableDefinition.getAnnotations());
        Table table;
        ConfigReader configReader = null;
        RecordTableHandlerManager recordTableHandlerManager = null;
        RecordTableHandler recordTableHandler = null;
        if (annotation != null) {
            annotation = updateAnnotationRef(annotation, SiddhiConstants.NAMESPACE_STORE, siddhiAppContext);
            String tableType = annotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
            Extension extension = new Extension() {

                @Override
                public String getNamespace() {
                    return SiddhiConstants.NAMESPACE_STORE;
                }

                @Override
                public String getName() {
                    return tableType;
                }
            };
            recordTableHandlerManager = siddhiAppContext.getSiddhiContext().getRecordTableHandlerManager();
            if (recordTableHandlerManager != null) {
                recordTableHandler = recordTableHandlerManager.generateRecordTableHandler();
            }
            table = (Table) SiddhiClassLoader.loadExtensionImplementation(extension, TableExtensionHolder.getInstance(siddhiAppContext));
            configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
        } else {
            table = new InMemoryTable();
        }
        table.initTable(tableDefinition, tableStreamEventPool, tableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
        if (recordTableHandler != null) {
            recordTableHandlerManager.registerRecordTableHandler(recordTableHandler.getElementId(), recordTableHandler);
        }
        tableMap.putIfAbsent(tableDefinition.getId(), table);
    }
}
Also used : Extension(org.ballerinalang.siddhi.query.api.extension.Extension) InMemoryTable(org.ballerinalang.siddhi.core.table.InMemoryTable) InMemoryTable(org.ballerinalang.siddhi.core.table.InMemoryTable) Table(org.ballerinalang.siddhi.core.table.Table) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) ConfigReader(org.ballerinalang.siddhi.core.util.config.ConfigReader) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool) StreamEventCloner(org.ballerinalang.siddhi.core.event.stream.StreamEventCloner) RecordTableHandler(org.ballerinalang.siddhi.core.table.record.RecordTableHandler) RecordTableHandlerManager(org.ballerinalang.siddhi.core.table.record.RecordTableHandlerManager) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation)

Example 34 with Attribute

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

the class Window method init.

/**
 * Initialize the WindowEvent table by creating {@link WindowProcessor} to handle the events.
 *
 * @param tableMap       map of {@link Table}s
 * @param eventWindowMap map of EventWindows
 * @param queryName      name of the query window belongs to.
 */
public void init(Map<String, Table> tableMap, Map<String, Window> eventWindowMap, String queryName) {
    if (this.windowProcessor != null) {
        return;
    }
    // Create and initialize MetaStreamEvent
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addInputDefinition(windowDefinition);
    metaStreamEvent.setEventType(MetaStreamEvent.EventType.WINDOW);
    metaStreamEvent.initializeAfterWindowData();
    for (Attribute attribute : windowDefinition.getAttributeList()) {
        metaStreamEvent.addOutputData(attribute);
    }
    this.streamEventPool = new StreamEventPool(metaStreamEvent, 5);
    StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, this.streamEventPool);
    OutputStream.OutputEventType outputEventType = windowDefinition.getOutputEventType();
    boolean outputExpectsExpiredEvents = outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS;
    WindowProcessor internalWindowProcessor = (WindowProcessor) SingleInputStreamParser.generateProcessor(windowDefinition.getWindow(), metaStreamEvent, new ArrayList<VariableExpressionExecutor>(), this.siddhiAppContext, tableMap, false, outputExpectsExpiredEvents, queryName);
    internalWindowProcessor.setStreamEventCloner(streamEventCloner);
    internalWindowProcessor.constructStreamEventPopulater(metaStreamEvent, 0);
    EntryValveProcessor entryValveProcessor = null;
    if (internalWindowProcessor instanceof SchedulingProcessor) {
        entryValveProcessor = new EntryValveProcessor(this.siddhiAppContext);
        Scheduler scheduler = SchedulerParser.parse(this.siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, this.siddhiAppContext);
        scheduler.init(this.lockWrapper, queryName);
        scheduler.setStreamEventPool(streamEventPool);
        ((SchedulingProcessor) internalWindowProcessor).setScheduler(scheduler);
    }
    if (entryValveProcessor != null) {
        entryValveProcessor.setToLast(internalWindowProcessor);
        this.windowProcessor = entryValveProcessor;
    } else {
        this.windowProcessor = internalWindowProcessor;
    }
    // StreamPublishProcessor must be the last in chain so that it can publish the events to StreamJunction
    this.windowProcessor.setToLast(new StreamPublishProcessor(outputEventType));
    this.internalWindowProcessor = internalWindowProcessor;
}
Also used : Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) Scheduler(org.ballerinalang.siddhi.core.util.Scheduler) OutputStream(org.ballerinalang.siddhi.query.api.execution.query.output.stream.OutputStream) ArrayList(java.util.ArrayList) SchedulingProcessor(org.ballerinalang.siddhi.core.query.processor.SchedulingProcessor) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool) StreamEventCloner(org.ballerinalang.siddhi.core.event.stream.StreamEventCloner) EntryValveProcessor(org.ballerinalang.siddhi.core.query.input.stream.single.EntryValveProcessor) WindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.WindowProcessor) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Example 35 with Attribute

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

the class MinIncrementalAttributeAggregator method init.

@Override
public void init(String attributeName, Attribute.Type attributeType) {
    if (attributeName == null) {
        throw new SiddhiAppCreationException("Min incremental attribute aggregation cannot be executed " + "when no parameters are given");
    }
    if (attributeType.equals(Attribute.Type.INT) || attributeType.equals(Attribute.Type.LONG) || attributeType.equals(Attribute.Type.DOUBLE) || attributeType.equals(Attribute.Type.FLOAT)) {
        this.baseAttributes = new Attribute[] { new Attribute("AGG_MIN_".concat(attributeName), attributeType) };
        this.baseAttributesInitialValues = new Expression[] { Expression.variable(attributeName) };
        this.returnType = attributeType;
        assert baseAttributes.length == baseAttributesInitialValues.length;
    } else {
        throw new SiddhiAppRuntimeException("Min aggregation cannot be executed on attribute type " + attributeType.toString());
    }
}
Also used : Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) ReturnAttribute(org.ballerinalang.siddhi.annotation.ReturnAttribute) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) SiddhiAppRuntimeException(org.ballerinalang.siddhi.core.exception.SiddhiAppRuntimeException)

Aggregations

Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)37 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)19 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)15 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)14 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)13 ArrayList (java.util.ArrayList)12 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)11 Expression (org.ballerinalang.siddhi.query.api.expression.Expression)11 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)10 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)9 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)8 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)8 OutputAttribute (org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute)8 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)7 Test (org.testng.annotations.Test)7 HashMap (java.util.HashMap)6 ReturnAttribute (org.ballerinalang.siddhi.annotation.ReturnAttribute)5 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)5 ZeroStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter)5 AndConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor)5