Search in sources :

Example 1 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.query.api.execution.partition.Partition) SiddhiQLParser(org.ballerinalang.siddhi.query.compiler.SiddhiQLParser)

Example 2 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

the class DefinePartitionTestCase method test1.

@Test
public void test1() {
    Partition partition = SiddhiCompiler.parsePartition("partition with (200>volume as 'LessValue' or 200<=volume" + " as 'HighValue' of cseEventStream) begin from cseEventStream select sum(volume) as sumvolume insert " + "into StockStream ;  end ");
    AssertJUnit.assertEquals(Partition.partition().with("cseEventStream", Partition.range("LessValue", Expression.compare(Expression.value(200), Compare.Operator.GREATER_THAN, Expression.variable("volume"))), Partition.range("HighValue", Expression.compare(Expression.value(200), Compare.Operator.LESS_THAN_EQUAL, Expression.variable("volume")))).toString().split("queryList")[0], partition.toString().split("queryList")[0]);
}
Also used : Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) Test(org.testng.annotations.Test)

Example 3 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

the class DefinePartitionTestCase method test2.

@Test
public void test2() {
    Partition partition = SiddhiCompiler.parsePartition("partition with (symbol of cseEventStream) begin from " + "cseEventStream select sum(volume) as sumvolume insert into StockStream ;  end ");
    AssertJUnit.assertEquals(Partition.partition().with("cseEventStream", Expression.variable("symbol")).toString().split("queryList")[0], partition.toString().split("queryList")[0]);
}
Also used : Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) Test(org.testng.annotations.Test)

Example 4 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

the class SiddhiCompiler method parsePartition.

public static Partition parsePartition(String source) throws SiddhiParserException {
    ANTLRInputStream input = new ANTLRInputStream(source);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.partition_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (Partition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) SiddhiQLBaseVisitorImpl(org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 5 with Partition

use of org.ballerinalang.siddhi.query.api.execution.partition.Partition in project ballerina by ballerina-lang.

the class PartitionTestCase1 method testPartitionQuery39.

@Test
public void testPartitionQuery39() throws InterruptedException {
    log.info("Partition test");
    SiddhiApp siddhiApp = SiddhiApp.siddhiApp("Test").defineStream(StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT));
    Query query = Query.query();
    query.from(InputStream.stream("streamA"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("StockQuote");
    Partition partition = Partition.partition().annotation(Annotation.annotation("info").element("name", "partitionA")).with("streamA", Expression.variable("symbol")).addQuery(query);
    siddhiApp.addPartition(partition);
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    StreamCallback streamCallback = new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            AssertJUnit.assertTrue("IBM".equals(events[0].getData(0)) || "WSO2".equals(events[0].getData(0)));
            count.addAndGet(events.length);
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("StockQuote", streamCallback);
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("streamA");
    siddhiAppRuntime.start();
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "IBM", 700 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
    inputHandler.send(new Event(System.currentTimeMillis(), new Object[] { "WSO2", 60 }));
    Thread.sleep(1000);
    AssertJUnit.assertEquals(3, count.get());
    siddhiAppRuntime.shutdown();
}
Also used : Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

Partition (org.ballerinalang.siddhi.query.api.execution.partition.Partition)13 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)9 Test (org.testng.annotations.Test)9 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)7 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)6 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)6 Event (org.ballerinalang.siddhi.core.event.Event)6 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)6 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)6 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)4 ExecutionElement (org.ballerinalang.siddhi.query.api.execution.ExecutionElement)2 SiddhiQLParser (org.ballerinalang.siddhi.query.compiler.SiddhiQLParser)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 ArrayList (java.util.ArrayList)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 SiddhiAppContext (org.ballerinalang.siddhi.core.config.SiddhiAppContext)1 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)1 PartitionRuntime (org.ballerinalang.siddhi.core.partition.PartitionRuntime)1