Search in sources :

Example 66 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class DefinitionParserHelper method addEventSource.

public static void addEventSource(StreamDefinition streamDefinition, ConcurrentMap<String, List<Source>> eventSourceMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sourceAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SOURCE.equalsIgnoreCase(sourceAnnotation.getName())) {
            sourceAnnotation = updateAnnotationRef(sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE, siddhiAppContext);
            Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sourceAnnotation.getAnnotations());
            if (mapAnnotation == null) {
                mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
            }
            final String sourceType = sourceAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
            final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
            if (sourceType != null && mapType != null) {
                SourceHandlerManager sourceHandlerManager = siddhiAppContext.getSiddhiContext().getSourceHandlerManager();
                SourceHandler sourceHandler = null;
                if (sourceHandlerManager != null) {
                    sourceHandler = sourceHandlerManager.generateSourceHandler();
                }
                // load input transport extension
                Extension sourceExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SOURCE, sourceType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE);
                Source source = (Source) SiddhiClassLoader.loadExtensionImplementation(sourceExtension, SourceExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sourceExtension.getNamespace(), sourceExtension.getName());
                // load input mapper extension
                Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE_MAPPER);
                SourceMapper sourceMapper = (SourceMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SourceMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(mapperExtension.getNamespace(), mapperExtension.getName());
                validateSourceMapperCompatibility(streamDefinition, sourceType, mapType, source, sourceMapper, sourceAnnotation);
                OptionHolder sourceOptionHolder = constructOptionHolder(streamDefinition, sourceAnnotation, source.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class), null);
                OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sourceMapper.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class), null);
                AttributesHolder attributesHolder = getAttributeMappings(mapAnnotation, mapType, streamDefinition);
                String[] transportPropertyNames = getTransportPropertyNames(attributesHolder);
                try {
                    source.init(sourceType, sourceOptionHolder, sourceMapper, transportPropertyNames, configReader, mapType, mapOptionHolder, attributesHolder.payloadMappings, attributesHolder.transportMappings, mapperConfigReader, sourceHandler, streamDefinition, siddhiAppContext);
                } catch (Throwable t) {
                    ExceptionUtil.populateQueryContext(t, sourceAnnotation, siddhiAppContext);
                    throw t;
                }
                siddhiAppContext.getSnapshotService().addSnapshotable(source.getStreamDefinition().getId(), source);
                if (sourceHandlerManager != null) {
                    sourceHandlerManager.registerSourceHandler(sourceHandler.getElementId(), sourceHandler);
                    siddhiAppContext.getSnapshotService().addSnapshotable(streamDefinition.getId(), sourceHandler);
                }
                List<Source> eventSources = eventSourceMap.get(streamDefinition.getId());
                if (eventSources == null) {
                    eventSources = new ArrayList<>();
                    eventSources.add(source);
                    eventSourceMap.put(streamDefinition.getId(), eventSources);
                } else {
                    eventSources.add(source);
                }
            } else {
                throw new SiddhiAppCreationException("Both @Sink(type=) and @map(type=) are required.", sourceAnnotation.getQueryContextStartIndex(), sourceAnnotation.getQueryContextEndIndex());
            }
        }
    }
}
Also used : SourceHandler(org.wso2.siddhi.core.stream.input.source.SourceHandler) SourceHandlerManager(org.wso2.siddhi.core.stream.input.source.SourceHandlerManager) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader) Annotation(org.wso2.siddhi.query.api.annotation.Annotation) Source(org.wso2.siddhi.core.stream.input.source.Source) Extension(org.wso2.siddhi.query.api.extension.Extension) OptionHolder(org.wso2.siddhi.core.util.transport.OptionHolder) SourceMapper(org.wso2.siddhi.core.stream.input.source.SourceMapper)

Example 67 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitDefinition_stream.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public StreamDefinition visitDefinition_stream(@NotNull SiddhiQLParser.Definition_streamContext ctx) {
    Source source = (Source) visit(ctx.source());
    if (source.isInnerStream) {
        throw newSiddhiParserException(ctx, " InnerStreams cannot be defined!");
    }
    try {
        StreamDefinition streamDefinition = StreamDefinition.id(source.streamId);
        populateQueryContext(streamDefinition, ctx);
        List<SiddhiQLParser.Attribute_nameContext> attribute_names = ctx.attribute_name();
        List<SiddhiQLParser.Attribute_typeContext> attribute_types = ctx.attribute_type();
        for (int i = 0; i < attribute_names.size(); i++) {
            SiddhiQLParser.Attribute_nameContext attributeNameContext = attribute_names.get(i);
            SiddhiQLParser.Attribute_typeContext attributeTypeContext = attribute_types.get(i);
            streamDefinition.attribute((String) visit(attributeNameContext), (Attribute.Type) visit(attributeTypeContext));
        }
        for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
            streamDefinition.annotation((Annotation) visit(annotationContext));
        }
        return streamDefinition;
    } catch (Throwable t) {
        throw newSiddhiParserException(ctx, t.getMessage(), t);
    }
}
Also used : SiddhiQLParser(org.wso2.siddhi.query.compiler.SiddhiQLParser) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) Attribute(org.wso2.siddhi.query.api.definition.Attribute) OrderByAttribute(org.wso2.siddhi.query.api.execution.query.selection.OrderByAttribute)

