View Javadoc

1   package org.kuali.student.common.messagebuilder.impl;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanNode;
7   import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException;
8   
9   /**
10   * This class creates failure messages for boolean binary tree nodes.
11   */
12  public class FailureMessageBuilder {
13  	/** Boolean operators to use in creating the failure message */
14  	private BooleanOperators booleanOperators;
15  
16  	/**
17  	 * Creates a failure message builder with boolean operators to use in 
18  	 * building the success message.
19  	 * 
20  	 * @param bo Boolean operators to build success message
21  	 */
22  	public FailureMessageBuilder(BooleanOperators bo) {
23  		this.booleanOperators = bo;
24  	}
25  
26  	/**
27  	 * Builds and sets the failure message for each of the 
28  	 * boolean nodes (binary tree) in the <code>nodeList</code>.
29  	 * 
30  	 * @param nodeList List of boolean nodes
31  	 * @return Complete failure message 
32  	 */
33  	public String buildFailureMessage(List<BooleanNode> nodeList) throws MessageBuilderException {
34  		// List must only contain one root node
35  		List<BooleanNode> rootNodeList = new ArrayList<BooleanNode>();
36  		for(BooleanNode node : nodeList) {
37  			if(node.getParent() == null) {
38  				rootNodeList.add(node);
39  			}
40  			buildFailureMessage(node);
41  		}
42          //Code Changed for JIRA-9075 - SONAR Critical issues - Use get(0) with caution - 5
43          int firstBooleanNode = 0;
44  		if(rootNodeList.size() > 1) {
45  			throw new MessageBuilderException("Node list contains more than one root node: " + rootNodeList);
46  		} else if (rootNodeList.isEmpty()) {
47              throw new MessageBuilderException("Node list contains no root node: " + rootNodeList);
48          }
49  		return rootNodeList.get(firstBooleanNode).getNodeMessage();
50  	}
51  
52  	/**
53  	 * Builds and sets the failure message for a single 
54  	 * boolean <code>node</code> (b-tree node).
55  	 * 
56  	 * @param node Boolean node
57  	 */
58  	public String buildFailureMessage(BooleanNode node) {
59  		// OR
60  		if(node.getLabel().equals("+")){
61  			buildOrNodeFailureMessage(node);
62  		}
63  		// AND
64  		else if(node.getLabel().equals("*")) {
65  			buildAnd1NodeFailureMessage(node);
66  			buildAnd2NodeFailureMessage(node);
67  		}
68  		return node.getNodeMessage();
69  	}
70  
71  	/**
72  	 * Builds a failure message for an OR node (b-tree node).
73  	 * 
74  	 * @param node Boolean node
75  	 */
76  	private void buildOrNodeFailureMessage(BooleanNode node) {
77  		// OR
78  		if(node.getLabel().equals("+") &&
79  				node.getLeftNode() != null && 
80  				node.getRightNode() != null &&
81  				node.getLeftNode().getValue() == false && 
82  				node.getRightNode().getValue() == false &&
83  				node.getLeftNode().getNodeMessage() != null && 
84  				node.getRightNode().getNodeMessage() != null) {
85  	        String logMessage = node.getLeftNode().getNodeMessage() + " " + 
86  	        	this.booleanOperators.getOrOperator() + " " + 
87  	        	node.getRightNode().getNodeMessage();
88  
89  	        if(node.getParent() != null && 
90  					( (node.getLabel().equals("+") && 
91  							node.getParent().getLabel().equals("*")) || 
92  							(node.getLabel().equals("*") && 
93  									node.getParent().getLabel().equals("+")))) {
94  			    logMessage = "(" + logMessage + ")";
95  			}
96  			node.setNodeMessage(logMessage);
97  		}
98  	}
99  
100 	/**
101 	 * Builds a failure message for an AND node (b-tree node) where 
102 	 * left node is true and right node is false or 
103 	 * left node is false and right node is true.
104 	 * 
105 	 * @param node Boolean node
106 	 */
107 	private void buildAnd1NodeFailureMessage(BooleanNode node) {
108 		// AND1
109 		if(node.getLabel().equals("*") &&
110 				node.getLeftNode() != null && 
111 				node.getRightNode() != null &&
112 				((node.getLeftNode().getValue() == false && 
113 						node.getRightNode().getValue() == true && 
114 						node.getLeftNode().getNodeMessage() != null ) || 
115 				(node.getLeftNode().getValue() == true && 
116 						node.getRightNode().getValue() == false && 
117 						node.getRightNode().getNodeMessage() != null))) {
118 			String logMessage = "test";
119 			
120 			if (node.getLeftNode().getValue() == false)
121 				logMessage = node.getLeftNode().getNodeMessage();
122 			else if (node.getRightNode().getValue() == false)
123 				logMessage = node.getRightNode().getNodeMessage();
124 			
125 			node.setNodeMessage(logMessage);
126 		}
127 	}
128 
129 	/**
130 	 * Builds a failure message for an AND node (b-tree node) where 
131 	 * left node and right node are false.
132 	 * 
133 	 * @param node Boolean node
134 	 */
135 	private void buildAnd2NodeFailureMessage(BooleanNode node) {
136 		// AND2
137 		if(node.getLabel().equals("*") && 
138 				node.getLeftNode() != null && 
139 				node.getRightNode() != null &&
140 				node.getLeftNode().getValue() == false && 
141 				node.getRightNode().getValue() == false && 
142 				node.getLeftNode().getNodeMessage() != null && 
143 				node.getRightNode().getNodeMessage() != null) {
144 	        String logMessage = node.getLeftNode().getNodeMessage() + " " + 
145 	        	this.booleanOperators.getAndOperator() + " " + 
146 	        	node.getRightNode().getNodeMessage();
147 			
148 			if(node.getParent() != null && 
149 					( (node.getLabel().equals("+") && 
150 							node.getParent().getLabel().equals("*")) || 
151 							(node.getLabel().equals("*") && 
152 									node.getParent().getLabel().equals("+")))) {
153 			    logMessage = "(" + logMessage + ")";
154 			}
155 			node.setNodeMessage(logMessage);
156 		}
157 	}
158 }