Search in sources :

Example 16 with Annotation

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;
}
Also used : HashMap(java.util.HashMap) Attribute(io.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) Element(io.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList) Annotation(io.siddhi.query.api.annotation.Annotation)

Example 17 with Annotation

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);
    }
}
Also used : InMemoryTable(io.siddhi.core.table.InMemoryTable) Table(io.siddhi.core.table.Table) InMemoryTable(io.siddhi.core.table.InMemoryTable) Attribute(io.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) ConfigReader(io.siddhi.core.util.config.ConfigReader) RecordTableHandler(io.siddhi.core.table.record.RecordTableHandler) Annotation(io.siddhi.query.api.annotation.Annotation) Extension(io.siddhi.query.api.extension.Extension) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) RecordTableHandlerManager(io.siddhi.core.table.record.RecordTableHandlerManager) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 18 with Annotation

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;
}
Also used : SiddhiQLParser(io.siddhi.query.compiler.SiddhiQLParser) Annotation(io.siddhi.query.api.annotation.Annotation)

Example 19 with 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());
}
Also used : StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) Annotation(io.siddhi.query.api.annotation.Annotation) Test(org.testng.annotations.Test)

Example 20 with Annotation

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);
}
Also used : InMemoryTable(io.siddhi.core.table.InMemoryTable) Attribute(io.siddhi.query.api.definition.Attribute) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) TableDefinition(io.siddhi.query.api.definition.TableDefinition) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Annotation(io.siddhi.query.api.annotation.Annotation)

Aggregations

Annotation (io.siddhi.query.api.annotation.Annotation)21 Attribute (io.siddhi.query.api.definition.Attribute)10 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)8 Element (io.siddhi.query.api.annotation.Element)8 ArrayList (java.util.ArrayList)7 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)6 TableDefinition (io.siddhi.query.api.definition.TableDefinition)6 HashMap (java.util.HashMap)6 StreamEventCloner (io.siddhi.core.event.stream.StreamEventCloner)5 InMemoryTable (io.siddhi.core.table.InMemoryTable)4 Table (io.siddhi.core.table.Table)4 ConfigReader (io.siddhi.core.util.config.ConfigReader)4 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)4 Extension (io.siddhi.query.api.extension.Extension)4 Map (java.util.Map)4 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)3 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)3 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)3 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)3 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)2