Search in sources :

Example 36 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class ComplexEventChunkTestCase method eventChunkRemoveTest4.

@Test
public void eventChunkRemoveTest4() {
    StreamEvent streamEvent1 = new StreamEvent(0, 0, 3);
    streamEvent1.setOutputData(new Object[] { "IBM", 700L, 100L });
    StreamEvent streamEvent2 = new StreamEvent(0, 0, 3);
    streamEvent2.setOutputData(new Object[] { "WSO2", 700L, 100L });
    StreamEvent streamEvent3 = new StreamEvent(0, 0, 3);
    streamEvent3.setOutputData(new Object[] { "WSO2", 700L, 100L });
    StreamEvent streamEvent4 = new StreamEvent(0, 0, 3);
    streamEvent4.setOutputData(new Object[] { "WSO2", 700L, 100L });
    streamEvent1.setNext(streamEvent2);
    streamEvent2.setNext(streamEvent3);
    streamEvent3.setNext(streamEvent4);
    StreamEventFactory streamEventFactory = new StreamEventFactory(0, 0, 3);
    ConversionStreamEventChunk streamEventChunk = new ConversionStreamEventChunk(streamEventConverter, streamEventFactory);
    streamEventChunk.convertAndAssign(streamEvent1);
    while (streamEventChunk.hasNext()) {
        count++;
        streamEventChunk.next();
        if (count == 2 || count == 4) {
            streamEventChunk.remove();
        }
    }
    StreamEvent streamEvent = streamEventChunk.getFirst();
    AssertJUnit.assertEquals(streamEvent1, streamEvent);
    AssertJUnit.assertEquals(streamEvent3, streamEvent.getNext());
    AssertJUnit.assertNull(streamEvent.getNext().getNext());
}
Also used : ConversionStreamEventChunk(io.siddhi.core.event.stream.converter.ConversionStreamEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) Test(org.testng.annotations.Test)

Example 37 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class JunctionTestCase method oneToOneTest.

@Test
public void oneToOneTest() throws InterruptedException {
    log.info("one to one");
    StreamDefinition streamA = StreamDefinition.id("streamA").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("parallel"));
    StreamJunction streamJunctionA = new StreamJunction(streamA, executorService, 1024, null, siddhiAppContext);
    StreamJunction.Publisher streamPublisherA = streamJunctionA.constructPublisher();
    StreamDefinition streamB = StreamDefinition.id("streamB").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).annotation(Annotation.annotation("parallel"));
    StreamJunction streamJunctionB = new StreamJunction(streamB, executorService, 1024, null, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherB = streamJunctionB.constructPublisher();
    StreamCallback streamCallbackA = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                innerStreamEvent.setOutputData(streamEvent.getData());
                streamPublisherB.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            count += streamEvents.length;
            eventArrived = true;
            for (Event streamEvent : streamEvents) {
                AssertJUnit.assertTrue(streamEvent.getData()[0].equals("IBM") || (streamEvent.getData()[0].equals("WSO2")));
            }
        }
    };
    streamJunctionA.subscribe(streamCallbackA);
    streamJunctionA.startProcessing();
    streamJunctionB.subscribe(streamCallbackB);
    streamJunctionB.startProcessing();
    // Thread.sleep(100);
    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(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(2, count);
    streamJunctionA.stopProcessing();
    streamJunctionB.stopProcessing();
}
Also used : StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) StreamEvent(io.siddhi.core.event.stream.StreamEvent) Event(io.siddhi.core.event.Event) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 38 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class JunctionTestCase method multiThreadedTest1.

@Test
public void multiThreadedTest1() throws InterruptedException {
    log.info("multi threaded 1");
    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, null, 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, null, siddhiAppContext);
    final StreamJunction.Publisher streamPublisherB1 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB2 = streamJunctionB.constructPublisher();
    final StreamJunction.Publisher streamPublisherB3 = streamJunctionB.constructPublisher();
    StreamCallback streamCallbackA1 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                innerStreamEvent.setOutputData(streamEvent.getData());
                streamPublisherB1.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackA2 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                innerStreamEvent.setOutputData(streamEvent.getData());
                streamPublisherB2.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackA3 = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                StreamEvent innerStreamEvent = new StreamEvent(2, 2, 2);
                innerStreamEvent.setTimestamp(streamEvent.getTimestamp());
                innerStreamEvent.setOutputData(streamEvent.getData());
                streamPublisherB3.send(innerStreamEvent);
            }
        }
    };
    StreamCallback streamCallbackB = new StreamCallback() {

        @Override
        public void receive(Event[] streamEvents) {
            for (Event streamEvent : streamEvents) {
                count++;
                eventArrived = true;
                AssertJUnit.assertTrue(streamEvent.getData()[0].equals("IBM") || (streamEvent.getData()[0].equals("WSO2")));
            }
        }
    };
    streamJunctionA.subscribe(streamCallbackA1);
    streamJunctionA.subscribe(streamCallbackA2);
    streamJunctionA.subscribe(streamCallbackA3);
    streamJunctionA.startProcessing();
    streamJunctionB.subscribe(streamCallbackB);
    streamJunctionB.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(100);
    AssertJUnit.assertTrue(eventArrived);
    AssertJUnit.assertEquals(6, count);
    streamJunctionA.stopProcessing();
    streamJunctionB.stopProcessing();
}
Also used : StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) StreamEvent(io.siddhi.core.event.stream.StreamEvent) Event(io.siddhi.core.event.Event) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 39 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class EventTestCase method testEventCreation.

