Search in sources :

Example 1 with ElementIdGenerator

use of org.ballerinalang.siddhi.core.util.ElementIdGenerator 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 2 with ElementIdGenerator

use of org.ballerinalang.siddhi.core.util.ElementIdGenerator in project ballerina by ballerina-lang.

the class SiddhiAppParser method parse.

/**
 * Parse an SiddhiApp returning SiddhiAppRuntime.
 *
 * @param siddhiApp       plan to be parsed
 * @param siddhiAppString content of Siddhi application as string
 * @param siddhiContext   SiddhiContext  @return SiddhiAppRuntime
 * @return SiddhiAppRuntimeBuilder
 */
public static SiddhiAppRuntimeBuilder parse(SiddhiApp siddhiApp, String siddhiAppString, SiddhiContext siddhiContext) {
    SiddhiAppContext siddhiAppContext = new SiddhiAppContext();
    siddhiAppContext.setSiddhiContext(siddhiContext);
    siddhiAppContext.setSiddhiAppString(siddhiAppString);
    try {
        Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_NAME, null, siddhiApp.getAnnotations());
        if (element != null) {
            siddhiAppContext.setName(element.getValue());
        } else {
            siddhiAppContext.setName(UUID.randomUUID().toString());
        }
        Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ENFORCE_ORDER, siddhiApp.getAnnotations());
        if (annotation != null) {
            siddhiAppContext.setEnforceOrder(true);
        }
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ASYNC, siddhiApp.getAnnotations());
        if (annotation != null) {
            throw new SiddhiAppCreationException("@Async not supported in SiddhiApp level, " + "instead use @Async with streams", annotation.getQueryContextStartIndex(), annotation.getQueryContextEndIndex());
        }
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STATISTICS, siddhiApp.getAnnotations());
        List<Element> statisticsElements = new ArrayList<>();
        if (annotation != null) {
            statisticsElements = annotation.getElements();
        }
        if (siddhiContext.getStatisticsConfiguration() != null) {
            siddhiAppContext.setStatisticsManager(siddhiContext.getStatisticsConfiguration().getFactory().createStatisticsManager(siddhiContext.getStatisticsConfiguration().getMetricPrefix(), siddhiAppContext.getName(), statisticsElements));
        }
        Element statStateEnableElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_ENABLE, siddhiApp.getAnnotations());
        if (statStateEnableElement != null && Boolean.valueOf(statStateEnableElement.getValue())) {
            siddhiAppContext.setStatsEnabled(true);
        } else {
            Element statStateElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, null, siddhiApp.getAnnotations());
            // where sp uses @app:statistics('true').
            if (annotation != null && (statStateElement == null || Boolean.valueOf(statStateElement.getValue()))) {
                siddhiAppContext.setStatsEnabled(true);
            }
        }
        Element statStateIncludElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_INCLUDE, siddhiApp.getAnnotations());
        siddhiAppContext.setIncludedMetrics(generateIncludedMetrics(statStateIncludElement));
        siddhiAppContext.setThreadBarrier(new ThreadBarrier());
        siddhiAppContext.setExecutorService(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-executor-thread-%d").build()));
        siddhiAppContext.setScheduledExecutorService(Executors.newScheduledThreadPool(5, new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-scheduler-thread-%d").build()));
        // Select the TimestampGenerator based on playback mode on/off
        annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PLAYBACK, siddhiApp.getAnnotations());
        if (annotation != null) {
            String idleTime = null;
            String increment = null;
            EventTimeBasedMillisTimestampGenerator timestampGenerator = new EventTimeBasedMillisTimestampGenerator(siddhiAppContext.getScheduledExecutorService());
            // Get the optional elements of playback annotation
            for (Element e : annotation.getElements()) {
                if (SiddhiConstants.ANNOTATION_ELEMENT_IDLE_TIME.equalsIgnoreCase(e.getKey())) {
                    idleTime = e.getValue();
                } else if (SiddhiConstants.ANNOTATION_ELEMENT_INCREMENT.equalsIgnoreCase(e.getKey())) {
                    increment = e.getValue();
                } else {
                    throw new SiddhiAppValidationException("Playback annotation accepts only idle.time and " + "increment but found " + e.getKey());
                }
            }
            // idleTime and increment are optional but if one presents, the other also should be given
            if (idleTime != null && increment == null) {
                throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but increment not found");
            } else if (idleTime == null && increment != null) {
                throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but idle.time does not found");
            } else if (idleTime != null) {
                // The fourth case idleTime == null && increment == null are ignored because it means no heartbeat.
                try {
                    timestampGenerator.setIdleTime(SiddhiCompiler.parseTimeConstantDefinition(idleTime).value());
                } catch (SiddhiParserException ex) {
                    throw new SiddhiParserException("Invalid idle.time constant '" + idleTime + "' in playback " + "annotation", ex);
                }
                try {
                    timestampGenerator.setIncrementInMilliseconds(SiddhiCompiler.parseTimeConstantDefinition(increment).value());
                } catch (SiddhiParserException ex) {
                    throw new SiddhiParserException("Invalid increment constant '" + increment + "' in playback " + "annotation", ex);
                }
            }
            siddhiAppContext.setTimestampGenerator(timestampGenerator);
            siddhiAppContext.setPlayback(true);
        } else {
            siddhiAppContext.setTimestampGenerator(new SystemCurrentTimeMillisTimestampGenerator());
        }
        siddhiAppContext.setSnapshotService(new SnapshotService(siddhiAppContext));
        siddhiAppContext.setPersistenceService(new PersistenceService(siddhiAppContext));
        siddhiAppContext.setElementIdGenerator(new ElementIdGenerator(siddhiAppContext.getName()));
    } catch (DuplicateAnnotationException e) {
        throw new DuplicateAnnotationException(e.getMessageWithOutContext() + " for the same Siddhi app " + siddhiApp.toString(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
    }
    SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder = new SiddhiAppRuntimeBuilder(siddhiAppContext);
    defineStreamDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getStreamDefinitionMap(), siddhiAppContext);
    defineTableDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTableDefinitionMap(), siddhiAppContext);
    defineWindowDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getWindowDefinitionMap(), siddhiAppContext);
    defineFunctionDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getFunctionDefinitionMap(), siddhiAppContext);
    defineAggregationDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getAggregationDefinitionMap(), siddhiAppContext);
    for (Window window : siddhiAppRuntimeBuilder.getWindowMap().values()) {
        try {
            window.init(siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getWindowMap(), window.getWindowDefinition().getId());
        } catch (Throwable t) {
            ExceptionUtil.populateQueryContext(t, window.getWindowDefinition(), siddhiAppContext);
            throw t;
        }
    }
    int queryIndex = 1;
    for (ExecutionElement executionElement : siddhiApp.getExecutionElementList()) {
        if (executionElement instanceof Query) {
            try {
                QueryRuntime queryRuntime = QueryParser.parse((Query) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), siddhiAppRuntimeBuilder.getTableDefinitionMap(), siddhiAppRuntimeBuilder.getWindowDefinitionMap(), siddhiAppRuntimeBuilder.getAggregationDefinitionMap(), siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getAggregationMap(), siddhiAppRuntimeBuilder.getWindowMap(), siddhiAppRuntimeBuilder.getLockSynchronizer(), String.valueOf(queryIndex));
                siddhiAppRuntimeBuilder.addQuery(queryRuntime);
                queryIndex++;
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Query) executionElement, siddhiAppContext);
                throw t;
            }
        } else {
            try {
                PartitionRuntime partitionRuntime = PartitionParser.parse(siddhiAppRuntimeBuilder, (Partition) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), queryIndex);
                siddhiAppRuntimeBuilder.addPartition(partitionRuntime);
                siddhiAppContext.getSnapshotService().addSnapshotable("partition", partitionRuntime);
                queryIndex += ((Partition) executionElement).getQueryList().size();
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, (Partition) executionElement, siddhiAppContext);
                throw t;
            }
        }
    }
    // Done last as they have to be started last
    defineTriggerDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTriggerDefinitionMap(), siddhiAppContext);
    return siddhiAppRuntimeBuilder;
}
Also used : Query(org.ballerinalang.siddhi.query.api.execution.query.Query) ExecutionElement(org.ballerinalang.siddhi.query.api.execution.ExecutionElement) Element(org.ballerinalang.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList) ElementIdGenerator(org.ballerinalang.siddhi.core.util.ElementIdGenerator) EventTimeBasedMillisTimestampGenerator(org.ballerinalang.siddhi.core.util.timestamp.EventTimeBasedMillisTimestampGenerator) ThreadBarrier(org.ballerinalang.siddhi.core.util.ThreadBarrier) QueryRuntime(org.ballerinalang.siddhi.core.query.QueryRuntime) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Window(org.ballerinalang.siddhi.core.window.Window) SnapshotService(org.ballerinalang.siddhi.core.util.snapshot.SnapshotService) Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation) PersistenceService(org.ballerinalang.siddhi.core.util.persistence.PersistenceService) SiddhiAppRuntimeBuilder(org.ballerinalang.siddhi.core.util.SiddhiAppRuntimeBuilder) DuplicateAnnotationException(org.ballerinalang.siddhi.query.api.exception.DuplicateAnnotationException) SiddhiParserException(org.ballerinalang.siddhi.query.compiler.exception.SiddhiParserException) ExecutionElement(org.ballerinalang.siddhi.query.api.execution.ExecutionElement) SystemCurrentTimeMillisTimestampGenerator(org.ballerinalang.siddhi.core.util.timestamp.SystemCurrentTimeMillisTimestampGenerator) SiddhiAppContext(org.ballerinalang.siddhi.core.config.SiddhiAppContext) PartitionRuntime(org.ballerinalang.siddhi.core.partition.PartitionRuntime)

Aggregations

SiddhiAppContext (org.ballerinalang.siddhi.core.config.SiddhiAppContext)2 QueryRuntime (org.ballerinalang.siddhi.core.query.QueryRuntime)2 ElementIdGenerator (org.ballerinalang.siddhi.core.util.ElementIdGenerator)2 SnapshotService (org.ballerinalang.siddhi.core.util.snapshot.SnapshotService)2 Window (org.ballerinalang.siddhi.core.window.Window)2 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AggregationRuntime (org.ballerinalang.siddhi.core.aggregation.AggregationRuntime)1 SiddhiContext (org.ballerinalang.siddhi.core.config.SiddhiContext)1 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)1 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)1 PartitionRuntime (org.ballerinalang.siddhi.core.partition.PartitionRuntime)1 SingleStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime)1 Source (org.ballerinalang.siddhi.core.stream.input.source.Source)1 Sink (org.ballerinalang.siddhi.core.stream.output.sink.Sink)1 Table (org.ballerinalang.siddhi.core.table.Table)1 SiddhiAppRuntimeBuilder (org.ballerinalang.siddhi.core.util.SiddhiAppRuntimeBuilder)1