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  		if(rootNodeList.size() > 1) {
43  			throw new MessageBuilderException("Node list contains more than one root node: " + rootNodeList);
44  		}
45  		return rootNodeList.get(0).getNodeMessage();
46  	}
47  
48  	/**
49  	 * Builds and sets the failure message for a single 
50  	 * boolean <code>node</code> (b-tree node).
51  	 * 
52  	 * @param node Boolean node
53  	 */
54  	public String buildFailureMessage(BooleanNode node) {
55  		// OR
56  		if(node.getLabel().equals("+")){
57  			buildOrNodeFailureMessage(node);
58  		}
59  		// AND
60  		else if(node.getLabel().equals("*")) {
61  			buildAnd1NodeFailureMessage(node);
62  			buildAnd2NodeFailureMessage(node);
63  		}
64  		return node.getNodeMessage();
65  	}
66  
67  	/**
68  	 * Builds a failure message for an OR node (b-tree node).
69  	 * 
70  	 * @param node Boolean node
71  	 */
72  	private void buildOrNodeFailureMessage(BooleanNode node) {
73  		// OR
74  		if(node.getLabel().equals("+") &&
75  				node.getLeftNode() != null && 
76  				node.getRightNode() != null &&
77  				node.getLeftNode().getValue() == false && 
78  				node.getRightNode().getValue() == false &&
79  				node.getLeftNode().getNodeMessage() != null && 
80  				node.getRightNode().getNodeMessage() != null) {
81  	        String logMessage = node.getLeftNode().getNodeMessage() + " " + 
82  	        	this.booleanOperators.getOrOperator() + " " + 
83  	        	node.getRightNode().getNodeMessage();
84  
85  	        if(node.getParent() != null && 
86  					( (node.getLabel().equals("+") && 
87  							node.getParent().getLabel().equals("*")) || 
88  							(node.getLabel().equals("*") && 
89  									node.getParent().getLabel().equals("+")))) {
90  			    logMessage = "(" + logMessage + ")";
91  			}
92  			node.setNodeMessage(logMessage);
93  		}
94  	}
95  
96  	/**
97  	 * Builds a failure message for an AND node (b-tree node) where 
98  	 * left node is true and right node is false or 
99  	 * left node is false and right node is true.
100 	 * 
101 	 * @param node Boolean node
102 	 */
103 	private void buildAnd1NodeFailureMessage(BooleanNode node) {
104 		// AND1
105 		if(node.getLabel().equals("*") &&
106 				node.getLeftNode() != null && 
107 				node.getRightNode() != null &&
108 				((node.getLeftNode().getValue() == false && 
109 						node.getRightNode().getValue() == true && 
110 						node.getLeftNode().getNodeMessage() != null ) || 
111 				(node.getLeftNode().getValue() == true && 
112 						node.getRightNode().getValue() == false && 
113 						node.getRightNode().getNodeMessage() != null))) {
114 			String logMessage = "test";
115 			
116 			if (node.getLeftNode().getValue() == false)
117 				logMessage = node.getLeftNode().getNodeMessage();
118 			else if (node.getRightNode().getValue() == false)
119 				logMessage = node.getRightNode().getNodeMessage();
120 			
121 			node.setNodeMessage(logMessage);
122 		}
123 	}
124 
125 	/**
126 	 * Builds a failure message for an AND node (b-tree node) where 
127 	 * left node and right node are false.
128 	 * 
129 	 * @param node Boolean node
130 	 */
131 	private void buildAnd2NodeFailureMessage(BooleanNode node) {
132 		// AND2
133 		if(node.getLabel().equals("*") && 
134 				node.getLeftNode() != null && 
135 				node.getRightNode() != null &&
136 				node.getLeftNode().getValue() == false && 
137 				node.getRightNode().getValue() == false && 
138 				node.getLeftNode().getNodeMessage() != null && 
139 				node.getRightNode().getNodeMessage() != null) {
140 	        String logMessage = node.getLeftNode().getNodeMessage() + " " + 
141 	        	this.booleanOperators.getAndOperator() + " " + 
142 	        	node.getRightNode().getNodeMessage();
143 			
144 			if(node.getParent() != null && 
145 					( (node.getLabel().equals("+") && 
146 							node.getParent().getLabel().equals("*")) || 
147 							(node.getLabel().equals("*") && 
148 									node.getParent().getLabel().equals("+")))) {
149 			    logMessage = "(" + logMessage + ")";
150 			}
151 			node.setNodeMessage(logMessage);
152 		}
153 	}
154 }