Coverage Report - org.kuali.rice.krms.impl.provider.repository.SimplePropositionTypeService
 
Classes in this File Line Coverage Branch Coverage Complexity
SimplePropositionTypeService
0%
0/57
0%
0/32
5
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krms.impl.provider.repository;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.LinkedList;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import java.util.TreeMap;
 23  
 
 24  
 import org.apache.commons.collections.CollectionUtils;
 25  
 import org.apache.commons.lang.StringUtils;
 26  
 import org.kuali.rice.krms.api.engine.Term;
 27  
 import org.kuali.rice.krms.api.repository.RepositoryDataException;
 28  
 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
 29  
 import org.kuali.rice.krms.api.repository.function.FunctionParameterDefinition;
 30  
 import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;
 31  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 32  
 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
 33  
 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
 34  
 import org.kuali.rice.krms.api.repository.term.TermDefinition;
 35  
 import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
 36  
 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
 37  
 import org.kuali.rice.krms.framework.engine.Function;
 38  
 import org.kuali.rice.krms.framework.engine.Proposition;
 39  
 import org.kuali.rice.krms.framework.engine.expression.BinaryOperatorExpression;
 40  
 import org.kuali.rice.krms.framework.engine.expression.BooleanValidatingExpression;
 41  
 import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
 42  
 import org.kuali.rice.krms.framework.engine.expression.ConstantExpression;
 43  
 import org.kuali.rice.krms.framework.engine.expression.Expression;
 44  
 import org.kuali.rice.krms.framework.engine.expression.ExpressionBasedProposition;
 45  
 import org.kuali.rice.krms.framework.engine.expression.FunctionExpression;
 46  
 import org.kuali.rice.krms.framework.engine.expression.TermExpression;
 47  
 import org.kuali.rice.krms.framework.type.FunctionTypeService;
 48  
 import org.kuali.rice.krms.framework.type.PropositionTypeService;
 49  
 import org.kuali.rice.krms.impl.repository.TermBoService;
 50  
 import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
 51  
 
 52  
 /**
 53  
  * A default implementation of {@link PropositionTypeService} for propositions
 54  
  * which are composed of terms, operators, and functions.  A simple proposition
 55  
  * is self-contained and has no compound "sub" propositions.  However, it's
 56  
  * behavior is defined by the set of parameters on the {@link PropositionDefinition}.
 57  
  * 
 58  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 59  
  *
 60  
  */
 61  0
 public class SimplePropositionTypeService implements PropositionTypeService {
 62  
 
 63  
         private TermBoService termBoService;
 64  
         private FunctionRepositoryService functionRepositoryService;
 65  
         private KrmsTypeResolver typeResolver;
 66  
         
 67  
         @Override
 68  
         public Proposition loadProposition(PropositionDefinition propositionDefinition) {
 69  0
                 return new ExpressionBasedProposition(translateToExpression(propositionDefinition));
 70  
         }
 71  
 
 72  
         /**
 73  
          * Translates the parameters on the given proposition definition to create an expression for evaluation.
 74  
          * The proposition parameters are defined in a reverse-polish notation so a stack is used for
 75  
          * evaluation purposes.
 76  
          * 
 77  
          * @param propositionDefinition the proposition definition to translate
 78  
          * 
 79  
          * @return the translated expression for the given proposition, this
 80  
          * expression, when evaluated, will return a Boolean.
 81  
          */
 82  
         protected Expression<Boolean> translateToExpression(PropositionDefinition propositionDefinition) {
 83  0
                 LinkedList<Expression<? extends Object>> stack = new LinkedList<Expression<? extends Object>>();
 84  0
                 for (PropositionParameter parameter : propositionDefinition.getParameters()) {
 85  0
                         PropositionParameterType parameterType = PropositionParameterType.fromCode(parameter.getParameterType());
 86  0
                         if (parameterType == PropositionParameterType.CONSTANT) {
 87  
                                 // TODO - need some way to define data type on the prop parameter as well?  Not all constants will actually be String values!!!
 88  0
                                 stack.addFirst(new ConstantExpression<String>(parameter.getValue()));
 89  0
                         } else if (parameterType == PropositionParameterType.FUNCTION) {
 90  0
                                 String functionId = parameter.getValue();
 91  0
                                 FunctionDefinition functionDefinition = functionRepositoryService.getFunction(functionId);
 92  0
                                 if (functionDefinition == null) {
 93  0
                                         throw new RepositoryDataException("Unable to locate function with the given id: " + functionId);
 94  
                                 }
 95  0
                                 FunctionTypeService functionTypeService = typeResolver.getFunctionTypeService(functionDefinition);
 96  0
                                 Function function = functionTypeService.loadFunction(functionDefinition);
 97  
                                 // TODO throw an exception if function is null?
 98  0
                                 List<FunctionParameterDefinition> parameters = functionDefinition.getParameters();
 99  0
                                 if (stack.size() < parameters.size()) {
 100  0
                                         throw new RepositoryDataException("Failed to initialize custom function '" + functionDefinition.getNamespace() + " " + functionDefinition.getName() +
 101  
                                                         "'.  There were only " + stack.size() + " values on the stack but function requires at least " + parameters.size());
 102  
                                 }
 103  0
                                 List<Expression<? extends Object>> arguments = new ArrayList<Expression<? extends Object>>();
 104  
                                 // work backward through the list to match params to the stack
 105  0
                                 for (int index = parameters.size() - 1; index >= 0; index--) {
 106  0
                                         FunctionParameterDefinition parameterDefinition = parameters.get(index);
 107  
                                         // TODO need to check types here? expression object probably needs a getType on it so that we can confirm that the types will be compatible?
 108  0
                                         Expression<? extends Object> argument = stack.removeFirst();
 109  0
                                         arguments.add(argument);
 110  
                                 }
 111  0
                                 stack.addFirst(new FunctionExpression(function, arguments));
 112  0
                         } else if (parameterType == PropositionParameterType.OPERATOR) {
 113  0
                                 ComparisonOperator operator = ComparisonOperator.fromCode(parameter.getValue());
 114  0
                                 if (stack.size() < 2) {
 115  0
                                         throw new RepositoryDataException("Failed to initialize expression for comparison operator " + operator + " because a sufficient number of arguments was not available on the stack.  Current contents of stack: " + stack.toString());
 116  
                                 }
 117  0
                                 Expression<? extends Object> rhs = stack.removeFirst();
 118  0
                                 Expression<? extends Object> lhs = stack.removeFirst();
 119  0
                                 stack.addFirst(new BinaryOperatorExpression(operator, lhs, rhs));
 120  0
                         } else if (parameterType == PropositionParameterType.TERM) {
 121  0
                                 String termId = parameter.getValue();
 122  
 
 123  0
                                 TermDefinition termDefinition = termBoService.getTermById(termId);
 124  0
                                 if (termDefinition == null) { throw new RepositoryDataException("unable to load term with id " + termId);}
 125  0
                                 Term term = translateTermDefinition(termDefinition);
 126  
                                 
 127  0
                                 stack.addFirst(new TermExpression(term));
 128  
                         }
 129  0
                 }
 130  0
                 if (stack.size() != 1) {
 131  0
                         throw new RepositoryDataException("Final contents of expression stack are incorrect, there should only be one entry but was " + stack.size() +".  Current contents of stack: " + stack.toString());
 132  
                 }
 133  0
                 return new BooleanValidatingExpression(stack.removeFirst());
 134  
         }
 135  
         
 136  
         protected Term translateTermDefinition(TermDefinition termDefinition) {
 137  0
                 if (termDefinition == null) {
 138  0
                         throw new RepositoryDataException("Given TermDefinition is null");
 139  
                 }
 140  0
                 TermSpecificationDefinition termSpecificationDefinition = termDefinition.getSpecification();
 141  0
                 if (termSpecificationDefinition == null) { throw new RepositoryDataException("term with id " + termDefinition.getId() + " has a null specification"); } 
 142  
                 
 143  0
                 List<TermParameterDefinition> params = termDefinition.getParameters();
 144  0
                 Map<String,String> paramsMap = new TreeMap<String,String>();
 145  0
                 if (!CollectionUtils.isEmpty(params)) for (TermParameterDefinition param : params) {
 146  0
                         if (StringUtils.isBlank(param.getName())) { 
 147  0
                                 throw new RepositoryDataException("TermParameterDefinition.name may not be blank"); 
 148  
                         }
 149  0
                         paramsMap.put(param.getName(), param.getValue());
 150  
                 }
 151  
                 
 152  0
                 return new Term(termSpecificationDefinition.getName(), paramsMap);
 153  
         }
 154  
 
 155  
         /**
 156  
          * @param termBoService the termBoService to set
 157  
          */
 158  
         public void setTermBoService(TermBoService termBoService) {
 159  0
                 this.termBoService = termBoService;
 160  0
         }
 161  
 
 162  
         public void setFunctionRepositoryService(FunctionRepositoryService functionRepositoryService) {
 163  0
                 this.functionRepositoryService = functionRepositoryService;
 164  0
         }
 165  
         
 166  
         public void setTypeResolver(KrmsTypeResolver typeResolver) {
 167  0
                 this.typeResolver = typeResolver;
 168  0
         }
 169  
         
 170  
 }