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 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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.engine.TermResolutionEngine;
 28  
 import org.kuali.rice.krms.api.engine.TermSpecification;
 29  
 import org.kuali.rice.krms.api.repository.RepositoryDataException;
 30  
 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
 31  
 import org.kuali.rice.krms.api.repository.function.FunctionParameterDefinition;
 32  
 import org.kuali.rice.krms.api.repository.function.FunctionRepositoryService;
 33  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 34  
 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
 35  
 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
 36  
 import org.kuali.rice.krms.api.repository.term.TermDefinition;
 37  
 import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
 38  
 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
 39  
 import org.kuali.rice.krms.framework.engine.Function;
 40  
 import org.kuali.rice.krms.framework.engine.Proposition;
 41  
 import org.kuali.rice.krms.framework.engine.expression.BinaryOperatorExpression;
 42  
 import org.kuali.rice.krms.framework.engine.expression.BooleanValidatingExpression;
 43  
 import org.kuali.rice.krms.framework.engine.expression.ComparisonOperator;
 44  
 import org.kuali.rice.krms.framework.engine.expression.ConstantExpression;
 45  
 import org.kuali.rice.krms.framework.engine.expression.Expression;
 46  
 import org.kuali.rice.krms.framework.engine.expression.ExpressionBasedProposition;
 47  
 import org.kuali.rice.krms.framework.engine.expression.FunctionExpression;
 48  
 import org.kuali.rice.krms.framework.engine.expression.TermExpression;
 49  
 import org.kuali.rice.krms.framework.type.FunctionTypeService;
 50  
 import org.kuali.rice.krms.framework.type.PropositionTypeService;
 51  
 import org.kuali.rice.krms.impl.repository.TermBoService;
 52  
 import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
 53  
 
 54  
 /**
 55  
  * A default implementation of {@link PropositionTypeService} for propositions
 56  
  * which are composed of terms, operators, and functions.  A simple proposition
 57  
  * is self-contained and has no compound "sub" propositions.  However, it's
 58  
  * behavior is defined by the set of parameters on the {@link PropositionDefinition}.
 59  
  * 
 60  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 61  
  *
 62  
  */
 63  0
 public class SimplePropositionTypeService implements PropositionTypeService {
 64  
 
 65  
         private TermBoService termBoService;
 66  
         private FunctionRepositoryService functionRepositoryService;
 67  
         private KrmsTypeResolver typeResolver;
 68  
         
 69  
         @Override
 70  
         public Proposition loadProposition(PropositionDefinition propositionDefinition) {
 71  0
                 return new ExpressionBasedProposition(translateToExpression(propositionDefinition));
 72  
         }
 73  
 
 74  
         /**
 75  
          * Translates the parameters on the given proposition definition to create an expression for evaluation.
 76  
          * The proposition parameters are defined in a reverse-polish notation so a stack is used for
 77  
          * evaluation purposes.
 78  
          * 
 79  
          * @param propositionDefinition the proposition definition to translate
 80  
          * 
 81  
          * @return the translated expression for the given proposition, this
 82  
          * expression, when evaluated, will return a Boolean.
 83  
          */
 84  
         protected Expression<Boolean> translateToExpression(PropositionDefinition propositionDefinition) {
 85  0
                 LinkedList<Expression<? extends Object>> stack = new LinkedList<Expression<? extends Object>>();
 86  0
                 for (PropositionParameter parameter : propositionDefinition.getParameters()) {
 87  0
                         PropositionParameterType parameterType = PropositionParameterType.fromCode(parameter.getParameterType());
 88  0
                         if (parameterType == PropositionParameterType.CONSTANT) {
 89  
                                 // TODO - need some way to define data type on the prop parameter as well?  Not all constants will actually be String values!!!
 90  0
                                 stack.addFirst(new ConstantExpression<String>(parameter.getValue()));
 91  0
                         } else if (parameterType == PropositionParameterType.FUNCTION) {
 92  0
                                 String functionId = parameter.getValue();
 93  0
                                 FunctionDefinition functionDefinition = functionRepositoryService.getFunction(functionId);
 94  0
                                 if (functionDefinition == null) {
 95  0
                                         throw new RepositoryDataException("Unable to locate function with the given id: " + functionId);
 96  
                                 }
 97  0
                                 FunctionTypeService functionTypeService = typeResolver.getFunctionTypeService(functionDefinition);
 98  0
                                 Function function = functionTypeService.loadFunction(functionDefinition);
 99  
                                 // TODO throw an exception if function is null?
 100  0
                                 List<FunctionParameterDefinition> parameters = functionDefinition.getParameters();
 101  0
                                 if (stack.size() < parameters.size()) {
 102  0
                                         throw new RepositoryDataException("Failed to initialize custom function '" + functionDefinition.getNamespace() + " " + functionDefinition.getName() +
 103  
                                                         "'.  There were only " + stack.size() + " values on the stack but function requires at least " + parameters.size());
 104  
                                 }
 105  0
                                 List<Expression<? extends Object>> arguments = new ArrayList<Expression<? extends Object>>();
 106  
                                 // work backward through the list to match params to the stack
 107  0
                                 for (int index = parameters.size() - 1; index >= 0; index--) {
 108  0
                                         FunctionParameterDefinition parameterDefinition = parameters.get(index);
 109  
                                         // 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?
 110  0
                                         Expression<? extends Object> argument = stack.removeFirst();
 111  0
                                         arguments.add(argument);
 112  
                                 }
 113  0
                                 stack.addFirst(new FunctionExpression(function, arguments));
 114  0
                         } else if (parameterType == PropositionParameterType.OPERATOR) {
 115  0
                                 ComparisonOperator operator = ComparisonOperator.fromCode(parameter.getValue());
 116  0
                                 if (stack.size() < 2) {
 117  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());
 118  
                                 }
 119  0
                                 Expression<? extends Object> rhs = stack.removeFirst();
 120  0
                                 Expression<? extends Object> lhs = stack.removeFirst();
 121  0
                                 stack.addFirst(new BinaryOperatorExpression(operator, lhs, rhs));
 122  0
                         } else if (parameterType == PropositionParameterType.TERM) {
 123  0
                                 String termId = parameter.getValue();
 124  
 
 125  0
                                 TermDefinition termDefinition = termBoService.getTermById(termId);
 126  0
                                 if (termDefinition == null) { throw new RepositoryDataException("unable to load term with id " + termId);}
 127  0
                                 Term term = translateTermDefinition(termDefinition);
 128  
                                 
 129  0
                                 stack.addFirst(new TermExpression(term));
 130  
                         }
 131  0
                 }
 132  0
                 if (stack.size() != 1) {
 133  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());
 134  
                 }
 135  0
                 return new BooleanValidatingExpression(stack.removeFirst());
 136  
         }
 137  
         
 138  
         protected Term translateTermDefinition(TermDefinition termDefinition) {
 139  0
                 if (termDefinition == null) {
 140  0
                         throw new RepositoryDataException("Given TermDefinition is null");
 141  
                 }
 142  0
                 TermSpecificationDefinition termSpecificationDefinition = termDefinition.getSpecification();
 143  0
                 if (termSpecificationDefinition == null) { throw new RepositoryDataException("term with id " + termDefinition.getId() + " has a null specification"); } 
 144  
                 
 145  0
                 List<TermParameterDefinition> params = termDefinition.getParameters();
 146  0
                 Map<String,String> paramsMap = new TreeMap<String,String>();
 147  0
                 if (!CollectionUtils.isEmpty(params)) for (TermParameterDefinition param : params) {
 148  0
                         if (StringUtils.isBlank(param.getName())) { 
 149  0
                                 throw new RepositoryDataException("TermParameterDefinition.name may not be blank"); 
 150  
                         }
 151  0
                         paramsMap.put(param.getName(), param.getValue());
 152  
                 }
 153  
                 
 154  0
                 return new Term(new TermSpecification(termSpecificationDefinition.getName(), termSpecificationDefinition.getType()), paramsMap);
 155  
         }
 156  
 
 157  
         /**
 158  
          * @param termBoService the termBoService to set
 159  
          */
 160  
         public void setTermBoService(TermBoService termBoService) {
 161  0
                 this.termBoService = termBoService;
 162  0
         }
 163  
 
 164  
         public void setFunctionRepositoryService(FunctionRepositoryService functionRepositoryService) {
 165  0
                 this.functionRepositoryService = functionRepositoryService;
 166  0
         }
 167  
         
 168  
         public void setTypeResolver(KrmsTypeResolver typeResolver) {
 169  0
                 this.typeResolver = typeResolver;
 170  0
         }
 171  
         
 172  
 }