use of org.wso2.siddhi.query.compiler.exception.SiddhiParserException in project siddhi by wso2.
the class SimpleQueryTestCase method test3.
@Test
public void test3() throws SiddhiParserException {
Query query = SiddhiCompiler.parseQuery("from AllStockQuotes#window.time(10 min)\n" + "select symbol as symbol, price, avg(price) as averagePrice \n" + "group by symbol \n" + "having ( price > ( averagePrice*1.02) ) or ( averagePrice > price ) " + "insert into FastMovingStockQuotes \n;");
Query api = Query.query().from(InputStream.stream("AllStockQuotes").window("time", Expression.Time.minute(10))).select(Selector.selector().select("symbol", Expression.variable("symbol")).select(Expression.variable("price")).select("averagePrice", Expression.function("avg", Expression.variable("price"))).groupBy(Expression.variable("symbol")).having(Expression.or(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression.multiply(Expression.variable("averagePrice"), Expression.value(1.02))), Expression.compare(Expression.variable("averagePrice"), Compare.Operator.GREATER_THAN, Expression.variable("price"))))).insertInto("FastMovingStockQuotes");
AssertJUnit.assertEquals(api, query);
}
use of org.wso2.siddhi.query.compiler.exception.SiddhiParserException in project siddhi by wso2.
the class SiddhiAppParser method parse.
/**
* Parse an SiddhiApp returning SiddhiAppRuntime
*
* @param siddhiApp plan to be parsed
* @param siddhiAppString content of Siddhi application as string
* @param siddhiContext SiddhiContext @return SiddhiAppRuntime
*
* @return SiddhiAppRuntimeBuilder
*/
public static SiddhiAppRuntimeBuilder parse(SiddhiApp siddhiApp, String siddhiAppString, SiddhiContext siddhiContext) {
SiddhiAppContext siddhiAppContext = new SiddhiAppContext();
siddhiAppContext.setSiddhiContext(siddhiContext);
siddhiAppContext.setSiddhiAppString(siddhiAppString);
try {
Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_NAME, null, siddhiApp.getAnnotations());
if (element != null) {
siddhiAppContext.setName(element.getValue());
} else {
siddhiAppContext.setName(UUID.randomUUID().toString());
}
Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ENFORCE_ORDER, siddhiApp.getAnnotations());
if (annotation != null) {
siddhiAppContext.setEnforceOrder(true);
}
annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ASYNC, siddhiApp.getAnnotations());
if (annotation != null) {
throw new SiddhiAppCreationException("@Async not supported in SiddhiApp level, " + "instead use @Async with streams", annotation.getQueryContextStartIndex(), annotation.getQueryContextEndIndex());
}
annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STATISTICS, siddhiApp.getAnnotations());
List<Element> statisticsElements = new ArrayList<>();
if (annotation != null) {
statisticsElements = annotation.getElements();
}
if (siddhiContext.getStatisticsConfiguration() != null) {
siddhiAppContext.setStatisticsManager(siddhiContext.getStatisticsConfiguration().getFactory().createStatisticsManager(siddhiContext.getStatisticsConfiguration().getMetricPrefix(), siddhiAppContext.getName(), statisticsElements));
}
Element statStateEnableElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_ENABLE, siddhiApp.getAnnotations());
if (statStateEnableElement != null && Boolean.valueOf(statStateEnableElement.getValue())) {
siddhiAppContext.setStatsEnabled(true);
} else {
Element statStateElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, null, siddhiApp.getAnnotations());
// where sp uses @app:statistics('true').
if (annotation != null && (statStateElement == null || Boolean.valueOf(statStateElement.getValue()))) {
siddhiAppContext.setStatsEnabled(true);
}
}
Element statStateIncludElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_INCLUDE, siddhiApp.getAnnotations());
siddhiAppContext.setIncludedMetrics(generateIncludedMetrics(statStateIncludElement));
siddhiAppContext.setThreadBarrier(new ThreadBarrier());
siddhiAppContext.setExecutorService(Executors.newCachedThreadPool(new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-executor-thread-%d").build()));
siddhiAppContext.setScheduledExecutorService(Executors.newScheduledThreadPool(5, new ThreadFactoryBuilder().setNameFormat("Siddhi-" + siddhiAppContext.getName() + "-scheduler-thread-%d").build()));
// Select the TimestampGenerator based on playback mode on/off
annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PLAYBACK, siddhiApp.getAnnotations());
if (annotation != null) {
String idleTime = null;
String increment = null;
EventTimeBasedMillisTimestampGenerator timestampGenerator = new EventTimeBasedMillisTimestampGenerator(siddhiAppContext.getScheduledExecutorService());
// Get the optional elements of playback annotation
for (Element e : annotation.getElements()) {
if (SiddhiConstants.ANNOTATION_ELEMENT_IDLE_TIME.equalsIgnoreCase(e.getKey())) {
idleTime = e.getValue();
} else if (SiddhiConstants.ANNOTATION_ELEMENT_INCREMENT.equalsIgnoreCase(e.getKey())) {
increment = e.getValue();
} else {
throw new SiddhiAppValidationException("Playback annotation accepts only idle.time and " + "increment but found " + e.getKey());
}
}
// idleTime and increment are optional but if one presents, the other also should be given
if (idleTime != null && increment == null) {
throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but increment not found");
} else if (idleTime == null && increment != null) {
throw new SiddhiAppValidationException("Playback annotation requires both idle.time and " + "increment but idle.time does not found");
} else if (idleTime != null) {
// The fourth case idleTime == null && increment == null are ignored because it means no heartbeat.
try {
timestampGenerator.setIdleTime(SiddhiCompiler.parseTimeConstantDefinition(idleTime).value());
} catch (SiddhiParserException ex) {
throw new SiddhiParserException("Invalid idle.time constant '" + idleTime + "' in playback " + "annotation", ex);
}
try {
timestampGenerator.setIncrementInMilliseconds(SiddhiCompiler.parseTimeConstantDefinition(increment).value());
} catch (SiddhiParserException ex) {
throw new SiddhiParserException("Invalid increment constant '" + increment + "' in playback " + "annotation", ex);
}
}
siddhiAppContext.setTimestampGenerator(timestampGenerator);
siddhiAppContext.setPlayback(true);
} else {
siddhiAppContext.setTimestampGenerator(new SystemCurrentTimeMillisTimestampGenerator());
}
siddhiAppContext.setSnapshotService(new SnapshotService(siddhiAppContext));
siddhiAppContext.setPersistenceService(new PersistenceService(siddhiAppContext));
siddhiAppContext.setElementIdGenerator(new ElementIdGenerator(siddhiAppContext.getName()));
} catch (DuplicateAnnotationException e) {
throw new DuplicateAnnotationException(e.getMessageWithOutContext() + " for the same Siddhi app " + siddhiApp.toString(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
}
SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder = new SiddhiAppRuntimeBuilder(siddhiAppContext);
defineStreamDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getStreamDefinitionMap(), siddhiAppContext);
defineTableDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTableDefinitionMap(), siddhiAppContext);
defineWindowDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getWindowDefinitionMap(), siddhiAppContext);
defineFunctionDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getFunctionDefinitionMap(), siddhiAppContext);
defineAggregationDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getAggregationDefinitionMap(), siddhiAppContext);
for (Window window : siddhiAppRuntimeBuilder.getWindowMap().values()) {
try {
window.init(siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getWindowMap(), window.getWindowDefinition().getId());
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, window.getWindowDefinition(), siddhiAppContext);
throw t;
}
}
int queryIndex = 1;
for (ExecutionElement executionElement : siddhiApp.getExecutionElementList()) {
if (executionElement instanceof Query) {
try {
QueryRuntime queryRuntime = QueryParser.parse((Query) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), siddhiAppRuntimeBuilder.getTableDefinitionMap(), siddhiAppRuntimeBuilder.getWindowDefinitionMap(), siddhiAppRuntimeBuilder.getAggregationDefinitionMap(), siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getAggregationMap(), siddhiAppRuntimeBuilder.getWindowMap(), siddhiAppRuntimeBuilder.getLockSynchronizer(), String.valueOf(queryIndex));
siddhiAppRuntimeBuilder.addQuery(queryRuntime);
queryIndex++;
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, (Query) executionElement, siddhiAppContext);
throw t;
}
} else {
try {
PartitionRuntime partitionRuntime = PartitionParser.parse(siddhiAppRuntimeBuilder, (Partition) executionElement, siddhiAppContext, siddhiAppRuntimeBuilder.getStreamDefinitionMap(), queryIndex);
siddhiAppRuntimeBuilder.addPartition(partitionRuntime);
siddhiAppContext.getSnapshotService().addSnapshotable("partition", partitionRuntime);
queryIndex += ((Partition) executionElement).getQueryList().size();
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, (Partition) executionElement, siddhiAppContext);
throw t;
}
}
}
// Done last as they have to be started last
defineTriggerDefinitions(siddhiAppRuntimeBuilder, siddhiApp.getTriggerDefinitionMap(), siddhiAppContext);
return siddhiAppRuntimeBuilder;
}
use of org.wso2.siddhi.query.compiler.exception.SiddhiParserException in project siddhi by wso2.
the class AbsentPatternTestCase method test4.
@Test
public void test4() throws SiddhiParserException {
Query query = SiddhiCompiler.parseQuery("from e1=Stream1[price>20] -> not Stream2[price>e1.price] for 2 sec " + "select e1.symbol as symbol1 " + "insert into OutputStream ;");
AssertJUnit.assertNotNull(query);
Query api = Query.query();
api.from(InputStream.patternStream(State.next(State.stream(InputStream.stream("e1", "Stream1").filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression.value(20)))), State.logicalNot(State.stream(InputStream.stream("Stream2").filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression.variable("price").ofStream("e1"))))).waitingTime(new TimeConstant(2000))))).select(Selector.selector().select("symbol1", Expression.variable("symbol").ofStream("e1"))).insertInto("OutputStream");
AssertJUnit.assertEquals(api, query);
}
use of org.wso2.siddhi.query.compiler.exception.SiddhiParserException in project siddhi by wso2.
the class DefineAggregationTestCase method test3.
@Test
public void test3() throws SiddhiParserException {
Query query = SiddhiCompiler.parseQuery("from barStream as b join cseEventAggregation as a " + "on a.symbol == b.symbol " + "within \"2014-02-15T00:00:00Z\", \"2014-03-16T00:00:00Z\" " + "per \"day\" " + "select a.symbol, a.total, a.avgPrice " + "insert into fooBar;");
Query queryApi = Query.query().from(InputStream.joinStream(InputStream.stream("b", "barStream"), JoinInputStream.Type.JOIN, InputStream.stream("a", "cseEventAggregation"), Expression.compare(Expression.variable("symbol").ofStream("a"), Compare.Operator.EQUAL, Expression.variable("symbol").ofStream("b")), Within.within(Expression.value("2014-02-15T00:00:00Z"), Expression.value("2014-03-16T00:00:00Z")), Expression.value("day"))).select(Selector.selector().select(Expression.variable("symbol").ofStream("a")).select(Expression.variable("total").ofStream("a")).select(Expression.variable("avgPrice").ofStream("a"))).insertInto("fooBar");
AssertJUnit.assertEquals(queryApi, query);
}
use of org.wso2.siddhi.query.compiler.exception.SiddhiParserException in project siddhi by wso2.
the class DefineAggregationTestCase method test1.
@Test
public void test1() throws SiddhiParserException {
AggregationDefinition aggregationDefinitionQuery = SiddhiCompiler.parseAggregationDefinition("define aggregation StockAggregation " + "from StockStream " + "select StockStream.timestamp as timestamp, StockStream.symbol as symbol, " + " StockStream.price as price " + " group by StockStream.symbol " + "aggregate by timestamp " + "every seconds ... days ;");
AggregationDefinition aggregationDefinition = AggregationDefinition.id("StockAggregation").from(InputStream.stream("StockStream")).select(Selector.basicSelector().select("timestamp", Expression.variable("timestamp").ofStream("StockStream")).select("symbol", Expression.variable("symbol").ofStream("StockStream")).select("price", Expression.variable("price").ofStream("StockStream")).groupBy(Expression.variable("symbol").ofStream("StockStream"))).aggregateBy(Expression.variable("timestamp")).every(TimePeriod.range(TimePeriod.Duration.SECONDS, TimePeriod.Duration.DAYS));
AssertJUnit.assertEquals(aggregationDefinition, aggregationDefinitionQuery);
}
Aggregations