1 package org.kuali.rice.krms.util;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
5 import org.kuali.rice.krms.api.repository.RuleManagementService;
6 import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
7 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
8 import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
9 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterContract;
10 import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
11 import org.kuali.rice.krms.api.repository.term.TermDefinition;
12 import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
13 import org.kuali.rice.krms.dto.PropositionEditor;
14 import org.kuali.rice.krms.dto.TermEditor;
15 import org.kuali.rice.krms.tree.AbstractTreeBuilder;
16 import org.kuali.student.enrollment.class2.courseoffering.service.decorators.PermissionServiceConstants;
17 import org.kuali.student.krms.naturallanguage.util.KsKrmsConstants;
18
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Queue;
27
28
29
30
31
32
33
34
35 public class NaturalLanguageHelper {
36
37 private RuleManagementService ruleManagementService;
38
39 private Map<String, String> usageMap = new HashMap<String, String>();
40
41
42
43
44
45
46
47
48 public void setNaturalLanguageForUsage(PropositionEditor proposition, String usageName){
49
50
51 List<PropositionParameter.Builder> parameters = new ArrayList<PropositionParameter.Builder>();
52 if(proposition.getParameters()!=null){
53 for(PropositionParameterContract parameter : proposition.getParameters()){
54 PropositionParameter.Builder parmBuilder = PropositionParameter.Builder.create(parameter);
55
56
57 if(parameter.getParameterType().equals(PropositionParameterType.TERM.getCode())){
58 TermDefinition.Builder termBuilder = TermDefinition.Builder.create(proposition.getTerm());
59 for (Map.Entry<String, String> entry : proposition.getNlParameters().entrySet()) {
60 termBuilder.getParameters().add(TermParameterDefinition.Builder.create(null, null, entry.getKey(), entry.getValue()));
61 }
62 parmBuilder.setTermValue(termBuilder.build());
63 }
64 parameters.add(parmBuilder);
65 }
66 }
67
68
69 PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(proposition.getId(),
70 proposition.getPropositionTypeCode(), proposition.getRuleId(), proposition.getTypeId(), parameters);
71
72
73 TreeIterator nlTree = this.buildNaturalLanguageTree(propBuilder, this.getNaturalLanguageUsageId(usageName));
74 this.setTranslatedNaturalLanguage(proposition, usageName, nlTree);
75 }
76
77
78
79
80
81
82
83
84 public void setNaturalLanguageTreeForUsage(PropositionEditor proposition, String usageName){
85 PropositionDefinition.Builder propBuilder = PropositionDefinition.Builder.create(proposition);
86 TreeIterator nlTree = this.buildNaturalLanguageTree(propBuilder, this.getNaturalLanguageUsageId(usageName));
87 this.setTranslatedNaturalLanguage(proposition, usageName, nlTree);
88 }
89
90 private TreeIterator buildNaturalLanguageTree(PropositionDefinition.Builder propBuilder, String usageId){
91 NaturalLanguageTree nlTree = this.getRuleManagementService().translateNaturalLanguageTreeForProposition(usageId, propBuilder.build(), "en");
92 return new TreeIterator(nlTree);
93 }
94
95 private void setTranslatedNaturalLanguage(PropositionEditor proposition, String usageName, TreeIterator nlDescriptions){
96 if(!nlDescriptions.hasNext()){
97 return;
98 }
99
100 proposition.getNaturalLanguage().put(usageName, nlDescriptions.next());
101
102 if (proposition.getCompoundEditors() != null){
103 for (PropositionEditor child : proposition.getCompoundEditors()){
104 setTranslatedNaturalLanguage(child, usageName, nlDescriptions);
105 }
106 }
107 }
108
109 private String getNaturalLanguageUsageId(String usageName){
110
111 if ((usageName == null) || (StringUtils.isBlank(usageName))){
112 return null;
113 }
114
115
116 String usageId = usageMap.get(usageName);
117 if (usageId == null){
118
119
120 NaturalLanguageUsage usage = this.getRuleManagementService().getNaturalLanguageUsageByNameAndNamespace(usageName,
121 PermissionServiceConstants.KS_SYS_NAMESPACE);
122
123
124 if (usage != null){
125 usageId = usage.getId();
126 usageMap.put(usageName, usageId);
127 }
128 }
129 return usageId;
130 }
131
132 public RuleManagementService getRuleManagementService() {
133 return ruleManagementService;
134 }
135
136 public void setRuleManagementService(RuleManagementService ruleManagementService) {
137 this.ruleManagementService = ruleManagementService;
138 }
139
140
141
142
143
144 protected class TreeIterator implements Serializable, Iterator<String> {
145
146 private Queue<String> nl;
147
148 public TreeIterator(NaturalLanguageTree tree){
149 nl = new LinkedList<String>();
150 this.addToStack(tree);
151 }
152
153 private void addToStack(NaturalLanguageTree tree){
154 if (tree == null){
155 return;
156 }
157
158 nl.offer(tree.getNaturalLanguage());
159 if (tree.getChildren() != null){
160 for (NaturalLanguageTree child : tree.getChildren()){
161 addToStack(child);
162 }
163 }
164 }
165
166 @Override
167 public boolean hasNext() {
168 return !nl.isEmpty();
169 }
170
171 @Override
172 public String next() {
173 return nl.poll();
174 }
175
176 @Override
177 public void remove() {
178
179 }
180 }
181 }