Search in sources :

Example 11 with Annotation

use of org.wso2.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class JunctionTestCase method multiThreadedWithEventPoolTest.

@Test
public void multiThreadedWithEventPoolTest() throws InterruptedException {
    log.info("multi threaded test using event pool");
    final StreamEventPool streamEventPoolA1 = new StreamEventPool(2, 2, 2, 4);
    final StreamEventPool streamEventPoolA2 = new StreamEventPool(2, 2, 2, 4);
    final StreamEventPool streamEventPoolA3 = new StreamEventPool(2, 2, 2, 4);
    final StreamEventPool streamEventPoolB1 = new StreamEventPool(2, 2, 2, 4);
    final StreamEventPool streamEventPoolB2 = new StreamEventPool(2, 2, 2, 4);
    StreamDefinition streamA = StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("async"));
    StreamJunction streamJunctionA = new StreamJunction(streamA, executorService, 1024, siddhiAppContext);
    StreamJunction.Publisher streamPublisherA = streamJunctionA.constructPublisher();
    StreamDefinition streamB = StreamDefinition.id("streamB").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("async"));
    StreamJunction streamJunctionB = new StreamJunction(streamB, executorService, 1024, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherB1 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB2 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB3 = streamJunctionB.constructPublisher();
    StreamDefinition streamC = StreamDefinition.id("streamC").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("async"));
    StreamJunction streamJunctionC = new StreamJunction(streamC, executorService, 1024, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherC1 = streamJunctionC.constructPublisher();
    final StreamJunction.Publisher streamPublisherC2 = streamJunctionC.constructPublisher();
    StreamCallback streamCallbackA1 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolA1.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A1");
                innerStreamEvent.setOutputData(data);
                streamPublisherB1.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackA2 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolA2.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A2");
                innerStreamEvent.setOutputData(data);
                streamPublisherB2.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackA3 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolA3.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("A3");
                innerStreamEvent.setOutputData(data);
                streamPublisherB3.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB1 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolB1.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("B1");
                innerStreamEvent.setOutputData(data);
                streamPublisherC1.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB2 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = streamEventPoolB2.borrowEvent();
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                Object[] data = new Object[] { streamEvent.getData()[0], streamEvent.getData()[1] };
                data[0] = ((String) data[0]).concat("B2");
                innerStreamEvent.setOutputData(data);
                streamPublisherC2.send(innerStreamEvent);
            }
        }
    };
    final boolean[] eventsArrived = { false, false, false, false, false, false, false, false, false, false, false, false };
    StreamCallback streamCallbackC = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                count++;
                eventArrived = true;
                Object symbol = streamEvent.getData()[0];
                if (symbol.equals("IBMA1B1")) {
                    eventsArrived[0] = true;
                } else if (symbol.equals("IBMA1B2")) {
                    eventsArrived[1] = true;
                } else if (symbol.equals("IBMA2B1")) {
                    eventsArrived[2] = true;
                } else if (symbol.equals("IBMA2B2")) {
                    eventsArrived[3] = true;
                } else if (symbol.equals("IBMA3B1")) {
                    eventsArrived[4] = true;
                } else if (symbol.equals("IBMA3B2")) {
                    eventsArrived[5] = true;
                }
                if (symbol.equals("WSO2A1B1")) {
                    eventsArrived[6] = true;
                } else if (symbol.equals("WSO2A1B2")) {
                    eventsArrived[7] = true;
                } else if (symbol.equals("WSO2A2B1")) {
                    eventsArrived[8] = true;
                } else if (symbol.equals("WSO2A2B2")) {
                    eventsArrived[9] = true;
                } else if (symbol.equals("WSO2A3B1")) {
                    eventsArrived[10] = true;
                } else if (symbol.equals("WSO2A3B2")) {
                    eventsArrived[11] = true;
                }
            }
        }
    };
    streamJunctionA.subscribe(streamCallbackA1);
    streamJunctionA.subscribe(streamCallbackA2);
    streamJunctionA.subscribe(streamCallbackA3);
    streamJunctionA.startProcessing();
    streamJunctionB.subscribe(streamCallbackB1);
    streamJunctionB.subscribe(streamCallbackB2);
    streamJunctionB.startProcessing();
    streamJunctionC.subscribe(streamCallbackC);
    streamJunctionC.startProcessing();
    StreamEvent streamEvent1 = new StreamEvent(2, 2, 2);
    streamEvent1.setTimestamp(System.currentTimeMillis());
    streamEvent1.setOutputData(new Object[] { "IBM", 12 });
    StreamEvent streamEvent2 = new StreamEvent(2, 2, 2);
    streamEvent2.setTimestamp(System.currentTimeMillis());
    streamEvent2.setOutputData(new Object[] { "WSO2", 112 });
    streamPublisherA.send(streamEvent1);
    streamPublisherA.send(streamEvent2);
    Thread.sleep(1000);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(12, count);
    for (boolean arrived : eventsArrived) {
        AssertJUnit.assertTrue(arrived);
    }
    streamJunctionA.stopProcessing();
    streamJunctionB.stopProcessing();
    streamJunctionC.stopProcessing();
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StreamEventPool(org.wso2.siddhi.core.event.stream.StreamEventPool) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) Event(org.wso2.siddhi.core.event.Event) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 12 with Annotation

