use of io.siddhi.core.SiddhiManager in project siddhi by wso2.
the class YAMLConfigManagerTestCase method yamlConfigManagerTest2.
@Test
public void yamlConfigManagerTest2() throws InterruptedException {
log.info("YAMLConfigManagerTest2 - References test");
String baseDir = Paths.get(".").toString();
Path path = Paths.get(baseDir, "src", "test", "resources", "systemProperties.yaml");
String fileContent = FileReader.readYAMLConfigFile(path);
YAMLConfigManager yamlConfigManager = new YAMLConfigManager(fileContent);
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setConfigManager(yamlConfigManager);
String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "@Store(ref='ref1')\n" + "define table StockTable (symbol string, volume long); ";
String query1 = "" + "@info(name = 'query1') " + "from StockStream\n" + "select symbol, volume\n" + "insert into StockTable ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query1);
InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
stockStream.send(new Object[] { "IBM", 75.6f, 100L });
Thread.sleep(1000);
Event[] events = siddhiAppRuntime.query("" + "from StockTable ");
EventPrinter.print(events);
AssertJUnit.assertEquals(2, events.length);
siddhiAppRuntime.shutdown();
}
use of io.siddhi.core.SiddhiManager in project siddhi by wso2.
the class IncrementalPersistenceTestCase method incrementalPersistenceTest2.
@Test
public void incrementalPersistenceTest2() throws InterruptedException {
log.info("Incremental persistence test 2 - length batch window query");
final int inputEventCount = 10;
final int eventWindowSize = 4;
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
String siddhiApp = "" + "@app:name('incrementalPersistenceTest2') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.lengthBatch(" + eventWindowSize + ") " + "select symbol, price, sum(volume) as totalVol " + "insert all events into OutStream";
QueryCallback queryCallback = new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
eventArrived = true;
for (Event inEvent : inEvents) {
count++;
AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
lastValue = (Long) inEvent.getData(2);
}
}
};
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
for (int i = 0; i < inputEventCount; i++) {
inputHandler.send(new Object[] { "IBM", 75.6f + i, 100 });
}
Thread.sleep(100);
AssertJUnit.assertTrue(eventArrived);
AssertJUnit.assertEquals(new Long(400), lastValue);
// persisting
siddhiAppRuntime.persist();
Thread.sleep(5000);
inputHandler.send(new Object[] { "WSO2", 100.4f, 150 });
// Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 200.4f, 110 });
inputHandler.send(new Object[] { "IBM", 300.4f, 100 });
// Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 400.4f, 300 });
inputHandler.send(new Object[] { "IBM", 500.6f, 120 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 600.6f, 400 });
Thread.sleep(100);
siddhiAppRuntime.persist();
Thread.sleep(5000);
siddhiAppRuntime.shutdown();
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
// loading
try {
siddhiAppRuntime.restoreLastRevision();
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed", e);
}
siddhiAppRuntime.start();
Thread.sleep(500);
inputHandler.send(new Object[] { "IBM", 700.6f, 230 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 800.6f, 125 });
inputHandler.send(new Object[] { "IBM", 900.6f, 370 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 1000.6f, 140 });
// shutdown Siddhi app
Thread.sleep(500);
siddhiAppRuntime.shutdown();
AssertJUnit.assertTrue(count <= (inputEventCount + 6));
AssertJUnit.assertEquals(new Long(865), lastValue);
AssertJUnit.assertEquals(true, eventArrived);
}
use of io.siddhi.core.SiddhiManager in project siddhi by wso2.
the class IncrementalPersistenceTestCase method incrementalPersistenceTest7.
@Test
public void incrementalPersistenceTest7() throws InterruptedException {
log.info("Incremental persistence test 7 - in-memory table persistance test with primary key.");
int inputEventCountPerCategory = 10;
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
String streams = "" + "@app:name('incrementalPersistenceTest7') " + "" + "define stream StockStream (symbol2 string, price float, volume long); " + "define stream CheckStockStream (symbol1 string); " + "define stream OutStream (symbol1 string, TB long); " + "@PrimaryKey('symbol2') " + "define table StockTable (symbol2 string, price float, volume long); ";
String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream join StockTable " + " on symbol1 == symbol2 " + "select symbol2 as symbol1, volume as TB " + "group by symbol2 " + "insert all events into OutStream;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
QueryCallback queryCallback = new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
if (inEvents != null) {
for (Event event : inEvents) {
inEventsList.add(event.getData());
inEventCount.incrementAndGet();
}
eventArrived = true;
}
if (removeEvents != null) {
removeEventCount = removeEventCount + removeEvents.length;
}
eventArrived = true;
}
};
try {
siddhiAppRuntime.addCallback("query2", queryCallback);
InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
for (int i = 0; i < inputEventCountPerCategory; i++) {
stockStream.send(new Object[] { "WSO2-" + i, 55.6f, 180L + i });
}
for (int i = 0; i < inputEventCountPerCategory; i++) {
stockStream.send(new Object[] { "IBM-" + i, 55.6f, 100L + i });
}
// persisting
siddhiAppRuntime.persist();
Thread.sleep(5000);
siddhiAppRuntime.shutdown();
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
siddhiAppRuntime.addCallback("query2", queryCallback);
stockStream = siddhiAppRuntime.getInputHandler("StockStream");
InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
// loading
try {
siddhiAppRuntime.restoreLastRevision();
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed.", e);
}
siddhiAppRuntime.start();
Thread.sleep(2000);
stockStream.send(new Object[] { "WSO2-" + (inputEventCountPerCategory + 1), 100.6f, 180L });
stockStream.send(new Object[] { "IBM-" + (inputEventCountPerCategory + 1), 100.6f, 100L });
stockStream.send(new Object[] { "WSO2-" + (inputEventCountPerCategory + 2), 8.6f, 13L });
stockStream.send(new Object[] { "IBM-" + (inputEventCountPerCategory + 2), 7.6f, 14L });
checkStockStream.send(new Object[] { "IBM-5" });
checkStockStream.send(new Object[] { "WSO2-5" });
List<Object[]> expected = Arrays.asList(new Object[] { "IBM-5", 105L }, new Object[] { "WSO2-5", 185L });
Thread.sleep(1000);
AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.SiddhiManager in project siddhi by wso2.
the class IncrementalPersistenceTestCase method incrementalPersistenceTest3.
@Test
public void incrementalPersistenceTest3() throws InterruptedException {
log.info("Incremental persistence test 3 - time window query");
final int inputEventCount = 10;
final int eventWindowSizeInSeconds = 2;
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
String siddhiApp = "" + "@app:name('incrementalPersistenceTest3') " + "@app:playback " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.time(" + eventWindowSizeInSeconds + " sec) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream";
QueryCallback queryCallback = new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
eventArrived = true;
for (Event inEvent : inEvents) {
count++;
AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
lastValue = (Long) inEvent.getData(2);
log.info("last value: " + lastValue);
}
}
};
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
for (int i = 0; i < inputEventCount; i++) {
inputHandler.send(i, new Object[] { "IBM", 75.6f + i, 100 });
}
Thread.sleep(4000);
AssertJUnit.assertTrue(eventArrived);
AssertJUnit.assertEquals(new Long(1000), lastValue);
// persisting
siddhiAppRuntime.persist();
Thread.sleep(5000);
inputHandler.send(3000, new Object[] { "WSO2", 100.4f, 150 });
// Thread.sleep(100);
inputHandler.send(3010, new Object[] { "WSO2", 200.4f, 110 });
inputHandler.send(3020, new Object[] { "IBM", 300.4f, 100 });
// Thread.sleep(100);
inputHandler.send(3030, new Object[] { "WSO2", 400.4f, 300 });
inputHandler.send(3040, new Object[] { "IBM", 500.6f, 120 });
Thread.sleep(10);
inputHandler.send(3050, new Object[] { "WSO2", 600.6f, 400 });
Thread.sleep(100);
siddhiAppRuntime.persist();
Thread.sleep(5000);
siddhiAppRuntime.shutdown();
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
try {
// loading
siddhiAppRuntime.restoreLastRevision();
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
}
siddhiAppRuntime.start();
Thread.sleep(500);
inputHandler.send(3500, new Object[] { "IBM", 700.6f, 230 });
Thread.sleep(10);
inputHandler.send(3540, new Object[] { "WSO2", 800.6f, 125 });
inputHandler.send(3590, new Object[] { "IBM", 900.6f, 370 });
Thread.sleep(10);
inputHandler.send(3600, new Object[] { "WSO2", 1000.6f, 140 });
// shutdown Siddhi app
Thread.sleep(5000);
siddhiAppRuntime.shutdown();
AssertJUnit.assertTrue(count <= (inputEventCount + 10));
AssertJUnit.assertEquals(new Long(2045), lastValue);
AssertJUnit.assertEquals(true, eventArrived);
}
use of io.siddhi.core.SiddhiManager in project siddhi by wso2.
the class IncrementalPersistenceTestCase method incrementalPersistenceTest4.
@Test
public void incrementalPersistenceTest4() throws InterruptedException {
log.info("Incremental persistence test 4 - time batch window query");
final int inputEventCount = 10;
final int eventWindowSizeInSeconds = 2;
SiddhiManager siddhiManager = new SiddhiManager();
siddhiManager.setIncrementalPersistenceStore(new IncrementalFileSystemPersistenceStore(storageFilePath));
String siddhiApp = "" + "@app:name('incrementalPersistenceTest4') " + "" + "define stream StockStream ( symbol string, price float, volume int );" + "" + "@info(name = 'query1')" + "from StockStream[price>10]#window.timeBatch(" + eventWindowSizeInSeconds + " sec) " + "select symbol, price, sum(volume) as totalVol " + "insert into OutStream";
QueryCallback queryCallback = new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
eventArrived = true;
for (Event inEvent : inEvents) {
count++;
AssertJUnit.assertTrue("IBM".equals(inEvent.getData(0)) || "WSO2".equals(inEvent.getData(0)));
lastValue = (Long) inEvent.getData(2);
log.info("last value: " + lastValue);
}
}
};
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
siddhiAppRuntime.start();
for (int i = 0; i < inputEventCount; i++) {
inputHandler.send(new Object[] { "IBM", 75.6f + i, 100 });
}
Thread.sleep(4000);
AssertJUnit.assertTrue(eventArrived);
AssertJUnit.assertEquals(new Long(1000), lastValue);
// persisting
siddhiAppRuntime.persist();
Thread.sleep(5000);
inputHandler.send(new Object[] { "WSO2", 100.4f, 150 });
// Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 200.4f, 110 });
inputHandler.send(new Object[] { "IBM", 300.4f, 100 });
// Thread.sleep(100);
inputHandler.send(new Object[] { "WSO2", 400.4f, 300 });
inputHandler.send(new Object[] { "IBM", 500.6f, 120 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 600.6f, 400 });
Thread.sleep(100);
siddhiAppRuntime.persist();
Thread.sleep(5000);
siddhiAppRuntime.shutdown();
siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", queryCallback);
inputHandler = siddhiAppRuntime.getInputHandler("StockStream");
// loading
try {
siddhiAppRuntime.restoreLastRevision();
} catch (CannotRestoreSiddhiAppStateException e) {
Assert.fail("Restoring of Siddhi app " + siddhiAppRuntime.getName() + " failed");
}
siddhiAppRuntime.start();
Thread.sleep(50);
inputHandler.send(new Object[] { "IBM", 700.6f, 230 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 800.6f, 125 });
inputHandler.send(new Object[] { "IBM", 900.6f, 370 });
Thread.sleep(10);
inputHandler.send(new Object[] { "WSO2", 1000.6f, 140 });
// shutdown Siddhi app
Thread.sleep(5000);
siddhiAppRuntime.shutdown();
AssertJUnit.assertTrue(count <= (inputEventCount + 10));
AssertJUnit.assertEquals(new Long(2045), lastValue);
AssertJUnit.assertEquals(true, eventArrived);
}
Aggregations