1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.repository.language;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.velocity.exception.VelocityException;
20 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
21 import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
22 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
23 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinitionContract;
24 import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
25 import org.kuali.rice.krms.impl.repository.KrmsTypeRepositoryServiceImpl;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33
34
35
36
37 public class PropositionNaturalLanguageTemplater implements NaturalLanguageTemplaterContract {
38
39
40
41 private final static Logger logger = LoggerFactory.getLogger(PropositionNaturalLanguageTemplater.class);
42
43 private TranslationContextRegistry<TranslationContext> translationContextRegistry;
44
45 private KrmsTypeRepositoryService krmsTypeRepositoryService = new KrmsTypeRepositoryServiceImpl();
46
47
48
49
50 public final static String OPERATOR_TOKEN = "relationalOperator";
51
52
53
54 public final static String CONSTANT_VALUE_TOKEN = "intValue";
55
56
57
58
59 private VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
60
61
62
63
64
65 public PropositionNaturalLanguageTemplater() {
66 }
67
68
69
70
71
72
73
74 public void setTranslationContextRegistry(
75 final TranslationContextRegistry<TranslationContext> translationContextRegistry) {
76 this.translationContextRegistry = translationContextRegistry;
77 }
78
79 public String translate(NaturalLanguageTemplate naturalLanguageTemplate, Map<String, Object> parametersMap) {
80
81 if (naturalLanguageTemplate == null) {
82 return StringUtils.EMPTY;
83 }
84
85 Map<String, Object> contextMap = null;
86 try {
87 contextMap = buildContextMap(naturalLanguageTemplate.getTypeId(), parametersMap);
88 } catch (Exception e) {
89 e.printStackTrace();
90 }
91
92 try {
93 String nl = this.templateEngine.evaluate(contextMap, naturalLanguageTemplate.getTemplate());
94 if (logger.isInfoEnabled()) {
95 logger.info("nl=" + nl);
96 }
97 return nl;
98 } catch (VelocityException e) {
99 String msg = "Generating template for proposition failed: template='" + naturalLanguageTemplate.getTemplate() + "', contextMap=" + contextMap;
100 logger.error(msg, e);
101
102 }
103 return "Error";
104 }
105
106
107
108
109
110
111
112
113 private Map<String, Object> buildContextMap(String typeId, Map<String, Object> parametersMap) throws Exception {
114
115 Map<String, Object> contextMap = new HashMap<String, Object>();
116
117 if (parametersMap.containsKey(PropositionParameterType.CONSTANT.getCode())) {
118 String constantString = (String) parametersMap.get(PropositionParameterType.CONSTANT.getCode());
119 if (StringUtils.isNumeric(constantString)) {
120 contextMap.put(CONSTANT_VALUE_TOKEN, Integer.valueOf(constantString));
121 } else {
122 contextMap.put(CONSTANT_VALUE_TOKEN, constantString);
123 }
124 }
125
126 if (parametersMap.containsKey(PropositionParameterType.OPERATOR.getCode())) {
127 String operatorString = (String) parametersMap.get(PropositionParameterType.OPERATOR.getCode());
128 contextMap.put(OPERATOR_TOKEN, operatorString);
129 }
130
131 KrmsTypeDefinitionContract type = getKrmsTypeRepositoryService().getTypeById(typeId);
132 List<TranslationContext> translationContextList = this.translationContextRegistry.get(type.getName());
133 if (translationContextList == null || translationContextList.isEmpty()) {
134 return contextMap;
135 }
136
137 for (TranslationContext translationContext : translationContextList) {
138 Map<String, Object> cm = translationContext.createContextMap(parametersMap);
139 contextMap.putAll(cm);
140 }
141
142 if (logger.isInfoEnabled()) {
143 logger.info("contextMap=" + contextMap);
144 }
145 return contextMap;
146 }
147
148
149 private KrmsTypeRepositoryService getKrmsTypeRepositoryService() {
150 return krmsTypeRepositoryService;
151 }
152
153 public void setKrmsTypeRepositoryService(KrmsTypeRepositoryService krmsTypeRepositoryService) {
154 this.krmsTypeRepositoryService = krmsTypeRepositoryService;
155 }
156
157 }