use of org.wso2.siddhi.query.api.execution.query.input.handler.Filter in project siddhi by wso2.
the class AbsentLogicalPreStateProcessor method processAndReturn.
@Override
public ComplexEventChunk<StateEvent> processAndReturn(ComplexEventChunk complexEventChunk) {
ComplexEventChunk<StateEvent> returnEventChunk = new ComplexEventChunk<>(false);
if (!this.active) {
return returnEventChunk;
}
complexEventChunk.reset();
// Sure only one will be sent
StreamEvent streamEvent = (StreamEvent) complexEventChunk.next();
this.lock.lock();
try {
for (Iterator<StateEvent> iterator = pendingStateEventList.iterator(); iterator.hasNext(); ) {
StateEvent stateEvent = iterator.next();
if (withinStates.size() > 0) {
if (isExpired(stateEvent, streamEvent.getTimestamp())) {
iterator.remove();
continue;
}
}
if (logicalType == LogicalStateElement.Type.OR && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) != null) {
iterator.remove();
continue;
}
StreamEvent currentStreamEvent = stateEvent.getStreamEvent(stateId);
stateEvent.setEvent(stateId, streamEventCloner.copyStreamEvent(streamEvent));
process(stateEvent);
if (waitingTime != -1 || (stateType == StateInputStream.Type.SEQUENCE && logicalType == LogicalStateElement.Type.AND && thisStatePostProcessor.nextEveryStatePerProcessor != null)) {
// Reset to the original state after processing
stateEvent.setEvent(stateId, currentStreamEvent);
}
if (this.thisLastProcessor.isEventReturned()) {
this.thisLastProcessor.clearProcessedEvent();
// The event has passed the filter condition. So remove from being an absent candidate.
iterator.remove();
if (stateType == StateInputStream.Type.SEQUENCE) {
partnerStatePreProcessor.pendingStateEventList.remove(stateEvent);
}
}
if (!stateChanged) {
switch(stateType) {
case PATTERN:
stateEvent.setEvent(stateId, currentStreamEvent);
break;
case SEQUENCE:
stateEvent.setEvent(stateId, currentStreamEvent);
iterator.remove();
break;
}
}
}
} finally {
this.lock.unlock();
}
return returnEventChunk;
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.Filter in project siddhi by wso2.
the class SingleInputStreamParser method generateProcessor.
public static Processor generateProcessor(StreamHandler streamHandler, MetaComplexEvent metaEvent, List<VariableExpressionExecutor> variableExpressionExecutors, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, String queryName) {
Expression[] parameters = streamHandler.getParameters();
MetaStreamEvent metaStreamEvent;
int stateIndex = SiddhiConstants.UNKNOWN_STATE;
if (metaEvent instanceof MetaStateEvent) {
stateIndex = ((MetaStateEvent) metaEvent).getStreamEventCount() - 1;
metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(stateIndex);
} else {
metaStreamEvent = (MetaStreamEvent) metaEvent;
}
ExpressionExecutor[] attributeExpressionExecutors;
if (parameters != null) {
if (parameters.length > 0) {
attributeExpressionExecutors = new ExpressionExecutor[parameters.length];
for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
attributeExpressionExecutors[i] = ExpressionParser.parseExpression(parameters[i], metaEvent, stateIndex, tableMap, variableExpressionExecutors, siddhiAppContext, false, SiddhiConstants.CURRENT, queryName);
}
} else {
List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
int parameterSize = attributeList.size();
attributeExpressionExecutors = new ExpressionExecutor[parameterSize];
for (int i = 0; i < parameterSize; i++) {
attributeExpressionExecutors[i] = ExpressionParser.parseExpression(new Variable(attributeList.get(i).getName()), metaEvent, stateIndex, tableMap, variableExpressionExecutors, siddhiAppContext, false, SiddhiConstants.CURRENT, queryName);
}
}
} else {
attributeExpressionExecutors = new ExpressionExecutor[0];
}
ConfigReader configReader;
if (streamHandler instanceof Filter) {
return new FilterProcessor(attributeExpressionExecutors[0]);
} else if (streamHandler instanceof Window) {
WindowProcessor windowProcessor = (WindowProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, WindowProcessorExtensionHolder.getInstance(siddhiAppContext));
configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((Window) streamHandler).getNamespace(), ((Window) streamHandler).getName());
windowProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler);
return windowProcessor;
} else if (streamHandler instanceof StreamFunction) {
AbstractStreamProcessor abstractStreamProcessor;
configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((StreamFunction) streamHandler).getNamespace(), ((StreamFunction) streamHandler).getName());
if (supportsBatchProcessing) {
try {
abstractStreamProcessor = (StreamProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamProcessorExtensionHolder.getInstance(siddhiAppContext));
metaStreamEvent.addInputDefinition(abstractStreamProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler));
return abstractStreamProcessor;
} catch (SiddhiAppCreationException e) {
if (!e.isClassLoadingIssue()) {
ExceptionUtil.populateQueryContext(e, streamHandler, siddhiAppContext);
throw e;
}
}
}
abstractStreamProcessor = (StreamFunctionProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamFunctionProcessorExtensionHolder.getInstance(siddhiAppContext));
metaStreamEvent.addInputDefinition(abstractStreamProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler));
return abstractStreamProcessor;
} else {
throw new SiddhiAppCreationException(streamHandler.getClass().getName() + " is not supported", streamHandler.getQueryContextStartIndex(), streamHandler.getQueryContextEndIndex());
}
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.Filter in project siddhi by wso2.
the class FilterTestCase2 method testFilterQuery112.
// ****************************************************************************************************************
// Expression Multiply
@Test
public void testFilterQuery112() throws InterruptedException {
log.info("Filter test112");
SiddhiManager siddhiManager = new SiddhiManager();
StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE).attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);
Query query = new Query();
query.from(InputStream.stream("cseEventStream"));
query.annotation(Annotation.annotation("info").element("name", "query1"));
query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("multipliedQuantity", Expression.multiply(Expression.variable("quantity"), Expression.value(4))).select("multipliedPrice", Expression.multiply(Expression.variable("price"), Expression.value(2))).select("multipliedVolume", Expression.multiply(Expression.variable("volume"), Expression.value(3))).select("multipliedAwards", Expression.multiply(Expression.variable("awards"), Expression.value(5))));
query.insertInto("OutputStream");
SiddhiApp siddhiApp = new SiddhiApp("ep1");
siddhiApp.defineStream(cseEventStream);
siddhiApp.addQuery(query);
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[1].toString()));
AssertJUnit.assertTrue("111.0".equals(inEvents[0].getData()[2].toString()));
AssertJUnit.assertTrue("300.0".equals(inEvents[0].getData()[3].toString()));
AssertJUnit.assertTrue("15".equals(inEvents[0].getData()[4].toString()));
count = count + inEvents.length;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "WSO2", 55.5f, 100d, 5, 3L });
Thread.sleep(100);
AssertJUnit.assertEquals(1, count);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.Filter in project siddhi by wso2.
the class FilterTestCase2 method testFilterQuery87.
@Test
public void testFilterQuery87() throws InterruptedException {
log.info("Filter test87");
SiddhiManager siddhiManager = new SiddhiManager();
StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE);
Query query = new Query();
query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"), Compare.Operator.LESS_THAN, Expression.value(50d))));
query.annotation(Annotation.annotation("info").element("name", "query1"));
query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
query.insertInto("outputStream");
SiddhiApp siddhiApp = new SiddhiApp("ep1");
siddhiApp.defineStream(cseEventStream);
siddhiApp.addQuery(query);
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
count = count + inEvents.length;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "WSO2", 50f, 60d });
inputHandler.send(new Object[] { "WSO2", 70f, 40d });
inputHandler.send(new Object[] { "WSO2", 44f, 200d });
Thread.sleep(100);
AssertJUnit.assertEquals(1, count);
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.execution.query.input.handler.Filter in project siddhi by wso2.
the class FilterTestCase2 method testFilterQuery110.
// *****************************************************************************************************************
// Expression-subtract
@Test
public void testFilterQuery110() throws InterruptedException {
log.info("Filter test110");
SiddhiManager siddhiManager = new SiddhiManager();
StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE).attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);
Query query = new Query();
query.from(InputStream.stream("cseEventStream"));
query.annotation(Annotation.annotation("info").element("name", "query1"));
query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("decreasedPrice", Expression.subtract(Expression.variable("price"), Expression.value(20))).select("decreasedVolume", Expression.subtract(Expression.variable("volume"), Expression.value(50))).select("decreasedQuantity", Expression.subtract(Expression.variable("quantity"), Expression.value(4))).select("decreasedAwards", Expression.subtract(Expression.variable("awards"), Expression.value(10))));
query.insertInto("OutputStream");
SiddhiApp siddhiApp = new SiddhiApp("ep1");
siddhiApp.defineStream(cseEventStream);
siddhiApp.addQuery(query);
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("query1", new QueryCallback() {
@Override
public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
EventPrinter.print(timeStamp, inEvents, removeEvents);
AssertJUnit.assertTrue("35.5".equals(inEvents[0].getData()[1].toString()));
AssertJUnit.assertTrue("50.0".equals(inEvents[0].getData()[2].toString()));
AssertJUnit.assertTrue("1".equals(inEvents[0].getData()[3].toString()));
AssertJUnit.assertTrue("0".equals(inEvents[0].getData()[4].toString()));
count = count + inEvents.length;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "WSO2", 55.5f, 100d, 5, 10L });
Thread.sleep(100);
AssertJUnit.assertEquals(1, count);
siddhiAppRuntime.shutdown();
}
Aggregations