Search in sources :

Example 36 with Attribute

use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.

the class SumIncrementalAttributeAggregator method init.

@Override
public void init(String attributeName, Attribute.Type attributeType) {
    Attribute sum;
    Expression sumInitialValue;
    if (attributeName == null) {
        throw new SiddhiAppCreationException("Sum incremental attribute aggregation cannot be executed " + "when no parameters are given");
    }
    if (attributeType.equals(Attribute.Type.FLOAT) || attributeType.equals(Attribute.Type.DOUBLE)) {
        sum = new Attribute("AGG_SUM_".concat(attributeName), Attribute.Type.DOUBLE);
        sumInitialValue = Expression.function("convert", Expression.variable(attributeName), Expression.value("double"));
        returnType = Attribute.Type.DOUBLE;
    } else if (attributeType.equals(Attribute.Type.INT) || attributeType.equals(Attribute.Type.LONG)) {
        sum = new Attribute("AGG_SUM_".concat(attributeName), Attribute.Type.LONG);
        sumInitialValue = Expression.function("convert", Expression.variable(attributeName), Expression.value("long"));
        returnType = Attribute.Type.LONG;
    } else {
        throw new SiddhiAppRuntimeException("Sum aggregation cannot be executed on attribute type " + attributeType.toString());
    }
    this.baseAttributes = new Attribute[] { sum };
    // Original attribute names
    this.baseAttributesInitialValues = new Expression[] { sumInitialValue };
    assert baseAttributes.length == baseAttributesInitialValues.length;
}
Also used : Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) ReturnAttribute(org.ballerinalang.siddhi.annotation.ReturnAttribute) Expression(org.ballerinalang.siddhi.query.api.expression.Expression) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) SiddhiAppRuntimeException(org.ballerinalang.siddhi.core.exception.SiddhiAppRuntimeException)

Example 37 with Attribute

use of org.ballerinalang.siddhi.query.api.definition.Attribute in project ballerina by ballerina-lang.

the class CountIncrementalAttributeAggregator method init.

@Override
public void init(String attributeName, Attribute.Type attributeType) {
    Attribute count;
    Expression countInitialValue;
    // Since we set the initial value of count, we can simply set it as long
    // However, since count is summed internally (in avg incremental calculation),
    // ensure that either double or long is used here (since return value of sum is long or
    // double. Long is chosen here)
    count = new Attribute("AGG_COUNT", Attribute.Type.LONG);
    countInitialValue = Expression.value(1L);
    this.baseAttributes = new Attribute[] { count };
    this.baseAttributesInitialValues = new Expression[] { countInitialValue };
    assert baseAttributes.length == baseAttributesInitialValues.length;
}
Also used : Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) ReturnAttribute(org.ballerinalang.siddhi.annotation.ReturnAttribute) Expression(org.ballerinalang.siddhi.query.api.expression.Expression)

Aggregations

Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)37 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)19 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)15 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)14 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)13 ArrayList (java.util.ArrayList)12 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)11 Expression (org.ballerinalang.siddhi.query.api.expression.Expression)11 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)10 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)9 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)8 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)8 OutputAttribute (org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute)8 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)7 Test (org.testng.annotations.Test)7 HashMap (java.util.HashMap)6 ReturnAttribute (org.ballerinalang.siddhi.annotation.ReturnAttribute)5 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)5 ZeroStreamEventConverter (org.ballerinalang.siddhi.core.event.stream.converter.ZeroStreamEventConverter)5 AndConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor)5