1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.krms.function;
17
18 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
19 import org.kuali.rice.krms.api.engine.TermResolver;
20 import org.kuali.rice.krms.api.repository.function.FunctionDefinition;
21 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
22
23 import java.util.*;
24
25
26
27
28
29 public abstract class FunctionTermResolver implements TermResolver<Object> {
30 private List<String> orderedInputParams;
31 private String output;
32 private Set<String> parameterNames;
33 private FunctionDefinition functionTerm;
34 public FunctionTermResolver(List<String> orderedInputParams,Set<String> parameterNames,String output){
35 this.orderedInputParams = orderedInputParams;
36 this.parameterNames = parameterNames;
37 this.output = output;
38 }
39
40 public List<String> getOrderedInputParams() {
41 return orderedInputParams;
42 }
43 @Override
44 public Set<String> getPrerequisites() {
45 Set<String> prereqs = new HashSet<String>();
46 for (String param : orderedInputParams) {
47 if(!parameterNames.contains(param)){
48 prereqs.add(param);
49 }
50 }
51 return new HashSet<String>(prereqs);
52 }
53
54
55
56
57 @Override
58 public String getOutput() {
59 return this.output;
60 }
61
62 @Override
63 public Set<String> getParameterNames() {
64 return parameterNames;
65 }
66
67 @Override
68 public int getCost() {
69 return 1;
70 }
71
72
73
74
75
76
77 @Override
78 public Object resolve(Map<String, Object> resolvedPrereqs, Map<String, String> parameters) {
79 String krmsTypeId = getFunctionTerm().getTypeId();
80 String serviceName = null;
81 if(krmsTypeId!=null){
82 KrmsTypeDefinition typeDefinition = KrmsApiServiceLocator.getKrmsTypeRepositoryService().getTypeById(krmsTypeId);
83 serviceName = typeDefinition.getServiceName();
84 }
85 String methodName = getFunctionTerm().getName();
86 Object result = executeFunction(serviceName,methodName,resolvedPrereqs,parameters);
87 return result;
88 }
89
90
91
92
93
94
95 protected abstract Object executeFunction(String serviceName,String methodName,Map<String, Object> resolvedPrereqs,Map<String,String> resolvedParameters);
96
97 protected List<Object> extractParamValues(Map<String, Object> resolvedPrereqs,Map<String,String> resolvedParameters) {
98 List<String> parameters = getOrderedInputParams();
99 List<Object> extractedParams = new ArrayList<Object>();
100 for (String parameter : parameters) {
101 Object paramValue = resolvedPrereqs.get(parameter);
102 if(paramValue==null)
103 paramValue = resolvedParameters.get(parameter);
104 extractedParams.add(paramValue);
105 }
106 return extractedParams;
107 }
108
109
110
111
112
113 public FunctionDefinition getFunctionTerm() {
114 return functionTerm;
115 }
116
117
118
119
120
121 public void setFunctionTerm(FunctionDefinition functionTerm) {
122 this.functionTerm = functionTerm;
123 }
124 }