| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BooleanFunction |
|
| 3.6666666666666665;3.667 |
| 1 | /** | |
| 2 | * Copyright 2010 The Kuali Foundation Licensed under the | |
| 3 | * Educational Community License, Version 2.0 (the "License"); you may | |
| 4 | * not use this file except in compliance with the License. You may | |
| 5 | * obtain a copy of the License at | |
| 6 | * | |
| 7 | * http://www.osedu.org/licenses/ECL-2.0 | |
| 8 | * | |
| 9 | * Unless required by applicable law or agreed to in writing, | |
| 10 | * software distributed under the License is distributed on an "AS IS" | |
| 11 | * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
| 12 | * or implied. See the License for the specific language governing | |
| 13 | * permissions and limitations under the License. | |
| 14 | */ | |
| 15 | ||
| 16 | /** | |
| 17 | * | |
| 18 | */ | |
| 19 | package org.kuali.student.common.messagebuilder.booleanmessage.ast; | |
| 20 | ||
| 21 | import java.util.ArrayList; | |
| 22 | import java.util.List; | |
| 23 | ||
| 24 | public class BooleanFunction { | |
| 25 | private String booleanExpression; | |
| 26 | private BinaryMessageTree ASTtree; | |
| 27 | private BooleanNode root; | |
| 28 | 0 | private ArrayList<String> funcVars = new ArrayList<String>(); |
| 29 | 0 | private ArrayList<String> symbols = new ArrayList<String>(); |
| 30 | ||
| 31 | 0 | public BooleanFunction(String booleanExpression) { |
| 32 | // Since the AST calls the Boolean Function parser, | |
| 33 | // calling the AST here asserts the function is valid | |
| 34 | 0 | ASTtree = new BinaryMessageTree(); |
| 35 | 0 | root = ASTtree.buildTree(booleanExpression); |
| 36 | 0 | this.booleanExpression = booleanExpression; |
| 37 | 0 | } |
| 38 | ||
| 39 | /** | |
| 40 | * Parse function string to get the variables | |
| 41 | * 1. Need this function at rule creation time (i.e when in BRMS) to insert | |
| 42 | * the action part of the rules | |
| 43 | * 2. Also need this at runtime to populate the HashMaps in the | |
| 44 | * proposition which are used as tree leaf nodes in the AST. | |
| 45 | * | |
| 46 | * @return List of variables | |
| 47 | */ | |
| 48 | public List<String> getVariables() { | |
| 49 | 0 | ASTtree.traverseTreePostOrderDontSetNode(root, null); |
| 50 | 0 | List<BooleanNode> treeNodes = ASTtree.getAllNodes(); |
| 51 | ||
| 52 | 0 | for (BooleanNode bnode : treeNodes) { |
| 53 | 0 | String label = bnode.getLabel(); |
| 54 | 0 | if (label.equals("+") || label.equals("*")) { |
| 55 | 0 | continue; |
| 56 | } | |
| 57 | 0 | funcVars.add(label); |
| 58 | 0 | } |
| 59 | 0 | return funcVars; |
| 60 | } | |
| 61 | ||
| 62 | /** | |
| 63 | * Parse function string to get all the symbols. | |
| 64 | * Need this only at rule creation time. Can't load the symbols in | |
| 65 | * getVariables() because the AST removes the parenthesis and | |
| 66 | * we need those. Get them from the string which has been | |
| 67 | * validated in the constructor example A0*B4+(C*D) | |
| 68 | * | |
| 69 | * @return List of symbols | |
| 70 | */ | |
| 71 | public List<String> getSymbols() { | |
| 72 | 0 | String workString = null; |
| 73 | // remove spaces | |
| 74 | 0 | this.booleanExpression = (booleanExpression == null ? null : booleanExpression.replaceAll("\\s+", "")); |
| 75 | ||
| 76 | 0 | workString = this.booleanExpression; |
| 77 | ||
| 78 | 0 | while (workString != null && workString.length() > 0) { |
| 79 | 0 | String propositionKeyPattern = "^[A-Z][0-9]*"; |
| 80 | 0 | String restOfFunctionString = null; |
| 81 | 0 | String theSymbol = null; |
| 82 | 0 | int restOfFunctionStringStartIndex = 0; |
| 83 | 0 | if (workString.matches(propositionKeyPattern + ".*")) { |
| 84 | 0 | restOfFunctionString = workString.replaceFirst(propositionKeyPattern, ""); |
| 85 | 0 | restOfFunctionStringStartIndex = workString.indexOf(restOfFunctionString); |
| 86 | 0 | if (restOfFunctionStringStartIndex <= 0) { |
| 87 | 0 | theSymbol = workString; |
| 88 | 0 | workString = ""; |
| 89 | } else { | |
| 90 | 0 | theSymbol = workString.substring(0, restOfFunctionStringStartIndex); |
| 91 | 0 | workString = restOfFunctionString; |
| 92 | } | |
| 93 | } else { | |
| 94 | 0 | theSymbol = workString.substring(0, 1); |
| 95 | 0 | workString = workString.substring(1, workString.length()); |
| 96 | } | |
| 97 | 0 | symbols.add(theSymbol); |
| 98 | 0 | } |
| 99 | 0 | return symbols; |
| 100 | } | |
| 101 | } |