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.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.kuali.student.core.exceptions.OperationFailedException; |
25 | |
import org.kuali.student.core.statement.dto.StatementOperatorTypeKey; |
26 | |
import org.kuali.student.core.statement.entity.ReqComponent; |
27 | |
import org.kuali.student.core.statement.entity.Statement; |
28 | |
import org.kuali.student.core.statement.naturallanguage.util.ReqComponentReference; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class StatementParser { |
36 | |
private StringBuilder sb; |
37 | |
private Map<String,String> idMap; |
38 | |
private List<ReqComponentReference> reqComponentList; |
39 | |
private String andOperator; |
40 | |
private String orOperator; |
41 | |
private int idCounter; |
42 | |
private final static String STATEMENT_ID = "S"; |
43 | |
private final static String REC_COMPONENT_ID = "R"; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public StatementParser() { |
50 | 7 | this(StatementOperatorTypeKey.AND.name(), StatementOperatorTypeKey.OR.name()); |
51 | 7 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 37 | public StatementParser(final String andOperator, final String orOperator) { |
60 | 37 | this.andOperator = andOperator.toLowerCase(); |
61 | 37 | this.orOperator = orOperator.toLowerCase(); |
62 | 37 | } |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private void init() { |
68 | 49 | idCounter = 1; |
69 | 49 | this.idMap = new HashMap<String, String>(); |
70 | 49 | this.sb = new StringBuilder(); |
71 | 49 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public String getBooleanExpressionAsStatements(final Statement rootStatement) throws OperationFailedException { |
85 | 3 | init(); |
86 | 3 | if(rootStatement.getChildren() == null || rootStatement.getChildren().isEmpty()) { |
87 | 0 | return parseReqComponents(rootStatement, false); |
88 | |
} else { |
89 | 3 | traverseStatementTreeAndReduce(rootStatement, false, null); |
90 | |
} |
91 | 3 | return sb.toString(); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public String getBooleanExpressionAsReqComponents(final Statement rootStatement) throws OperationFailedException { |
106 | 24 | init(); |
107 | 24 | if(rootStatement.getChildren() == null || rootStatement.getChildren().isEmpty()) { |
108 | 17 | return parseReqComponents(rootStatement, true); |
109 | |
} else { |
110 | 7 | traverseStatementTreeAndReduce(rootStatement, true, null); |
111 | |
} |
112 | 7 | return sb.toString(); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public Map<String, String> getIdMap() { |
121 | 0 | return this.idMap; |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public List<ReqComponentReference> getLeafReqComponents(final Statement rootStatement) throws OperationFailedException { |
131 | 22 | init(); |
132 | 22 | this.reqComponentList = new ArrayList<ReqComponentReference>(); |
133 | 22 | if(rootStatement.getChildren() == null || rootStatement.getChildren().isEmpty()) { |
134 | 16 | this.reqComponentList.addAll(getReqComponents(rootStatement.getRequiredComponents())); |
135 | |
} else { |
136 | 6 | traverseStatementTree(rootStatement); |
137 | |
} |
138 | 22 | return this.reqComponentList; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
private void traverseStatementTree(Statement rootStatement) throws OperationFailedException { |
148 | 18 | for(Iterator<Statement> it = rootStatement.getChildren().iterator(); it.hasNext();) { |
149 | 39 | Statement stmt = it.next(); |
150 | 39 | if (stmt.getChildren() == null || stmt.getChildren().isEmpty()) { |
151 | 27 | this.reqComponentList.addAll(getReqComponents(stmt.getRequiredComponents())); |
152 | |
} else { |
153 | 12 | traverseStatementTree(stmt); |
154 | |
} |
155 | 39 | } |
156 | 18 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
private List<ReqComponentReference> getReqComponents(List<ReqComponent > list) throws OperationFailedException { |
166 | 43 | List<ReqComponentReference> newList = new ArrayList<ReqComponentReference>(list.size()); |
167 | 43 | for(ReqComponent reqComp : list) { |
168 | 57 | newList.add(new ReqComponentReference(reqComp, getReqComponentReferenceId(reqComp))); |
169 | |
} |
170 | 43 | return newList; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
private void traverseStatementTreeAndReduce(Statement rootStatement, boolean parseReqComponent, Statement parent) throws OperationFailedException { |
181 | 30 | if(rootStatement.getChildren() != null && |
182 | |
(rootStatement.getOperator() == StatementOperatorTypeKey.OR || |
183 | |
(parent != null && parent.getOperator() == StatementOperatorTypeKey.OR)) ) { |
184 | 14 | this.sb.append("("); |
185 | |
} |
186 | 30 | for(Iterator<Statement> it = rootStatement.getChildren().iterator(); it.hasNext();) { |
187 | 65 | Statement stmt = it.next(); |
188 | 65 | if (stmt.getChildren() == null || stmt.getChildren().isEmpty()) { |
189 | 45 | if (parseReqComponent) { |
190 | 30 | this.sb.append(parseReqComponents(stmt, false)); |
191 | |
} else { |
192 | |
|
193 | 15 | this.sb.append(getStatementReferenceId(stmt)); |
194 | |
} |
195 | |
} else { |
196 | 20 | traverseStatementTreeAndReduce(stmt, parseReqComponent, rootStatement); |
197 | |
} |
198 | |
|
199 | 65 | if (it.hasNext() && rootStatement != null && rootStatement.getOperator() != null) { |
200 | 35 | this.sb.append(" "); |
201 | 35 | this.sb.append(getOperator(rootStatement.getOperator())); |
202 | 35 | this.sb.append(" "); |
203 | |
} |
204 | 65 | } |
205 | 30 | if(rootStatement.getChildren() != null && |
206 | |
(rootStatement.getOperator() == StatementOperatorTypeKey.OR || |
207 | |
(parent != null && parent.getOperator() == StatementOperatorTypeKey.OR)) ) { |
208 | 14 | this.sb.append(")"); |
209 | |
} |
210 | 30 | } |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
private String parseReqComponents(Statement statement, boolean reduce) throws OperationFailedException { |
221 | 47 | if (statement.getRequiredComponents() == null) { |
222 | 0 | return ""; |
223 | |
} |
224 | |
|
225 | 47 | StringBuilder sb = new StringBuilder(); |
226 | 47 | if(!reduce && statement.getRequiredComponents().size() > 1) |
227 | |
{ |
228 | 7 | sb.append("("); |
229 | |
} |
230 | 47 | for(Iterator<ReqComponent> it = statement.getRequiredComponents().iterator(); it.hasNext(); ) { |
231 | 62 | ReqComponent reqComponent = it.next(); |
232 | 62 | sb.append(getReqComponentReferenceId(reqComponent)); |
233 | 62 | if (it.hasNext()) { |
234 | 15 | sb.append(" "); |
235 | 15 | sb.append(getOperator(statement.getOperator())); |
236 | 15 | sb.append(" "); |
237 | |
} |
238 | 62 | } |
239 | 47 | if(!reduce && statement.getRequiredComponents().size() > 1) |
240 | |
{ |
241 | 7 | sb.append(")"); |
242 | |
} |
243 | 47 | return sb.toString(); |
244 | |
} |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
private String getOperator(StatementOperatorTypeKey operator) throws OperationFailedException { |
254 | 4 | switch(operator) { |
255 | |
case AND: |
256 | 25 | return this.andOperator; |
257 | |
case OR: |
258 | 25 | return this.orOperator; |
259 | |
default: |
260 | 0 | throw new OperationFailedException("Invalid statement operator: "+operator); |
261 | |
} |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
private String getStatementReferenceId(Statement statement) throws OperationFailedException { |
272 | 15 | if(statement.getId() == null || statement.getId().isEmpty()) { |
273 | 0 | throw new OperationFailedException("Statement id cannot be null"); |
274 | |
} |
275 | |
|
276 | 15 | if(this.idMap.containsKey(statement.getId())) { |
277 | 0 | return this.idMap.get(statement.getId()); |
278 | |
} |
279 | 15 | String id = STATEMENT_ID + this.idCounter++; |
280 | 15 | this.idMap.put(statement.getId(), id); |
281 | 15 | return id; |
282 | |
} |
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
private String getReqComponentReferenceId(ReqComponent reqComponent) throws OperationFailedException { |
292 | 119 | if(reqComponent.getId() == null || reqComponent.getId().isEmpty()) { |
293 | 0 | throw new OperationFailedException("Requirement component id cannot be null"); |
294 | |
} |
295 | |
|
296 | 119 | if(this.idMap.containsKey(reqComponent.getId())) { |
297 | 0 | return this.idMap.get(reqComponent.getId()); |
298 | |
} |
299 | 119 | String id = REC_COMPONENT_ID + this.idCounter++; |
300 | 119 | this.idMap.put(reqComponent.getId(), id); |
301 | 119 | return id; |
302 | |
} |
303 | |
} |