use of org.wso2.siddhi.query.api.execution.query.input.handler.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();
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.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;
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.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();
}
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.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();
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.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();
}
Aggregations