001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krms.framework.engine.expression;
017
018 import java.util.List;
019
020 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
021 import org.kuali.rice.krms.api.engine.expression.ComparisonOperatorService;
022 import org.kuali.rice.krms.framework.engine.Function;
023
024 /**
025 * An implementation of {@link Expression} which invokes a {@link Function} with the results of the invocation of the given
026 * List of {@link Expression}s (of the given {@link ExecutionEnvironment}).
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 *
029 */
030 public final class FunctionExpression implements Expression<Object> {
031
032 private final Function function;
033 private final String [] parameterTypes;
034 private final List<Expression<? extends Object>> parameters;
035 private final ComparisonOperatorService comparisonOperatorService;
036
037 /**
038 * Create a FunctionExpression with the given values.
039 * @param function {@link Function} to be invoked using the invoked results of the given List of {@link Expression}s
040 * @param parameterTypes the full class names for the function's parameter types in sequential order
041 * @param parameters List of {@link Expression}s to be invoked whose results (of the given {@link ExecutionEnvironment})
042 * @param comparisonOperatorService -- TODO:
043 * will be used to invoke the given {@link Function}.
044 */
045 public FunctionExpression(Function function, String[] parameterTypes,
046 List<Expression<? extends Object>> parameters,
047 ComparisonOperatorService comparisonOperatorService) {
048 this.function = function;
049 this.parameterTypes = parameterTypes;
050 this.parameters = parameters;
051 this.comparisonOperatorService = comparisonOperatorService;
052 }
053
054 @Override
055 public Object invoke(ExecutionEnvironment environment) {
056 Object[] argumentValues = new Object[parameters.size()];
057
058 int argValIndex = 0;
059
060 for (Expression<? extends Object> argument : parameters) {
061 Object argumentValue = argument.invoke(environment);
062 String expectedArgumentType = parameterTypes[argValIndex];
063
064 argumentValue = ComparisonOperatorServiceUtils.coerceIfNeeded(argumentValue, expectedArgumentType, comparisonOperatorService);
065
066 argumentValues[argValIndex] = argumentValue;
067 argValIndex += 1;
068 }
069
070 return function.invoke(argumentValues);
071 }
072
073
074 }