Search in sources :

Example 1 with StreamEventConverter

use of org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter in project ballerina by ballerina-lang.

the class OutputParser method constructOutputCallback.

public static OutputCallback constructOutputCallback(final OutputStream outStream, StreamDefinition outputStreamDefinition, Map<String, Table> tableMap, Map<String, Window> eventWindowMap, SiddhiAppContext siddhiAppContext, boolean convertToStreamEvent, String queryName) {
    String id = outStream.getId();
    Table table = null;
    Window window = null;
    if (id != null) {
        table = tableMap.get(id);
        window = eventWindowMap.get(id);
    }
    StreamEventPool streamEventPool = null;
    StreamEventConverter streamEventConverter = null;
    MetaStreamEvent tableMetaStreamEvent = null;
    if (table != null) {
        tableMetaStreamEvent = new MetaStreamEvent();
        tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
        TableDefinition matchingTableDefinition = TableDefinition.id("");
        for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
            tableMetaStreamEvent.addOutputData(attribute);
            matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
        }
        matchingTableDefinition.setQueryContextStartIndex(outStream.getQueryContextStartIndex());
        matchingTableDefinition.setQueryContextEndIndex(outStream.getQueryContextEndIndex());
        tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
        streamEventPool = new StreamEventPool(tableMetaStreamEvent, 10);
        streamEventConverter = new ZeroStreamEventConverter();
    }
    // Construct CallBack
    if (outStream instanceof InsertIntoStream) {
        if (window != null) {
            return new InsertIntoWindowCallback(window, outputStreamDefinition, queryName);
        } else if (table != null) {
            DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
            return new InsertIntoTableCallback(table, outputStreamDefinition, convertToStreamEvent, streamEventPool, streamEventConverter, queryName);
        } else {
            return new InsertIntoStreamCallback(outputStreamDefinition, queryName);
        }
    } else if (outStream instanceof DeleteStream || outStream instanceof UpdateStream || outStream instanceof UpdateOrInsertStream) {
        if (table != null) {
            if (outStream instanceof UpdateStream) {
                if (((UpdateStream) outStream).getUpdateSet() == null) {
                    TableDefinition tableDefinition = table.getTableDefinition();
                    for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
                        if (!tableDefinition.getAttributeList().contains(attribute)) {
                            throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
                        }
                    }
                }
            }
            if (outStream instanceof UpdateOrInsertStream) {
                TableDefinition tableDefinition = table.getTableDefinition();
                for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
                    if (!tableDefinition.getAttributeList().contains(attribute)) {
                        throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
                    }
                }
            }
            if (outStream instanceof DeleteStream) {
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((DeleteStream) outStream).getOnDeleteExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
                    return new DeleteTableCallback(table, compiledCondition, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create delete for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
                }
            } else if (outStream instanceof UpdateStream) {
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((UpdateStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    UpdateSet updateSet = ((UpdateStream) outStream).getUpdateSet();
                    if (updateSet == null) {
                        updateSet = new UpdateSet();
                        for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
                            updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
                        }
                    }
                    CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
                    return new UpdateTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create update for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext);
                }
            } else {
                DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((UpdateOrInsertStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    UpdateSet updateSet = ((UpdateOrInsertStream) outStream).getUpdateSet();
                    if (updateSet == null) {
                        updateSet = new UpdateSet();
                        for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
                            updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
                        }
                    }
                    CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
                    return new UpdateOrInsertTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create update or insert into for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext);
                }
            }
        } else {
            throw new SiddhiAppCreationException("Event table with id :" + id + " does not exist", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
        }
    } else {
        throw new SiddhiAppCreationException(outStream.getClass().getName() + " not supported", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
    }
}
Also used : Variable(org.ballerinalang.siddhi.query.api.expression.Variable) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) InsertIntoWindowCallback(org.ballerinalang.siddhi.core.query.output.callback.InsertIntoWindowCallback) UpdateStream(org.ballerinalang.siddhi.query.api.execution.query.output.stream.UpdateStream) ZeroStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter) UpdateTableCallback(org.ballerinalang.siddhi.core.query.output.callback.UpdateTableCallback) DeleteStream(org.ballerinalang.siddhi.query.api.execution.query.output.stream.DeleteStream) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool) UpdateOrInsertTableCallback(org.ballerinalang.siddhi.core.query.output.callback.UpdateOrInsertTableCallback) TableDefinition(org.ballerinalang.siddhi.query.api.definition.TableDefinition) InsertIntoTableCallback(org.ballerinalang.siddhi.core.query.output.callback.InsertIntoTableCallback) Window(org.ballerinalang.siddhi.core.window.Window) Table(org.ballerinalang.siddhi.core.table.Table) StateEventPool(org.ballerinalang.siddhi.core.event.state.StateEventPool) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) StreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter) ZeroStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter) InsertIntoStream(org.ballerinalang.siddhi.query.api.execution.query.output.stream.InsertIntoStream) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) InsertIntoStreamCallback(org.ballerinalang.siddhi.core.query.output.callback.InsertIntoStreamCallback) CompiledUpdateSet(org.ballerinalang.siddhi.core.table.CompiledUpdateSet) UpdateOrInsertStream(org.ballerinalang.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) CompiledCondition(org.ballerinalang.siddhi.core.util.collection.operator.CompiledCondition) MatchingMetaInfoHolder(org.ballerinalang.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) DeleteTableCallback(org.ballerinalang.siddhi.core.query.output.callback.DeleteTableCallback) UpdateSet(org.ballerinalang.siddhi.query.api.execution.query.output.stream.UpdateSet) CompiledUpdateSet(org.ballerinalang.siddhi.core.table.CompiledUpdateSet) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Example 2 with StreamEventConverter

