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
11
12 public class FailureMessageBuilder {
13
14 private BooleanOperators booleanOperators;
15
16
17
18
19
20
21
22 public FailureMessageBuilder(BooleanOperators bo) {
23 this.booleanOperators = bo;
24 }
25
26
27
28
29
30
31
32
33 public String buildFailureMessage(List<BooleanNode> nodeList) throws MessageBuilderException {
34
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
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
54
55
56
57
58 public String buildFailureMessage(BooleanNode node) {
59
60 if(node.getLabel().equals("+")){
61 buildOrNodeFailureMessage(node);
62 }
63
64 else if(node.getLabel().equals("*")) {
65 buildAnd1NodeFailureMessage(node);
66 buildAnd2NodeFailureMessage(node);
67 }
68 return node.getNodeMessage();
69 }
70
71
72
73
74
75
76 private void buildOrNodeFailureMessage(BooleanNode node) {
77
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
102
103
104
105
106
107 private void buildAnd1NodeFailureMessage(BooleanNode node) {
108
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
131
132
133
134
135 private void buildAnd2NodeFailureMessage(BooleanNode node) {
136
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 }