1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42 public class NaturalLanguageHelper {
43
44 private RuleManagementService ruleManagementService;
45
46 private Map<String, String> usageMap = new HashMap<String, String>();
47
48
49
50
51
52
53
54
55 public void setNaturalLanguageForUsage(PropositionEditor proposition, String usageName, String namespace){
56
57 List<PropositionParameter.Builder> parameters = new ArrayList<PropositionParameter.Builder>();
58 if(proposition.getParameters()!=null){
59 for(PropositionParameterContract parameter : proposition.getParameters()){
60 PropositionParameter.Builder parmBuilder = PropositionParameter.Builder.create(parameter);
61
62
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
77 PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(proposition.getId(),
78 proposition.getPropositionTypeCode(), proposition.getRuleId(), proposition.getTypeId(), parameters);
79
80
81 TreeIterator nlTree = this.buildNaturalLanguageTree(propBuilder, this.getNaturalLanguageUsageId(usageName, namespace));
82 this.setTranslatedNaturalLanguage(proposition, usageName, nlTree);
83 }
84
85
86
87
88
89
90
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
123 if ((usageName == null) || (StringUtils.isBlank(usageName))){
124 return null;
125 }
126
127
128 String usageId = usageMap.get(usageName);
129 if (usageId == null){
130
131
132 NaturalLanguageUsage usage = this.getRuleManagementService().getNaturalLanguageUsageByNameAndNamespace(usageName,
133 namespace);
134
135
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
154
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
191 }
192 }
193 }