use of org.wso2.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.
the class LengthBatchWindowTestCase method lengthBatchWindowTest2.
@Test
public void lengthBatchWindowTest2() throws InterruptedException {
log.info("Testing length batch window with no of events greater than window size");
final int length = 4;
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.lengthBatch(" + length + ") " + "select symbol,price,volume " + "insert into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
for (Event event : events) {
count++;
AssertJUnit.assertEquals("In event order", count, event.getData(2));
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700f, 1 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
inputHandler.send(new Object[] { "IBM", 700f, 3 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
inputHandler.send(new Object[] { "IBM", 700f, 5 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
Thread.sleep(500);
AssertJUnit.assertEquals("Total event count", 4, count);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.
the class LengthWindowTestCase method lengthWindowTest3.
@Test
public void lengthWindowTest3() throws InterruptedException {
log.info("Testing length window with no of events greater than window size");
final int length = 4;
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
String query = "@info(name = 'query1') from cseEventStream#window.length(" + length + ") 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) {
for (Event event : inEvents) {
if (event.isExpired()) {
removeEventCount++;
} else {
inEventCount++;
}
}
}
if (removeEvents != null) {
for (Event event : removeEvents) {
if (event.isExpired()) {
removeEventCount++;
} else {
inEventCount++;
}
}
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 700f, 1 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
inputHandler.send(new Object[] { "IBM", 700f, 3 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
inputHandler.send(new Object[] { "IBM", 700f, 5 });
inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
Thread.sleep(500);
AssertJUnit.assertEquals("In event count", 6, inEventCount);
AssertJUnit.assertEquals("Remove event count", 2, removeEventCount);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.
the class LengthWindowTestCase method lengthWindowTest4.
@Test
public void lengthWindowTest4() throws InterruptedException {
log.info("Testing length window with no of events smaller than window size");
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "" + "define stream cseEventStream (symbol string, price float, volume int, price2 double, volume2 long, " + "active bool);";
String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.length(4) " + "select " + "max(price) as maxp, min(price) as minp, sum(price) as sump, avg(price) as avgp, " + "stdDev(price) as stdp, count(price) as cp, distinctCount(price) as dcp," + "max(volume) as maxvolumep, min(volume) as minvolumep, sum(volume) as sumvolumep," + " avg(volume) as avgvolumep, " + "stdDev(volume) as stdvolumep, count(volume) as cvolumep, distinctCount(volume) as dcvolumep," + "max(price2) as maxprice2p, min(price2) as minprice2p, sum(price2) as sumprice2p," + " avg(price2) as avgprice2p, " + "stdDev(price2) as stdprice2p, count(price2) as cpprice2, distinctCount(price2) as dcprice2p," + "max(volume2) as maxvolume2p, min(volume2) as minvolume2p, sum(volume2) as sumvolume2p," + " avg(volume2) as avgvolume2p, " + "stdDev(volume2) as stdvolume2p, count(volume2) as cvolume2p, distinctCount(volume2) as dcvolume2p" + " " + "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);
inEventCount = inEventCount + inEvents.length;
if (inEventCount == 2) {
storedEvent = inEvents[0];
} else if (inEventCount == 3) {
Assert.assertEquals(inEvents[0].getData(1), storedEvent.getData(1), "2nd and 3rd message should be same");
Assert.assertEquals(inEvents[0].getData(2), storedEvent.getData(2), "2nd and 3rd message should be same");
Assert.assertEquals(inEvents[0].getData(3), storedEvent.getData(3), "2nd and 3rd message should be same");
}
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { null, null, null, null, null, null });
inputHandler.send(new Object[] { "IBM", 700F, 0, 0.0D, 5L, true });
inputHandler.send(new Object[] { null, null, null, null, null, null });
inputHandler.send(new Object[] { "IBM", 700F, 0, 0.0D, 5L, true });
inputHandler.send(new Object[] { "IBM", 700F, 0, 0.0D, 5L, true });
inputHandler.send(new Object[] { "IBM", 700F, 0, 0.0D, 5L, true });
inputHandler.send(new Object[] { "IBM", 700F, 0, 0.0D, 5L, true });
inputHandler.send(new Object[] { "IBM", 700F, 0, 0.0D, 5L, true });
Thread.sleep(500);
AssertJUnit.assertEquals(8, inEventCount);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.
the class EventTestCase method testQueryParser.
@Test
public void testQueryParser() {
StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
StreamDefinition outStreamDefinition = StreamDefinition.id("outputStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT);
Query query = new Query();
query.annotation(Annotation.annotation("info").element("name", "query1"));
query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"), Compare.Operator.NOT_EQUAL, Expression.value(50))));
query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
query.insertInto("outputStream");
Map<String, AbstractDefinition> tableDefinitionMap = new HashMap<>();
Map<String, AbstractDefinition> windowDefinitionMap = new HashMap<>();
Map<String, AbstractDefinition> aggregationDefinitionMap = new HashMap<>();
Map<String, Table> tableMap = new HashMap<String, Table>();
Map<String, Window> eventWindowMap = new HashMap<String, Window>();
Map<String, AggregationRuntime> aggregationMap = new HashMap<String, AggregationRuntime>();
Map<String, List<Source>> eventSourceMap = new HashMap<String, List<Source>>();
Map<String, List<Sink>> eventSinkMap = new HashMap<String, List<Sink>>();
Map<String, AbstractDefinition> streamDefinitionMap = new HashMap<String, AbstractDefinition>();
LockSynchronizer lockSynchronizer = new LockSynchronizer();
streamDefinitionMap.put("cseEventStream", streamDefinition);
streamDefinitionMap.put("outputStream", outStreamDefinition);
SiddhiContext siddhicontext = new SiddhiContext();
SiddhiAppContext context = new SiddhiAppContext();
context.setSiddhiContext(siddhicontext);
context.setElementIdGenerator(new ElementIdGenerator(context.getName()));
context.setSnapshotService(new SnapshotService(context));
QueryRuntime runtime = QueryParser.parse(query, context, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, aggregationMap, eventWindowMap, lockSynchronizer, "1");
AssertJUnit.assertNotNull(runtime);
AssertJUnit.assertTrue(runtime.getStreamRuntime() instanceof SingleStreamRuntime);
AssertJUnit.assertNotNull(runtime.getSelector());
AssertJUnit.assertTrue(runtime.getMetaComplexEvent() instanceof MetaStreamEvent);
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.
the class SiddhiQLBaseVisitorImpl method visitJoin_source.
/**
* {@inheritDoc}
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*
* @param ctx
*/
@Override
public Object visitJoin_source(@NotNull SiddhiQLParser.Join_sourceContext ctx) {
// join_source
// :io (basic_source_stream_handler)* window? (AS alias)?
// ;
Source source = (Source) visit(ctx.source());
String streamAlias = null;
if (ctx.alias() != null) {
streamAlias = (String) visit(ctx.alias());
activeStreams.remove(ctx.source().getText());
activeStreams.add(streamAlias);
}
BasicSingleInputStream basicSingleInputStream = new BasicSingleInputStream(streamAlias, source.streamId, source.isInnerStream);
if (ctx.basic_source_stream_handlers() != null) {
basicSingleInputStream.addStreamHandlers((List<StreamHandler>) visit(ctx.basic_source_stream_handlers()));
}
if (ctx.window() != null) {
SingleInputStream inputStream = new SingleInputStream(basicSingleInputStream, (Window) visit(ctx.window()));
populateQueryContext(inputStream, ctx);
return inputStream;
} else {
populateQueryContext(basicSingleInputStream, ctx);
return basicSingleInputStream;
}
}
Aggregations