use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.
the class DefinitionParserHelper method getAttributeMappings.
private static AttributesHolder getAttributeMappings(Annotation mapAnnotation, String mapType, StreamDefinition streamDefinition) {
List<Annotation> attributeAnnotations = mapAnnotation.getAnnotations(SiddhiConstants.ANNOTATION_ATTRIBUTES);
DefinitionParserHelper.AttributesHolder attributesHolder = new DefinitionParserHelper.AttributesHolder();
if (attributeAnnotations.size() > 0) {
Map<String, String> elementMap = new HashMap<>();
List<String> elementList = new ArrayList<>();
Boolean attributesNameDefined = null;
for (Element element : attributeAnnotations.get(0).getElements()) {
if (element.getKey() == null) {
if (attributesNameDefined != null && attributesNameDefined) {
throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream'" + streamDefinition.getId() + "', some attributes are defined and some are not defined.", element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
}
attributesNameDefined = false;
elementList.add(element.getValue());
} else {
if (attributesNameDefined != null && !attributesNameDefined) {
throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', some attributes are defined and some are not defined.", element.getQueryContextStartIndex(), element.getQueryContextEndIndex());
}
attributesNameDefined = true;
elementMap.put(element.getKey(), element.getValue());
}
}
if (elementMap.size() > 0) {
List<Attribute> attributeList = streamDefinition.getAttributeList();
for (int i = 0, attributeListSize = attributeList.size(); i < attributeListSize; i++) {
Attribute attribute = attributeList.get(i);
String value = elementMap.get(attribute.getName());
if (value == null) {
throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', attribute '" + attribute.getName() + "' is not mapped.", mapAnnotation.getQueryContextStartIndex(), mapAnnotation.getQueryContextEndIndex());
}
assignMapping(attributesHolder, elementMap, i, attribute);
}
} else {
List<Attribute> attributeList = streamDefinition.getAttributeList();
if (elementList.size() != attributeList.size()) {
throw new SiddhiAppCreationException("Error at '" + mapType + "' defined at stream '" + streamDefinition.getId() + "', '" + elementList.size() + "' mapping attributes are " + "provided but expected attributes are '" + attributeList.size() + "'.", mapAnnotation.getQueryContextStartIndex(), mapAnnotation.getQueryContextEndIndex());
} else {
for (int i = 0; i < attributeList.size(); i++) {
Attribute attribute = attributeList.get(i);
String value = elementList.get(i);
elementMap.put(attribute.getName(), value);
}
}
for (int i = 0; i < attributeList.size(); i++) {
Attribute attribute = attributeList.get(i);
assignMapping(attributesHolder, elementMap, i, attribute);
}
}
}
return attributesHolder;
}
use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.
the class DefinitionParserHelper method addTable.
public static void addTable(TableDefinition tableDefinition, ConcurrentMap<String, Table> tableMap, SiddhiAppContext siddhiAppContext) {
if (!tableMap.containsKey(tableDefinition.getId())) {
MetaStreamEvent tableMetaStreamEvent = new MetaStreamEvent();
tableMetaStreamEvent.addInputDefinition(tableDefinition);
for (Attribute attribute : tableDefinition.getAttributeList()) {
tableMetaStreamEvent.addOutputData(attribute);
}
StreamEventFactory tableStreamEventFactory = new StreamEventFactory(tableMetaStreamEvent);
StreamEventCloner tableStreamEventCloner = new StreamEventCloner(tableMetaStreamEvent, tableStreamEventFactory);
Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STORE, tableDefinition.getAnnotations());
Table table;
ConfigReader configReader = null;
RecordTableHandlerManager recordTableHandlerManager = null;
RecordTableHandler recordTableHandler = null;
if (annotation != null) {
annotation = updateAnnotationRef(annotation, SiddhiConstants.NAMESPACE_STORE, siddhiAppContext);
String tableType = annotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
if (tableType == null) {
throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + annotation + "'", annotation, siddhiAppContext);
}
Extension extension = new Extension() {
@Override
public String getNamespace() {
return SiddhiConstants.NAMESPACE_STORE;
}
@Override
public String getName() {
return tableType;
}
};
recordTableHandlerManager = siddhiAppContext.getSiddhiContext().getRecordTableHandlerManager();
if (recordTableHandlerManager != null) {
recordTableHandler = recordTableHandlerManager.generateRecordTableHandler();
}
table = (Table) SiddhiClassLoader.loadExtensionImplementation(extension, TableExtensionHolder.getInstance(siddhiAppContext));
configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
} else {
table = new InMemoryTable();
}
table.initTable(tableDefinition, tableStreamEventFactory, tableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
if (recordTableHandler != null) {
recordTableHandlerManager.registerRecordTableHandler(recordTableHandler.getId(), recordTableHandler);
}
tableMap.putIfAbsent(tableDefinition.getId(), table);
}
}
use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.
the class SiddhiQLBaseVisitorImpl method visitAnnotation.
/**
* {@inheritDoc}
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*
* @param ctx
*/
@Override
public Annotation visitAnnotation(@NotNull SiddhiQLParser.AnnotationContext ctx) {
Annotation annotation = Annotation.annotation((String) visit(ctx.name()));
for (SiddhiQLParser.Annotation_elementContext elementContext : ctx.annotation_element()) {
annotation.element((Element) visit(elementContext));
}
for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
annotation.annotation((Annotation) visit(annotationContext));
}
populateQueryContext(annotation, ctx);
return annotation;
}
use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.
the class DefineStreamTestCase method testAnnotatingStreamDefinition2.
@Test
public void testAnnotatingStreamDefinition2() {
Annotation annotation = AbstractDefinition.annotation("distribute");
StreamDefinition streamDefinition = StreamDefinition.id("StockStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT).attribute("volume", Attribute.Type.FLOAT).annotation(annotation.element("true"));
String annotationString = "[@distribute( \"true\")]";
SiddhiApp.siddhiApp("Test").defineStream(streamDefinition);
AssertJUnit.assertEquals(annotationString, streamDefinition.getAnnotations().toString());
}
use of io.siddhi.query.api.annotation.Annotation in project siddhi by wso2.
the class TestStoreContainingInMemoryTable method init.
@Override
protected void init(TableDefinition tableDefinition, ConfigReader configReader) {
inMemoryTable = new InMemoryTable();
MetaStreamEvent cacheTableMetaStreamEvent = new MetaStreamEvent();
cacheTableMetaStreamEvent.addInputDefinition(tableDefinition);
for (Attribute attribute : tableDefinition.getAttributeList()) {
cacheTableMetaStreamEvent.addOutputData(attribute);
}
StreamEventCloner testTableStreamEventCloner = new StreamEventCloner(cacheTableMetaStreamEvent, storeEventPool);
TableDefinition testStoreContainingIMTableDefinition = TableDefinition.id(tableDefinition.getId());
for (Attribute attribute : tableDefinition.getAttributeList()) {
testStoreContainingIMTableDefinition.attribute(attribute.getName(), attribute.getType());
}
for (Annotation annotation : tableDefinition.getAnnotations()) {
if (!annotation.getName().equalsIgnoreCase("Store")) {
testStoreContainingIMTableDefinition.annotation(annotation);
}
}
inMemoryTable.init(testStoreContainingIMTableDefinition, storeEventPool, testTableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
}
Aggregations