use of org.wso2.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitDefinition_table.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public TableDefinition visitDefinition_table(@NotNull SiddhiQLParser.Definition_tableContext ctx) {
    // definition_table
    // : annotation* DEFINE TABLE source '(' attribute_name attribute_type (',' attribute_name attribute_type )*
    // ')' definition_store?
    // ;
    Source source = (Source) visit(ctx.source());
    if (source.isInnerStream) {
        throw newSiddhiParserException(ctx, "'#' cannot be used, because Tables can't be defined as " + "InnerStream!");
    }
    TableDefinition tableDefinition = TableDefinition.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);
        tableDefinition.attribute((String) visit(attributeNameContext), (Attribute.Type) visit(attributeTypeContext));
    }
    for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
        tableDefinition.annotation((Annotation) visit(annotationContext));
    }
    populateQueryContext(tableDefinition, ctx);
    return tableDefinition;
}
Also used : 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) TableDefinition(org.wso2.siddhi.query.api.definition.TableDefinition)

Example 13 with Annotation

use of org.wso2.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitPartition.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Partition visitPartition(@NotNull SiddhiQLParser.PartitionContext ctx) {
    Partition partition = Partition.partition();
    for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
        partition.annotation((Annotation) visit(annotationContext));
    }
    for (SiddhiQLParser.Partition_with_streamContext with_streamContext : ctx.partition_with_stream()) {
        partition.with((PartitionType) visit(with_streamContext));
    }
    for (SiddhiQLParser.QueryContext queryContext : ctx.query()) {
        partition.addQuery((Query) visit(queryContext));
    }
    populateQueryContext(partition, ctx);
    return partition;
}
Also used : Partition(org.wso2.siddhi.query.api.execution.partition.Partition) SiddhiQLParser(org.wso2.siddhi.query.compiler.SiddhiQLParser)

Example 14 with Annotation

use of org.wso2.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class DefineTableTestCase method test4.

@Test
public void test4() throws SiddhiParserException {
    TableDefinition streamDefinition = SiddhiCompiler.parseTableDefinition("" + " @from(datasource='MyDatabase','CUSTOM')" + " define table cseStream ( symbol string, price int, volume float )");
    AssertJUnit.assertEquals(TableDefinition.id("cseStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).attribute("volume", Attribute.Type.FLOAT).annotation(Annotation.annotation("from").element("datasource", "MyDatabase").element("CUSTOM")).toString(), streamDefinition.toString());
}
Also used : TableDefinition(org.wso2.siddhi.query.api.definition.TableDefinition) Test(org.testng.annotations.Test)

Example 15 with Annotation

use of org.wso2.siddhi.query.api.annotation.Annotation in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitQuery.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Query visitQuery(@NotNull SiddhiQLParser.QueryContext ctx) {
    try {
        Query query = Query.query().from((InputStream) visit(ctx.query_input()));
        if (ctx.query_section() != null) {
            query.select((Selector) visit(ctx.query_section()));
        }
        if (ctx.output_rate() != null) {
            query.output((OutputRate) visit(ctx.output_rate()));
        }
        for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
            query.annotation((Annotation) visit(annotationContext));
        }
        if (ctx.query_output() != null) {
            query.outStream((OutputStream) visit(ctx.query_output()));
        }
        populateQueryContext(query, ctx);
        return query;
    } finally {
        activeStreams.clear();
    }
}
Also used : SiddhiQLParser(org.wso2.siddhi.query.compiler.SiddhiQLParser) StoreQuery(org.wso2.siddhi.query.api.execution.query.StoreQuery) Query(org.wso2.siddhi.query.api.execution.query.Query)

Aggregations

Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)14 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)12 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)11 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)11 HashMap (java.util.HashMap)9 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)9 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)9 List (java.util.List)8 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)8 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)8 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)8 Annotation (org.wso2.siddhi.query.api.annotation.Annotation)8 BLangConnector (org.wso2.ballerinalang.compiler.tree.BLangConnector)7 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)7 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)7 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)7 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)7 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)7 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)7