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 contextMap.put(CONSTANT_VALUE_TOKEN, (String) parametersMap.get(PropositionParameterType.CONSTANT.getCode()));
119 }
120
121 if (parametersMap.containsKey(PropositionParameterType.OPERATOR.getCode())) {
122 contextMap.put(OPERATOR_TOKEN, (String) parametersMap.get(PropositionParameterType.OPERATOR.getCode()));
123 }
124
125 KrmsTypeDefinitionContract type = getKrmsTypeRepositoryService().getTypeById(typeId);
126 List<TranslationContext> translationContextList = this.translationContextRegistry.get(type.getName());
127 if (translationContextList == null || translationContextList.isEmpty()) {
128 return contextMap;
129 }
130
131 for (TranslationContext translationContext : translationContextList) {
132 Map<String, Object> cm = translationContext.createContextMap(parametersMap);
133 contextMap.putAll(cm);
134 }
135
136 if (logger.isInfoEnabled()) {
137 logger.info("contextMap=" + contextMap);
138 }
139 return contextMap;
140 }
141
142
143 private KrmsTypeRepositoryService getKrmsTypeRepositoryService() {
144 return krmsTypeRepositoryService;
145 }
146
147 public void setKrmsTypeRepositoryService(KrmsTypeRepositoryService krmsTypeRepositoryService) {
148 this.krmsTypeRepositoryService = krmsTypeRepositoryService;
149 }
150
151 }