use of io.siddhi.query.api.exception.SiddhiAppValidationException 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);
siddhiAppContext.setSiddhiApp(siddhiApp);
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.setRootMetricsLevel(Level.BASIC);
} 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.setRootMetricsLevel(Level.BASIC);
}
}
Element statStateIncludeElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_STATISTICS, SiddhiConstants.ANNOTATION_ELEMENT_INCLUDE, siddhiApp.getAnnotations());
siddhiAppContext.setIncludedMetrics(io.siddhi.core.util.parser.helper.AnnotationHelper.generateIncludedMetrics(statStateIncludeElement));
Element transportCreationEnabledElement = AnnotationHelper.getAnnotationElement(SiddhiConstants.TRANSPORT_CHANNEL_CREATION_IDENTIFIER, null, siddhiApp.getAnnotations());
if (transportCreationEnabledElement == null) {
siddhiAppContext.setTransportChannelCreationEnabled(true);
} else {
siddhiAppContext.setTransportChannelCreationEnabled(Boolean.valueOf(transportCreationEnabledElement.getValue()));
}
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;
TimestampGenerator timestampGenerator = new TimestampGeneratorImpl(siddhiAppContext);
// 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 TimestampGeneratorImpl(siddhiAppContext));
}
siddhiAppContext.setSnapshotService(new SnapshotService(siddhiAppContext));
siddhiAppContext.setIdGenerator(new IdGenerator());
} 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);
// todo fix for query API usecase
List<String> findExecutedElements = getFindExecutedElements(siddhiApp);
for (Window window : siddhiAppRuntimeBuilder.getWindowMap().values()) {
try {
window.init(siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getWindowMap(), window.getWindowDefinition().getId(), findExecutedElements.contains(window.getWindowDefinition().getId()));
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, window.getWindowDefinition(), siddhiAppContext);
throw t;
}
}
int queryIndex = 1;
int partitionIndex = 1;
for (ExecutionElement executionElement : siddhiApp.getExecutionElementList()) {
if (executionElement instanceof Query) {
try {
QueryRuntimeImpl 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), false, SiddhiConstants.PARTITION_ID_DEFAULT);
siddhiAppRuntimeBuilder.addQuery(queryRuntime);
siddhiAppContext.addEternalReferencedHolder(queryRuntime);
queryIndex++;
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, (Query) executionElement, siddhiAppContext);
throw t;
}
} else {
try {
PartitionRuntimeImpl partitionRuntime = PartitionParser.parse(siddhiAppRuntimeBuilder, (Partition) executionElement, siddhiAppContext, queryIndex, partitionIndex);
siddhiAppRuntimeBuilder.addPartition(partitionRuntime);
queryIndex += ((Partition) executionElement).getQueryList().size();
partitionIndex++;
} 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 io.siddhi.query.api.exception.SiddhiAppValidationException in project siddhi by wso2.
the class SiddhiApp method defineTrigger.
public SiddhiApp defineTrigger(TriggerDefinition triggerDefinition) {
if (triggerDefinition == null) {
throw new SiddhiAppValidationException("Trigger Definition should not be null");
} else if (triggerDefinition.getId() == null) {
throw new SiddhiAppValidationException("Trigger Id should not be null for Trigger Definition", triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
}
StreamDefinition streamDefinition = StreamDefinition.id(triggerDefinition.getId()).attribute(SiddhiConstants.TRIGGERED_TIME, Attribute.Type.LONG);
streamDefinition.setQueryContextStartIndex(triggerDefinition.getQueryContextStartIndex());
streamDefinition.setQueryContextEndIndex(triggerDefinition.getQueryContextEndIndex());
try {
checkDuplicateDefinition(streamDefinition);
} catch (DuplicateDefinitionException e) {
throw new DuplicateDefinitionException("Trigger '" + triggerDefinition.getId() + "' cannot be defined as," + " " + e.getMessageWithOutContext(), e, triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
}
if (triggerDefinitionMap.containsKey(triggerDefinition.getId())) {
throw new DuplicateDefinitionException("Trigger Definition with same Id '" + triggerDefinition.getId() + "' already exist '" + triggerDefinitionMap.get(triggerDefinition.getId()) + "', hence cannot add '" + triggerDefinition + "'", triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
}
this.triggerDefinitionMap.put(triggerDefinition.getId(), triggerDefinition);
this.streamDefinitionMap.put(streamDefinition.getId(), streamDefinition);
return this;
}
use of io.siddhi.query.api.exception.SiddhiAppValidationException in project siddhi by wso2.
the class SiddhiApp method defineFunction.
public SiddhiApp defineFunction(FunctionDefinition functionDefinition) {
if (functionDefinition == null) {
throw new SiddhiAppValidationException("Function Definition should not be null");
} else if (functionDefinition.getId() == null) {
throw new SiddhiAppValidationException("Function Id should not be null for Function Definition", functionDefinition.getQueryContextStartIndex(), functionDefinition.getQueryContextEndIndex());
} else if (functionDefinition.getReturnType() == null) {
throw new SiddhiAppValidationException("Return type should not be null for Function Definition", functionDefinition.getQueryContextStartIndex(), functionDefinition.getQueryContextEndIndex());
} else if (functionDefinition.getBody() == null) {
throw new SiddhiAppValidationException("Body should not be null for Function Definition", functionDefinition.getQueryContextStartIndex(), functionDefinition.getQueryContextEndIndex());
} else if (functionDefinition.getLanguage() == null) {
throw new SiddhiAppValidationException("Language should not be null for Function Definition", functionDefinition.getQueryContextStartIndex(), functionDefinition.getQueryContextEndIndex());
}
checkDuplicateFunctionExist(functionDefinition);
this.functionDefinitionMap.put(functionDefinition.getId(), functionDefinition);
return this;
}
use of io.siddhi.query.api.exception.SiddhiAppValidationException in project siddhi by wso2.
the class SiddhiApp method defineWindow.
public SiddhiApp defineWindow(WindowDefinition windowDefinition) {
if (windowDefinition == null) {
throw new SiddhiAppValidationException("Window Definition should not be null");
} else if (windowDefinition.getId() == null) {
throw new SiddhiAppValidationException("Window Id should not be null for Window Definition", windowDefinition.getQueryContextStartIndex(), windowDefinition.getQueryContextEndIndex());
}
checkDuplicateDefinition(windowDefinition);
this.windowDefinitionMap.put(windowDefinition.getId(), windowDefinition);
return this;
}
use of io.siddhi.query.api.exception.SiddhiAppValidationException in project siddhi by wso2.
the class SiddhiApp method addQuery.
public SiddhiApp addQuery(Query query) {
if (query == null) {
throw new SiddhiAppValidationException("Query should not be null");
}
String name = null;
Element element = AnnotationHelper.getAnnotationElement(SiddhiConstants.ANNOTATION_INFO, SiddhiConstants.ANNOTATION_ELEMENT_NAME, query.getAnnotations());
if (element != null) {
name = element.getValue();
}
if (name != null && executionElementNameList.contains(name)) {
throw new SiddhiAppValidationException("Cannot add Query as another Execution Element already uses " + "its name=" + name, element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
}
executionElementNameList.add(name);
this.executionElementList.add(query);
return this;
}
Aggregations