Search in sources :

Example 26 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class WindowPartitionTestCase method testWindowPartitionQuery2.

@Test
public void testWindowPartitionQuery2() throws InterruptedException {
    log.info("Window Partition test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "define stream cseEventStream (symbol string, price float,volume int);" + "partition with (symbol of cseEventStream) begin @info(name = 'query1') from cseEventStream#window" + ".lengthBatch(2)  select symbol,sum(price) as price,volume insert all events into OutStockStream ;  " + "end ";
    SiddhiAppRuntime executionRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    executionRuntime.addCallback("OutStockStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                inEventCount++;
                eventArrived = true;
                if (inEventCount == 1) {
                    AssertJUnit.assertEquals(170.0, event.getData()[1]);
                } else if (inEventCount == 2) {
                    AssertJUnit.assertEquals(1700.0, event.getData()[1]);
                }
            }
        }
    });
    InputHandler inputHandler = executionRuntime.getInputHandler("cseEventStream");
    executionRuntime.start();
    inputHandler.send(new Object[] { "IBM", 70f, 100 });
    inputHandler.send(new Object[] { "WSO2", 700f, 100 });
    inputHandler.send(new Object[] { "IBM", 100f, 100 });
    inputHandler.send(new Object[] { "IBM", 200f, 100 });
    inputHandler.send(new Object[] { "WSO2", 1000f, 100 });
    Thread.sleep(2000);
    AssertJUnit.assertEquals(2, inEventCount);
    executionRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) 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 27 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class Window method init.

/**
 * Initialize the WindowEvent table by creating {@link WindowProcessor} to handle the events.
 *
 * @param tableMap       map of {@link Table}s
 * @param eventWindowMap map of EventWindows
 * @param queryName      name of the query window belongs to.
 */
