use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class EventTestCase method testConditionExpressionExecutors.
@Test
public void testConditionExpressionExecutors() {
// StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute
// .Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
VariableExpressionExecutor priceVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("price", Attribute.Type.FLOAT), 0, 0);
priceVariableExpressionExecutor.setPosition(new int[] { 0, SiddhiConstants.UNKNOWN_STATE, SiddhiConstants.OUTPUT_DATA_INDEX, 1 });
VariableExpressionExecutor volumeVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("volume", Attribute.Type.INT), 0, 0);
volumeVariableExpressionExecutor.setPosition(new int[] { 0, SiddhiConstants.UNKNOWN_STATE, SiddhiConstants.OUTPUT_DATA_INDEX, 2 });
ExpressionExecutor compareLessThanExecutor = new LessThanCompareConditionExpressionExecutorFloatFloat(new ConstantExpressionExecutor(10f, Attribute.Type.FLOAT), priceVariableExpressionExecutor);
ExpressionExecutor compareGreaterThanExecutor = new GreaterThanCompareConditionExpressionExecutorIntInt(new ConstantExpressionExecutor(10, Attribute.Type.INT), volumeVariableExpressionExecutor);
ExpressionExecutor andExecutor = new AndConditionExpressionExecutor(compareLessThanExecutor, compareGreaterThanExecutor);
int count = 0;
for (int i = 0; i < 3; i++) {
StreamEvent event = new StreamEvent(0, 0, 3);
event.setOutputData(new Object[] { "WSO2", i * 11f, 5 });
if ((Boolean) andExecutor.execute(event)) {
count++;
}
}
AssertJUnit.assertEquals("Two events should pass through executor", 2, count);
}
use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class StreamEventPopulaterFactory method constructEventPopulator.
/**
* Constructs StreamEventPopulater according to MetaStateEvent and to be mapped attributes.
*
* @param metaStreamEvent info for populating the StreamEvent
* @param streamEventChainIndex StreamEvent chain index
* @param attributes mapped attributes
* @return StateEventPopulater
*/
public static ComplexEventPopulater constructEventPopulator(MetaStreamEvent metaStreamEvent, int streamEventChainIndex, List<Attribute> attributes) {
List<StreamMappingElement> streamMappingElements = new ArrayList<StreamMappingElement>();
for (int i = 0, attributesSize = attributes.size(); i < attributesSize; i++) {
Attribute attribute = attributes.get(i);
StreamMappingElement streamMappingElement = new StreamMappingElement();
streamMappingElement.setFromPosition(i);
int index = metaStreamEvent.getOutputData().indexOf(attribute);
if (index > -1) {
streamMappingElement.setToPosition(new int[] { streamEventChainIndex, 0, OUTPUT_DATA_INDEX, index });
} else {
index = metaStreamEvent.getOnAfterWindowData().indexOf(attribute);
if (index > -1) {
streamMappingElement.setToPosition(new int[] { streamEventChainIndex, 0, ON_AFTER_WINDOW_DATA_INDEX, index });
} else {
index = metaStreamEvent.getBeforeWindowData().indexOf(attribute);
if (index > -1) {
streamMappingElement.setToPosition(new int[] { streamEventChainIndex, 0, BEFORE_WINDOW_DATA_INDEX, index });
} else {
streamMappingElement.setToPosition(null);
}
}
}
streamMappingElements.add(streamMappingElement);
}
return new SelectiveComplexEventPopulater(streamMappingElements);
}
use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class AbstractStreamProcessor method initProcessor.
public AbstractDefinition initProcessor(AbstractDefinition inputDefinition, ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext, boolean outputExpectsExpiredEvents, String queryName, SiddhiElement siddhiElement) {
this.configReader = configReader;
this.outputExpectsExpiredEvents = outputExpectsExpiredEvents;
try {
this.inputDefinition = inputDefinition;
this.attributeExpressionExecutors = attributeExpressionExecutors;
this.siddhiAppContext = siddhiAppContext;
this.attributeExpressionLength = attributeExpressionExecutors.length;
this.queryName = queryName;
if (elementId == null) {
elementId = "AbstractStreamProcessor-" + siddhiAppContext.getElementIdGenerator().createNewId();
}
siddhiAppContext.getSnapshotService().addSnapshotable(queryName, this);
this.additionalAttributes = init(inputDefinition, attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents);
siddhiAppContext.addEternalReferencedHolder(this);
StreamDefinition outputDefinition = StreamDefinition.id(inputDefinition.getId());
outputDefinition.setQueryContextStartIndex(siddhiElement.getQueryContextStartIndex());
outputDefinition.setQueryContextEndIndex(siddhiElement.getQueryContextEndIndex());
for (Attribute attribute : inputDefinition.getAttributeList()) {
outputDefinition.attribute(attribute.getName(), attribute.getType());
}
for (Attribute attribute : additionalAttributes) {
outputDefinition.attribute(attribute.getName(), attribute.getType());
}
return outputDefinition;
} catch (Throwable t) {
throw new SiddhiAppCreationException(t);
}
}
use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class PartitionParser method createMetaEventForPartitioner.
/**
* Create metaEvent to be used by StreamPartitioner with output attributes.
*
* @param stateEvent metaStateEvent of the queryRuntime
* @return metaStateEvent
*/
private static MetaStateEvent createMetaEventForPartitioner(MetaComplexEvent stateEvent) {
MetaStateEvent metaStateEvent;
if (stateEvent instanceof MetaStateEvent) {
metaStateEvent = new MetaStateEvent(((MetaStateEvent) stateEvent).getStreamEventCount());
for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) stateEvent).getMetaStreamEvents()) {
AbstractDefinition definition = metaStreamEvent.getLastInputDefinition();
MetaStreamEvent newMetaStreamEvent = new MetaStreamEvent();
for (Attribute attribute : definition.getAttributeList()) {
newMetaStreamEvent.addOutputData(attribute);
}
newMetaStreamEvent.addInputDefinition(definition);
newMetaStreamEvent.setEventType(metaStreamEvent.getEventType());
metaStateEvent.addEvent(newMetaStreamEvent);
}
} else {
metaStateEvent = new MetaStateEvent(1);
AbstractDefinition definition = ((MetaStreamEvent) stateEvent).getLastInputDefinition();
MetaStreamEvent newMetaStreamEvent = new MetaStreamEvent();
for (Attribute attribute : definition.getAttributeList()) {
newMetaStreamEvent.addOutputData(attribute);
}
newMetaStreamEvent.addInputDefinition(definition);
newMetaStreamEvent.setEventType(((MetaStreamEvent) stateEvent).getEventType());
metaStateEvent.addEvent(newMetaStreamEvent);
}
return metaStateEvent;
}
use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.
the class SelectorParser method getAttributeProcessors.
/**
* Method to construct AttributeProcessor list for the selector.
*
* @param selector Selector
* @param id stream id
* @param siddhiAppContext siddhi app context
* @param metaComplexEvent meta ComplexEvent
* @param tableMap Table Map
* @param variableExpressionExecutors list of VariableExpressionExecutors
* @param outputStream
* @return list of AttributeProcessors
*/
private static List<AttributeProcessor> getAttributeProcessors(Selector selector, String id, SiddhiAppContext siddhiAppContext, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, OutputStream outputStream, String queryName, int metaPosition) {
List<AttributeProcessor> attributeProcessorList = new ArrayList<>();
StreamDefinition outputDefinition = StreamDefinition.id(id);
outputDefinition.setQueryContextStartIndex(outputStream.getQueryContextStartIndex());
outputDefinition.setQueryContextEndIndex(outputStream.getQueryContextEndIndex());
List<OutputAttribute> outputAttributes = selector.getSelectionList();
if (selector.getSelectionList().size() == 0) {
if (metaComplexEvent instanceof MetaStreamEvent) {
List<Attribute> attributeList = ((MetaStreamEvent) metaComplexEvent).getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
}
} else {
int position = 0;
for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
if (metaPosition == SiddhiConstants.UNKNOWN_STATE || metaPosition == position) {
List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
for (Attribute attribute : attributeList) {
OutputAttribute outputAttribute = new OutputAttribute(new Variable(attribute.getName()));
if (!outputAttributes.contains(outputAttribute)) {
outputAttributes.add(outputAttribute);
} else {
List<AbstractDefinition> definitions = new ArrayList<>();
for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
definitions.add(aMetaStreamEvent.getLastInputDefinition());
}
throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, outputStream.getQueryContextStartIndex(), outputStream.getQueryContextEndIndex());
}
}
}
++position;
}
}
}
int i = 0;
for (OutputAttribute outputAttribute : outputAttributes) {
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(outputAttribute.getExpression(), metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, !(selector.getGroupByList().isEmpty()), 0, queryName);
if (expressionExecutor instanceof VariableExpressionExecutor) {
// for variables we will directly put
// value at conversion stage
VariableExpressionExecutor executor = ((VariableExpressionExecutor) expressionExecutor);
if (metaComplexEvent instanceof MetaStateEvent) {
((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(new MetaStateEventAttribute(executor.getAttribute(), executor.getPosition()));
} else {
((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(executor.getAttribute());
}
outputDefinition.attribute(outputAttribute.getRename(), ((VariableExpressionExecutor) expressionExecutor).getAttribute().getType());
} else {
// To maintain output variable positions
if (metaComplexEvent instanceof MetaStateEvent) {
((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
} else {
((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
}
AttributeProcessor attributeProcessor = new AttributeProcessor(expressionExecutor);
attributeProcessor.setOutputPosition(i);
attributeProcessorList.add(attributeProcessor);
outputDefinition.attribute(outputAttribute.getRename(), attributeProcessor.getOutputType());
}
i++;
}
metaComplexEvent.setOutputDefinition(outputDefinition);
return attributeProcessorList;
}
Aggregations