| 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.common.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 | 0 | this(StatementOperatorTypeKey.AND.name(), StatementOperatorTypeKey.OR.name()); |
| 51 | 0 | } |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | 0 | public StatementParser(final String andOperator, final String orOperator) { |
| 60 | 0 | this.andOperator = andOperator.toLowerCase(); |
| 61 | 0 | this.orOperator = orOperator.toLowerCase(); |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
private void init() { |
| 68 | 0 | idCounter = 1; |
| 69 | 0 | this.idMap = new HashMap<String, String>(); |
| 70 | 0 | this.sb = new StringBuilder(); |
| 71 | 0 | } |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
public String getBooleanExpressionAsStatements(final Statement rootStatement) throws OperationFailedException { |
| 85 | 0 | init(); |
| 86 | 0 | if(rootStatement.getChildren() == null || rootStatement.getChildren().isEmpty()) { |
| 87 | 0 | return parseReqComponents(rootStatement, false); |
| 88 | |
} else { |
| 89 | 0 | traverseStatementTreeAndReduce(rootStatement, false, null); |
| 90 | |
} |
| 91 | 0 | 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 | 0 | init(); |
| 107 | 0 | if(rootStatement.getChildren() == null || rootStatement.getChildren().isEmpty()) { |
| 108 | 0 | return parseReqComponents(rootStatement, true); |
| 109 | |
} else { |
| 110 | 0 | traverseStatementTreeAndReduce(rootStatement, true, null); |
| 111 | |
} |
| 112 | 0 | 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 | 0 | init(); |
| 132 | 0 | this.reqComponentList = new ArrayList<ReqComponentReference>(); |
| 133 | 0 | if(rootStatement.getChildren() == null || rootStatement.getChildren().isEmpty()) { |
| 134 | 0 | this.reqComponentList.addAll(getReqComponents(rootStatement.getRequiredComponents())); |
| 135 | |
} else { |
| 136 | 0 | traverseStatementTree(rootStatement); |
| 137 | |
} |
| 138 | 0 | return this.reqComponentList; |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
private void traverseStatementTree(Statement rootStatement) throws OperationFailedException { |
| 148 | 0 | for(Iterator<Statement> it = rootStatement.getChildren().iterator(); it.hasNext();) { |
| 149 | 0 | Statement stmt = it.next(); |
| 150 | 0 | if (stmt.getChildren() == null || stmt.getChildren().isEmpty()) { |
| 151 | 0 | this.reqComponentList.addAll(getReqComponents(stmt.getRequiredComponents())); |
| 152 | |
} else { |
| 153 | 0 | traverseStatementTree(stmt); |
| 154 | |
} |
| 155 | 0 | } |
| 156 | 0 | } |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
private List<ReqComponentReference> getReqComponents(List<ReqComponent > list) throws OperationFailedException { |
| 166 | 0 | List<ReqComponentReference> newList = new ArrayList<ReqComponentReference>(list.size()); |
| 167 | 0 | for(ReqComponent reqComp : list) { |
| 168 | 0 | newList.add(new ReqComponentReference(reqComp, getReqComponentReferenceId(reqComp))); |
| 169 | |
} |
| 170 | 0 | return newList; |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
private void traverseStatementTreeAndReduce(Statement rootStatement, boolean parseReqComponent, Statement parent) throws OperationFailedException { |
| 181 | 0 | if(rootStatement.getChildren() != null && |
| 182 | |
(rootStatement.getOperator() == StatementOperatorTypeKey.OR || |
| 183 | |
(parent != null && parent.getOperator() == StatementOperatorTypeKey.OR)) ) { |
| 184 | 0 | this.sb.append("("); |
| 185 | |
} |
| 186 | 0 | for(Iterator<Statement> it = rootStatement.getChildren().iterator(); it.hasNext();) { |
| 187 | 0 | Statement stmt = it.next(); |
| 188 | 0 | if (stmt.getChildren() == null || stmt.getChildren().isEmpty()) { |
| 189 | 0 | if (parseReqComponent) { |
| 190 | 0 | this.sb.append(parseReqComponents(stmt, false)); |
| 191 | |
} else { |
| 192 | |
|
| 193 | 0 | this.sb.append(getStatementReferenceId(stmt)); |
| 194 | |
} |
| 195 | |
} else { |
| 196 | 0 | traverseStatementTreeAndReduce(stmt, parseReqComponent, rootStatement); |
| 197 | |
} |
| 198 | |
|
| 199 | 0 | if (it.hasNext() && rootStatement != null && rootStatement.getOperator() != null) { |
| 200 | 0 | this.sb.append(" "); |
| 201 | 0 | this.sb.append(getOperator(rootStatement.getOperator())); |
| 202 | 0 | this.sb.append(" "); |
| 203 | |
} |
| 204 | 0 | } |
| 205 | 0 | if(rootStatement.getChildren() != null && |
| 206 | |
(rootStatement.getOperator() == StatementOperatorTypeKey.OR || |
| 207 | |
(parent != null && parent.getOperator() == StatementOperatorTypeKey.OR)) ) { |
| 208 | 0 | this.sb.append(")"); |
| 209 | |
} |
| 210 | 0 | } |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
private String parseReqComponents(Statement statement, boolean reduce) throws OperationFailedException { |
| 221 | 0 | if (statement.getRequiredComponents() == null) { |
| 222 | 0 | return ""; |
| 223 | |
} |
| 224 | |
|
| 225 | 0 | StringBuilder sb = new StringBuilder(); |
| 226 | 0 | if(!reduce && statement.getRequiredComponents().size() > 1) |
| 227 | |
{ |
| 228 | 0 | sb.append("("); |
| 229 | |
} |
| 230 | 0 | for(Iterator<ReqComponent> it = statement.getRequiredComponents().iterator(); it.hasNext(); ) { |
| 231 | 0 | ReqComponent reqComponent = it.next(); |
| 232 | 0 | sb.append(getReqComponentReferenceId(reqComponent)); |
| 233 | 0 | if (it.hasNext()) { |
| 234 | 0 | sb.append(" "); |
| 235 | 0 | sb.append(getOperator(statement.getOperator())); |
| 236 | 0 | sb.append(" "); |
| 237 | |
} |
| 238 | 0 | } |
| 239 | 0 | if(!reduce && statement.getRequiredComponents().size() > 1) |
| 240 | |
{ |
| 241 | 0 | sb.append(")"); |
| 242 | |
} |
| 243 | 0 | return sb.toString(); |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
private String getOperator(StatementOperatorTypeKey operator) throws OperationFailedException { |
| 254 | 0 | switch(operator) { |
| 255 | |
case AND: |
| 256 | 0 | return this.andOperator; |
| 257 | |
case OR: |
| 258 | 0 | 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 | 0 | if(statement.getId() == null || statement.getId().isEmpty()) { |
| 273 | 0 | throw new OperationFailedException("Statement id cannot be null"); |
| 274 | |
} |
| 275 | |
|
| 276 | 0 | if(this.idMap.containsKey(statement.getId())) { |
| 277 | 0 | return this.idMap.get(statement.getId()); |
| 278 | |
} |
| 279 | 0 | String id = STATEMENT_ID + this.idCounter++; |
| 280 | 0 | this.idMap.put(statement.getId(), id); |
| 281 | 0 | return id; |
| 282 | |
} |
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
private String getReqComponentReferenceId(ReqComponent reqComponent) throws OperationFailedException { |
| 292 | 0 | if(reqComponent.getId() == null || reqComponent.getId().isEmpty()) { |
| 293 | 0 | throw new OperationFailedException("Requirement component id cannot be null"); |
| 294 | |
} |
| 295 | |
|
| 296 | 0 | if(this.idMap.containsKey(reqComponent.getId())) { |
| 297 | 0 | return this.idMap.get(reqComponent.getId()); |
| 298 | |
} |
| 299 | 0 | String id = REC_COMPONENT_ID + this.idCounter++; |
| 300 | 0 | this.idMap.put(reqComponent.getId(), id); |
| 301 | 0 | return id; |
| 302 | |
} |
| 303 | |
} |