2020import static org .junit .jupiter .api .Assertions .*;
2121
2222/**
23- * Validates {@link Neo4jHeuristicsCalculator} end-to-end (parser → calculator) against two worked
23+ * Validates {@link Neo4jHeuristicsCalculator} end-to-end (parser -> calculator) against two worked
2424 * examples, plus unit checks of the building blocks. Queries here are already in canonical form (no
2525 * quantified path patterns, no variable-length edges) — that expansion is a separate, later step.
2626 * <p>
3131 */
3232class Neo4jHeuristicsCalculatorTest {
3333
34+ private static final double DELTA = 1e-9 ;
35+
3436 private final CypherParser parser = CypherParserFactory .buildParser ();
3537 private final Neo4jHeuristicsCalculator calculator = new Neo4jHeuristicsCalculator ();
3638
@@ -62,7 +64,7 @@ void testExample1NotSatisfiedButStrongGradient() throws CypherParserException {
6264
6365 // Not satisfied: b.age (28) is not > 30 on any mapping.
6466 assertFalse (h .isTrue ());
65- assertEquals (1.0 , h .getOfFalse (), 1e-9 );
67+ assertEquals (1.0 , h .getOfFalse (), DELTA );
6668 // Structure fully available and the best mapping satisfies 4 of 5 conditions: close to satisfied.
6769 assertTrue (h .getOfTrue () > 0.9 );
6870 }
@@ -73,17 +75,17 @@ void testComputeDistanceMirrorsHeuristic() throws CypherParserException {
7375 Neo4jGraph g = example1Graph (28 );
7476 // The distance reported to the fitness DTO is 1 - ofTrue, and is positive for an unsatisfied query.
7577 double distance = calculator .computeDistance (q , g );
76- assertEquals (1.0 - calculator .computeHeuristic (q , g ).getOfTrue (), distance , 1e-9 );
78+ assertEquals (1.0 - calculator .computeHeuristic (q , g ).getOfTrue (), distance , DELTA );
7779 assertTrue (distance > 0 );
7880 }
7981
8082 @ Test
8183 void testExample1SatisfiedAfterAgeMutation () throws CypherParserException {
8284 MatchOperation q = parser .parse (EXAMPLE_QUERY );
83- // Mutating Luis's age 28 → 31 makes the best mapping (a→ n1, b→ n2) fully satisfy the query.
85+ // Mutating Luis age 28 -> 31 makes the best mapping (a-> n1, b-> n2) fully satisfy the query.
8486 Truthness h = calculator .computeHeuristic (q , example1Graph (31 ));
8587 assertTrue (h .isTrue ());
86- assertEquals (1.0 , h .getOfTrue (), 1e-9 );
88+ assertEquals (1.0 , h .getOfTrue (), DELTA );
8789 }
8890
8991 // Worked example 2: partial match, clear gradient
@@ -103,7 +105,7 @@ void testExample2PartialMatch() throws CypherParserException {
103105 Truthness h = calculator .computeHeuristic (q , example2Graph ());
104106
105107 assertFalse (h .isTrue ());
106- assertEquals (1.0 , h .getOfFalse (), 1e-9 );
108+ assertEquals (1.0 , h .getOfFalse (), DELTA );
107109 // Partial match: a clear gradient, well above the unsatisfiable floor.
108110 assertTrue (h .getOfTrue () > 0.8 );
109111 }
@@ -129,58 +131,54 @@ void testNoStructuralMatchStillScoresNodesByCount() throws CypherParserException
129131 Neo4jGraph g = new Neo4jGraph (Arrays .asList (a , b ), Collections .emptyList ());
130132 Truthness h = calculator .computeHeuristic (q , g );
131133 assertFalse (h .isTrue ());
132- assertEquals (1.0 , h .getOfFalse (), 1e-9 );
134+ assertEquals (1.0 , h .getOfFalse (), DELTA );
133135 assertTrue (h .getOfTrue () > Neo4jHeuristicsCalculator .C );
134136 }
135137
136- // Unit checks of the building blocks
137-
138138 @ Test
139139 void testHMatchNodesCountBased () {
140- // enough nodes → TRUE
140+ // enough nodes -> TRUE
141141 assertTrue (calculator .computeHeuristicMatchNodes (2 , 4 ).isTrue ());
142142 assertTrue (calculator .computeHeuristicMatchNodes (2 , 2 ).isTrue ());
143- // no nodes in graph → FALSE
144- assertEquals (1.0 , calculator .computeHeuristicMatchNodes (2 , 0 ).getOfFalse (), 1e-9 );
143+ // no nodes in graph -> FALSE
144+ assertEquals (1.0 , calculator .computeHeuristicMatchNodes (2 , 0 ).getOfFalse (), DELTA );
145145 assertFalse (calculator .computeHeuristicMatchNodes (2 , 0 ).isTrue ());
146- // no nodes required → TRUE
146+ // no nodes required -> TRUE
147147 assertTrue (calculator .computeHeuristicMatchNodes (0 , 0 ).isTrue ());
148- // partial availability → scaled in (C, 1)
148+ // partial availability -> scaled in (C, 1)
149149 Truthness partial = calculator .computeHeuristicMatchNodes (4 , 2 );
150- assertEquals (1.0 , partial .getOfFalse (), 1e-9 );
151- assertEquals (Neo4jHeuristicsCalculator .C + 0.9 * (2.0 / 4.0 ), partial .getOfTrue (), 1e-9 );
150+ assertEquals (1.0 , partial .getOfFalse (), DELTA );
151+ assertEquals (Neo4jHeuristicsCalculator .C + 0.9 * (2.0 / 4.0 ), partial .getOfTrue (), DELTA );
152152 }
153153
154154 @ Test
155155 void testStringEqualityTruthness () {
156156 assertTrue (Neo4jConditionEvaluator .stringEqualityTruthness ("KNOWS" , "KNOWS" ).isTrue ());
157157 Truthness diff = Neo4jConditionEvaluator .stringEqualityTruthness ("KNOWS" , "LIKES" );
158- assertEquals (1.0 , diff .getOfFalse (), 1e-9 );
158+ assertEquals (1.0 , diff .getOfFalse (), DELTA );
159159 assertTrue (diff .getOfTrue () < 1.0 );
160160 }
161161
162162 @ Test
163163 void testLabelInSet () {
164164 assertTrue (Neo4jConditionEvaluator .labelInSet ("Person" , labels ("Person" )).isTrue ());
165165 assertEquals (1.0 ,
166- Neo4jConditionEvaluator .labelInSet ("Person" , labels ()).getOfFalse (), 1e-9 );
166+ Neo4jConditionEvaluator .labelInSet ("Person" , labels ()).getOfFalse (), DELTA );
167167 assertFalse (Neo4jConditionEvaluator .labelInSet ("Person" , labels ()).isTrue ());
168- // present- but- different label: scaled, never reaches 1
168+ // present but different label: scaled, never reaches 1
169169 Truthness t = Neo4jConditionEvaluator .labelInSet ("Person" , labels ("Animal" ));
170- assertEquals (1.0 , t .getOfFalse (), 1e-9 );
170+ assertEquals (1.0 , t .getOfFalse (), DELTA );
171171 assertTrue (t .getOfTrue () >= Neo4jHeuristicsCalculator .C && t .getOfTrue () < 1.0 );
172172 }
173173
174174 @ Test
175175 void testStartsWith () {
176176 assertTrue (Neo4jConditionEvaluator .getStartsWith ("hello" , "hel" ).isTrue ());
177177 Truthness t = Neo4jConditionEvaluator .getStartsWith ("hello" , "xyz" );
178- assertEquals (1.0 , t .getOfFalse (), 1e-9 );
178+ assertEquals (1.0 , t .getOfFalse (), DELTA );
179179 assertTrue (t .getOfTrue () >= Neo4jHeuristicsCalculator .C && t .getOfTrue () < 1.0 );
180180 }
181181
182- // helpers
183-
184182 private static Neo4jNode node (String id , Set <String > labels , Map <String , Object > props ) {
185183 return new Neo4jNode (id , labels , props );
186184 }
0 commit comments