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  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   * This class translates requirement components into a specific
35   * natural language. This class is not thread safe.
36   */
37  public class PropositionNaturalLanguageTemplater implements NaturalLanguageTemplaterContract {
38      /**
39       * SLF4J logging framework
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       * Relational operator token.
49       */
50      public final static String OPERATOR_TOKEN = "relationalOperator";
51      /**
52       * An integer value token.
53       */
54      public final static String CONSTANT_VALUE_TOKEN = "intValue";
55  
56      /**
57       * Velocity template engine.
58       */
59      private VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
60  
61  
62      /**
63       * Constructs a new proposition natural language templater.
64       */
65      public PropositionNaturalLanguageTemplater() {
66      }
67  
68  
69      /**
70       * Sets the template context registry.
71       *
72       * @param translationContextRegistry Template context registry
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();  //TODO hand back to service.
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             //TODO hand back to service throw new Exception(msg);
102         }
103         return "Error";
104     }
105 
106     /**
107      * Builds a proposition type context map.
108      *
109      * @param typeId        the natural language template id
110      * @param parametersMap map containing the proposition parameter types and their values
111      * @throws java.lang.Exception Creating context map failed
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         //Add proposition constant to contextMap.
117         if (parametersMap.containsKey(PropositionParameterType.CONSTANT.getCode())) {
118             contextMap.put(CONSTANT_VALUE_TOKEN, (String) parametersMap.get(PropositionParameterType.CONSTANT.getCode()));
119         }
120         //Add proposition operator to contextMap.
121         if (parametersMap.containsKey(PropositionParameterType.OPERATOR.getCode())) {
122             contextMap.put(OPERATOR_TOKEN, (String) parametersMap.get(PropositionParameterType.OPERATOR.getCode()));
123         }
124         //Access type service to retrieve type name.
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 }