use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.
the class SiddhiDebuggerClient method start.
/**
* Start the {@link SiddhiDebuggerClient} and configure the breakpoints.
*
* @param siddhiApp the Siddhi query
* @param input the user input as a whole text
*/
public void start(final String siddhiApp, String input) {
SiddhiManager siddhiManager = new SiddhiManager();
info("Deploying the siddhi app");
final SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
// Add callbacks for all the streams
final Set<String> streamNames = SiddhiCompiler.parse(siddhiApp).getStreamDefinitionMap().keySet();
for (String streamName : streamNames) {
final String stream = streamName;
siddhiAppRuntime.addCallback(stream, new StreamCallback() {
@Override
public void receive(Event[] events) {
info("@Receive: Stream: " + stream + ", Event: " + Arrays.deepToString(events));
}
});
}
SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
final InputFeeder inputFeeder = new InputFeeder(siddhiAppRuntime, input);
System.out.println("Configure the breakpoints.\nYou can use the following commands:\n - " + ADD_BREAKPOINT + "<query name>:<IN/OUT>\n - " + REMOVE_BREAKPOINT + "<query name>:<IN/OUT>\n - " + START + "\n - " + STOP);
printNextLine();
final Scanner scanner = new Scanner(System.in, "UTF-8");
while (scanner.hasNextLine()) {
String userInput = scanner.nextLine().trim();
String command = userInput.toLowerCase();
if (command.startsWith(ADD_BREAKPOINT)) {
if (!command.contains(QUERY_DELIMITER)) {
error("Invalid add query. The query must be " + ADD_BREAKPOINT + "<query " + "name>:<IN/OUT>. Please try again");
printNextLine();
continue;
}
String[] components = userInput.substring(ADD_BREAKPOINT.length(), userInput.length()).split(QUERY_DELIMITER);
String queryName = components[0];
String terminal = components[1].toLowerCase();
if (IN.equals(terminal)) {
siddhiDebugger.acquireBreakPoint(queryName, SiddhiDebugger.QueryTerminal.IN);
info("Added a breakpoint at the IN terminal of " + queryName);
printNextLine();
} else if (OUT.equals(terminal)) {
siddhiDebugger.acquireBreakPoint(queryName, SiddhiDebugger.QueryTerminal.OUT);
info("Added a breakpoint at the OUT terminal of " + queryName);
printNextLine();
} else {
error("The terminal must be either IN or OUT but found: " + terminal.toUpperCase() + ". Please try again");
printNextLine();
}
} else if (command.startsWith(REMOVE_BREAKPOINT)) {
if (!command.contains(QUERY_DELIMITER)) {
error("Invalid add query. The query must be " + REMOVE_BREAKPOINT + "<query " + "name>:<IN/OUT>. Please try again");
printNextLine();
continue;
}
String[] components = command.substring(ADD_BREAKPOINT.length(), command.length()).split(QUERY_DELIMITER);
String queryName = components[0];
String terminal = components[1];
if (IN.equals(terminal)) {
siddhiDebugger.releaseBreakPoint(queryName, SiddhiDebugger.QueryTerminal.IN);
info("Removed the breakpoint at the IN terminal of " + queryName);
printNextLine();
} else if (OUT.equals(terminal)) {
siddhiDebugger.releaseBreakPoint(queryName, SiddhiDebugger.QueryTerminal.OUT);
info("Removed the breakpoint at the OUT terminal of " + queryName);
printNextLine();
} else {
error("The terminal must be either IN or OUT but found: " + terminal.toUpperCase());
printNextLine();
}
} else if (STOP.equals(command)) {
inputFeeder.stop();
siddhiAppRuntime.shutdown();
break;
} else if (START.equals(command)) {
inputFeeder.start();
info("Siddhi Debugger starts sending input to Siddhi");
System.out.println("You can use the following commands:\n - " + NEXT + "\n - " + PLAY + "\n - " + STATE + ":<query name>\n - " + STOP);
break;
} else {
error("Invalid command: " + command);
printNextLine();
}
}
siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {
@Override
public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
info("@Debug: Query: " + queryName + ", Terminal: " + queryTerminal + ", Event: " + event);
printNextLine();
while (scanner.hasNextLine()) {
String command = scanner.nextLine().trim().toLowerCase();
if (STOP.equals(command)) {
debugger.releaseAllBreakPoints();
debugger.play();
inputFeeder.stop();
siddhiAppRuntime.shutdown();
break;
} else if (NEXT.equals(command)) {
debugger.next();
break;
} else if (PLAY.equals(command)) {
debugger.play();
break;
} else if (command.startsWith(STATE)) {
if (!command.contains(QUERY_DELIMITER)) {
error("Invalid get state request. The query must be " + STATE + ":<query " + "name>. Please try again");
printNextLine();
continue;
}
String[] components = command.split(QUERY_DELIMITER);
String requestQueryName = components[1];
Map<String, Object> state = debugger.getQueryState(requestQueryName.trim());
System.out.println("Query '" + requestQueryName + "' state : ");
for (Map.Entry<String, Object> entry : state.entrySet()) {
System.out.println(" '" + entry.getKey() + "' : " + entry.getValue());
}
printNextLine();
continue;
} else {
error("Invalid command: " + command);
printNextLine();
}
}
}
});
inputFeeder.join();
if (inputFeeder.isRunning()) {
info("Input feeder has sopped sending all inputs. If you want to stop the execution, use " + "the STOP command");
printNextLine();
while (scanner.hasNextLine()) {
String command = scanner.nextLine().trim().toLowerCase();
if (STOP.equals(command)) {
inputFeeder.stop();
siddhiAppRuntime.shutdown();
break;
} else {
error("Invalid command: " + command);
printNextLine();
}
}
}
scanner.close();
info("Siddhi Debugger is stopped successfully");
}
use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.
the class DistinctCountAggregationTestCase method incrementalStreamProcessorTest1.
@Test
public void incrementalStreamProcessorTest1() throws InterruptedException {
LOG.info("incrementalStreamProcessorTest1: testing distinctCount incremental aggregator");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
String query = "define aggregation stockAggregation " + "from stockStream " + "select distinctCount(symbol) as distinctCnt " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"days\" " + "select AGG_TIMESTAMP, s.distinctCnt " + "order by AGG_TIMESTAMP " + "insert all events into outputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
if (inEvents != null) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event event : inEvents) {
inEventsList.add(event.getData());
inEventCount.incrementAndGet();
}
eventArrived = true;
}
if (removeEvents != null) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event event : removeEvents) {
removeEventsList.add(event.getData());
removeEventCount.incrementAndGet();
}
}
eventArrived = true;
}
});
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
siddhiAppRuntime.start();
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
stockStreamInputHandler.send(new Object[] { "WSO22", 70f, null, 40L, 10, 1496289950000L });
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952000L });
stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952000L });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954000L });
stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 96, 1496289954000L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956000L });
stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956000L });
// Thursday, June 1, 2017 4:06:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496290016000L });
// Thursday, June 1, 2017 4:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM2", 600f, null, 200L, 6, 1496290076000L });
// Thursday, June 1, 2017 5:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 700f, null, 200L, 20, 1496293676000L });
// Thursday, June 1, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 61f, 44f, 200L, 56, 1496297276000L });
// Friday, June 2, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 801f, null, 100L, 10, 1496383676000L });
// Saturday, June 3, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 901f, null, 100L, 15, 1496470076000L });
// Monday, July 3, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 96, 1499062076000L });
// Thursday, August 3, 2017 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 402f, null, 200L, 9, 1501740476000L });
// Friday, August 3, 2018 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 63f, 44f, 200L, 6, 1533276476000L });
// Saturday, August 3, 2019 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 260f, 44f, 200L, 16, 1564812476000L });
// Monday, August 3, 2020 6:07:56 AM
stockStreamInputHandler.send(new Object[] { "CISCO", 26f, 44f, 200L, 16, 1596434876000L });
Thread.sleep(100);
inputStreamInputHandler.send(new Object[] { "IBM" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { 1496275200000L, 8 }, new Object[] { 1496361600000L, 1 }, new Object[] { 1496448000000L, 1 }, new Object[] { 1499040000000L, 1 }, new Object[] { 1501718400000L, 1 }, new Object[] { 1533254400000L, 1 }, new Object[] { 1564790400000L, 1 }, new Object[] { 1596412800000L, 1 });
SiddhiTestHelper.waitForEvents(100, 8, inEventCount, 10000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 8, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isEventsMatch(inEventsList, expected));
AssertJUnit.assertEquals("Number of remove events", 8, removeEventCount.get());
AssertJUnit.assertTrue("Remove events matched", SiddhiTestHelper.isEventsMatch(removeEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.
the class PurgingTestCase method incrementalPurgingTest2.
@Test
public void incrementalPurgingTest2() throws InterruptedException {
LOG.info("incrementalPurgingTest2");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = " define stream stockStream (symbol string, price float, lastClosingPrice float," + " volume long ,quantity int);";
String query = " @purge(enable='true',interval='5 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'" + " ,days='all',months='all',years='all')) " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, sum(price) as totalPrice, (price * quantity) " + "as lastTradeValue " + "group by symbol " + "aggregate every sec...years ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
siddhiAppRuntime.start();
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 70f, null, 40L, 10 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 60f, 44f, 200L, 56 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 16 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26 });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 96 });
Thread.sleep(1000);
Event[] events = siddhiAppRuntime.query("from stockAggregation " + "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " + "per \"seconds\"");
EventPrinter.print(events);
AssertJUnit.assertNotNull("Initial queried events cannot be null", events);
AssertJUnit.assertEquals("Number of events before purging", 6, events.length);
Thread.sleep(120100);
events = siddhiAppRuntime.query("from stockAggregation " + "within \"" + Calendar.getInstance().get(Calendar.YEAR) + "-**-** **:**:**\" " + "per \"seconds\"");
EventPrinter.print(events);
AssertJUnit.assertNull("No events expected after purging", events);
siddhiAppRuntime.shutdown();
}
use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.
the class PurgingTestCase method incrementalPurgingTestCase3.
@Test
public void incrementalPurgingTestCase3() throws InterruptedException {
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, " + "volume long , quantity int, timestamp long); ";
String query = " @purge(enable='true',interval='10 sec',@retentionPeriod(sec='120 sec',min='all',hours='all'," + "days='all',months='all',years='all')) " + "define aggregation stockAggregation " + "from stockStream " + "select symbol, sum(price) as totalPrice " + "group by symbol " + "aggregate by timestamp every sec...year ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
siddhiAppRuntime.start();
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:51 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 16, 1496289951011L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "IBM", 400f, null, 200L, 9, 1496289952000L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289950000L });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 500f, null, 200L, 7, 1496289951011L });
Thread.sleep(1000);
// Thursday, June 1, 2017 4:05:53 AM
stockStreamInputHandler.send(new Object[] { "IBM", 100f, null, 200L, 26, 1496289953000L });
Thread.sleep(1000);
stockStreamInputHandler.send(new Object[] { "WSO2", 100f, null, 200L, 96, 1496289953000L });
Thread.sleep(2000);
Event[] events = siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'seconds' select AGG_TIMESTAMP, symbol, totalPrice ");
EventPrinter.print(events);
AssertJUnit.assertEquals("Check time windows", 7, events.length);
List<Object[]> eventsList = new ArrayList<>();
for (Event event : events) {
eventsList.add(event.getData());
}
List<Object[]> expected = Arrays.asList(new Object[] { 1496289950000L, "WSO2", 50.0 }, new Object[] { 1496289950000L, "IBM", 900.0 }, new Object[] { 1496289951000L, "IBM", 100.0 }, new Object[] { 1496289951000L, "WSO2", 500.0 }, new Object[] { 1496289952000L, "IBM", 400.0 }, new Object[] { 1496289953000L, "IBM", 100.0 }, new Object[] { 1496289953000L, "WSO2", 100.0 });
AssertJUnit.assertTrue("Data Matched", SiddhiTestHelper.isUnsortedEventsMatch(eventsList, expected));
Thread.sleep(120000);
events = siddhiAppRuntime.query("from stockAggregation within 0L, 1543664151000L per " + "'seconds' select AGG_TIMESTAMP, symbol, totalPrice ");
EventPrinter.print(events);
Assert.assertNull(events);
} finally {
siddhiAppRuntime.shutdown();
}
}
use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.
the class LatestAggregationTestCase method latestTestCase4.
@Test(dependsOnMethods = "latestTestCase3")
public void latestTestCase4() throws InterruptedException {
LOG.info("latestTestCase4: testing latest incremental aggregator - different group by");
SiddhiManager siddhiManager = new SiddhiManager();
String stockStream = "define stream stockStream (symbol string, price float, lastClosingPrice float, volume long , " + "quantity int, timestamp long);";
String query = "define aggregation stockAggregation " + "from stockStream " + "select symbol, avg(price) as avgPrice, (price * quantity) as latestPrice " + "aggregate by timestamp every sec...year ;" + "define stream inputStream (symbol string); " + "@info(name = 'query1') " + "from inputStream as i join stockAggregation as s " + "within 1496200000000L, 1596535449000L " + "per \"seconds\" " + "select s.symbol, s.latestPrice, sum(s.avgPrice) as totalAvg " + "group by s.symbol " + "order by AGG_TIMESTAMP " + "insert all events into outputStream; ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(stockStream + query);
try {
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
if (inEvents != null) {
EventPrinter.print(timestamp, inEvents, removeEvents);
for (Event event : inEvents) {
inEventsList.add(event.getData());
inEventCount.incrementAndGet();
}
eventArrived = true;
}
}
});
InputHandler stockStreamInputHandler = siddhiAppRuntime.getInputHandler("stockStream");
InputHandler inputStreamInputHandler = siddhiAppRuntime.getInputHandler("inputStream");
siddhiAppRuntime.start();
// Thursday, June 1, 2017 4:05:50 AM
stockStreamInputHandler.send(new Object[] { "WSO2", 50f, 60f, 90L, 6, 1496289950010L });
stockStreamInputHandler.send(new Object[] { "WSO22", 75f, null, 40L, 10, 1496289950100L });
// Thursday, June 1, 2017 4:05:52 AM
stockStreamInputHandler.send(new Object[] { "WSO23", 60f, 44f, 200L, 56, 1496289952010L });
stockStreamInputHandler.send(new Object[] { "WSO24", 100f, null, 200L, 16, 1496289952020L });
// Thursday, June 1, 2017 4:05:50 AM - Out of order event
stockStreamInputHandler.send(new Object[] { "WSO23", 70f, null, 40L, 10, 1496289950090L });
// Thursday, June 1, 2017 4:05:54 AM
stockStreamInputHandler.send(new Object[] { "IBM", 101f, null, 200L, 26, 1496289954010L });
stockStreamInputHandler.send(new Object[] { "IBM1", 102f, null, 200L, 100, 1496289954020L });
// Thursday, June 1, 2017 4:05:56 AM
stockStreamInputHandler.send(new Object[] { "IBM", 900f, null, 200L, 60, 1496289956010L });
stockStreamInputHandler.send(new Object[] { "IBM1", 500f, null, 200L, 7, 1496289956030L });
Thread.sleep(100);
inputStreamInputHandler.send(new Object[] { "IBM" });
Thread.sleep(100);
List<Object[]> expected = Arrays.asList(new Object[] { "WSO22", 750f, 65.0 }, new Object[] { "WSO24", 1600f, 80.0 }, new Object[] { "IBM1", 3500f, 801.5 });
SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 10000);
AssertJUnit.assertTrue("Event arrived", eventArrived);
AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
AssertJUnit.assertTrue("In events matched", SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
} finally {
siddhiAppRuntime.shutdown();
}
}
Aggregations