Search in sources :

Example 66 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class DefineStreamTestCase method test2.

@Test
public void test2() throws SiddhiParserException {
    StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition("define stream `define` ( `string` " + "string, price int, volume float );");
    AssertJUnit.assertEquals(StreamDefinition.id("define").attribute("string", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).attribute("volume", Attribute.Type.FLOAT).toString(), streamDefinition.toString());
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Test(org.testng.annotations.Test)

Example 67 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class SiddhiCompiler method parseStreamDefinition.

public static StreamDefinition parseStreamDefinition(String source) {
    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.definition_stream_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (StreamDefinition) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) SiddhiQLBaseVisitorImpl(org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 68 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class AbstractStreamProcessor method initProcessor.

public AbstractDefinition initProcessor(AbstractDefinition inputDefinition, ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext, boolean outputExpectsExpiredEvents, String queryName, SiddhiElement siddhiElement) {
    this.configReader = configReader;
    this.outputExpectsExpiredEvents = outputExpectsExpiredEvents;
    try {
        this.inputDefinition = inputDefinition;
        this.attributeExpressionExecutors = attributeExpressionExecutors;
        this.siddhiAppContext = siddhiAppContext;
        this.attributeExpressionLength = attributeExpressionExecutors.length;
        this.queryName = queryName;
        if (elementId == null) {
            elementId = "AbstractStreamProcessor-" + siddhiAppContext.getElementIdGenerator().createNewId();
        }
        siddhiAppContext.getSnapshotService().addSnapshotable(queryName, this);
        this.additionalAttributes = init(inputDefinition, attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents);
        siddhiAppContext.addEternalReferencedHolder(this);
        StreamDefinition outputDefinition = StreamDefinition.id(inputDefinition.getId());
        outputDefinition.setQueryContextStartIndex(siddhiElement.getQueryContextStartIndex());
        outputDefinition.setQueryContextEndIndex(siddhiElement.getQueryContextEndIndex());
        for (Attribute attribute : inputDefinition.getAttributeList()) {
            outputDefinition.attribute(attribute.getName(), attribute.getType());
        }
        for (Attribute attribute : additionalAttributes) {
            outputDefinition.attribute(attribute.getName(), attribute.getType());
        }
        return outputDefinition;
    } catch (Throwable t) {
        throw new SiddhiAppCreationException(t);
    }
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)

Example 69 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class SelectorParser method getAttributeProcessors.

/**
 * Method to construct AttributeProcessor list for the selector.
 *
 * @param selector                    Selector
 * @param id                          stream id
 * @param siddhiAppContext            siddhi app context
 * @param metaComplexEvent            meta ComplexEvent
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors list of VariableExpressionExecutors
 * @param outputStream
 * @return list of AttributeProcessors
 */
private static List<AttributeProcessor> getAttributeProcessors(Selector selector, String id, SiddhiAppContext siddhiAppContext, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, OutputStream outputStream, String queryName, int metaPosition) {
    List<AttributeProcessor> attributeProcessorList = new ArrayList<>();
    StreamDefinition outputDefinition = StreamDefinition.id(id);
    outputDefinition.setQueryContextStartIndex(outputStream.getQueryContextStartIndex());
    outputDefinition.setQueryContextEndIndex(outputStream.getQueryContextEndIndex());
    List<OutputAttribute> outputAttributes = selector.getSelectionList();
    if (selector.getSelectionList().size() == 0) {
        if (metaComplexEvent instanceof MetaStreamEvent) {
            List<Attribute> attributeList = ((MetaStreamEvent) metaComplexEvent).getLastInputDefinition().getAttributeList();
            for (Attribute attribute : attributeList) {
                outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
            }
        } else {
            int position = 0;
            for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                if (metaPosition == SiddhiConstants.UNKNOWN_STATE || metaPosition == position) {
                    List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
                    for (Attribute attribute : attributeList) {
                        OutputAttribute outputAttribute = new OutputAttribute(new Variable(attribute.getName()));
                        if (!outputAttributes.contains(outputAttribute)) {
                            outputAttributes.add(outputAttribute);
                        } else {
                            List<AbstractDefinition> definitions = new ArrayList<>();
                            for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                                definitions.add(aMetaStreamEvent.getLastInputDefinition());
                            }
                            throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, outputStream.getQueryContextStartIndex(), outputStream.getQueryContextEndIndex());
                        }
                    }
                }
                ++position;
            }
        }
    }
    int i = 0;
    for (OutputAttribute outputAttribute : outputAttributes) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(outputAttribute.getExpression(), metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, !(selector.getGroupByList().isEmpty()), 0, queryName);
        if (expressionExecutor instanceof VariableExpressionExecutor) {
            // for variables we will directly put
            // value at conversion stage
            VariableExpressionExecutor executor = ((VariableExpressionExecutor) expressionExecutor);
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(new MetaStateEventAttribute(executor.getAttribute(), executor.getPosition()));
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(executor.getAttribute());
            }
            outputDefinition.attribute(outputAttribute.getRename(), ((VariableExpressionExecutor) expressionExecutor).getAttribute().getType());
        } else {
            // To maintain output variable positions
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            }
            AttributeProcessor attributeProcessor = new AttributeProcessor(expressionExecutor);
            attributeProcessor.setOutputPosition(i);
            attributeProcessorList.add(attributeProcessor);
            outputDefinition.attribute(outputAttribute.getRename(), attributeProcessor.getOutputType());
        }
        i++;
    }
    metaComplexEvent.setOutputDefinition(outputDefinition);
    return attributeProcessorList;
}
Also used : Variable(org.ballerinalang.siddhi.query.api.expression.Variable) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) ConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute) MetaStateEventAttribute(org.ballerinalang.siddhi.core.event.state.MetaStateEventAttribute) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute) DuplicateAttributeException(org.ballerinalang.siddhi.query.api.exception.DuplicateAttributeException) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) MetaStateEventAttribute(org.ballerinalang.siddhi.core.event.state.MetaStateEventAttribute) AttributeProcessor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.AttributeProcessor) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Example 70 with StreamDefinition

use of org.ballerinalang.siddhi.query.api.definition.StreamDefinition in project ballerina by ballerina-lang.

the class FilterTestCase1 method testFilterQuery54.

@Test
public void testFilterQuery54() throws InterruptedException {
    log.info("Filter test54");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"), Compare.Operator.EQUAL, Expression.value(50d))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("outputStream");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count = count + inEvents.length;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 50f, 60d });
    inputHandler.send(new Object[] { "WSO2", 70f, 40d });
    inputHandler.send(new Object[] { "WSO2", 44f, 200d });
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) 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) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)110 Test (org.testng.annotations.Test)98 Event (org.ballerinalang.siddhi.core.event.Event)83 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)82 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)80 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)80 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)80 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)77 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)73 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)9 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)8 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)8 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)8 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)5 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)5 ArrayList (java.util.ArrayList)3 SelectiveStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.SelectiveStreamEventConverter)3 SimpleStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.SimpleStreamEventConverter)3 StreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter)3 ZeroStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter)3