Search in sources :

Example 16 with VariableExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor in project ballerina by ballerina-lang.

the class SortWindowProcessor method init.

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
    if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
        lengthToKeep = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue()));
    } else {
        throw new UnsupportedOperationException("The first parameter should be an integer");
    }
    parameterInfo = new ArrayList<Object[]>();
    eventComparator = new EventComparator();
    for (int i = 1, parametersLength = attributeExpressionExecutors.length; i < parametersLength; i++) {
        if (!(attributeExpressionExecutors[i] instanceof VariableExpressionExecutor)) {
            throw new UnsupportedOperationException("Required a variable, but found a string parameter");
        } else {
            ExpressionExecutor variableExpressionExecutor = attributeExpressionExecutors[i];
            int order;
            String nextParameter;
            if (i + 1 < parametersLength && attributeExpressionExecutors[i + 1].getReturnType() == Attribute.Type.STRING) {
                nextParameter = (String) ((ConstantExpressionExecutor) attributeExpressionExecutors[i + 1]).getValue();
                if (nextParameter.equalsIgnoreCase(DESC)) {
                    order = -1;
                    i++;
                } else if (nextParameter.equalsIgnoreCase(ASC)) {
                    order = 1;
                    i++;
                } else {
                    throw new UnsupportedOperationException("Parameter string literals should only be \"asc\" or " + "\"desc\"");
                }
            } else {
                // assigning the default order: "asc"
                order = 1;
            }
            parameterInfo.add(new Object[] { variableExpressionExecutor, order });
        }
    }
}
Also used : ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)

Example 17 with VariableExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor in project ballerina by ballerina-lang.

the class EventTestCase method testUpdateMetaEvent.

@Test
public void testUpdateMetaEvent() {
    StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    Attribute price = new Attribute("price", Attribute.Type.FLOAT);
    Attribute volume = new Attribute("volume", Attribute.Type.INT);
    Attribute symbol = new Attribute("symbol", Attribute.Type.STRING);
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addData(volume);
    metaStreamEvent.addData(price);
    metaStreamEvent.addData(symbol);
    metaStreamEvent.initializeAfterWindowData();
    metaStreamEvent.addData(price);
    metaStreamEvent.addOutputData(symbol);
    metaStreamEvent.addOutputData(null);
    MetaStateEvent metaStateEvent = new MetaStateEvent(1);
    metaStateEvent.addEvent(metaStreamEvent);
    VariableExpressionExecutor priceVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("price", Attribute.Type.FLOAT), 0, 0);
    VariableExpressionExecutor volumeVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("volume", Attribute.Type.INT), 0, 0);
    VariableExpressionExecutor symbolVariableExpressionExecutor = new VariableExpressionExecutor(new Attribute("symbol", Attribute.Type.STRING), 0, 0);
    QueryParserHelper.reduceMetaComplexEvent(metaStateEvent);
    QueryParserHelper.updateVariablePosition(metaStateEvent, Arrays.asList(priceVariableExpressionExecutor, volumeVariableExpressionExecutor, symbolVariableExpressionExecutor));
    AssertJUnit.assertEquals(1, metaStreamEvent.getBeforeWindowData().size());
    AssertJUnit.assertEquals(1, metaStreamEvent.getOnAfterWindowData().size());
    AssertJUnit.assertEquals(2, metaStreamEvent.getOutputData().size());
    AssertJUnit.assertArrayEquals(new int[] { 0, 0, 1, 0 }, priceVariableExpressionExecutor.getPosition());
    AssertJUnit.assertArrayEquals(new int[] { 0, 0, 0, 0 }, volumeVariableExpressionExecutor.getPosition());
    AssertJUnit.assertArrayEquals(new int[] { 0, 0, 2, 0 }, symbolVariableExpressionExecutor.getPosition());
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) Test(org.testng.annotations.Test)

Example 18 with VariableExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor in project ballerina by ballerina-lang.

the class EventTestCase method testConditionExpressionExecutorValidation.

@Test(expectedExceptions = OperationNotSupportedException.class)
public void testConditionExpressionExecutorValidation() {
    // StreamDefinition streamDefinition = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute
    // .Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.INT);
    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 });
    ConstantExpressionExecutor constantExpressionExecutor = new ConstantExpressionExecutor(10f, Attribute.Type.FLOAT);
    ExpressionExecutor compareGreaterThanExecutor = new GreaterThanCompareConditionExpressionExecutorIntInt(new ConstantExpressionExecutor(10, Attribute.Type.INT), volumeVariableExpressionExecutor);
    ExpressionExecutor andExecutor = new AndConditionExpressionExecutor(constantExpressionExecutor, compareGreaterThanExecutor);
}
Also used : ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) GreaterThanCompareConditionExpressionExecutorIntInt(org.ballerinalang.siddhi.core.executor.condition.compare.greaterthan.GreaterThanCompareConditionExpressionExecutorIntInt) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) Test(org.testng.annotations.Test)

Example 19 with VariableExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor in project ballerina by ballerina-lang.

the class EventTestCase method testExpressionExecutors.

@Test
public void testExpressionExecutors() {
    // 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 });
    ExpressionExecutor addExecutor = new AddExpressionExecutorFloat(new ConstantExpressionExecutor(10f, Attribute.Type.FLOAT), priceVariableExpressionExecutor);
    StreamEvent event = new StreamEvent(0, 0, 3);
    event.setOutputData(new Object[] { "WSO2", 10f, 5 });
    AssertJUnit.assertEquals("Result of adding should be 20.0", 20f, addExecutor.execute(event));
}
Also used : ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) AddExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorFloat) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) Test(org.testng.annotations.Test)

Example 20 with VariableExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor 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);
}
Also used : LessThanCompareConditionExpressionExecutorFloatFloat(org.ballerinalang.siddhi.core.executor.condition.compare.lessthan.LessThanCompareConditionExpressionExecutorFloatFloat) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) GreaterThanCompareConditionExpressionExecutorIntInt(org.ballerinalang.siddhi.core.executor.condition.compare.greaterthan.GreaterThanCompareConditionExpressionExecutorIntInt) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) Test(org.testng.annotations.Test)

Aggregations

VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)32 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)23 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)15 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)15 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)13 ArrayList (java.util.ArrayList)10 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)10 Expression (org.ballerinalang.siddhi.query.api.expression.Expression)9 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)8 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)8 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)7 HashMap (java.util.HashMap)6 AndConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor)5 OutputAttribute (org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute)5 Map (java.util.Map)4 ConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor)4 Table (org.ballerinalang.siddhi.core.table.Table)4 Test (org.testng.annotations.Test)4 AggregationRuntime (org.ballerinalang.siddhi.core.aggregation.AggregationRuntime)3 SingleStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime)3