Example 68 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitBasic_source.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public BasicSingleInputStream visitBasic_source(@NotNull SiddhiQLParser.Basic_sourceContext ctx) {
    // basic_source
    // : io (basic_source_stream_handler)*
    // ;
    Source source = (Source) visit(ctx.source());
    BasicSingleInputStream basicSingleInputStream = new BasicSingleInputStream(null, source.streamId, source.isInnerStream);
    if (ctx.basic_source_stream_handlers() != null) {
        basicSingleInputStream.addStreamHandlers((List<StreamHandler>) visit(ctx.basic_source_stream_handlers()));
    }
    populateQueryContext(basicSingleInputStream, ctx);
    return basicSingleInputStream;
}
Also used : BasicSingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) StreamHandler(org.wso2.siddhi.query.api.execution.query.input.handler.StreamHandler)

Example 69 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitDefinition_window.

@Override
public Object visitDefinition_window(@NotNull SiddhiQLParser.Definition_windowContext ctx) {
    Source source = (Source) visit(ctx.source());
    if (source.isInnerStream) {
        throw newSiddhiParserException(ctx, "'#' cannot be used, because Windows can't be defined as InnerStream!");
    }
    WindowDefinition windowDefinition = WindowDefinition.id(source.streamId);
    List<SiddhiQLParser.Attribute_nameContext> attribute_names = ctx.attribute_name();
    List<SiddhiQLParser.Attribute_typeContext> attribute_types = ctx.attribute_type();
    for (int i = 0; i < attribute_names.size(); i++) {
        SiddhiQLParser.Attribute_nameContext attributeNameContext = attribute_names.get(i);
        SiddhiQLParser.Attribute_typeContext attributeTypeContext = attribute_types.get(i);
        windowDefinition.attribute((String) visit(attributeNameContext), (Attribute.Type) visit(attributeTypeContext));
    }
    for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
        windowDefinition.annotation((Annotation) visit(annotationContext));
    }
    AttributeFunction attributeFunction = (AttributeFunction) visit(ctx.function_operation());
    Window window = new Window(attributeFunction.getNamespace(), attributeFunction.getName(), attributeFunction.getParameters());
    windowDefinition.window(window);
    // Optional output event type
    if (ctx.output_event_type() != null) {
        windowDefinition.setOutputEventType((OutputStream.OutputEventType) visit(ctx.output_event_type()));
    }
    populateQueryContext(windowDefinition, ctx);
    return windowDefinition;
}
Also used : Window(org.wso2.siddhi.query.api.execution.query.input.handler.Window) SiddhiQLParser(org.wso2.siddhi.query.compiler.SiddhiQLParser) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) Attribute(org.wso2.siddhi.query.api.definition.Attribute) OrderByAttribute(org.wso2.siddhi.query.api.execution.query.selection.OrderByAttribute) OutputStream(org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream) AttributeFunction(org.wso2.siddhi.query.api.expression.AttributeFunction) WindowDefinition(org.wso2.siddhi.query.api.definition.WindowDefinition)

Example 70 with Source

use of org.wso2.siddhi.core.stream.input.source.Source in project siddhi by wso2.

the class DefineStreamTestCase method testMultilevelNestedAnnotations2.

@Test
public void testMultilevelNestedAnnotations2() throws SiddhiParserException {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("" + "@source(" + "   type='http', " + "   context='/test', " + "   transport='http,https', " + "   @map(" + "       type='xml', " + "       namespace = \"h=uri, a=uri\", " + "       @attributes(" + "           '//h:time', " + "           '//h:data'" + "       )" + "   )" + ") " + "define stream fooStream (id int, name string);");
    AssertJUnit.assertEquals(StreamDefinition.id("fooStream").attribute("id", Attribute.Type.INT).attribute("name", Attribute.Type.STRING).annotation(Annotation.annotation("source").element("type", "http").element("context", "/test").element("transport", "http,https").annotation(Annotation.annotation("map").element("type", "xml").element("namespace", "h=uri, a=uri").annotation(Annotation.annotation("attributes").element("//h:time").element("//h:data")))), streamDefinition);
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)22 CompilerContext (org.wso2.ballerinalang.compiler.util.CompilerContext)15 ArrayList (java.util.ArrayList)11 CompilerOptions (org.wso2.ballerinalang.compiler.util.CompilerOptions)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)8 ParseTree (org.antlr.v4.runtime.tree.ParseTree)8 OMElement (org.apache.axiom.om.OMElement)8 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)8 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)8 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)8 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)8 Event (org.wso2.siddhi.core.event.Event)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Compiler (org.wso2.ballerinalang.compiler.Compiler)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)6 SiddhiQLBaseVisitorImpl (org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)6