use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.
the class FilterTestCase2 method testFilterQuery109.
// *****************************************************************************************************************
// Expression-Add
@Test
public void testFilterQuery109() throws InterruptedException {
log.info("Filter test109");
SiddhiManager siddhiManager = new SiddhiManager();
StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE).attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);
Query query = new Query();
query.from(InputStream.stream("cseEventStream"));
query.annotation(Annotation.annotation("info").element("name", "query1"));
query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("increasedPrice", Expression.add(Expression.value(100), Expression.variable("price"))).select("increasedVolume", Expression.add(Expression.value(50), Expression.variable("volume"))).select("increasedQuantity", Expression.add(Expression.value(4), Expression.variable("quantity"))).select("increasedAwards", Expression.add(Expression.value(10), Expression.variable("awards"))));
query.insertInto("outputStream");
SiddhiApp siddhiApp = new SiddhiApp("ep1");
siddhiApp.defineStream(cseEventStream);
siddhiApp.addQuery(query);
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
AssertJUnit.assertTrue("155.5".equals(inEvents[0].getData()[1].toString()));
AssertJUnit.assertTrue("150.0".equals(inEvents[0].getData()[2].toString()));
AssertJUnit.assertTrue("9".equals(inEvents[0].getData()[3].toString()));
AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[4].toString()));
count = count + inEvents.length;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "WSO2", 55.5f, 100d, 5, 10L });
Thread.sleep(100);
AssertJUnit.assertEquals(1, count);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.
the class FilterTestCase2 method testFilterQuery111.
// ***************************************************************************************************************
// Expression Divide
@Test
public void testFilterQuery111() throws InterruptedException {
log.info("Filter test111");
SiddhiManager siddhiManager = new SiddhiManager();
StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE).attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);
Query query = new Query();
query.from(InputStream.stream("cseEventStream"));
query.annotation(Annotation.annotation("info").element("name", "query1"));
query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("dividedPrice", Expression.divide(Expression.variable("price"), Expression.value(2))).select("dividedVolume", Expression.divide(Expression.variable("volume"), Expression.value(2))).select("dividedQuantity", Expression.divide(Expression.variable("quantity"), Expression.value(5))).select("dividedAwards", Expression.divide(Expression.variable("awards"), Expression.value(10))));
query.insertInto("OutputStream");
SiddhiApp siddhiApp = new SiddhiApp("ep1");
siddhiApp.defineStream(cseEventStream);
siddhiApp.addQuery(query);
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
AssertJUnit.assertTrue("30.0".equals(inEvents[0].getData()[1].toString()));
AssertJUnit.assertTrue("50.0".equals(inEvents[0].getData()[2].toString()));
AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[3].toString()));
AssertJUnit.assertTrue("7".equals(inEvents[0].getData()[4].toString()));
count = count + inEvents.length;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "WSO2", 60f, 100d, 100, 70L });
Thread.sleep(100);
AssertJUnit.assertEquals(1, count);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.
the class GroupByTestCase method testGroupByQuery1.
@Test
public void testGroupByQuery1() throws InterruptedException {
log.info("GroupBy test1");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@app:name('GroupByTest1') " + "" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream#window.time(1 sec) " + "select symbol, sum(volume) as totalVolume, avg(price) as avgPrice " + " group by symbol " + "insert into outputStream;" + "";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
log.info("Running : " + siddhiAppRuntime.getName());
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
count = count + inEvents.length;
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 50f, 200L });
inputHandler.send(new Object[] { "WSO2", 50f, 200L });
Thread.sleep(200);
inputHandler.send(new Object[] { "WSO2", 50f, 200L });
inputHandler.send(new Object[] { "IBM", 50f, 200L });
Thread.sleep(4200);
inputHandler.send(new Object[] { "WSO2", 50f, 200L });
inputHandler.send(new Object[] { "WSO2", 50f, 200L });
Thread.sleep(100);
AssertJUnit.assertEquals(6, count);
AssertJUnit.assertTrue(eventArrived);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.
the class PassThroughTestCase method passThroughTest4.
@Test
public void passThroughTest4() throws InterruptedException {
log.info("pass through test4");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "" + "@app:name('passThroughTest4') " + "" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream " + "insert into outputStream;" + "" + "@info(name = 'query2') " + "from outputStream " + "select * " + "insert into outputStream2 ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
log.info("Running : " + siddhiAppRuntime.getName());
QueryCallback queryCallback = new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timestamp, inEvents, removeEvents);
AssertJUnit.assertTrue("WSO2".equals(inEvents[0].getData(0)));
count = count + inEvents.length;
eventArrived = true;
}
};
siddhiAppRuntime.addCallback("query2", queryCallback);
queryCallback.startProcessing();
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "WSO2", 700f, 100L });
inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
Thread.sleep(100);
AssertJUnit.assertEquals(2, count);
AssertJUnit.assertTrue(eventArrived);
queryCallback.stopProcessing();
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.
the class PassThroughTestCase method passThroughTest2.
@Test
public void passThroughTest2() throws InterruptedException {
log.info("pass through test2");
SiddhiManager siddhiManager = new SiddhiManager();
StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT);
StreamDefinition cseEventStream1 = StreamDefinition.id("cseEventStream1").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT);
Query query = new Query();
query.from(InputStream.stream("cseEventStream"));
query.annotation(Annotation.annotation("info").element("name", "query1"));
query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
query.insertInto("StockQuote");
SiddhiApp siddhiApp = new SiddhiApp("ep1");
siddhiApp.defineStream(cseEventStream);
siddhiApp.defineStream(cseEventStream1);
siddhiApp.addQuery(query);
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(inEvents);
count += inEvents.length;
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream1");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 100 });
inputHandler.send(new Object[] { "WSO2", 100 });
Thread.sleep(100);
AssertJUnit.assertEquals(0, count);
AssertJUnit.assertFalse(eventArrived);
siddhiAppRuntime.shutdown();
}
Aggregations