/* * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.wso2.siddhi.sample.util; import org.wso2.siddhi.core.config.ExecutionPlanContext; import org.wso2.siddhi.core.exception.ExecutionPlanCreationException; import org.wso2.siddhi.core.executor.ExpressionExecutor; import org.wso2.siddhi.core.executor.function.FunctionExecutor; import org.wso2.siddhi.core.util.config.ConfigReader; import org.wso2.siddhi.query.api.definition.Attribute; import java.util.Map; public class CustomFunctionExtension extends FunctionExecutor { private Attribute.Type returnType; /** * The initialization method for FunctionExecutor, this method will be called before the other methods * @param attributeExpressionExecutors are the executors of each function parameters * @param configReader * @param executionPlanContext the context of the execution plan */ @Override protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, ExecutionPlanContext executionPlanContext) { for (ExpressionExecutor expressionExecutor : attributeExpressionExecutors) { Attribute.Type attributeType = expressionExecutor.getReturnType(); if (attributeType == Attribute.Type.DOUBLE) { returnType = attributeType; } else if ((attributeType == Attribute.Type.STRING) || (attributeType == Attribute.Type.BOOL)) { throw new ExecutionPlanCreationException("Plus cannot have parameters with types String or Bool"); } else { returnType = Attribute.Type.LONG; } } } /** * The main execution method which will be called upon event arrival * when there are more then one function parameter * * @param data the runtime values of function parameters * @return the function result */ @Override protected Object execute(Object[] data) { if (returnType == Attribute.Type.DOUBLE) { double total = 0; for (Object aObj : data) { total += Double.parseDouble(String.valueOf(aObj)); } return total; } else { long total = 0; for (Object aObj : data) { total += Long.parseLong(String.valueOf(aObj)); } return total; } } /** * The main execution method which will be called upon event arrival * when there are zero or one function parameter * * @param data null if the function parameter count is zero or * runtime data value of the function parameter * @return the function result */ @Override protected Object execute(Object data) { if (returnType == Attribute.Type.DOUBLE) { return Double.parseDouble(String.valueOf(data)); } else { return Long.parseLong(String.valueOf(data)); } } /** * This will be called only once and this can be used to acquire * required resources for the processing element. * This will be called after initializing the system and before * starting to process the events. */ @Override public void start() { } /** * This will be called only once and this can be used to release * the acquired resources for processing. * This will be called before shutting down the system. */ @Override public void stop() { } @Override public Attribute.Type getReturnType() { return returnType; } /** * Used to collect the serializable state of the processing element, that need to be * persisted for the reconstructing the element to the same state on a different point of time * * @return stateful objects of the processing element as an array */ @Override public Map<String, Object> currentState() { return null; } /** * Used to restore serialized state of the processing element, for reconstructing * the element to the same state as if was on a previous point of time. * * @param state the stateful objects of the element as an array on * the same order provided by currentState(). */ @Override public void restoreState(Map<String, Object> state) { } }