use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.
the class DefinitionParserHelper method addEventSink.
public static void addEventSink(StreamDefinition streamDefinition, ConcurrentMap<String, List<Sink>> eventSinkMap, SiddhiAppContext siddhiAppContext) {
for (Annotation sinkAnnotation : streamDefinition.getAnnotations()) {
if (SiddhiConstants.ANNOTATION_SINK.equalsIgnoreCase(sinkAnnotation.getName())) {
sinkAnnotation = updateAnnotationRef(sinkAnnotation, SiddhiConstants.NAMESPACE_SINK, siddhiAppContext);
Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sinkAnnotation.getAnnotations());
if (mapAnnotation == null) {
mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
}
Annotation distributionAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_DISTRIBUTION, sinkAnnotation.getAnnotations());
if (mapAnnotation != null) {
String[] supportedDynamicOptions = null;
List<OptionHolder> destinationOptHolders = new ArrayList<>();
String sinkType = sinkAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
Extension sinkExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, sinkType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
ConfigReader sinkConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sinkExtension.getNamespace(), sinkExtension.getName());
final boolean isDistributedTransport = (distributionAnnotation != null);
boolean isMultiClient = false;
if (isDistributedTransport) {
Sink sink = createSink(sinkExtension, siddhiAppContext);
isMultiClient = isMultiClientDistributedTransport(sink, streamDefinition, distributionAnnotation, siddhiAppContext);
supportedDynamicOptions = sink.getSupportedDynamicOptions();
destinationOptHolders = createDestinationOptionHolders(distributionAnnotation, streamDefinition, sink, siddhiAppContext);
}
final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
if (mapType != null) {
Sink sink;
if (isDistributedTransport) {
sink = (isMultiClient) ? new MultiClientDistributedSink() : new SingleClientDistributedSink();
} else {
sink = createSink(sinkExtension, siddhiAppContext);
}
if (supportedDynamicOptions == null) {
supportedDynamicOptions = sink.getSupportedDynamicOptions();
}
// load output mapper extension
Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK_MAPPER);
ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sinkExtension.getNamespace(), sinkExtension.getName());
SinkMapper sinkMapper = (SinkMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SinkMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
org.wso2.siddhi.annotation.Extension sinkExt = sink.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class);
OptionHolder transportOptionHolder = constructOptionHolder(streamDefinition, sinkAnnotation, sinkExt, supportedDynamicOptions);
OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sinkMapper.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class), sinkMapper.getSupportedDynamicOptions());
List<Element> payloadElementList = getPayload(mapAnnotation);
OptionHolder distributionOptHolder = null;
SinkHandlerManager sinkHandlerManager = siddhiAppContext.getSiddhiContext().getSinkHandlerManager();
SinkHandler sinkHandler = null;
if (sinkHandlerManager != null) {
sinkHandler = sinkHandlerManager.generateSinkHandler();
}
if (isDistributedTransport) {
distributionOptHolder = constructOptionHolder(streamDefinition, distributionAnnotation, sinkExt, supportedDynamicOptions);
String strategyType = distributionOptHolder.validateAndGetStaticValue(SiddhiConstants.DISTRIBUTION_STRATEGY_KEY);
Extension strategyExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, strategyType, sinkAnnotation, SiddhiConstants.NAMESPACE_DISTRIBUTION_STRATEGY);
ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(strategyExtension.getNamespace(), strategyExtension.getName());
DistributionStrategy distributionStrategy = (DistributionStrategy) SiddhiClassLoader.loadExtensionImplementation(strategyExtension, DistributionStrategyExtensionHolder.getInstance(siddhiAppContext));
try {
distributionStrategy.init(streamDefinition, transportOptionHolder, distributionOptHolder, destinationOptHolders, configReader);
((DistributedTransport) sink).init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, siddhiAppContext, destinationOptHolders, sinkAnnotation, distributionStrategy, supportedDynamicOptions);
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
throw t;
}
} else {
try {
sink.init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, siddhiAppContext);
} catch (Throwable t) {
ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
throw t;
}
}
if (sinkHandlerManager != null) {
sinkHandlerManager.registerSinkHandler(sinkHandler.getElementId(), sinkHandler);
siddhiAppContext.getSnapshotService().addSnapshotable(streamDefinition.getId(), sinkHandler);
}
validateSinkMapperCompatibility(streamDefinition, sinkType, mapType, sink, sinkMapper, sinkAnnotation);
// Setting the output group determiner
OutputGroupDeterminer groupDeterminer = constructOutputGroupDeterminer(transportOptionHolder, distributionOptHolder, streamDefinition, destinationOptHolders.size());
if (groupDeterminer != null) {
sink.getMapper().setGroupDeterminer(groupDeterminer);
}
siddhiAppContext.getSnapshotService().addSnapshotable(sink.getStreamDefinition().getId(), sink);
List<Sink> eventSinks = eventSinkMap.get(streamDefinition.getId());
if (eventSinks == null) {
eventSinks = new ArrayList<>();
eventSinks.add(sink);
eventSinkMap.put(streamDefinition.getId(), eventSinks);
} else {
eventSinks.add(sink);
}
}
} else {
throw new SiddhiAppCreationException("Both @sink(type=) and @map(type=) are required.", sinkAnnotation.getQueryContextStartIndex(), sinkAnnotation.getQueryContextEndIndex());
}
}
}
}
use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.
the class SiddhiExtensionLoader method addExtensionToMap.
/**
* Adding extensions to Siddhi siddhiExtensionsMap.
*
* @param extensionClass extension class
* @param siddhiExtensionsMap reference map for the Siddhi extension
*/
private static void addExtensionToMap(Class extensionClass, Map<String, Class> siddhiExtensionsMap) {
Extension siddhiExtensionAnnotation = (Extension) extensionClass.getAnnotation(Extension.class);
if (siddhiExtensionAnnotation != null) {
if (!siddhiExtensionAnnotation.name().isEmpty()) {
Class previousClass = null;
if (!siddhiExtensionAnnotation.namespace().isEmpty()) {
String key = siddhiExtensionAnnotation.namespace() + SiddhiConstants.EXTENSION_SEPARATOR + siddhiExtensionAnnotation.name();
Class existingValue = siddhiExtensionsMap.get(key);
if (existingValue == null) {
previousClass = siddhiExtensionsMap.put(key, extensionClass);
}
if (previousClass != null) {
log.warn("Dropping extension '" + extensionClass + "' as '" + previousClass + "' was already " + "loaded with the same namespace and name '" + siddhiExtensionAnnotation.namespace() + SiddhiConstants.EXTENSION_SEPARATOR + siddhiExtensionAnnotation.name() + "'");
}
} else {
previousClass = siddhiExtensionsMap.put(siddhiExtensionAnnotation.name(), extensionClass);
if (previousClass != null) {
log.warn("Dropping extension '" + extensionClass + "' as '" + previousClass + "' was already " + "loaded with the " + "same name '" + siddhiExtensionAnnotation.name() + "'");
}
}
} else {
log.error("Unable to load extension " + extensionClass.getName() + ", missing Extension annotation.");
}
} else {
log.error("Unable to load extension " + extensionClass.getName() + ", empty name element given in " + "Extension annotation.");
}
}
use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.
the class PartitionTestCase1 method testIsNullStreamConditionCase.
@Test
public void testIsNullStreamConditionCase() throws InterruptedException {
log.info("Partition testIsNullStreamConditionCase");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "@app:name('testIsNullStreamConditionCase') " + "define stream cseEventStream (atr1 string, atr2 string, atr3 int, atr4 double, " + "atr5 long, atr6 long, atr7 double, atr8 float , atr9 bool, atr10 bool, atr11 int);" + "partition with (atr1 of cseEventStream) begin @info(name = 'query1') from " + "cseEventStream[atr5 < 700 AND atr2 is null ] select atr5 as threshold, atr1 as " + "symbol, " + "cast" + "(atr2, 'double') as priceInDouble, sum(atr7) as summedValue insert into " + "OutStockStream ; end ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
count.addAndGet(events.length);
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", null, 100, 101.0, 500L, 200L, 102.0, 75.7f, false, true, 105 });
inputHandler.send(new Object[] { "WSO2", "aa", 100, 101.0, 501L, 201L, 103.0, 76.7f, false, true, 106 });
inputHandler.send(new Object[] { "IBM", null, 100, 102.0, 502L, 202L, 104.0, 77.7f, false, true, 107 });
inputHandler.send(new Object[] { "ORACLE", null, 100, 101.0, 502L, 202L, 104.0, 77.7f, false, false, 108 });
SiddhiTestHelper.waitForEvents(100, 3, count, 60000);
AssertJUnit.assertEquals(3, count.get());
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.
the class PartitionTestCase1 method testPartitionQuery37.
@Test
public void testPartitionQuery37() throws InterruptedException {
log.info("Partition test36");
SiddhiManager siddhiManager = new SiddhiManager();
String siddhiApp = "@app:name('PartitionTest36') " + "define stream cseEventStream (atr1 string, atr2 object, atr3 int, atr4 double, " + "atr5 long, atr6 long, atr7 double, atr8 float , atr9 bool, atr10 bool, atr11 int);" + "partition with (atr1 of cseEventStream) begin @info(name = 'query1') from " + "cseEventStream[atr5 < 700 AND atr10] select atr5 as threshold, atr1 as symbol, cast" + "(atr2, 'double') as priceInDouble, sum(atr7) as summedValue insert into " + "OutStockStream ; end ";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
siddhiAppRuntime.addCallback("OutStockStream", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
count.addAndGet(events.length);
eventArrived = true;
}
});
InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
siddhiAppRuntime.start();
inputHandler.send(new Object[] { "IBM", 75.6, 100, 101.0, 500L, 200L, 102.0, 75.7f, false, true, 105 });
inputHandler.send(new Object[] { "WSO2", 75.6, 100, 101.0, 501L, 201L, 103.0, 76.7f, false, true, 106 });
inputHandler.send(new Object[] { "IBM", 75.6, 100, 102.0, 502L, 202L, 104.0, 77.7f, false, true, 107 });
inputHandler.send(new Object[] { "ORACLE", 75.6, 100, 101.0, 502L, 202L, 104.0, 77.7f, false, false, 108 });
SiddhiTestHelper.waitForEvents(100, 3, count, 60000);
AssertJUnit.assertEquals(3, count.get());
siddhiAppRuntime.shutdown();
}
use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.
the class PatternPartitionTestCase method testPatternPartitionQuery19.
@Test
public void testPatternPartitionQuery19() throws InterruptedException {
log.info("testPatternCount11 - OUT 1");
SiddhiManager siddhiManager = new SiddhiManager();
String streams = "" + "define stream EventStream (symbol string, price float, volume int, quantity int); ";
String partitionStart = "partition with (quantity of EventStream) begin ";
String query = "" + "@info(name = 'query1') " + "from e1 = EventStream [price >= 50 and volume > 100] -> e2 = EventStream [price <= 40] <:5> " + " -> e3 = EventStream [volume <= 70] " + "select e1.symbol as symbol1, e2[last].symbol as symbol2, e3.symbol as symbol3 " + "insert into StockQuote;";
String partitionEnd = "end";
SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd);
siddhiAppRuntime.addCallback("StockQuote", new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
for (Event event : events) {
if (event.isExpired()) {
removeEventCount.incrementAndGet();
} else {
inEventCount.incrementAndGet();
switch(inEventCount.get()) {
case 1:
AssertJUnit.assertArrayEquals(new Object[] { "IBM", null, "GOOG" }, event.getData());
break;
default:
AssertJUnit.assertSame(1, inEventCount.get());
}
}
eventArrived = true;
}
}
});
InputHandler eventStream = siddhiAppRuntime.getInputHandler("EventStream");
siddhiAppRuntime.start();
eventStream.send(new Object[] { "IBM", 75.6f, 105, 4 });
Thread.sleep(100);
eventStream.send(new Object[] { "GOOG", 21f, 61, 4 });
eventStream.send(new Object[] { "WSO2", 21f, 61, 4 });
SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 60000);
AssertJUnit.assertEquals("Number of success events", 1, inEventCount.get());
AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
AssertJUnit.assertEquals("Event arrived", true, eventArrived);
siddhiAppRuntime.shutdown();
}
Aggregations