use of org.wso2.siddhi.query.api.expression.constant.Constant in project ballerina by ballerina-lang.
the class CodeGenerator method createEnumInfoEntry.
private void createEnumInfoEntry(BLangEnum enumNode) {
BTypeSymbol enumSymbol = (BTypeSymbol) enumNode.symbol;
// Add Enum name as an UTFCPEntry to the constant pool
int enumNameCPIndex = addUTF8CPEntry(currentPkgInfo, enumSymbol.name.value);
EnumInfo enumInfo = new EnumInfo(currentPackageRefCPIndex, enumNameCPIndex, enumSymbol.flags);
currentPkgInfo.addEnumInfo(enumSymbol.name.value, enumInfo);
enumInfo.enumType = (BEnumType) enumSymbol.type;
for (int i = 0; i < enumNode.enumerators.size(); i++) {
BLangEnumerator enumeratorNode = enumNode.enumerators.get(i);
enumeratorNode.symbol.varIndex = new RegIndex(i, enumSymbol.type.tag);
enumeratorNode.symbol.varIndex.isVarIndex = true;
int enumeratorNameCPIndex = addUTF8CPEntry(currentPkgInfo, enumeratorNode.symbol.name.toString());
EnumeratorInfo enumeratorInfo = new EnumeratorInfo(enumeratorNameCPIndex, i, enumInfo.enumType);
enumInfo.enumeratorInfoList.add(enumeratorInfo);
}
}
use of org.wso2.siddhi.query.api.expression.constant.Constant in project carbon-apimgt by wso2.
the class ThrottleStreamProcessor method init.
@Override
protected List<Attribute> init(AbstractDefinition abstractDefinition, ExpressionExecutor[] expressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
this.siddhiAppContext = siddhiAppContext;
if (attributeExpressionExecutors.length == 1) {
if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else {
throw new SiddhiAppValidationException("Throttle batch window's 1st parameter attribute should be " + "either int or long, but found " + attributeExpressionExecutors[0].getReturnType());
}
} else {
throw new SiddhiAppValidationException("Throttle batch window 1st parameter needs to be constant " + "parameter attribute but found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
}
} else if (attributeExpressionExecutors.length == 2) {
if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
} else {
throw new SiddhiAppValidationException("Throttle batch window's 1st parameter attribute should be " + "either int or long, but found " + attributeExpressionExecutors[0].getReturnType());
}
} else {
throw new SiddhiAppValidationException("Throttle batch window 1st parameter needs to be constant " + "attribute but found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
}
if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
} else if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.LONG) {
startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
} else {
throw new SiddhiAppValidationException("Throttle batch window 2nd parameter needs to be a Long " + "or Int type but found a " + attributeExpressionExecutors[2].getReturnType());
}
} else {
throw new SiddhiAppValidationException("Throttle batch window should only have one/two parameter " + "(<int|long|time> windowTime (and <int|long> startTime), but found " + attributeExpressionExecutors.length + " input attributes");
}
List<Attribute> attributeList = new ArrayList<Attribute>();
attributeList.add(new Attribute(EXPIRY_TIME_STAMP, Attribute.Type.LONG));
return attributeList;
}
use of org.wso2.siddhi.query.api.expression.constant.Constant in project siddhi by wso2.
the class ExpressionParser method parseExpression.
/**
* Parse the given expression and create the appropriate Executor by recursively traversing the expression
*
* @param expression Expression to be parsed
* @param metaEvent Meta Event
* @param currentState Current state number
* @param tableMap Event Table Map
* @param executorList List to hold VariableExpressionExecutors to update after query parsing @return
* @param siddhiAppContext SiddhiAppContext
* @param groupBy is for groupBy expression
* @param defaultStreamEventIndex Default StreamEvent Index
* @param queryName query name of expression belongs to.
* @return ExpressionExecutor
*/
public static ExpressionExecutor parseExpression(Expression expression, MetaComplexEvent metaEvent, int currentState, Map<String, Table> tableMap, List<VariableExpressionExecutor> executorList, SiddhiAppContext siddhiAppContext, boolean groupBy, int defaultStreamEventIndex, String queryName) {
try {
if (expression instanceof And) {
return new AndConditionExpressionExecutor(parseExpression(((And) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((And) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
} else if (expression instanceof Or) {
return new OrConditionExpressionExecutor(parseExpression(((Or) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Or) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
} else if (expression instanceof Not) {
return new NotConditionExpressionExecutor(parseExpression(((Not) expression).getExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
} else if (expression instanceof Compare) {
if (((Compare) expression).getOperator() == Compare.Operator.EQUAL) {
return parseEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
} else if (((Compare) expression).getOperator() == Compare.Operator.NOT_EQUAL) {
return parseNotEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
} else if (((Compare) expression).getOperator() == Compare.Operator.GREATER_THAN) {
return parseGreaterThanCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
} else if (((Compare) expression).getOperator() == Compare.Operator.GREATER_THAN_EQUAL) {
return parseGreaterThanEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
} else if (((Compare) expression).getOperator() == Compare.Operator.LESS_THAN) {
return parseLessThanCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
} else if (((Compare) expression).getOperator() == Compare.Operator.LESS_THAN_EQUAL) {
return parseLessThanEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
}
} else if (expression instanceof Constant) {
if (expression instanceof BoolConstant) {
return new ConstantExpressionExecutor(((BoolConstant) expression).getValue(), Attribute.Type.BOOL);
} else if (expression instanceof StringConstant) {
return new ConstantExpressionExecutor(((StringConstant) expression).getValue(), Attribute.Type.STRING);
} else if (expression instanceof IntConstant) {
return new ConstantExpressionExecutor(((IntConstant) expression).getValue(), Attribute.Type.INT);
} else if (expression instanceof LongConstant) {
return new ConstantExpressionExecutor(((LongConstant) expression).getValue(), Attribute.Type.LONG);
} else if (expression instanceof FloatConstant) {
return new ConstantExpressionExecutor(((FloatConstant) expression).getValue(), Attribute.Type.FLOAT);
} else if (expression instanceof DoubleConstant) {
return new ConstantExpressionExecutor(((DoubleConstant) expression).getValue(), Attribute.Type.DOUBLE);
}
} else if (expression instanceof Variable) {
return parseVariable((Variable) expression, metaEvent, currentState, executorList, defaultStreamEventIndex);
} else if (expression instanceof Multiply) {
ExpressionExecutor left = parseExpression(((Multiply) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
ExpressionExecutor right = parseExpression(((Multiply) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
Attribute.Type type = parseArithmeticOperationResultType(left, right);
switch(type) {
case INT:
return new MultiplyExpressionExecutorInt(left, right);
case LONG:
return new MultiplyExpressionExecutorLong(left, right);
case FLOAT:
return new MultiplyExpressionExecutorFloat(left, right);
case DOUBLE:
return new MultiplyExpressionExecutorDouble(left, right);
// Will not happen. Handled in parseArithmeticOperationResultType()
default:
}
} else if (expression instanceof Add) {
ExpressionExecutor left = parseExpression(((Add) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
ExpressionExecutor right = parseExpression(((Add) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
Attribute.Type type = parseArithmeticOperationResultType(left, right);
switch(type) {
case INT:
return new AddExpressionExecutorInt(left, right);
case LONG:
return new AddExpressionExecutorLong(left, right);
case FLOAT:
return new AddExpressionExecutorFloat(left, right);
case DOUBLE:
return new AddExpressionExecutorDouble(left, right);
// Will not happen. Handled in parseArithmeticOperationResultType()
default:
}
} else if (expression instanceof Subtract) {
ExpressionExecutor left = parseExpression(((Subtract) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
ExpressionExecutor right = parseExpression(((Subtract) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
Attribute.Type type = parseArithmeticOperationResultType(left, right);
switch(type) {
case INT:
return new SubtractExpressionExecutorInt(left, right);
case LONG:
return new SubtractExpressionExecutorLong(left, right);
case FLOAT:
return new SubtractExpressionExecutorFloat(left, right);
case DOUBLE:
return new SubtractExpressionExecutorDouble(left, right);
// Will not happen. Handled in parseArithmeticOperationResultType()
default:
}
} else if (expression instanceof Mod) {
ExpressionExecutor left = parseExpression(((Mod) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
ExpressionExecutor right = parseExpression(((Mod) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
Attribute.Type type = parseArithmeticOperationResultType(left, right);
switch(type) {
case INT:
return new ModExpressionExecutorInt(left, right);
case LONG:
return new ModExpressionExecutorLong(left, right);
case FLOAT:
return new ModExpressionExecutorFloat(left, right);
case DOUBLE:
return new ModExpressionExecutorDouble(left, right);
// Will not happen. Handled in parseArithmeticOperationResultType()
default:
}
} else if (expression instanceof Divide) {
ExpressionExecutor left = parseExpression(((Divide) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
ExpressionExecutor right = parseExpression(((Divide) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
Attribute.Type type = parseArithmeticOperationResultType(left, right);
switch(type) {
case INT:
return new DivideExpressionExecutorInt(left, right);
case LONG:
return new DivideExpressionExecutorLong(left, right);
case FLOAT:
return new DivideExpressionExecutorFloat(left, right);
case DOUBLE:
return new DivideExpressionExecutorDouble(left, right);
// Will not happen. Handled in parseArithmeticOperationResultType()
default:
}
} else if (expression instanceof AttributeFunction) {
// extensions
Object executor;
try {
if ((siddhiAppContext.isFunctionExist(((AttributeFunction) expression).getName())) && (((AttributeFunction) expression).getNamespace()).isEmpty()) {
executor = new ScriptFunctionExecutor(((AttributeFunction) expression).getName());
} else {
executor = SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiAppContext));
}
} catch (SiddhiAppCreationException ex) {
try {
executor = SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, AttributeAggregatorExtensionHolder.getInstance(siddhiAppContext));
} catch (SiddhiAppCreationException e) {
throw new SiddhiAppCreationException("'" + ((AttributeFunction) expression).getName() + "' is" + " neither a function extension nor an aggregated attribute extension", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
}
}
ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((AttributeFunction) expression).getNamespace(), ((AttributeFunction) expression).getName());
if (executor instanceof FunctionExecutor) {
FunctionExecutor expressionExecutor = (FunctionExecutor) executor;
Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
ExpressionExecutor[] innerExpressionExecutors = parseInnerExpression(innerExpressions, metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
expressionExecutor.initExecutor(innerExpressionExecutors, siddhiAppContext, queryName, configReader);
if (expressionExecutor.getReturnType() == Attribute.Type.BOOL) {
return new BoolConditionExpressionExecutor(expressionExecutor);
}
return expressionExecutor;
} else {
AttributeAggregator attributeAggregator = (AttributeAggregator) executor;
Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
ExpressionExecutor[] innerExpressionExecutors = parseInnerExpression(innerExpressions, metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
attributeAggregator.initAggregator(innerExpressionExecutors, siddhiAppContext, configReader);
AbstractAggregationAttributeExecutor aggregationAttributeProcessor;
if (groupBy) {
aggregationAttributeProcessor = new GroupByAggregationAttributeExecutor(attributeAggregator, innerExpressionExecutors, configReader, siddhiAppContext, queryName);
} else {
aggregationAttributeProcessor = new AggregationAttributeExecutor(attributeAggregator, innerExpressionExecutors, siddhiAppContext, queryName);
}
SelectorParser.getContainsAggregatorThreadLocal().set("true");
return aggregationAttributeProcessor;
}
} else if (expression instanceof In) {
Table table = tableMap.get(((In) expression).getSourceId());
MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaEvent, defaultStreamEventIndex, table.getTableDefinition(), defaultStreamEventIndex);
CompiledCondition compiledCondition = table.compileCondition(((In) expression).getExpression(), matchingMetaInfoHolder, siddhiAppContext, executorList, tableMap, queryName);
return new InConditionExpressionExecutor(table, compiledCondition, matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length, metaEvent instanceof StateEvent, 0);
} else if (expression instanceof IsNull) {
IsNull isNull = (IsNull) expression;
if (isNull.getExpression() != null) {
ExpressionExecutor innerExpressionExecutor = parseExpression(isNull.getExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
return new IsNullConditionExpressionExecutor(innerExpressionExecutor);
} else {
String streamId = isNull.getStreamId();
Integer streamIndex = isNull.getStreamIndex();
if (metaEvent instanceof MetaStateEvent) {
int[] eventPosition = new int[2];
if (streamIndex != null) {
if (streamIndex <= LAST) {
eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex + 1;
} else {
eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex;
}
} else {
eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = defaultStreamEventIndex;
}
eventPosition[STREAM_EVENT_CHAIN_INDEX] = UNKNOWN_STATE;
MetaStateEvent metaStateEvent = (MetaStateEvent) metaEvent;
if (streamId == null) {
throw new SiddhiAppCreationException("IsNull does not support streamId being null", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
} else {
MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
AbstractDefinition definition = metaStreamEvent.getLastInputDefinition();
if (metaStreamEvent.getInputReferenceId() == null) {
if (definition.getId().equals(streamId)) {
eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
break;
}
} else {
if (metaStreamEvent.getInputReferenceId().equals(streamId)) {
eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
if (currentState > -1 && metaStreamEvents[currentState].getInputReferenceId() != null && streamIndex != null && streamIndex <= LAST) {
if (streamId.equals(metaStreamEvents[currentState].getInputReferenceId())) {
eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex;
}
}
break;
}
}
}
}
return new IsNullStreamConditionExpressionExecutor(eventPosition);
} else {
return new IsNullStreamConditionExpressionExecutor(null);
}
}
}
throw new UnsupportedOperationException(expression.toString() + " not supported!");
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, expression, siddhiAppContext);
throw t;
}
}
use of org.wso2.siddhi.query.api.expression.constant.Constant 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.api.expression.constant.Constant in project siddhi by wso2.
the class PlaybackTestCase method playbackTest10.
@Test(expectedExceptions = SiddhiParserException.class)
public void playbackTest10() throws InterruptedException {
log.info("Playback Test 10: Testing playback with invalid idle.time time constant");
SiddhiManager siddhiManager = new SiddhiManager();
String cseEventStream = "" + "@app:playback(idle.time = '', increment = '2 sec') " + "define stream cseEventStream (symbol string, price float, volume int);";
String query = "" + "@info(name = 'query1') " + "from cseEventStream#window.time(2 sec) " + "select symbol,price,volume " + "insert all events into outputStream ;";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
}
Aggregations