use of io.siddhi.core.query.input.stream.single.EntryValveProcessor in project siddhi by wso2.
the class StateInputStreamParser method parse.
private static InnerStateRuntime parse(StateElement stateElement, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, MetaStateEvent metaStateEvent, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, ProcessStreamReceiver> processStreamReceiverMap, StreamPreStateProcessor streamPreStateProcessor, StreamPostStateProcessor streamPostStateProcessor, StateInputStream.Type stateType, boolean multiValue, List<PreStateProcessor> preStateProcessors, boolean isStartState, List<PreStateProcessor> startupPreStateProcessors, SiddhiQueryContext siddhiQueryContext) {
if (stateElement instanceof StreamStateElement) {
BasicSingleInputStream basicSingleInputStream = ((StreamStateElement) stateElement).getBasicSingleInputStream();
SingleStreamRuntime singleStreamRuntime = SingleInputStreamParser.parseInputStream(basicSingleInputStream, variableExpressionExecutors, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, processStreamReceiverMap.get(basicSingleInputStream.getUniqueStreamIds().get(0)), false, false, false, multiValue, siddhiQueryContext);
int stateIndex = metaStateEvent.getStreamEventCount() - 1;
if (streamPreStateProcessor == null) {
if (stateElement instanceof AbsentStreamStateElement) {
AbsentStreamPreStateProcessor absentProcessor = new AbsentStreamPreStateProcessor(stateType, ((AbsentStreamStateElement) stateElement).getWaitingTime().value());
// Set the scheduler
startupPreStateProcessors.add(absentProcessor);
EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiQueryContext.getSiddhiAppContext());
entryValveProcessor.setToLast(absentProcessor);
Scheduler scheduler = SchedulerParser.parse(entryValveProcessor, siddhiQueryContext);
absentProcessor.setScheduler(scheduler);
// Assign the AbsentStreamPreStateProcessor to streamPreStateProcessor
streamPreStateProcessor = absentProcessor;
} else {
streamPreStateProcessor = new StreamPreStateProcessor(stateType);
}
streamPreStateProcessor.init(siddhiQueryContext);
}
streamPreStateProcessor.setStateId(stateIndex);
streamPreStateProcessor.setStartState(isStartState);
streamPreStateProcessor.setNextProcessor(singleStreamRuntime.getProcessorChain());
singleStreamRuntime.setProcessorChain(streamPreStateProcessor);
if (streamPostStateProcessor == null) {
if (stateElement instanceof AbsentStreamStateElement) {
streamPostStateProcessor = new AbsentStreamPostStateProcessor();
} else {
streamPostStateProcessor = new StreamPostStateProcessor();
}
}
streamPostStateProcessor.setStateId(stateIndex);
singleStreamRuntime.getProcessorChain().setToLast(streamPostStateProcessor);
streamPostStateProcessor.setThisStatePreProcessor(streamPreStateProcessor);
streamPreStateProcessor.setThisStatePostProcessor(streamPostStateProcessor);
streamPreStateProcessor.setThisLastProcessor(streamPostStateProcessor);
StreamInnerStateRuntime innerStateRuntime = new StreamInnerStateRuntime(stateType);
innerStateRuntime.setFirstProcessor(streamPreStateProcessor);
innerStateRuntime.setLastProcessor(streamPostStateProcessor);
innerStateRuntime.addStreamRuntime(singleStreamRuntime);
preStateProcessors.add(streamPreStateProcessor);
return innerStateRuntime;
} else if (stateElement instanceof NextStateElement) {
StateElement currentElement = ((NextStateElement) stateElement).getStateElement();
InnerStateRuntime currentInnerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, multiValue, preStateProcessors, isStartState, startupPreStateProcessors, siddhiQueryContext);
StateElement nextElement = ((NextStateElement) stateElement).getNextStateElement();
InnerStateRuntime nextInnerStateRuntime = parse(nextElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, multiValue, preStateProcessors, false, startupPreStateProcessors, siddhiQueryContext);
currentInnerStateRuntime.getLastProcessor().setNextStatePreProcessor(nextInnerStateRuntime.getFirstProcessor());
NextInnerStateRuntime nextStateRuntime = new NextInnerStateRuntime(currentInnerStateRuntime, nextInnerStateRuntime, stateType);
nextStateRuntime.setFirstProcessor(currentInnerStateRuntime.getFirstProcessor());
nextStateRuntime.setLastProcessor(nextInnerStateRuntime.getLastProcessor());
for (SingleStreamRuntime singleStreamRuntime : currentInnerStateRuntime.getSingleStreamRuntimeList()) {
nextStateRuntime.addStreamRuntime(singleStreamRuntime);
}
for (SingleStreamRuntime singleStreamRuntime : nextInnerStateRuntime.getSingleStreamRuntimeList()) {
nextStateRuntime.addStreamRuntime(singleStreamRuntime);
}
return nextStateRuntime;
} else if (stateElement instanceof EveryStateElement) {
StateElement currentElement = ((EveryStateElement) stateElement).getStateElement();
List<PreStateProcessor> withinEveryPreStateProcessors = new ArrayList<>();
InnerStateRuntime innerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, multiValue, withinEveryPreStateProcessors, isStartState, startupPreStateProcessors, siddhiQueryContext);
EveryInnerStateRuntime everyInnerStateRuntime = new EveryInnerStateRuntime(innerStateRuntime, stateType);
everyInnerStateRuntime.setFirstProcessor(innerStateRuntime.getFirstProcessor());
everyInnerStateRuntime.setLastProcessor(innerStateRuntime.getLastProcessor());
for (SingleStreamRuntime singleStreamRuntime : innerStateRuntime.getSingleStreamRuntimeList()) {
everyInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
}
everyInnerStateRuntime.getLastProcessor().setNextEveryStatePreProcessor(everyInnerStateRuntime.getFirstProcessor());
for (PreStateProcessor preStateProcessor : withinEveryPreStateProcessors) {
preStateProcessor.setWithinEveryPreStateProcessor(everyInnerStateRuntime.getFirstProcessor());
}
preStateProcessors.addAll(withinEveryPreStateProcessors);
return everyInnerStateRuntime;
} else if (stateElement instanceof LogicalStateElement) {
LogicalStateElement.Type type = ((LogicalStateElement) stateElement).getType();
LogicalPreStateProcessor logicalPreStateProcessor1;
if (((LogicalStateElement) stateElement).getStreamStateElement1() instanceof AbsentStreamStateElement) {
logicalPreStateProcessor1 = new AbsentLogicalPreStateProcessor(type, stateType, ((AbsentStreamStateElement) ((LogicalStateElement) stateElement).getStreamStateElement1()).getWaitingTime());
// Set the scheduler
startupPreStateProcessors.add(logicalPreStateProcessor1);
EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiQueryContext.getSiddhiAppContext());
entryValveProcessor.setToLast(logicalPreStateProcessor1);
Scheduler scheduler = SchedulerParser.parse(entryValveProcessor, siddhiQueryContext);
((SchedulingProcessor) logicalPreStateProcessor1).setScheduler(scheduler);
} else {
logicalPreStateProcessor1 = new LogicalPreStateProcessor(type, stateType);
}
logicalPreStateProcessor1.init(siddhiQueryContext);
LogicalPostStateProcessor logicalPostStateProcessor1;
if (((LogicalStateElement) stateElement).getStreamStateElement1() instanceof AbsentStreamStateElement) {
logicalPostStateProcessor1 = new AbsentLogicalPostStateProcessor(type);
} else {
logicalPostStateProcessor1 = new LogicalPostStateProcessor(type);
}
LogicalPreStateProcessor logicalPreStateProcessor2;
if (((LogicalStateElement) stateElement).getStreamStateElement2() instanceof AbsentStreamStateElement) {
logicalPreStateProcessor2 = new AbsentLogicalPreStateProcessor(type, stateType, ((AbsentStreamStateElement) ((LogicalStateElement) stateElement).getStreamStateElement2()).getWaitingTime());
startupPreStateProcessors.add(logicalPreStateProcessor2);
EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiQueryContext.getSiddhiAppContext());
entryValveProcessor.setToLast(logicalPreStateProcessor2);
Scheduler scheduler = SchedulerParser.parse(entryValveProcessor, siddhiQueryContext);
((SchedulingProcessor) logicalPreStateProcessor2).setScheduler(scheduler);
} else {
logicalPreStateProcessor2 = new LogicalPreStateProcessor(type, stateType);
}
logicalPreStateProcessor2.init(siddhiQueryContext);
LogicalPostStateProcessor logicalPostStateProcessor2;
if (((LogicalStateElement) stateElement).getStreamStateElement2() instanceof AbsentStreamStateElement) {
logicalPostStateProcessor2 = new AbsentLogicalPostStateProcessor(type);
} else {
logicalPostStateProcessor2 = new LogicalPostStateProcessor(type);
}
logicalPostStateProcessor1.setPartnerPreStateProcessor(logicalPreStateProcessor2);
logicalPostStateProcessor2.setPartnerPreStateProcessor(logicalPreStateProcessor1);
logicalPostStateProcessor1.setPartnerPostStateProcessor(logicalPostStateProcessor2);
logicalPostStateProcessor2.setPartnerPostStateProcessor(logicalPostStateProcessor1);
logicalPreStateProcessor1.setPartnerStatePreProcessor(logicalPreStateProcessor2);
logicalPreStateProcessor2.setPartnerStatePreProcessor(logicalPreStateProcessor1);
StateElement stateElement2 = ((LogicalStateElement) stateElement).getStreamStateElement2();
InnerStateRuntime innerStateRuntime2 = parse(stateElement2, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, variableExpressionExecutors, processStreamReceiverMap, logicalPreStateProcessor2, logicalPostStateProcessor2, stateType, multiValue, preStateProcessors, isStartState, startupPreStateProcessors, siddhiQueryContext);
StateElement stateElement1 = ((LogicalStateElement) stateElement).getStreamStateElement1();
InnerStateRuntime innerStateRuntime1 = parse(stateElement1, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, variableExpressionExecutors, processStreamReceiverMap, logicalPreStateProcessor1, logicalPostStateProcessor1, stateType, multiValue, preStateProcessors, isStartState, startupPreStateProcessors, siddhiQueryContext);
LogicalInnerStateRuntime logicalInnerStateRuntime = new LogicalInnerStateRuntime(innerStateRuntime1, innerStateRuntime2, stateType);
logicalInnerStateRuntime.setFirstProcessor(innerStateRuntime1.getFirstProcessor());
logicalInnerStateRuntime.setLastProcessor(innerStateRuntime2.getLastProcessor());
for (SingleStreamRuntime singleStreamRuntime : innerStateRuntime2.getSingleStreamRuntimeList()) {
logicalInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
}
for (SingleStreamRuntime singleStreamRuntime : innerStateRuntime1.getSingleStreamRuntimeList()) {
logicalInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
}
return logicalInnerStateRuntime;
} else if (stateElement instanceof CountStateElement) {
int minCount = ((CountStateElement) stateElement).getMinCount();
int maxCount = ((CountStateElement) stateElement).getMaxCount();
if (minCount == SiddhiConstants.ANY) {
minCount = 0;
}
if (maxCount == SiddhiConstants.ANY) {
maxCount = Integer.MAX_VALUE;
}
CountPreStateProcessor countPreStateProcessor = new CountPreStateProcessor(minCount, maxCount, stateType);
countPreStateProcessor.init(siddhiQueryContext);
CountPostStateProcessor countPostStateProcessor = new CountPostStateProcessor(minCount, maxCount);
countPreStateProcessor.setCountPostStateProcessor(countPostStateProcessor);
StateElement currentElement = ((CountStateElement) stateElement).getStreamStateElement();
InnerStateRuntime innerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, variableExpressionExecutors, processStreamReceiverMap, countPreStateProcessor, countPostStateProcessor, stateType, true, preStateProcessors, isStartState, startupPreStateProcessors, siddhiQueryContext);
return new CountInnerStateRuntime((StreamInnerStateRuntime) innerStateRuntime);
} else {
throw new OperationNotSupportedException();
}
}
use of io.siddhi.core.query.input.stream.single.EntryValveProcessor in project siddhi by wso2.
the class Window method init.
/**
* Initialize the WindowEvent table by creating {@link WindowProcessor} to handle the events.
*
* @param tableMap map of {@link Table}s
* @param eventWindowMap map of EventWindows
* @param windowName name of the query window belongs to.
* @param findToBeExecuted will find will be executed on the window.
*/
public void init(Map<String, Table> tableMap, Map<String, Window> eventWindowMap, String windowName, boolean findToBeExecuted) {
if (this.windowProcessor != null) {
return;
}
// Create and initialize MetaStreamEvent
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
metaStreamEvent.addInputDefinition(windowDefinition);
metaStreamEvent.setEventType(MetaStreamEvent.EventType.WINDOW);
for (Attribute attribute : windowDefinition.getAttributeList()) {
metaStreamEvent.addOutputData(attribute);
}
this.streamEventFactory = new StreamEventFactory(metaStreamEvent);
StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, this.streamEventFactory);
OutputStream.OutputEventType outputEventType = windowDefinition.getOutputEventType();
boolean outputExpectsExpiredEvents = outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS;
SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext, windowName);
WindowProcessor internalWindowProcessor = (WindowProcessor) SingleInputStreamParser.generateProcessor(windowDefinition.getWindow(), metaStreamEvent, new ArrayList<VariableExpressionExecutor>(), tableMap, false, outputExpectsExpiredEvents, findToBeExecuted, siddhiQueryContext);
internalWindowProcessor.setStreamEventCloner(streamEventCloner);
internalWindowProcessor.constructStreamEventPopulater(metaStreamEvent, 0);
EntryValveProcessor entryValveProcessor = null;
if (internalWindowProcessor instanceof SchedulingProcessor) {
entryValveProcessor = new EntryValveProcessor(this.siddhiAppContext);
Scheduler scheduler = SchedulerParser.parse(entryValveProcessor, siddhiQueryContext);
scheduler.init(this.lockWrapper, windowName);
scheduler.setStreamEventFactory(streamEventFactory);
((SchedulingProcessor) internalWindowProcessor).setScheduler(scheduler);
}
if (entryValveProcessor != null) {
entryValveProcessor.setToLast(internalWindowProcessor);
this.windowProcessor = entryValveProcessor;
} else {
this.windowProcessor = internalWindowProcessor;
}
// StreamPublishProcessor must be the last in chain so that it can publish the events to StreamJunction
this.windowProcessor.setToLast(new StreamPublishProcessor(outputEventType));
this.internalWindowProcessor = internalWindowProcessor;
}
use of io.siddhi.core.query.input.stream.single.EntryValveProcessor in project siddhi by wso2.
the class SingleInputStreamParser method parseInputStream.
/**
* Parse single InputStream and return SingleStreamRuntime
*
* @param inputStream single input stream to be parsed
* @param variableExpressionExecutors List to hold VariableExpressionExecutors to update after query parsing
* @param streamDefinitionMap Stream Definition Map
* @param tableDefinitionMap Table Definition Map
* @param windowDefinitionMap window definition map
* @param aggregationDefinitionMap aggregation definition map
* @param tableMap Table Map
* @param metaComplexEvent MetaComplexEvent
* @param processStreamReceiver ProcessStreamReceiver
* @param supportsBatchProcessing supports batch processing
* @param outputExpectsExpiredEvents is expired events sent as output
* @param findToBeExecuted find will be executed in the steam stores
* @param multiValue event has the possibility to produce multiple values
* @param siddhiQueryContext @return SingleStreamRuntime
*/
public static SingleStreamRuntime parseInputStream(SingleInputStream inputStream, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, MetaComplexEvent metaComplexEvent, ProcessStreamReceiver processStreamReceiver, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, boolean findToBeExecuted, boolean multiValue, SiddhiQueryContext siddhiQueryContext) {
Processor processor = null;
EntryValveProcessor entryValveProcessor = null;
ProcessingMode processingMode = ProcessingMode.BATCH;
boolean first = true;
MetaStreamEvent metaStreamEvent;
if (metaComplexEvent instanceof MetaStateEvent) {
metaStreamEvent = new MetaStreamEvent();
((MetaStateEvent) metaComplexEvent).addEvent(metaStreamEvent);
initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, multiValue, metaStreamEvent);
} else {
metaStreamEvent = (MetaStreamEvent) metaComplexEvent;
initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, multiValue, metaStreamEvent);
}
// A window cannot be defined for a window stream
if (!inputStream.getStreamHandlers().isEmpty() && windowDefinitionMap != null && windowDefinitionMap.containsKey(inputStream.getStreamId())) {
for (StreamHandler handler : inputStream.getStreamHandlers()) {
if (handler instanceof Window) {
throw new OperationNotSupportedException("Cannot create " + ((Window) handler).getName() + " " + "window for the window stream " + inputStream.getStreamId());
}
}
}
if (!inputStream.getStreamHandlers().isEmpty()) {
for (StreamHandler handler : inputStream.getStreamHandlers()) {
Processor currentProcessor = generateProcessor(handler, metaComplexEvent, variableExpressionExecutors, tableMap, supportsBatchProcessing, outputExpectsExpiredEvents, findToBeExecuted, siddhiQueryContext);
if (currentProcessor instanceof SchedulingProcessor) {
if (entryValveProcessor == null) {
entryValveProcessor = new EntryValveProcessor(siddhiQueryContext.getSiddhiAppContext());
if (first) {
processor = entryValveProcessor;
first = false;
} else {
processor.setToLast(entryValveProcessor);
}
}
Scheduler scheduler = SchedulerParser.parse(entryValveProcessor, siddhiQueryContext);
((SchedulingProcessor) currentProcessor).setScheduler(scheduler);
}
if (currentProcessor instanceof AbstractStreamProcessor) {
processingMode = ProcessingMode.findUpdatedProcessingMode(processingMode, ((AbstractStreamProcessor) currentProcessor).getProcessingMode());
}
if (first) {
processor = currentProcessor;
first = false;
} else {
processor.setToLast(currentProcessor);
}
}
}
metaStreamEvent.initializeOnAfterWindowData();
return new SingleStreamRuntime(processStreamReceiver, processor, processingMode, metaComplexEvent);
}
Aggregations