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.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
20  import org.kuali.rice.krms.api.repository.RuleManagementService;
21  import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
22  import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
23  import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
24  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterContract;
25  import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
26  import org.kuali.rice.krms.api.repository.term.TermDefinition;
27  import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
28  import org.kuali.rice.krms.dto.PropositionEditor;
29  
30  import java.io.Serializable;
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.LinkedList;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Queue;
38  
39  /**
40   * @author Kuali Student Team
41   */
42  public class NaturalLanguageHelper {
43  
44      private RuleManagementService ruleManagementService;
45  
46      private Map<String, String> usageMap = new HashMap<String, String>();
47  
48      /**
49       * This method should set the natural language only for the given proposition. It does not
50       * do a recursive call through the tree structure.
51       *
52       * @param proposition
53       * @param usageName
54       */
55      public void setNaturalLanguageForUsage(PropositionEditor proposition, String usageName, String namespace){
56          //Setup the Proposition
57          List<PropositionParameter.Builder> parameters = new ArrayList<PropositionParameter.Builder>();
58          if(proposition.getParameters()!=null){  //Parameters on Compound Propositions could be null.
59              for(PropositionParameterContract parameter : proposition.getParameters()){
60                  PropositionParameter.Builder parmBuilder = PropositionParameter.Builder.create(parameter);
61  
62                  //Add the edited term.
63                  if(parameter.getParameterType().equals(PropositionParameterType.TERM.getCode())){
64                      if(proposition.getTerm()!=null){
65                          TermDefinition.Builder termBuilder = TermDefinition.Builder.create(proposition.getTerm());
66                          for (Map.Entry<String, String> entry : proposition.getNlParameters().entrySet()) {
67                              termBuilder.getParameters().add(TermParameterDefinition.Builder.create(null, null, entry.getKey(), entry.getValue()));
68                          }
69                          parmBuilder.setTermValue(termBuilder.build());
70                      }
71                  }
72                  parameters.add(parmBuilder);
73              }
74          }
75  
76          //Create the propbuilder but don set compound editors here, we only want to get the nl for current proposition
77          PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(proposition.getId(),
78                  proposition.getPropositionTypeCode(), proposition.getRuleId(), proposition.getTypeId(), parameters);
79  
80          //Build the tree.
81          TreeIterator nlTree = this.buildNaturalLanguageTree(propBuilder, this.getNaturalLanguageUsageId(usageName, namespace));
82          this.setTranslatedNaturalLanguage(proposition, usageName, nlTree);
83      }
84  
85      /**
86       * This method sets the natural language descriptions for the given proposition and all its child propositions as
87       * well.
88       *
89       * @param proposition
90       * @param usageName
91       */
92      public void setNaturalLanguageTreeForUsage(PropositionEditor proposition, String usageName, String namespace){
93          if(proposition==null){
94              return;
95          }
96  
97          PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(proposition);
98          TreeIterator nlTree = this.buildNaturalLanguageTree(propBuilder, this.getNaturalLanguageUsageId(usageName, namespace));
99          this.setTranslatedNaturalLanguage(proposition, usageName, nlTree);
100     }
101 
102     private TreeIterator buildNaturalLanguageTree(PropositionDefinition.Builder propBuilder, String usageId){
103         NaturalLanguageTree nlTree = this.getRuleManagementService().translateNaturalLanguageTreeForProposition(usageId, propBuilder.build(), "en");
104         return new TreeIterator(nlTree);
105     }
106 
107     private void setTranslatedNaturalLanguage(PropositionEditor proposition, String usageName, TreeIterator nlDescriptions){
108         if(!nlDescriptions.hasNext()){
109             return;
110         }
111 
112         proposition.setNaturalLanguageForUsage(usageName, nlDescriptions.next());
113 
114         if (proposition.getCompoundEditors() != null){
115             for (PropositionEditor child : proposition.getCompoundEditors()){
116                 setTranslatedNaturalLanguage(child, usageName, nlDescriptions);
117             }
118         }
119     }
120 
121     private String getNaturalLanguageUsageId(String usageName, String namespace){
122         //usageName cannot be null or blank
123         if ((usageName == null) || (StringUtils.isBlank(usageName))){
124             return null;
125         }
126 
127         //Retrieve usageId form map.
128         String usageId = usageMap.get(usageName);
129         if (usageId == null){
130 
131             //Retrieve usage from service if usageId not on map.
132             NaturalLanguageUsage usage = this.getRuleManagementService().getNaturalLanguageUsageByNameAndNamespace(usageName,
133                     namespace);
134 
135             //Set the usage id to the map.
136             if (usage != null){
137                 usageId = usage.getId();
138                 usageMap.put(usageName, usageId);
139             }
140         }
141         return usageId;
142     }
143 
144     public RuleManagementService getRuleManagementService() {
145         return ruleManagementService;
146     }
147 
148     public void setRuleManagementService(RuleManagementService ruleManagementService) {
149         this.ruleManagementService = ruleManagementService;
150     }
151 
152     /**
153      * Put the natural language descriptions in a queue and retrieve them in
154      * the same order as the propostion tree.
155      */
156     protected class TreeIterator implements Serializable, Iterator<String> {
157 
158         private Queue<String> nl;
159 
160         public TreeIterator(NaturalLanguageTree tree){
161             nl = new LinkedList<String>();
162             this.addToStack(tree);
163         }
164 
165         private void addToStack(NaturalLanguageTree tree){
166             if (tree == null){
167                 return;
168             }
169 
170             nl.offer(tree.getNaturalLanguage());
171             if (tree.getChildren() != null){
172                 for (NaturalLanguageTree child : tree.getChildren()){
173                     addToStack(child);
174                 }
175             }
176         }
177 
178         @Override
179         public boolean hasNext() {
180             return !nl.isEmpty();
181         }
182 
183         @Override
184         public String next() {
185             return nl.poll();
186         }
187 
188         @Override
189         public void remove() {
190             //To change body of implemented methods use File | Settings | File Templates.
191         }
192     }
193 }