use of org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream 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, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, ProcessStreamReceiver> processStreamReceiverMap, StreamPreStateProcessor streamPreStateProcessor, StreamPostStateProcessor streamPostStateProcessor, StateInputStream.Type stateType, ArrayList<Map.Entry<Long, Set<Integer>>> withinStates, LatencyTracker latencyTracker, String queryName) {
if (stateElement instanceof StreamStateElement) {
BasicSingleInputStream basicSingleInputStream = ((StreamStateElement) stateElement).getBasicSingleInputStream();
SingleStreamRuntime singleStreamRuntime = SingleInputStreamParser.parseInputStream(basicSingleInputStream, siddhiAppContext, variableExpressionExecutors, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, processStreamReceiverMap.get(basicSingleInputStream.getUniqueStreamIds().get(0)), false, false, queryName);
int stateIndex = metaStateEvent.getStreamEventCount() - 1;
if (streamPreStateProcessor == null) {
if (stateElement.getWithin() != null) {
Set<Integer> withinStateset = new HashSet<Integer>();
withinStateset.add(SiddhiConstants.ANY);
withinStates.add(0, new AbstractMap.SimpleEntry<Long, Set<Integer>>(stateElement.getWithin().getValue(), withinStateset));
}
if (stateElement instanceof AbsentStreamStateElement) {
AbsentStreamPreStateProcessor absentProcessor = new AbsentStreamPreStateProcessor(stateType, clonewithinStates(withinStates), ((AbsentStreamStateElement) stateElement).getWaitingTime());
// Set the scheduler
siddhiAppContext.addEternalReferencedHolder(absentProcessor);
EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
entryValveProcessor.setToLast(absentProcessor);
Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
absentProcessor.setScheduler(scheduler);
// Assign the AbsentStreamPreStateProcessor to streamPreStateProcessor
streamPreStateProcessor = absentProcessor;
} else {
streamPreStateProcessor = new StreamPreStateProcessor(stateType, clonewithinStates(withinStates));
}
streamPreStateProcessor.init(siddhiAppContext, queryName);
if (stateElement.getWithin() != null) {
withinStates.remove(0);
}
}
streamPreStateProcessor.setStateId(stateIndex);
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);
return innerStateRuntime;
} else if (stateElement instanceof NextStateElement) {
StateElement currentElement = ((NextStateElement) stateElement).getStateElement();
InnerStateRuntime currentInnerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, withinStates, latencyTracker, queryName);
if (stateElement.getWithin() != null) {
Set<Integer> withinStateSet = new HashSet<Integer>();
withinStateSet.add(currentInnerStateRuntime.getFirstProcessor().getStateId());
withinStateSet.add(currentInnerStateRuntime.getLastProcessor().getStateId());
withinStates.add(0, new AbstractMap.SimpleEntry<Long, Set<Integer>>(stateElement.getWithin().getValue(), withinStateSet));
}
StateElement nextElement = ((NextStateElement) stateElement).getNextStateElement();
InnerStateRuntime nextInnerStateRuntime = parse(nextElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, withinStates, latencyTracker, queryName);
if (stateElement.getWithin() != null) {
withinStates.remove(0);
}
// currentInnerStateRuntime.getFirstProcessor().getStateId()
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();
InnerStateRuntime innerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, streamPreStateProcessor, streamPostStateProcessor, stateType, withinStates, latencyTracker, queryName);
EveryInnerStateRuntime everyInnerStateRuntime = new EveryInnerStateRuntime(innerStateRuntime, stateType);
everyInnerStateRuntime.setFirstProcessor(innerStateRuntime.getFirstProcessor());
everyInnerStateRuntime.setLastProcessor(innerStateRuntime.getLastProcessor());
for (SingleStreamRuntime singleStreamRuntime : innerStateRuntime.getSingleStreamRuntimeList()) {
everyInnerStateRuntime.addStreamRuntime(singleStreamRuntime);
}
everyInnerStateRuntime.getLastProcessor().setNextEveryStatePerProcessor(everyInnerStateRuntime.getFirstProcessor());
return everyInnerStateRuntime;
} else if (stateElement instanceof LogicalStateElement) {
LogicalStateElement.Type type = ((LogicalStateElement) stateElement).getType();
if (stateElement.getWithin() != null) {
Set<Integer> withinStateset = new HashSet<Integer>();
withinStateset.add(SiddhiConstants.ANY);
withinStates.add(0, new AbstractMap.SimpleEntry<Long, Set<Integer>>(stateElement.getWithin().getValue(), withinStateset));
}
LogicalPreStateProcessor logicalPreStateProcessor1;
if (((LogicalStateElement) stateElement).getStreamStateElement1() instanceof AbsentStreamStateElement) {
logicalPreStateProcessor1 = new AbsentLogicalPreStateProcessor(type, stateType, clonewithinStates(withinStates), ((AbsentStreamStateElement) ((LogicalStateElement) stateElement).getStreamStateElement1()).getWaitingTime());
// Set the scheduler
siddhiAppContext.addEternalReferencedHolder((AbsentLogicalPreStateProcessor) logicalPreStateProcessor1);
EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
entryValveProcessor.setToLast(logicalPreStateProcessor1);
Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
((SchedulingProcessor) logicalPreStateProcessor1).setScheduler(scheduler);
} else {
logicalPreStateProcessor1 = new LogicalPreStateProcessor(type, stateType, clonewithinStates(withinStates));
}
logicalPreStateProcessor1.init(siddhiAppContext, queryName);
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, clonewithinStates(withinStates), ((AbsentStreamStateElement) ((LogicalStateElement) stateElement).getStreamStateElement2()).getWaitingTime());
siddhiAppContext.addEternalReferencedHolder((AbsentLogicalPreStateProcessor) logicalPreStateProcessor2);
EntryValveProcessor entryValveProcessor = new EntryValveProcessor(siddhiAppContext);
entryValveProcessor.setToLast(logicalPreStateProcessor2);
Scheduler scheduler = SchedulerParser.parse(siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, siddhiAppContext);
((SchedulingProcessor) logicalPreStateProcessor2).setScheduler(scheduler);
} else {
logicalPreStateProcessor2 = new LogicalPreStateProcessor(type, stateType, clonewithinStates(withinStates));
}
logicalPreStateProcessor2.init(siddhiAppContext, queryName);
LogicalPostStateProcessor logicalPostStateProcessor2;
if (((LogicalStateElement) stateElement).getStreamStateElement2() instanceof AbsentStreamStateElement) {
logicalPostStateProcessor2 = new AbsentLogicalPostStateProcessor(type);
} else {
logicalPostStateProcessor2 = new LogicalPostStateProcessor(type);
}
if (stateElement.getWithin() != null) {
withinStates.remove(0);
}
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, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, logicalPreStateProcessor2, logicalPostStateProcessor2, stateType, withinStates, latencyTracker, queryName);
StateElement stateElement1 = ((LogicalStateElement) stateElement).getStreamStateElement1();
InnerStateRuntime innerStateRuntime1 = parse(stateElement1, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, logicalPreStateProcessor1, logicalPostStateProcessor1, stateType, withinStates, latencyTracker, queryName);
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;
}
if (stateElement.getWithin() != null) {
Set<Integer> withinStateset = new HashSet<Integer>();
withinStateset.add(SiddhiConstants.ANY);
withinStates.add(0, new AbstractMap.SimpleEntry<Long, Set<Integer>>(stateElement.getWithin().getValue(), withinStateset));
}
CountPreStateProcessor countPreStateProcessor = new CountPreStateProcessor(minCount, maxCount, stateType, withinStates);
countPreStateProcessor.init(siddhiAppContext, queryName);
CountPostStateProcessor countPostStateProcessor = new CountPostStateProcessor(minCount, maxCount);
if (stateElement.getWithin() != null) {
withinStates.remove(0);
}
countPreStateProcessor.setCountPostStateProcessor(countPostStateProcessor);
StateElement currentElement = ((CountStateElement) stateElement).getStreamStateElement();
InnerStateRuntime innerStateRuntime = parse(currentElement, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, metaStateEvent, siddhiAppContext, variableExpressionExecutors, processStreamReceiverMap, countPreStateProcessor, countPostStateProcessor, stateType, withinStates, latencyTracker, queryName);
return new CountInnerStateRuntime((StreamInnerStateRuntime) innerStateRuntime);
} else {
throw new OperationNotSupportedException();
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream in project siddhi by wso2.
the class SiddhiQLBaseVisitorImpl method visitJoin_source.
/**
* {@inheritDoc}
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*
* @param ctx
*/
@Override
public Object visitJoin_source(@NotNull SiddhiQLParser.Join_sourceContext ctx) {
// join_source
// :io (basic_source_stream_handler)* window? (AS alias)?
// ;
Source source = (Source) visit(ctx.source());
String streamAlias = null;
if (ctx.alias() != null) {
streamAlias = (String) visit(ctx.alias());
activeStreams.remove(ctx.source().getText());
activeStreams.add(streamAlias);
}
BasicSingleInputStream basicSingleInputStream = new BasicSingleInputStream(streamAlias, source.streamId, source.isInnerStream);
if (ctx.basic_source_stream_handlers() != null) {
basicSingleInputStream.addStreamHandlers((List<StreamHandler>) visit(ctx.basic_source_stream_handlers()));
}
if (ctx.window() != null) {
SingleInputStream inputStream = new SingleInputStream(basicSingleInputStream, (Window) visit(ctx.window()));
populateQueryContext(inputStream, ctx);
return inputStream;
} else {
populateQueryContext(basicSingleInputStream, ctx);
return basicSingleInputStream;
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream in project siddhi by wso2.
the class SiddhiQLBaseVisitorImpl method visitStandard_stream.
/**
* {@inheritDoc}
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*
* @param ctx
*/
@Override
public SingleInputStream visitStandard_stream(@NotNull SiddhiQLParser.Standard_streamContext ctx) {
// standard_stream
// : io (basic_source_stream_handler)* window? (basic_source_stream_handler)*
// ;
Source source = (Source) visit(ctx.source());
BasicSingleInputStream basicSingleInputStream = new BasicSingleInputStream(null, source.streamId, source.isInnerStream);
if (ctx.pre_window_handlers != null) {
basicSingleInputStream.addStreamHandlers((List<StreamHandler>) visit(ctx.pre_window_handlers));
}
if (ctx.window() == null && ctx.post_window_handlers == null) {
populateQueryContext(basicSingleInputStream, ctx);
return basicSingleInputStream;
} else if (ctx.window() != null) {
SingleInputStream singleInputStream = new SingleInputStream(basicSingleInputStream, (Window) visit(ctx.window()));
if (ctx.post_window_handlers != null) {
singleInputStream.addStreamHandlers((List<StreamHandler>) visit(ctx.post_window_handlers));
}
populateQueryContext(singleInputStream, ctx);
return singleInputStream;
} else {
throw newSiddhiParserException(ctx);
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream in project siddhi by wso2.
the class SiddhiQLBaseVisitorImpl method visitLogical_absent_stateful_source.
@Override
public Object visitLogical_absent_stateful_source(SiddhiQLParser.Logical_absent_stateful_sourceContext ctx) {
// ;
if (ctx.logical_absent_stateful_source() != null) {
return visit(ctx.logical_absent_stateful_source());
} else if (ctx.AND() != null) {
if (ctx.basic_absent_pattern_source().size() == 2) {
StateElement stateElement = State.logicalNotAnd((AbsentStreamStateElement) visit(ctx.basic_absent_pattern_source(0)), (AbsentStreamStateElement) visit(ctx.basic_absent_pattern_source(1)));
populateQueryContext(stateElement, ctx);
return stateElement;
} else {
StreamStateElement presentStreamState = (StreamStateElement) visit(ctx.standard_stateful_source());
AbsentStreamStateElement absentStreamState;
if (!ctx.basic_absent_pattern_source().isEmpty()) {
absentStreamState = (AbsentStreamStateElement) visit(ctx.basic_absent_pattern_source(0));
} else {
absentStreamState = State.logicalNot(new StreamStateElement((BasicSingleInputStream) visit(ctx.basic_source())));
}
StateElement stateElement = State.logicalNotAnd(absentStreamState, presentStreamState);
populateQueryContext(stateElement, ctx);
return stateElement;
}
} else if (ctx.OR() != null) {
if (ctx.basic_absent_pattern_source().size() == 2) {
return State.logicalOr((AbsentStreamStateElement) visit(ctx.basic_absent_pattern_source(0)), (AbsentStreamStateElement) visit(ctx.basic_absent_pattern_source(1)));
} else {
StreamStateElement streamStateElement1 = (StreamStateElement) visit(ctx.standard_stateful_source());
AbsentStreamStateElement streamStateElement2 = (AbsentStreamStateElement) visit(ctx.basic_absent_pattern_source(0));
StateElement stateElement = State.logicalOr(streamStateElement2, streamStateElement1);
populateQueryContext(stateElement, ctx);
return stateElement;
}
} else {
throw newSiddhiParserException(ctx);
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream in project siddhi by wso2.
the class InputStreamParser method parse.
/**
* Parse an InputStream returning corresponding StreamRuntime
*
* @param inputStream input stream to be parsed
* @param siddhiAppContext associated siddhi siddhiAppContext
* @param streamDefinitionMap map containing user given stream definitions
* @param tableDefinitionMap table definition map
* @param windowDefinitionMap window definition map
* @param aggregationDefinitionMap aggregation definition map
* @param tableMap Table Map
* @param windowMap event window map
* @param aggregationMap aggregator map
* @param executors List to hold VariableExpressionExecutors to update after query parsing
* @param latencyTracker latency tracker
* @param outputExpectsExpiredEvents is output expects ExpiredEvents
* @param queryName query name of input stream belongs to.
* @return StreamRuntime
*/
public static StreamRuntime parse(InputStream inputStream, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap, List<VariableExpressionExecutor> executors, LatencyTracker latencyTracker, boolean outputExpectsExpiredEvents, String queryName) {
if (inputStream instanceof BasicSingleInputStream || inputStream instanceof SingleInputStream) {
SingleInputStream singleInputStream = (SingleInputStream) inputStream;
Window window = windowMap.get(singleInputStream.getStreamId());
// If stream is from window, allow batch
boolean batchProcessingAllowed = window != null;
// processing
ProcessStreamReceiver processStreamReceiver = new ProcessStreamReceiver(singleInputStream.getStreamId(), latencyTracker, queryName, siddhiAppContext);
processStreamReceiver.setBatchProcessingAllowed(batchProcessingAllowed);
return SingleInputStreamParser.parseInputStream((SingleInputStream) inputStream, siddhiAppContext, executors, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, new MetaStreamEvent(), processStreamReceiver, true, outputExpectsExpiredEvents, queryName);
} else if (inputStream instanceof JoinInputStream) {
return JoinInputStreamParser.parseInputStream(((JoinInputStream) inputStream), siddhiAppContext, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, executors, latencyTracker, outputExpectsExpiredEvents, queryName);
} else if (inputStream instanceof StateInputStream) {
MetaStateEvent metaStateEvent = new MetaStateEvent(inputStream.getAllStreamIds().size());
return StateInputStreamParser.parseInputStream(((StateInputStream) inputStream), siddhiAppContext, metaStateEvent, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, executors, latencyTracker, queryName);
} else {
throw new OperationNotSupportedException();
}
}
Aggregations