1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.core.statement.naturallanguage.translators; |
17 | |
|
18 | |
import java.util.HashMap; |
19 | |
import java.util.List; |
20 | |
import java.util.Locale; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
24 | |
import org.kuali.student.common.exceptions.OperationFailedException; |
25 | |
import org.kuali.student.core.statement.dto.ReqComponentInfo; |
26 | |
import org.kuali.student.core.statement.entity.ReqComponent; |
27 | |
import org.kuali.student.core.statement.entity.ReqComponentType; |
28 | |
import org.kuali.student.core.statement.entity.ReqComponentTypeNLTemplate; |
29 | |
import org.kuali.student.core.statement.naturallanguage.Context; |
30 | |
import org.kuali.student.core.statement.naturallanguage.ContextRegistry; |
31 | |
import org.kuali.student.core.statement.service.impl.StatementAssembler; |
32 | |
import org.slf4j.Logger; |
33 | |
import org.slf4j.LoggerFactory; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class ReqComponentTranslator { |
40 | |
|
41 | 1 | private final static Logger logger = LoggerFactory.getLogger(ReqComponentTranslator.class); |
42 | |
|
43 | |
private String language; |
44 | |
private ContextRegistry<Context<ReqComponentInfo>> contextRegistry; |
45 | 31 | private TemplateTranslator templateTranslator = new TemplateTranslator(); |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | 31 | public ReqComponentTranslator() { |
52 | 31 | this.language = Locale.getDefault().getLanguage(); |
53 | 31 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public void setLanguage(final String language) { |
61 | 33 | this.language = language; |
62 | 33 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public void setContextRegistry(final ContextRegistry<Context<ReqComponentInfo>> contextRegistry) { |
70 | 31 | this.contextRegistry = contextRegistry; |
71 | 31 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public String translate(final ReqComponent reqComponent, final String nlUsageTypeKey) throws DoesNotExistException, OperationFailedException { |
89 | 18 | return translate(reqComponent, nlUsageTypeKey, this.language); |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public String translate(final ReqComponent reqComponent, final String nlUsageTypeKey, final String language) throws DoesNotExistException, OperationFailedException { |
110 | 55 | if(reqComponent == null) { |
111 | 2 | throw new DoesNotExistException("ReqComponent cannot be null"); |
112 | |
} |
113 | |
|
114 | 53 | ReqComponentType reqComponentType = reqComponent.getRequiredComponentType(); |
115 | 53 | ReqComponentInfo reqComponentInfo = StatementAssembler.toReqComponentInfo(reqComponent); |
116 | 53 | Map<String, Object> contextMap = buildContextMap(reqComponentInfo); |
117 | 52 | ReqComponentTypeNLTemplate template = getTemplate(reqComponentType, nlUsageTypeKey, language); |
118 | |
|
119 | |
try { |
120 | 50 | String nl = this.templateTranslator.translate(contextMap, template.getTemplate()); |
121 | |
|
122 | 50 | if(logger.isInfoEnabled()) { |
123 | 0 | logger.info("nl="+nl); |
124 | |
} |
125 | |
|
126 | 50 | return nl; |
127 | 0 | } catch (OperationFailedException e) { |
128 | 0 | String msg = "Generating template for requirement component failed: "+reqComponent; |
129 | 0 | logger.error(msg, e); |
130 | 0 | throw new OperationFailedException(msg); |
131 | |
} |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
private Map<String, Object> buildContextMap(ReqComponentInfo reqComponent) throws DoesNotExistException, OperationFailedException { |
142 | 53 | List<Context<ReqComponentInfo>> contextList = this.contextRegistry.get(reqComponent.getType()); |
143 | 53 | if(contextList == null || contextList.isEmpty()) { |
144 | 1 | throw new DoesNotExistException("Requirement component context not found in registry for requirement component type id: " + reqComponent.getType()); |
145 | |
} |
146 | 52 | Map<String, Object> contextMap = new HashMap<String, Object>(); |
147 | 52 | for(Context<ReqComponentInfo> context : contextList) { |
148 | 53 | Map<String, Object> cm = context.createContextMap(reqComponent); |
149 | 53 | contextMap.putAll(cm); |
150 | 53 | } |
151 | |
|
152 | 52 | if(logger.isInfoEnabled()) { |
153 | 0 | logger.info("contextMap="+contextMap); |
154 | |
} |
155 | |
|
156 | 52 | return contextMap; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
private ReqComponentTypeNLTemplate getTemplate(ReqComponentType reqComponentType, String nlUsageTypeKey, String language) throws DoesNotExistException { |
170 | 52 | List<ReqComponentTypeNLTemplate> templateList = reqComponentType.getNlUsageTemplates(); |
171 | 52 | for (ReqComponentTypeNLTemplate template : templateList) { |
172 | 64 | if (nlUsageTypeKey.equals(template.getNlUsageTypeKey()) && language.equals(template.getLanguage())) { |
173 | |
|
174 | 50 | if(logger.isInfoEnabled()) { |
175 | 0 | logger.info("template="+template); |
176 | |
} |
177 | |
|
178 | 50 | return template; |
179 | |
} |
180 | |
} |
181 | 2 | throw new DoesNotExistException("Natural language usage type key '" + nlUsageTypeKey + "'" + |
182 | |
" and language code '" + language + "' for requirement component type " + reqComponentType.getId() + " template not found"); |
183 | |
} |
184 | |
} |