use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testJoinWindowWithWindow.
@Test
public void testJoinWindowWithWindow() throws InterruptedException {
log.info("Test join window with another window");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream TempStream(deviceID long, roomNo int, temp double); " + "define stream RegulatorStream(deviceID long, roomNo int, isOn bool); " + "define window TempWindow(deviceID long, roomNo int, temp double) time(1 min); " + "define window RegulatorWindow(deviceID long, roomNo int, isOn bool) length(1); ";
String query = "" + "@info(name = 'query1') " + "from TempStream[temp > 30.0] " + "insert into TempWindow; " + "" + "@info(name = 'query2') " + "from RegulatorStream[isOn == false] " + "insert into RegulatorWindow; " + "" + "@info(name = 'query3') " + "from TempWindow " + "join RegulatorWindow " + "on TempWindow.roomNo == RegulatorWindow.roomNo " + "select TempWindow.roomNo, RegulatorWindow.deviceID, 'start' as action " + "insert into RegulatorActionStream;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("RegulatorActionStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
inEventCount++;
}
});
InputHandler tempStream = siddhiAppRuntime.getInputHandler("TempStream");
InputHandler regulatorStream = siddhiAppRuntime.getInputHandler("RegulatorStream");
siddhiAppRuntime.start();
tempStream.send(new Object[] { 100L, 1, 20.0 });
tempStream.send(new Object[] { 100L, 2, 25.0 });
tempStream.send(new Object[] { 100L, 3, 30.0 });
tempStream.send(new Object[] { 100L, 4, 35.0 });
tempStream.send(new Object[] { 100L, 5, 40.0 });
regulatorStream.send(new Object[] { 100L, 1, false });
regulatorStream.send(new Object[] { 100L, 2, false });
regulatorStream.send(new Object[] { 100L, 3, false });
regulatorStream.send(new Object[] { 100L, 4, false });
regulatorStream.send(new Object[] { 100L, 5, false });
Thread.sleep(500);
assertEquals("Number of success events", 2, inEventCount);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testMultipleStreamsToWindow.
@Test
public void testMultipleStreamsToWindow() throws InterruptedException {
log.info("Test sending events from multiple streams into a single window");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream Stream1 (symbol string, price float, volume long); " + "define stream Stream2 (symbol string, price float, volume long); " + "define stream Stream3 (symbol string, price float, volume long); " + "define stream Stream4 (symbol string, price float, volume long); " + "define stream Stream5 (symbol string, price float, volume long); " + "define stream Stream6 (symbol string, price float, volume long); " + "define window StockWindow (symbol string, price float, volume long) lengthBatch(5); ";
String query = "" + "@info(name = 'query0') " + "from Stream1 " + "insert into StockWindow; " + "from Stream2 " + "insert into StockWindow; " + "from Stream3 " + "insert into StockWindow; " + "from Stream4 " + "insert into StockWindow; " + "from Stream5 " + "insert into StockWindow; " + "from Stream6 " + "insert into StockWindow; " + "" + "@info(name = 'query1') " + "from StockWindow " + "select symbol, sum(price) as totalPrice, sum(volume) as volumes " + "insert into OutputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
assertEquals("Invalid number of output events", 1, events.length);
assertArrayEquals(new Object[] { "WSO2", 150.0, 5L }, events[0].getData());
}
});
siddhiAppRuntime.start();
for (int i = 1; i <= 6; i++) {
siddhiAppRuntime.getInputHandler("Stream" + i).send(new Object[] { "WSO2", (i * 10.0f), 1L });
}
Thread.sleep(500);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testMultipleStreamsFromWindow.
@Test
public void testMultipleStreamsFromWindow() throws InterruptedException {
log.info("Test joining a single window by multiple streams");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream StockInputStream (symbol string, price float, volume long); " + "define stream SummaryOfCompanyTriggerStream (symbol string); " + "define stream VolumeGreaterThanTriggerStream (volume long); " + "define window StockWindow (symbol string, price float, volume long) lengthBatch(10); ";
String query = "" + "@info(name = 'query0') " + "from StockInputStream " + "insert into StockWindow; " + "@info(name = 'query1') " + "from StockWindow join SummaryOfCompanyTriggerStream#window.length(1) " + "on StockWindow.symbol == SummaryOfCompanyTriggerStream.symbol " + "select StockWindow.symbol, sum(StockWindow.price) as totalPrice, sum(StockWindow.volume) as volumes " + "insert into SummaryOfCompanyOutputStream; " + "" + "@info(name = 'query2') " + "from StockWindow join VolumeGreaterThanTriggerStream#window.length(1) " + "on StockWindow.volume > VolumeGreaterThanTriggerStream.volume " + "select StockWindow.symbol, StockWindow.price, StockWindow.volume as volume " + "insert into VolumeGreaterThanOutputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("SummaryOfCompanyOutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
Object[] data = events[0].getData();
if ("WSO2".equals(data[0])) {
assertArrayEquals(new Object[] { "WSO2", 430.0, 57L }, data);
} else if ("IBM".equals(data[0])) {
assertArrayEquals(new Object[] { "IBM", 222.0, 16L }, data);
}
}
});
siddhiAppRuntime.addCallback("VolumeGreaterThanOutputStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
assertEquals("Error in number of events with volume > 6", 6, events.length);
}
});
siddhiAppRuntime.start();
InputHandler stockInputStreamInputHandler = siddhiAppRuntime.getInputHandler("StockInputStream");
InputHandler summaryOfCompanyTriggerStreamInputHandler = siddhiAppRuntime.getInputHandler("SummaryOfCompanyTriggerStream");
InputHandler volumeGreaterThanTriggerStreamInputHandler = siddhiAppRuntime.getInputHandler("VolumeGreaterThanTriggerStream");
// 10 inputs
stockInputStreamInputHandler.send(new Object[] { "WSO2", 84.0f, 20L });
stockInputStreamInputHandler.send(new Object[] { "IBM", 90.0f, 1L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 55.0f, 5L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 77.0f, 10L });
stockInputStreamInputHandler.send(new Object[] { "IBM", 46.0f, 5L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 36.0f, 2L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 56.0f, 6L });
stockInputStreamInputHandler.send(new Object[] { "IBM", 86.0f, 10L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 26.0f, 6L });
stockInputStreamInputHandler.send(new Object[] { "WSO2", 96.0f, 8L });
// out of 10
stockInputStreamInputHandler.send(new Object[] { "WSO2", 56.0f, 10L });
stockInputStreamInputHandler.send(new Object[] { "IBM", 36.0f, 15L });
Thread.sleep(100);
summaryOfCompanyTriggerStreamInputHandler.send(new Object[] { "WSO2" });
summaryOfCompanyTriggerStreamInputHandler.send(new Object[] { "IBM" });
volumeGreaterThanTriggerStreamInputHandler.send(new Object[] { 5L });
Thread.sleep(500);
siddhiAppRuntime.shutdown();
}
use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testJoinWindowWithSameWindow.
/**
* Behaviour of traditional Window and Window are different in join with themselves.
* Traditional Windows joins always maintain two windows and the event is passed to them one after
* the other. Therefore, when left window receives a current event right window will bbe empty and
* if an event is expired from left window it can join with right window since it will not expire at the same
* time. Since Window maintains only one instance, when an event arrives to the window it will
* be compared to the internal event chunk, where the current event will match with itself.
* When an event expires, it will be immediately removed from the internal event chunk so that,
* when joined with the window, the expired event will not match with itself.
*
* @throws InterruptedException throw exception if interrupted the input handler sender.
*/
@Test
public void testJoinWindowWithSameWindow() throws InterruptedException {
log.info("Test join window with the same window");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseEventWindow (symbol string, price float, volume int) length(2); ";
String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into " + "cseEventWindow; " + "" + "@info(name = 'query1') " + "from cseEventWindow as a " + "join cseEventWindow as b " + "on a.symbol== b.symbol " + "select a.symbol as symbol, a.price as priceA, b.price as priceB " + "insert all events into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
try {
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 cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
// Match with itself
cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
// Match with itself
cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
// When the next event enters, the expired {"IBM", 75.6f, 100} will come out and joined
// with the window which contains [{"WSO2", 57.6f, 100}, {"IBM", 59.6f, 100}] and latter the
// current event {"IBM", 59.6f, 100} will match with the window.
cseEventStreamHandler.send(new Object[] { "IBM", 59.6f, 100 });
Thread.sleep(1000);
AssertJUnit.assertEquals(3, inEventCount);
AssertJUnit.assertEquals(1, removeEventCount);
AssertJUnit.assertTrue(eventArrived);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of org.ballerinalang.siddhi.core.SiddhiManager in project ballerina by ballerina-lang.
the class CustomJoinWindowTestCase method testJoinWindowWithTable.
@Test
public void testJoinWindowWithTable() throws InterruptedException {
log.info("Test join window with table");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string); " + "define window CheckStockWindow(symbol string) length(1) output all events; " + "define table StockTable (symbol string, price float, volume long); ";
String query = "" + "@info(name = 'query0') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query1') " + "from CheckStockStream " + "insert into CheckStockWindow ;" + "" + "@info(name = 'query2') " + "from CheckStockWindow join StockTable " + " on CheckStockWindow.symbol==StockTable.symbol " + "select CheckStockWindow.symbol as checkSymbol, StockTable.symbol as symbol, StockTable.volume as " + "volume " + "insert into OutputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("query2", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
if (inEvents != null) {
for (Event event : inEvents) {
inEventCount++;
switch(inEventCount) {
case 1:
assertArrayEquals(new Object[] { "WSO2", "WSO2", 100L }, event.getData());
break;
default:
org.testng.AssertJUnit.assertSame(1, inEventCount);
}
}
eventArrived = true;
}
if (removeEvents != null) {
removeEventCount = removeEventCount + removeEvents.length;
}
eventArrived = true;
}
});
InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
siddhiAppRuntime.start();
stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
stockStream.send(new Object[] { "IBM", 75.6f, 10L });
checkStockStream.send(new Object[] { "WSO2" });
Thread.sleep(500);
assertEquals("Number of success events", 1, inEventCount);
assertEquals("Number of remove events", 0, removeEventCount);
assertEquals("Event arrived", true, eventArrived);
siddhiAppRuntime.shutdown();
}
Aggregations