use of org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter in project ballerina by ballerina-lang.

the class Scheduler method setStreamEventPool.

public void setStreamEventPool(StreamEventPool streamEventPool) {
    this.streamEventPool = streamEventPool;
    streamEventChunk = new ConversionStreamEventChunk((StreamEventConverter) null, streamEventPool);
}
Also used : ConversionStreamEventChunk(org.ballerinalang.siddhi.core.event.stream.converter.ConversionStreamEventChunk) StreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter)

Example 3 with StreamEventConverter

use of org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter in project ballerina by ballerina-lang.

the class MultiProcessStreamReceiver method receive.

@Override
public void receive(ComplexEvent complexEvent) {
    ComplexEvent aComplexEvent = complexEvent;
    while (aComplexEvent != null) {
        synchronized (this) {
            stabilizeStates();
            for (int anEventSequence : eventSequence) {
                StreamEventConverter aStreamEventConverter = streamEventConverters[anEventSequence];
                StreamEventPool aStreamEventPool = streamEventPools[anEventSequence];
                StreamEvent borrowedEvent = aStreamEventPool.borrowEvent();
                aStreamEventConverter.convertComplexEvent(aComplexEvent, borrowedEvent);
                process(anEventSequence, borrowedEvent);
            }
        }
        aComplexEvent = aComplexEvent.getNext();
    }
}
Also used : ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) StreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool)

Example 4 with StreamEventConverter

use of org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter in project ballerina by ballerina-lang.

the class MultiProcessStreamReceiver method receive.

@Override
public void receive(Event event) {
    synchronized (this) {
        stabilizeStates();
        for (int anEventSequence : eventSequence) {
            StreamEventConverter aStreamEventConverter = streamEventConverters[anEventSequence];
            StreamEventPool aStreamEventPool = streamEventPools[anEventSequence];
            StreamEvent borrowedEvent = aStreamEventPool.borrowEvent();
            aStreamEventConverter.convertEvent(event, borrowedEvent);
            process(anEventSequence, borrowedEvent);
        }
    }
}
Also used : StreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool)

Example 5 with StreamEventConverter

use of org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter in project ballerina by ballerina-lang.

the class EventTestCase method testStreamEventConverter.

@Test
public void testStreamEventConverter() {
    Attribute price = new Attribute("price", Attribute.Type.DOUBLE);
    Attribute volume = new Attribute("volume", Attribute.Type.INT);
    Attribute symbol = new Attribute("symbol", Attribute.Type.STRING);
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addData(volume);
    metaStreamEvent.initializeAfterWindowData();
    metaStreamEvent.addData(price);
    metaStreamEvent.addOutputData(symbol);
    // complex attribute
    metaStreamEvent.addOutputData(null);
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.INT);
    Event event = new Event(System.currentTimeMillis(), new Object[] { "WSO2", 200, 50 });
    metaStreamEvent.addInputDefinition(streamDefinition);
    StreamEventConverter converter = StreamEventConverterFactory.constructEventConverter(metaStreamEvent);
    StreamEventPool eventPool = new StreamEventPool(metaStreamEvent, 5);
    StreamEvent borrowedEvent = eventPool.borrowEvent();
    converter.convertEvent(event, borrowedEvent);
    AssertJUnit.assertTrue(converter instanceof SelectiveStreamEventConverter);
    // volume
    AssertJUnit.assertEquals(1, borrowedEvent.getBeforeWindowData().length);
    // price
    AssertJUnit.assertEquals(1, borrowedEvent.getOnAfterWindowData().length);
    // symbol and avgPrice
    AssertJUnit.assertEquals(2, borrowedEvent.getOutputData().length);
    AssertJUnit.assertEquals(50, borrowedEvent.getBeforeWindowData()[0]);
    AssertJUnit.assertEquals(200, borrowedEvent.getOnAfterWindowData()[0]);
    AssertJUnit.assertEquals("WSO2", borrowedEvent.getOutputData()[0]);
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) SimpleStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.SimpleStreamEventConverter) StreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter) ZeroStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter) SelectiveStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.SelectiveStreamEventConverter) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEventPool(org.ballerinalang.siddhi.core.event.stream.StreamEventPool) Event(org.ballerinalang.siddhi.core.event.Event) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) SelectiveStreamEventConverter(org.ballerinalang.siddhi.core.event.stream.converter.SelectiveStreamEventConverter) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) Test(org.testng.annotations.Test)

Aggregations

StreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.StreamEventConverter)10 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)9 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)9 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)8 Event (org.ballerinalang.siddhi.core.event.Event)5 ZeroStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter)4 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)4 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)3 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)3 SelectiveStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.SelectiveStreamEventConverter)3 SimpleStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.SimpleStreamEventConverter)3 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)3 Test (org.testng.annotations.Test)3 StateEventPool (org.ballerinalang.siddhi.core.event.state.StateEventPool)1 ConversionStreamEventChunk (org.ballerinalang.siddhi.core.event.stream.converter.ConversionStreamEventChunk)1 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)1 DeleteTableCallback (org.ballerinalang.siddhi.core.query.output.callback.DeleteTableCallback)1 InsertIntoStreamCallback (org.ballerinalang.siddhi.core.query.output.callback.InsertIntoStreamCallback)1 InsertIntoTableCallback (org.ballerinalang.siddhi.core.query.output.callback.InsertIntoTableCallback)1 InsertIntoWindowCallback (org.ballerinalang.siddhi.core.query.output.callback.InsertIntoWindowCallback)1