public void init(Map<String, Table> tableMap, Map<String, Window> eventWindowMap, String queryName) {
    if (this.windowProcessor != null) {
        return;
    }
    // Create and initialize MetaStreamEvent
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addInputDefinition(windowDefinition);
    metaStreamEvent.setEventType(MetaStreamEvent.EventType.WINDOW);
    metaStreamEvent.initializeAfterWindowData();
    for (Attribute attribute : windowDefinition.getAttributeList()) {
        metaStreamEvent.addOutputData(attribute);
    }
    this.streamEventPool = new StreamEventPool(metaStreamEvent, 5);
    StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, this.streamEventPool);
    OutputStream.OutputEventType outputEventType = windowDefinition.getOutputEventType();
    boolean outputExpectsExpiredEvents = outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS;
    WindowProcessor internalWindowProcessor = (WindowProcessor) SingleInputStreamParser.generateProcessor(windowDefinition.getWindow(), metaStreamEvent, new ArrayList<VariableExpressionExecutor>(), this.siddhiAppContext, tableMap, false, outputExpectsExpiredEvents, queryName);
    internalWindowProcessor.setStreamEventCloner(streamEventCloner);
    internalWindowProcessor.constructStreamEventPopulater(metaStreamEvent, 0);
    EntryValveProcessor entryValveProcessor = null;
    if (internalWindowProcessor instanceof SchedulingProcessor) {
        entryValveProcessor = new EntryValveProcessor(this.siddhiAppContext);
        Scheduler scheduler = SchedulerParser.parse(this.siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, this.siddhiAppContext);
        scheduler.init(this.lockWrapper, queryName);
        scheduler.setStreamEventPool(streamEventPool);
        ((SchedulingProcessor) internalWindowProcessor).setScheduler(scheduler);
    }
    if (entryValveProcessor != null) {
        entryValveProcessor.setToLast(internalWindowProcessor);
        this.windowProcessor = entryValveProcessor;
    } else {
        this.windowProcessor = internalWindowProcessor;
    }
    // StreamPublishProcessor must be the last in chain so that it can publish the events to StreamJunction
    this.windowProcessor.setToLast(new StreamPublishProcessor(outputEventType));
    this.internalWindowProcessor = internalWindowProcessor;
}
Also used : Attribute(org.wso2.siddhi.query.api.definition.Attribute) Scheduler(org.wso2.siddhi.core.util.Scheduler) OutputStream(org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream) ArrayList(java.util.ArrayList) SchedulingProcessor(org.wso2.siddhi.core.query.processor.SchedulingProcessor) StreamEventPool(org.wso2.siddhi.core.event.stream.StreamEventPool) StreamEventCloner(org.wso2.siddhi.core.event.stream.StreamEventCloner) EntryValveProcessor(org.wso2.siddhi.core.query.input.stream.single.EntryValveProcessor) WindowProcessor(org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 28 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class Window method add.

/**
 * Add the given ComplexEventChunk to the Window.
 *
 * @param complexEventChunk the event chunk to be added
 */
public void add(ComplexEventChunk complexEventChunk) {
    try {
        this.lockWrapper.lock();
        complexEventChunk.reset();
        // Convert all events to StreamEvent because StateEvents can be passed if directly received from a join
        ComplexEvent complexEvents = complexEventChunk.getFirst();
        StreamEvent firstEvent = streamEventPool.borrowEvent();
        eventConverter.convertComplexEvent(complexEvents, firstEvent);
        StreamEvent currentEvent = firstEvent;
        complexEvents = complexEvents.getNext();
        int numberOfEvents = 0;
        while (complexEvents != null) {
            numberOfEvents++;
            StreamEvent nextEvent = streamEventPool.borrowEvent();
            eventConverter.convertComplexEvent(complexEvents, nextEvent);
            currentEvent.setNext(nextEvent);
            currentEvent = nextEvent;
            complexEvents = complexEvents.getNext();
        }
        try {
            if (throughputTrackerInsert != null && siddhiAppContext.isStatsEnabled()) {
                throughputTrackerInsert.eventsIn(numberOfEvents);
                latencyTrackerInsert.markIn();
            }
            // Send to the window windowProcessor
            windowProcessor.process(new ComplexEventChunk<StreamEvent>(firstEvent, currentEvent, complexEventChunk.isBatch()));
        } finally {
            if (throughputTrackerInsert != null && siddhiAppContext.isStatsEnabled()) {
                latencyTrackerInsert.markOut();
            }
        }
    } finally {
        this.lockWrapper.unlock();
    }
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 29 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class LossyFrequentWindowTestCase method frequentUniqueWindowTest2.

@Test
public void frequentUniqueWindowTest2() throws InterruptedException {
    log.info("lossyFrequentWindow test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "define stream purchase (cardNo string, price float);";
    String query = "" + "@info(name = 'query1') " + "from purchase[price >= 30]#window.lossyFrequent(0.3,0.05) " + "select cardNo, price " + "insert all events into PotentialFraud ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount += inEvents.length;
            }
            if (removeEvents != null) {
                removeEventCount += removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("purchase");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "3224-3244-2432-4124", 73.36f });
    for (int i = 0; i < 25; i++) {
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 73.36f });
        inputHandler.send(new Object[] { "3234-3244-2432-4124", 78.36f });
        inputHandler.send(new Object[] { "1234-3244-2432-123", 86.36f });
        // this event will not include in to the
        inputHandler.send(new Object[] { "5768-3244-2432-5646", 48.36f });
    // window during first iteration because 1+0<5*0.25
    }
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    AssertJUnit.assertEquals("Out Event count", 1, removeEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 30 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeLengthWindowTestCase method timeLengthWindowTest1.

/*
        Time Period < Window time
        Number of Events < Window length
    */
@Test
public void timeLengthWindowTest1() throws InterruptedException {
    log.info("Testing timeLength window with no of events less than window length and time period less than " + "window time");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1') from cseEventStream#window.timeLength(4 sec,10) select symbol,price," + "volume insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    Thread.sleep(5000);
    AssertJUnit.assertEquals(4, inEventCount);
    AssertJUnit.assertEquals(4, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)161 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)130 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)130 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)118 Event (org.wso2.siddhi.core.event.Event)117 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)82 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)35 Query (org.wso2.siddhi.query.api.execution.query.Query)32 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)14 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)12 ArrayList (java.util.ArrayList)10 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)10 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)10 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)9 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)8 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)8 Window (org.wso2.siddhi.core.window.Window)7 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)6 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)6