@Test
public void testEventCreation() {
    EventExchangeHolderFactory eventExchangeHolderFactory = new EventExchangeHolderFactory(2);
    AssertJUnit.assertEquals(2, eventExchangeHolderFactory.newInstance().getEvent().getData().length);
    StreamEventFactory streamEventFactory = new StreamEventFactory(2, 3, 4);
    StreamEvent streamEvent = streamEventFactory.newInstance();
    AssertJUnit.assertEquals(2, streamEvent.getBeforeWindowData().length);
    AssertJUnit.assertEquals(3, streamEvent.getOnAfterWindowData().length);
    AssertJUnit.assertEquals(4, streamEvent.getOutputData().length);
}
Also used : EventExchangeHolderFactory(io.siddhi.core.util.event.handler.EventExchangeHolderFactory) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Test(org.testng.annotations.Test)

Example 40 with StreamEvent

use of io.siddhi.core.event.stream.StreamEvent in project siddhi by wso2.

the class IncrementalDataAggregator method aggregateInMemoryData.

public ComplexEventChunk<StreamEvent> aggregateInMemoryData(Map<TimePeriod.Duration, Executor> incrementalExecutorMap) {
    int startIndex = incrementalDurations.indexOf(durationToAggregate);
    Set<String> groupByKeys = new HashSet<>();
    for (int k = startIndex; k >= 0; k--) {
        TimePeriod.Duration duration = incrementalDurations.get(k);
        Executor incrementalExecutor = incrementalExecutorMap.get(duration);
        if (incrementalExecutor instanceof IncrementalExecutor) {
            BaseIncrementalValueStore aBaseIncrementalValueStore = ((IncrementalExecutor) incrementalExecutor).getBaseIncrementalValueStore();
            Map<String, StreamEvent> groupedByEvents = aBaseIncrementalValueStore.getGroupedByEvents();
            for (Map.Entry<String, StreamEvent> eventEntry : groupedByEvents.entrySet()) {
                long startTimeOfAggregates = IncrementalTimeConverterUtil.getStartTimeOfAggregates(eventEntry.getValue().getTimestamp(), durationToAggregate, timeZone);
                String groupByKey = eventEntry.getKey() + "-" + startTimeOfAggregates;
                synchronized (this) {
                    groupByKeys.add(groupByKey);
                    SiddhiAppContext.startGroupByFlow(groupByKey);
                    ValueState state = (ValueState) valueStateHolder.getState();
                    try {
                        boolean shouldUpdate = true;
                        if (shouldUpdateTimestamp != null) {
                            shouldUpdate = shouldUpdate(shouldUpdateTimestamp.execute(eventEntry.getValue()), state);
                        } else {
                            state.lastTimestamp = oldestEventTimestamp;
                        }
                        // keeping timestamp value location as null
                        for (int i = 0; i < baseExecutorsForFind.size(); i++) {
                            ExpressionExecutor expressionExecutor = baseExecutorsForFind.get(i);
                            if (shouldUpdate) {
                                state.setValue(expressionExecutor.execute(eventEntry.getValue()), i + 1);
                            } else if (!(expressionExecutor instanceof VariableExpressionExecutor)) {
                                state.setValue(expressionExecutor.execute(eventEntry.getValue()), i + 1);
                            }
                        }
                    } finally {
                        valueStateHolder.returnState(state);
                        SiddhiAppContext.stopGroupByFlow();
                    }
                }
            }
        }
    }
    // clean all executors
    for (String groupByKey : groupByKeys) {
        SiddhiAppContext.startGroupByFlow(groupByKey);
        try {
            for (ExpressionExecutor expressionExecutor : baseExecutorsForFind) {
                expressionExecutor.execute(resetEvent);
            }
        } finally {
            SiddhiAppContext.stopGroupByFlow();
        }
    }
    return getProcessedEventChunk();
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) TimePeriod(io.siddhi.query.api.aggregation.TimePeriod) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

StreamEvent (io.siddhi.core.event.stream.StreamEvent)203 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)92 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)74 StateEvent (io.siddhi.core.event.state.StateEvent)52 ComplexEvent (io.siddhi.core.event.ComplexEvent)33 ArrayList (java.util.ArrayList)27 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)26 Map (java.util.Map)24 Event (io.siddhi.core.event.Event)23 Test (org.testng.annotations.Test)23 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)20 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)18 Attribute (io.siddhi.query.api.definition.Attribute)17 HashMap (java.util.HashMap)17 Operation (io.siddhi.core.event.stream.Operation)12 LinkedList (java.util.LinkedList)12 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)11 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)10 HashSet (java.util.HashSet)10 StreamEventConverter (io.siddhi.core.event.stream.converter.StreamEventConverter)8