View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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  /*
17   * To change this template, choose Tools | Templates
18   * and open the template in the editor.
19   */
20  package org.kuali.rice.krms.impl.repository;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
30  import org.kuali.rice.krms.api.repository.LogicalOperator;
31  import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
32  import org.kuali.rice.krms.api.repository.RuleManagementService;
33  import org.kuali.rice.krms.api.repository.TranslateBusinessMethods;
34  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
35  import org.kuali.rice.krms.api.repository.agenda.AgendaItemDefinition;
36  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
37  import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplaterContract;
38  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
39  import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
40  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
41  import org.kuali.rice.krms.api.repository.proposition.PropositionType;
42  import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
43  import org.kuali.rice.krms.api.repository.term.TermDefinition;
44  import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
45  import org.kuali.rice.krms.api.repository.term.TermRepositoryService;
46  
47  /**
48   * @author nwright
49   */
50  public class KSTranslationUtility implements TranslateBusinessMethods {
51  
52      private RuleManagementService ruleManagementService;
53      private TermRepositoryService termRepositoryService;
54      private NaturalLanguageTemplaterContract templater;
55  
56      public KSTranslationUtility(RuleManagementService ruleManagementService, TermRepositoryService termRepositoryService,
57                                NaturalLanguageTemplaterContract templater) {
58          this.ruleManagementService = ruleManagementService;
59          this.termRepositoryService = termRepositoryService;
60          this.templater = templater;
61      }
62  
63      public RuleManagementService getRuleManagementService() {
64          return ruleManagementService;
65      }
66  
67      public void setRuleManagementService(RuleManagementService ruleManagementService) {
68          this.ruleManagementService = ruleManagementService;
69      }
70  
71      public NaturalLanguageTemplaterContract getTemplater() {
72          return templater;
73      }
74  
75      public void setTemplater(NaturalLanguageTemplaterContract templater) {
76          this.templater = templater;
77      }
78  
79      @Override
80      public String translateNaturalLanguageForObject(String naturalLanguageUsageId, String typeId, String krmsObjectId, String languageCode)
81              throws RiceIllegalArgumentException {
82  
83          Map<String, NaturalLanguageTemplate> templateMap = getNaturalLanguageTemplateMap(naturalLanguageUsageId, languageCode);
84  
85          if (typeId.equals("agenda")) {
86              AgendaDefinition agenda = this.ruleManagementService.getAgenda(krmsObjectId);
87              if (agenda == null) {
88                  throw new RiceIllegalArgumentException(krmsObjectId + " is not an Id for an agenda");
89              }
90              return this.translateNaturalLanguageForAgenda(agenda, templateMap);
91          } else if (typeId.equals("rule")) {
92              RuleDefinition rule = this.ruleManagementService.getRule(krmsObjectId);
93              if (rule == null) {
94                  throw new RiceIllegalArgumentException(krmsObjectId + " is not an Id for a rule");
95              }
96              return this.translateNaturalLanguageForRule(rule, templateMap);
97          } else if (typeId.equals("proposition")) {
98              PropositionDefinition proposition = this.ruleManagementService.getProposition(krmsObjectId);
99              if (proposition == null) {
100                 throw new RiceIllegalArgumentException(krmsObjectId + " is not an Id for a proposition");
101             }
102             return this.translateNaturalLanguageForProposition(naturalLanguageUsageId, proposition, languageCode);
103         }
104 
105         return StringUtils.EMPTY;
106     }
107 
108     protected String translateNaturalLanguageForAgenda(AgendaDefinition agenda, Map<String, NaturalLanguageTemplate> templateMap) throws RiceIllegalArgumentException {
109         if (agenda.getFirstItemId() == null) {
110             throw new RiceIllegalArgumentException("Agenda has no first item");
111         }
112 
113         AgendaItemDefinition item = this.ruleManagementService.getAgendaItem(agenda.getFirstItemId());
114         return translateNaturalLanguageForAgendaItem(item, templateMap);
115     }
116 
117     protected String translateNaturalLanguageForAgendaItem(AgendaItemDefinition item, Map<String, NaturalLanguageTemplate> templateMap) {
118         if(item==null){
119             return StringUtils.EMPTY;
120         }
121 
122         String naturalLanguage = StringUtils.EMPTY;
123         if (item.getRuleId() != null) {
124             RuleDefinition rule = this.ruleManagementService.getRule(item.getRuleId());
125             naturalLanguage += this.translateNaturalLanguageForRule(rule, templateMap);
126         }
127         naturalLanguage += translateNaturalLanguageForAgendaItem(item.getWhenTrue(), templateMap);
128         naturalLanguage += translateNaturalLanguageForAgendaItem(item.getWhenFalse(), templateMap);
129         naturalLanguage += translateNaturalLanguageForAgendaItem(item.getAlways(), templateMap);
130         return naturalLanguage;
131     }
132 
133     protected String translateNaturalLanguageForRule(RuleDefinition rule, Map<String, NaturalLanguageTemplate> templateMap) throws RiceIllegalArgumentException {
134         if(rule==null){
135             return StringUtils.EMPTY;
136         }
137 
138         NaturalLanguageTemplate nlTemplate = templateMap.get(rule.getTypeId());
139         String naturalLanguage = nlTemplate.getTemplate() + " ";
140 
141         if(rule.getProposition()!=null){
142             naturalLanguage += this.translateNaturalLanguageForProposition(rule.getProposition(), templateMap, true) + ". ";
143         }
144 
145         return naturalLanguage;
146     }
147 
148     @Override
149     public String translateNaturalLanguageForProposition(String naturalLanguageUsageId,
150                                                          PropositionDefinition proposition, String languageCode)
151             throws RiceIllegalArgumentException {
152 
153         Map<String, NaturalLanguageTemplate> templateMap = getNaturalLanguageTemplateMap(naturalLanguageUsageId, languageCode);
154         return translateNaturalLanguageForProposition(proposition, templateMap, true) + ". ";
155     }
156 
157     private Map<String, NaturalLanguageTemplate> getNaturalLanguageTemplateMap(String naturalLanguageUsageId, String languageCode) {
158         Map<String, NaturalLanguageTemplate> templateMap = new HashMap<String, NaturalLanguageTemplate>();
159         List<NaturalLanguageTemplate> templates = this.ruleManagementService.findNaturalLanguageTemplatesByNaturalLanguageUsage(naturalLanguageUsageId);
160         for(NaturalLanguageTemplate nlTemplate : templates){
161             if(languageCode.equals(nlTemplate.getLanguageCode())){
162                 templateMap.put(nlTemplate.getTypeId(), nlTemplate);
163             }
164         }
165         return templateMap;
166     }
167 
168     /**
169      * This method is added because from a functional point of view the root proposition is ignored when it is a group
170      * and therefore handled differently.
171      *
172      * @param proposition
173      * @param templateMap
174      * @param isRoot
175      * @return
176      */
177     private String translateNaturalLanguageForProposition(PropositionDefinition proposition, Map<String, NaturalLanguageTemplate> templateMap, boolean isRoot) {
178         NaturalLanguageTemplate naturalLanguageTemplate = templateMap.get(proposition.getTypeId());
179 
180         StringBuilder naturalLanguage = new StringBuilder();
181         if (proposition.getPropositionTypeCode().equals(PropositionType.SIMPLE.getCode())) {
182             if(naturalLanguageTemplate!=null){
183                 Map<String, Object> contextMap = this.buildSimplePropositionContextMap(proposition);
184                 naturalLanguage.append(templater.translate(naturalLanguageTemplate, contextMap));
185             }
186 
187         } else if (proposition.getPropositionTypeCode().equals(PropositionType.COMPOUND.getCode())) {
188             if(naturalLanguageTemplate!=null){
189                 Map<String, Object> contextMap = this.buildCompoundPropositionContextMap(proposition, templateMap);
190                 naturalLanguage.append(templater.translate(naturalLanguageTemplate, contextMap));
191             }
192 
193             //Null check because newly created compound propositions should also be translateable.
194             if(proposition.getCompoundComponents()!=null){
195                 String operator = getCompoundSeperator(proposition, isRoot);
196                 for (PropositionDefinition child : proposition.getCompoundComponents()) {
197                     if(proposition.getCompoundComponents().indexOf(child)!=0){
198                         naturalLanguage.append(operator);
199                     }
200                     naturalLanguage.append(this.translateNaturalLanguageForProposition(child, templateMap, false));
201                 }
202             }
203 
204         } else {
205             throw new RiceIllegalArgumentException("Unknown proposition type: " + proposition.getPropositionTypeCode());
206         }
207 
208         return naturalLanguage.toString();
209     }
210 
211     private String getCompoundSeperator(PropositionDefinition proposition, boolean isRoot) {
212         String operator = getCompoundOperator(proposition);
213         if (isRoot){
214             return ". " + StringUtils.capitalize(operator) + " ";
215         }
216         return "; " + operator + " ";
217     }
218 
219     private String getCompoundOperator(PropositionDefinition proposition) {
220         String operator = null;
221         if (LogicalOperator.AND.getCode().equalsIgnoreCase(proposition.getCompoundOpCode())) {
222             operator = "and";
223         } else if (LogicalOperator.OR.getCode().equalsIgnoreCase(proposition.getCompoundOpCode())) {
224             operator = "or";
225         }
226         return operator;
227     }
228 
229     @Override
230     public NaturalLanguageTree translateNaturalLanguageTreeForProposition(String naturalLanguageUsageId,
231                                                                           PropositionDefinition proposition,
232                                                                           String languageCode) throws RiceIllegalArgumentException {
233 
234         Map<String, NaturalLanguageTemplate> templateMap = getNaturalLanguageTemplateMap(naturalLanguageUsageId, languageCode);
235         return translateNaturalLanguageTreeForProposition(proposition, templateMap);
236     }
237 
238     public NaturalLanguageTree translateNaturalLanguageTreeForProposition(PropositionDefinition proposition,
239                                                                           Map<String, NaturalLanguageTemplate> templateMap) throws RiceIllegalArgumentException {
240 
241         NaturalLanguageTemplate naturalLanguageTemplate = templateMap.get(proposition.getTypeId());
242 
243         NaturalLanguageTree.Builder tree = NaturalLanguageTree.Builder.create();
244         if (proposition.getPropositionTypeCode().equals(PropositionType.SIMPLE.getCode())) {
245             Map<String, Object> contextMap = this.buildSimplePropositionContextMap(proposition);
246             String naturalLanguage = templater.translate(naturalLanguageTemplate, contextMap);
247             tree.setNaturalLanguage(naturalLanguage);
248 
249         } else if (proposition.getPropositionTypeCode().equals(PropositionType.COMPOUND.getCode())) {
250             Map<String, Object> contextMap = this.buildCompoundPropositionContextMap(proposition, templateMap);
251             String naturalLanguage = templater.translate(naturalLanguageTemplate, contextMap);
252             tree.setNaturalLanguage(naturalLanguage);
253 
254             //Null check because newly created compound propositions should also be translateable.
255             if(proposition.getCompoundComponents()!=null){
256                 List<NaturalLanguageTree> children = new ArrayList<NaturalLanguageTree>();
257                 for (PropositionDefinition child : proposition.getCompoundComponents()) {
258                     children.add(this.translateNaturalLanguageTreeForProposition(child, templateMap));
259                 }
260                 tree.setChildren(children);
261             }
262 
263         } else {
264             throw new RiceIllegalArgumentException("Unknown proposition type: " + proposition.getPropositionTypeCode());
265         }
266 
267         return tree.build();
268     }
269 
270     protected Map<String, Object> buildSimplePropositionContextMap(PropositionDefinition proposition) {
271         if (!proposition.getPropositionTypeCode().equals(PropositionType.SIMPLE.getCode())) {
272             throw new RiceIllegalArgumentException("proposition is not simple " + proposition.getPropositionTypeCode() + " " + proposition.getId() + proposition.getDescription());
273         }
274         Map<String, Object> contextMap = new LinkedHashMap<String, Object>();
275         for (PropositionParameter param : proposition.getParameters()) {
276             if (param.getParameterType().equals(PropositionParameterType.TERM.getCode())) {
277                 TermDefinition term = param.getTermValue();
278                 if ((term == null) && (StringUtils.isNotBlank(param.getValue()))) {
279                     term = this.termRepositoryService.getTerm(param.getValue());
280                 }
281                 if (term != null) {
282                     for (TermParameterDefinition termParam : term.getParameters()) {
283                         contextMap.put(termParam.getName(), termParam.getValue());
284                     }
285                 } else {
286                     contextMap.put(param.getParameterType(), param.getValue());
287                 }
288             } else {
289                 contextMap.put(param.getParameterType(), param.getValue());
290             }
291         }
292         return contextMap;
293     }
294 
295     protected Map<String, Object> buildCompoundPropositionContextMap(PropositionDefinition proposition, Map<String, NaturalLanguageTemplate> templateMap) {
296         if (!proposition.getPropositionTypeCode().equals(PropositionType.COMPOUND.getCode())) {
297             throw new RiceIllegalArgumentException("proposition us not compound " + proposition.getPropositionTypeCode() + " " + proposition.getId() + proposition.getDescription());
298         }
299         return new LinkedHashMap<String, Object>();
300     }
301 
302 }