Search in sources :

Example 6 with Source

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

the class InMemoryTransportTestCase method inMemorySourceSinkAndEventMappingWithSiddhiQL.

@Test
public void inMemorySourceSinkAndEventMappingWithSiddhiQL() throws InterruptedException {
    log.info("Test inMemory Source Sink And EventMapping With SiddhiQL");
    InMemoryBroker.Subscriber subscriptionWSO2 = new InMemoryBroker.Subscriber() {

        @Override
        public void onMessage(Object msg) {
            wso2Count.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "WSO2";
        }
    };
    InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {

        @Override
        public void onMessage(Object msg) {
            ibmCount.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "IBM";
        }
    };
    // subscribe to "inMemory" broker per topic
    InMemoryBroker.subscribe(subscriptionWSO2);
    InMemoryBroker.subscribe(subscriptionIBM);
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='inMemory', topic='Foo', @map(type='passThrough')) " + "define stream FooStream (symbol string, price float, volume long); " + "@sink(type='inMemory', topic='{{symbol}}', @map(type='passThrough')) " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.start();
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    AssertJUnit.assertEquals("Number of IBM events", 1, ibmCount.get());
    siddhiAppRuntime.shutdown();
    // unsubscribe from "inMemory" broker per topic
    InMemoryBroker.unsubscribe(subscriptionWSO2);
    InMemoryBroker.unsubscribe(subscriptionIBM);
}
Also used : InMemoryBroker(org.wso2.siddhi.core.util.transport.InMemoryBroker) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 7 with Source

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

the class InMemoryTransportTestCase method inMemorySourceSinkAndEventMappingWithSiddhiQLAndRef.

@Test
public void inMemorySourceSinkAndEventMappingWithSiddhiQLAndRef() throws InterruptedException {
    log.info("Test inMemory Source Sink And EventMapping With SiddhiQL and Ref");
    InMemoryBroker.Subscriber subscriptionWSO2 = new InMemoryBroker.Subscriber() {

        @Override
        public void onMessage(Object msg) {
            wso2Count.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "WSO2";
        }
    };
    InMemoryBroker.Subscriber subscriptionIBM = new InMemoryBroker.Subscriber() {

        @Override
        public void onMessage(Object msg) {
            ibmCount.incrementAndGet();
        }

        @Override
        public String getTopic() {
            return "IBM";
        }
    };
    // subscribe to "inMemory" broker per topic
    InMemoryBroker.subscribe(subscriptionWSO2);
    InMemoryBroker.subscribe(subscriptionIBM);
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(ref='test1', @map(type='passThrough')) " + "define stream FooStream (symbol string, price float, volume long); " + "@sink(ref='test2', @map(type='passThrough')) " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    Map<String, String> systemConfigs = new HashMap<>();
    systemConfigs.put("test1.topic", "Foo");
    systemConfigs.put("test1.type", "inMemory");
    systemConfigs.put("test2.type", "inMemory");
    systemConfigs.put("test2.topic", "{{symbol}}");
    InMemoryConfigManager inMemoryConfigManager = new InMemoryConfigManager(null, systemConfigs);
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setConfigManager(inMemoryConfigManager);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.start();
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    AssertJUnit.assertEquals("Number of IBM events", 1, ibmCount.get());
    siddhiAppRuntime.shutdown();
    // unsubscribe from "inMemory" broker per topic
    InMemoryBroker.unsubscribe(subscriptionWSO2);
    InMemoryBroker.unsubscribe(subscriptionIBM);
}
Also used : InMemoryConfigManager(org.wso2.siddhi.core.util.config.InMemoryConfigManager) HashMap(java.util.HashMap) InMemoryBroker(org.wso2.siddhi.core.util.transport.InMemoryBroker) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 8 with Source

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

the class InMemoryTransportTestCase method inMemoryTestCase7.

@Test
public void inMemoryTestCase7() throws InterruptedException {
    log.info("Test inMemory 7");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testTrpInMemory', topic='Foo', prop1='hi', prop2='test', fail='true', " + "   @map(type='passThrough', @attributes(symbol='trp:symbol'," + "        volume='volume',price='trp:price'))) " + "define stream FooStream (symbol string, price string, volume long); " + "define stream BarStream (symbol string, price string, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            wso2Count.incrementAndGet();
            for (Event event : events) {
                AssertJUnit.assertArrayEquals(event.getData(), new Object[] { "hi", "test", 100L });
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("Foo", new Event(System.currentTimeMillis(), new Object[] { "WSO2", "in", 100L }));
    Thread.sleep(100);
    // assert event count
    AssertJUnit.assertEquals("Number of events", 0, wso2Count.get());
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 9 with Source

use of org.wso2.siddhi.core.stream.input.source.Source 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 10 with Source

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

the class SiddhiQLBaseVisitorImpl method visitJoin_source.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Object visitJoin_source(@NotNull SiddhiQLParser.Join_sourceContext ctx) {
    // join_source
    // :io (basic_source_stream_handler)* window? (AS alias)?
    // ;
    Source source = (Source) visit(ctx.source());
    String streamAlias = null;
    if (ctx.alias() != null) {
        streamAlias = (String) visit(ctx.alias());
        activeStreams.remove(ctx.source().getText());
        activeStreams.add(streamAlias);
    }
    BasicSingleInputStream basicSingleInputStream = new BasicSingleInputStream(streamAlias, source.streamId, source.isInnerStream);
    if (ctx.basic_source_stream_handlers() != null) {
        basicSingleInputStream.addStreamHandlers((List<StreamHandler>) visit(ctx.basic_source_stream_handlers()));
    }
    if (ctx.window() != null) {
        SingleInputStream inputStream = new SingleInputStream(basicSingleInputStream, (Window) visit(ctx.window()));
        populateQueryContext(inputStream, ctx);
        return inputStream;
    } else {
        populateQueryContext(basicSingleInputStream, ctx);
        return basicSingleInputStream;
    }
}
Also used : BasicSingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) SingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.SingleInputStream) BasicSingleInputStream(org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) StreamHandler(org.wso2.siddhi.query.api.execution.query.input.handler.StreamHandler)

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