use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class ExpressionParser method parseInnerExpression.
/**
* Parse the set of inner expression of AttributeFunctionExtensions and handling all (*) cases.
*
* @param innerExpressions InnerExpressions to be parsed
* @param metaEvent Meta Event
* @param currentState Current state number
* @param tableMap Event Table Map
* @param executorList List to hold VariableExpressionExecutors to update after query parsing @return
* @param siddhiAppContext SiddhiAppContext
* @param groupBy is for groupBy expression
* @param defaultStreamEventIndex Default StreamEvent Index
* @return List of expressionExecutors
*/
private static ExpressionExecutor[] parseInnerExpression(Expression[] innerExpressions, MetaComplexEvent metaEvent, int currentState, Map<String, Table> tableMap, List<VariableExpressionExecutor> executorList, SiddhiAppContext siddhiAppContext, boolean groupBy, int defaultStreamEventIndex, String queryName) {
ExpressionExecutor[] innerExpressionExecutors;
if (innerExpressions != null) {
if (innerExpressions.length > 0) {
innerExpressionExecutors = new ExpressionExecutor[innerExpressions.length];
for (int i = 0, innerExpressionsLength = innerExpressions.length; i < innerExpressionsLength; i++) {
innerExpressionExecutors[i] = parseExpression(innerExpressions[i], metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
}
} else {
List<Expression> outputAttributes = new ArrayList<Expression>();
if (metaEvent instanceof MetaStreamEvent) {
List<Attribute> attributeList = ((MetaStreamEvent) metaEvent).getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
outputAttributes.add(new Variable(attribute.getName()));
}
} else {
for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaEvent).getMetaStreamEvents()) {
List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
Expression outputAttribute = new Variable(attribute.getName());
if (!outputAttributes.contains(outputAttribute)) {
outputAttributes.add(outputAttribute);
} else {
List<AbstractDefinition> definitions = new ArrayList<AbstractDefinition>();
for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaEvent).getMetaStreamEvents()) {
definitions.add(aMetaStreamEvent.getLastInputDefinition());
}
throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, attribute.getQueryContextStartIndex(), attribute.getQueryContextEndIndex());
}
}
}
}
innerExpressionExecutors = new ExpressionExecutor[outputAttributes.size()];
for (int i = 0, innerExpressionsLength = outputAttributes.size(); i < innerExpressionsLength; i++) {
innerExpressionExecutors[i] = parseExpression(outputAttributes.get(i), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
}
}
} else {
innerExpressionExecutors = new ExpressionExecutor[0];
}
return innerExpressionExecutors;
}
use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class MatcherParser method constructMatchingMetaStateHolder.
public static MatchingMetaInfoHolder constructMatchingMetaStateHolder(MetaComplexEvent matchingMetaComplexEvent, int defaultStreamEventIndex, AbstractDefinition candsidateDefinition, int currentState) {
int storeEventIndex = 0;
MetaStreamEvent tableStreamEvent = new MetaStreamEvent();
tableStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
tableStreamEvent.addInputDefinition(candsidateDefinition);
for (Attribute attribute : candsidateDefinition.getAttributeList()) {
tableStreamEvent.addOutputData(attribute);
}
MetaStateEvent metaStateEvent = null;
if (matchingMetaComplexEvent instanceof MetaStreamEvent) {
metaStateEvent = new MetaStateEvent(2);
metaStateEvent.addEvent(((MetaStreamEvent) matchingMetaComplexEvent));
metaStateEvent.addEvent(tableStreamEvent);
storeEventIndex = 1;
defaultStreamEventIndex = 0;
if (currentState == UNKNOWN_STATE) {
currentState = defaultStreamEventIndex;
}
} else {
MetaStreamEvent[] metaStreamEvents = ((MetaStateEvent) matchingMetaComplexEvent).getMetaStreamEvents();
// for join
for (; storeEventIndex < metaStreamEvents.length; storeEventIndex++) {
MetaStreamEvent metaStreamEvent = metaStreamEvents[storeEventIndex];
if (storeEventIndex != defaultStreamEventIndex && metaStreamEvent.getLastInputDefinition().equalsIgnoreAnnotations(candsidateDefinition)) {
metaStateEvent = ((MetaStateEvent) matchingMetaComplexEvent);
break;
}
}
if (metaStateEvent == null) {
metaStateEvent = new MetaStateEvent(metaStreamEvents.length + 1);
for (MetaStreamEvent metaStreamEvent : metaStreamEvents) {
metaStateEvent.addEvent(metaStreamEvent);
}
metaStateEvent.addEvent(tableStreamEvent);
storeEventIndex = metaStreamEvents.length;
}
}
return new MatchingMetaInfoHolder(metaStateEvent, defaultStreamEventIndex, storeEventIndex, metaStateEvent.getMetaStreamEvent(defaultStreamEventIndex).getLastInputDefinition(), candsidateDefinition, currentState);
}
use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
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);
}
StreamEventPool tableStreamEventPool = new StreamEventPool(tableMetaStreamEvent, 10);
StreamEventCloner tableStreamEventCloner = new StreamEventCloner(tableMetaStreamEvent, tableStreamEventPool);
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);
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, tableStreamEventPool, tableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
if (recordTableHandler != null) {
recordTableHandlerManager.registerRecordTableHandler(recordTableHandler.getElementId(), recordTableHandler);
}
tableMap.putIfAbsent(tableDefinition.getId(), table);
}
}
use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class Window method init.
/**
* Initialize the WindowEvent table by creating {@link WindowProcessor} to handle the events.
*
* @param tableMap map of {@link Table}s
* @param eventWindowMap map of EventWindows
* @param queryName name of the query window belongs to.
*/
public void init(Map<String, Table> tableMap, Map<String, Window> eventWindowMap, String queryName) {
if (this.windowProcessor != null) {
return;
}
// Create and initialize MetaStreamEvent
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
metaStreamEvent.addInputDefinition(windowDefinition);
metaStreamEvent.setEventType(MetaStreamEvent.EventType.WINDOW);
metaStreamEvent.initializeAfterWindowData();
for (Attribute attribute : windowDefinition.getAttributeList()) {
metaStreamEvent.addOutputData(attribute);
}
this.streamEventPool = new StreamEventPool(metaStreamEvent, 5);
StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, this.streamEventPool);
OutputStream.OutputEventType outputEventType = windowDefinition.getOutputEventType();
boolean outputExpectsExpiredEvents = outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS;
WindowProcessor internalWindowProcessor = (WindowProcessor) SingleInputStreamParser.generateProcessor(windowDefinition.getWindow(), metaStreamEvent, new ArrayList<VariableExpressionExecutor>(), this.siddhiAppContext, tableMap, false, outputExpectsExpiredEvents, queryName);
internalWindowProcessor.setStreamEventCloner(streamEventCloner);
internalWindowProcessor.constructStreamEventPopulater(metaStreamEvent, 0);
EntryValveProcessor entryValveProcessor = null;
if (internalWindowProcessor instanceof SchedulingProcessor) {
entryValveProcessor = new EntryValveProcessor(this.siddhiAppContext);
Scheduler scheduler = SchedulerParser.parse(this.siddhiAppContext.getScheduledExecutorService(), entryValveProcessor, this.siddhiAppContext);
scheduler.init(this.lockWrapper, queryName);
scheduler.setStreamEventPool(streamEventPool);
((SchedulingProcessor) internalWindowProcessor).setScheduler(scheduler);
}
if (entryValveProcessor != null) {
entryValveProcessor.setToLast(internalWindowProcessor);
this.windowProcessor = entryValveProcessor;
} else {
this.windowProcessor = internalWindowProcessor;
}
// StreamPublishProcessor must be the last in chain so that it can publish the events to StreamJunction
this.windowProcessor.setToLast(new StreamPublishProcessor(outputEventType));
this.internalWindowProcessor = internalWindowProcessor;
}
use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class MinIncrementalAttributeAggregator method init.
@Override
public void init(String attributeName, Attribute.Type attributeType) {
if (attributeName == null) {
throw new SiddhiAppCreationException("Min incremental attribute aggregation cannot be executed " + "when no parameters are given");
}
if (attributeType.equals(Attribute.Type.INT) || attributeType.equals(Attribute.Type.LONG) || attributeType.equals(Attribute.Type.DOUBLE) || attributeType.equals(Attribute.Type.FLOAT)) {
this.baseAttributes = new Attribute[] { new Attribute("AGG_MIN_".concat(attributeName), attributeType) };
this.baseAttributesInitialValues = new Expression[] { Expression.variable(attributeName) };
this.returnType = attributeType;
assert baseAttributes.length == baseAttributesInitialValues.length;
} else {
throw new SiddhiAppRuntimeException("Min aggregation cannot be executed on attribute type " + attributeType.toString());
}
}
Aggregations