|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BinaryMessageTree | Line # 38 | 52 | 0% | 22 | 8 | 90.4% |
0.90361446
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(44) | |||
Result | |||
0.8674699
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement5 org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement5 | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage2_True org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage2_True | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage8 org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage8 | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage13 org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage13 | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage9 org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage9 | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage10 org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage10 | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage12 org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage12 | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage4_False org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage4_False | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage5_True org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage5_True | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage6 org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage6 | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage7 org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage7 | 1 PASS | |
0.8674699
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement6 org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement6 | 1 PASS | |
0.8674699
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage11 org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage11 | 1 PASS | |
0.8192771
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate2_NullContextMap_False org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate2_NullContextMap_False | 1 PASS | |
0.8192771
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage1_True org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage1_True | 1 PASS | |
0.8192771
|
org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageMessageBuilderTest.testConstructor1_BuildMessage_Success org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageMessageBuilderTest.testConstructor1_BuildMessage_Success | 1 PASS | |
0.8192771
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate4_False org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate4_False | 1 PASS | |
0.8192771
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement4 org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement4 | 1 PASS | |
0.8192771
|
org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageMessageBuilderTest.testConstructor2_BuildMessage_Success org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageMessageBuilderTest.testConstructor2_BuildMessage_Success | 1 PASS | |
0.8192771
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate1_NullContextMap_True org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate1_NullContextMap_True | 1 PASS | |
0.8192771
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate3_True org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate3_True | 1 PASS | |
0.8192771
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage3_False org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage3_False | 1 PASS | |
0.79518074
|
org.kuali.student.core.statement.service.impl.TestStatementServiceImpl.testGetNaturalLanguageForRefStatementRelation org.kuali.student.core.statement.service.impl.TestStatementServiceImpl.testGetNaturalLanguageForRefStatementRelation | 1 PASS | |
0.79518074
|
org.kuali.student.core.statement.service.impl.TestStatementServiceImpl.testGetNaturalLanguageForStatement org.kuali.student.core.statement.service.impl.TestStatementServiceImpl.testGetNaturalLanguageForStatement | 1 PASS | |
0.79518074
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement3 org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement3 | 1 PASS | |
0.79518074
|
org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageTranslatorTest.testTranslateStatement_EnglishGerman org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageTranslatorTest.testTranslateStatement_EnglishGerman | 1 PASS | |
0.79518074
|
org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageTranslatorTest.testTranslateStatement_English org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageTranslatorTest.testTranslateStatement_English | 1 PASS | |
0.79518074
|
org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageTranslatorTest.testTranslateStatement_German org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageTranslatorTest.testTranslateStatement_German | 1 PASS | |
0.79518074
|
org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageMessageBuilderTest.testConstructor3_BuildMessage_Success org.kuali.student.core.statement.naturallanguage.translators.NaturalLanguageMessageBuilderTest.testConstructor3_BuildMessage_Success | 1 PASS | |
0.60240966
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_English_NullCluId org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_English_NullCluId | 1 PASS | |
0.60240966
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_English org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_English | 1 PASS | |
0.60240966
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatementId1 org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatementId1 | 1 PASS | |
0.60240966
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_EnglishGerman org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_EnglishGerman | 1 PASS | |
0.60240966
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement2 org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement2 | 1 PASS | |
0.60240966
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_German org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_German | 1 PASS | |
0.60240966
|
org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_English_EmptyCluId org.kuali.student.core.statement.naturallanguage.translators.StatementTranslatorTest.testTranslateStatement1_English_EmptyCluId | 1 PASS | |
0.5903614
|
org.kuali.student.common.messagebuilder.booleanmessage.ast.BinaryMessageTreeTest.testTraverseTreePostOrder_GetAllNodes org.kuali.student.common.messagebuilder.booleanmessage.ast.BinaryMessageTreeTest.testTraverseTreePostOrder_GetAllNodes | 1 PASS | |
0.38554215
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate5_False_Exception org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessageTemplate5_False_Exception | 1 PASS | |
0.38554215
|
org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanFunctionTest.testVariables org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanFunctionTest.testVariables | 1 PASS | |
0.38554215
|
org.kuali.student.common.messagebuilder.booleanmessage.ast.BinaryMessageTreeTest.testTraverseTreePostOrderDontSetNode_GetAllNodes2 org.kuali.student.common.messagebuilder.booleanmessage.ast.BinaryMessageTreeTest.testTraverseTreePostOrderDontSetNode_GetAllNodes2 | 1 PASS | |
0.22891566
|
org.kuali.student.common.messagebuilder.booleanmessage.ast.BinaryMessageTreeTest.testGetRoot org.kuali.student.common.messagebuilder.booleanmessage.ast.BinaryMessageTreeTest.testGetRoot | 1 PASS | |
0.21686748
|
org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage1_InvalidBooleanExpression org.kuali.student.common.messagebuilder.impl.MessageBuilderImplTest.testBuildMessage1_InvalidBooleanExpression | 1 PASS | |
0.20481928
|
org.kuali.student.common.messagebuilder.booleanmessage.ast.BinaryMessageTreeTest.testBuildTree org.kuali.student.common.messagebuilder.booleanmessage.ast.BinaryMessageTreeTest.testBuildTree | 1 PASS | |
0.19277108
|
org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanFunctionTest.testSymbols org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanFunctionTest.testSymbols | 1 PASS | |
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 | package org.kuali.student.common.messagebuilder.booleanmessage.ast; | |
17 | ||
18 | import java.util.ArrayList; | |
19 | import java.util.List; | |
20 | import java.util.Locale; | |
21 | import java.util.Map; | |
22 | ||
23 | import org.antlr.runtime.ANTLRStringStream; | |
24 | import org.antlr.runtime.CommonTokenStream; | |
25 | import org.antlr.runtime.RecognitionException; | |
26 | import org.antlr.runtime.Token; | |
27 | import org.antlr.runtime.tree.CommonTreeAdaptor; | |
28 | import org.antlr.runtime.tree.TreeAdaptor; | |
29 | ||
30 | import org.kuali.student.common.messagebuilder.booleanmessage.BooleanMessage; | |
31 | import org.kuali.student.common.messagebuilder.booleanmessage.ast.exceptions.BooleanFunctionException; | |
32 | import org.kuali.student.common.messagebuilder.booleanmessage.ast.parsers.BooleanFunctionLexer; | |
33 | import org.kuali.student.common.messagebuilder.booleanmessage.ast.parsers.BooleanFunctionParser; | |
34 | ||
35 | import org.slf4j.Logger; | |
36 | import org.slf4j.LoggerFactory; | |
37 | ||
38 | public class BinaryMessageTree { | |
39 | /** SLF4J logging framework */ | |
40 | final static Logger logger = LoggerFactory.getLogger(BinaryMessageTree.class); | |
41 | ||
42 | private BooleanNode root; | |
43 | private ArrayList<BooleanNode> nodes; | |
44 | private String language; | |
45 | ||
46 | private Map<String, ? extends BooleanMessage> nodeMessageMap; | |
47 | ||
48 | private static final TreeAdaptor adaptor = new CommonTreeAdaptor() { | |
49 | 1144 | public Object create(Token payload) { |
50 | 1144 | return new BooleanNode(payload); |
51 | } | |
52 | }; | |
53 | ||
54 | /** | |
55 | * Constructor. | |
56 | */ | |
57 | 47 | public BinaryMessageTree() { |
58 | 47 | language = Locale.getDefault().getLanguage(); |
59 | 47 | nodes = new ArrayList<BooleanNode>(); |
60 | } | |
61 | ||
62 | /** | |
63 | * Constructor. | |
64 | * Creates a new binary message tree for the in the specified language | |
65 | * for the boolean message map. | |
66 | * | |
67 | * @param messageLanguage Language for the message | |
68 | * @param messageMap map of boolean messages | |
69 | */ | |
70 | 45 | public BinaryMessageTree(String language, Map<String, ? extends BooleanMessage> messageMap) { |
71 | 45 | this.language = language; |
72 | 45 | nodeMessageMap = messageMap; |
73 | 45 | nodes = new ArrayList<BooleanNode>(); |
74 | } | |
75 | ||
76 | /** | |
77 | * <p>Builds the binary message tree from the <code>booleanFunction</code>.</p> | |
78 | * <p>Order of boolean operation: ANDs before ORs and | |
79 | * operations inside parentheses before anything else.</p> | |
80 | * <p>Example: A AND B OR C AND D evaluates to (A AND B) OR (C AND D).</p> | |
81 | * | |
82 | * @param booleanFunction: Boolean expression | |
83 | * @return Root boolean node | |
84 | */ | |
85 | 92 | public BooleanNode buildTree(String booleanFunction){ |
86 | 92 | if (booleanFunction == null || booleanFunction.trim().isEmpty()) { |
87 | 0 | return null; |
88 | } | |
89 | ||
90 | 92 | BooleanFunctionLexer lex = new BooleanFunctionLexer(new ANTLRStringStream(booleanFunction) ); |
91 | 92 | CommonTokenStream tokens = new CommonTokenStream(lex); |
92 | ||
93 | 92 | BooleanFunctionParser parser = new BooleanFunctionParser(tokens); |
94 | 92 | parser.setTreeAdaptor(adaptor); |
95 | ||
96 | // parse the expression | |
97 | 92 | BooleanFunctionParser.booleanExpression_return booleanExpression = null; |
98 | 92 | try { |
99 | 92 | booleanExpression = parser.booleanExpression(); |
100 | } catch (RecognitionException e) { | |
101 | 1 | String parserErrorMsg = parser.getErrorMessage(e, parser.getTokenNames()); |
102 | 1 | String errorMsg = "Boolean Function Parser Error. " + |
103 | "Invalid Boolean Expression: '" + booleanFunction + "'; " + parserErrorMsg; | |
104 | 1 | throw new BooleanFunctionException(errorMsg, e); |
105 | } | |
106 | // get the root of the AST from the parsed expression | |
107 | 91 | return root = (BooleanNode) booleanExpression.getTree(); |
108 | } | |
109 | ||
110 | /** | |
111 | * This method walks the tree depth first, while setting node values with | |
112 | * setNode(). Setting a nodes parent and adding the node to the ArrayList | |
113 | * for later. This method has to be called after buildTree. | |
114 | * | |
115 | * @param bnode Boolean node | |
116 | * @param parent Parent boolean node | |
117 | */ | |
118 | 185 | public void traverseTreePostOrder(BooleanNode bnode, BooleanNode parent) { |
119 | 327 | for (int i = 0; i < bnode.getChildCount(); i++) { |
120 | 142 | traverseTreePostOrder((BooleanNode) bnode.getChild(i), bnode); |
121 | } | |
122 | 185 | setNode(bnode); |
123 | 185 | if (parent != null) { |
124 | 142 | bnode.setParent(parent); |
125 | } | |
126 | ||
127 | 185 | if(logger.isDebugEnabled()) { |
128 | 0 | logger.debug(bnode.getText()); |
129 | } | |
130 | 185 | nodes.add(bnode); |
131 | } | |
132 | ||
133 | /** | |
134 | * This method walks the tree depth first. Setting a nodes parent and | |
135 | * adding the node to the ArrayList for later. | |
136 | * This method has to be called after buildTree. | |
137 | * | |
138 | * @param bnode Boolean node | |
139 | * @param parent Parent boolean node | |
140 | */ | |
141 | 195 | public void traverseTreePostOrderDontSetNode(BooleanNode bnode, BooleanNode parent) { |
142 | 195 | if (bnode != null) { |
143 | 345 | for (int i = 0; i < bnode.getChildCount(); i++ ) { |
144 | 150 | traverseTreePostOrderDontSetNode((BooleanNode)bnode.getChild(i), bnode); |
145 | } | |
146 | ||
147 | 195 | if (parent != null) { |
148 | 150 | bnode.setParent(parent); |
149 | } | |
150 | ||
151 | 195 | if(logger.isDebugEnabled()) { |
152 | 0 | logger.debug(bnode.getText()); |
153 | } | |
154 | 195 | nodes.add(bnode); |
155 | } | |
156 | } | |
157 | ||
158 | /** | |
159 | * Here we set the value(true or false) and the failure message for each node. | |
160 | * Setting the failure message here does not mean the function failed, the node just holds the message. | |
161 | * Failure is determined by the rules engine which will extract the failure message. | |
162 | * | |
163 | * @param bnode Boolean node | |
164 | */ | |
165 | 185 | private void setNode(BooleanNode bnode) { |
166 | 185 | bnode.setLanguage(language); |
167 | ||
168 | 185 | if (bnode.getChildCount() == 0) { |
169 | // If node is a leaf then set value and message | |
170 | 114 | BooleanMessage message = nodeMessageMap.get(bnode.getLabel()); |
171 | 114 | bnode.setValue(message.isSuccesful()); |
172 | 114 | bnode.setNodeMessage(message.getMessage()); |
173 | } | |
174 | else { | |
175 | // If node is intermediate, compute value and set it | |
176 | 71 | BooleanNode child0 = (BooleanNode)bnode.getChild(0); |
177 | 71 | BooleanNode child1 = (BooleanNode)bnode.getChild(1); |
178 | ||
179 | 71 | if ( bnode.getLabel().equalsIgnoreCase("+") ) { |
180 | // OR node | |
181 | 33 | Boolean newValue = child0.getValue() || child1.getValue(); |
182 | 33 | bnode.setValue(newValue); |
183 | 33 | bnode.setNodeMessage("null"); |
184 | } | |
185 | 38 | else if ( bnode.getLabel().equalsIgnoreCase("*") ) { |
186 | // AND node | |
187 | 38 | Boolean newValue = child0.getValue() && child1.getValue(); |
188 | 38 | bnode.setValue(newValue); |
189 | 38 | bnode.setNodeMessage("null"); |
190 | } | |
191 | } | |
192 | } | |
193 | ||
194 | /** | |
195 | * Gets all boolean nodes. | |
196 | * | |
197 | * @return All boolean nodes | |
198 | */ | |
199 | 88 | public List<BooleanNode> getAllNodes() { |
200 | 88 | return nodes; |
201 | } | |
202 | ||
203 | /** | |
204 | * Gets the root boolean node. | |
205 | * | |
206 | * @return Root boolean node | |
207 | */ | |
208 | 85 | public BooleanNode getRoot() { |
209 | 85 | return root; |
210 | } | |
211 | } |
|