Search in sources :

Example 11 with BArrayType

use of org.ballerinalang.model.types.BArrayType in project ballerina by ballerina-lang.

the class StreamingRuntimeManager method addCallback.

public void addCallback(String streamId, BFunctionPointer functionPointer, SiddhiAppRuntime siddhiAppRuntime) {
    BType[] parameters = functionPointer.value().getFunctionInfo().getParamTypes();
    BStructType structType = (BStructType) ((BArrayType) parameters[0]).getElementType();
    if (!(parameters[0] instanceof BArrayType)) {
        throw new BallerinaException("incompatible function: inline function needs to be a function accepting" + " a struct array");
    }
    siddhiAppRuntime.addCallback(streamId, new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            for (Event event : events) {
                AtomicInteger intVarIndex = new AtomicInteger(-1);
                AtomicInteger floatVarIndex = new AtomicInteger(-1);
                AtomicInteger boolVarIndex = new AtomicInteger(-1);
                AtomicInteger stringVarIndex = new AtomicInteger(-1);
                BStruct output = new BStruct(structType);
                for (Object field : event.getData()) {
                    if (field instanceof Long) {
                        output.setIntField(intVarIndex.incrementAndGet(), (Long) field);
                    } else if (field instanceof Double) {
                        output.setFloatField(floatVarIndex.incrementAndGet(), (Double) field);
                    } else if (field instanceof Boolean) {
                        output.setBooleanField(boolVarIndex.incrementAndGet(), (Integer) field);
                    } else if (field instanceof String) {
                        output.setStringField(stringVarIndex.incrementAndGet(), (String) field);
                    }
                }
                BValue[] args = { output };
                BLangFunctions.invokeCallable(functionPointer.value().getFunctionInfo(), args);
            }
        }
    });
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BArrayType(org.ballerinalang.model.types.BArrayType) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) BStructType(org.ballerinalang.model.types.BStructType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BType(org.ballerinalang.model.types.BType) Event(org.ballerinalang.siddhi.core.event.Event) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Aggregations

BArrayType (org.ballerinalang.model.types.BArrayType)11 BType (org.ballerinalang.model.types.BType)7 BMapType (org.ballerinalang.model.types.BMapType)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 ArrayList (java.util.ArrayList)3 BStructType (org.ballerinalang.model.types.BStructType)3 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 BFunctionType (org.ballerinalang.model.types.BFunctionType)2 BJSONType (org.ballerinalang.model.types.BJSONType)2 BStreamType (org.ballerinalang.model.types.BStreamType)2 BTableType (org.ballerinalang.model.types.BTableType)2 BUnionType (org.ballerinalang.model.types.BUnionType)2 TypeTags (org.ballerinalang.model.types.TypeTags)2 BRefType (org.ballerinalang.model.values.BRefType)2 BStruct (org.ballerinalang.model.values.BStruct)2 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1