1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
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 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
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 | |
|
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 | |
|
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 | |
|
107 | 0 | for (int index = parameters.size() - 1; index >= 0; index--) { |
108 | 0 | FunctionParameterDefinition parameterDefinition = parameters.get(index); |
109 | |
|
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 | |
|
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 | |
} |