From e298c0d70ac977ea9c9e95e09238d14e4fc2b11a Mon Sep 17 00:00:00 2001 From: Barry Nouwt Date: Fri, 22 Aug 2025 10:59:08 +0200 Subject: [PATCH] Fixed a major bug in the Match.merge(...) method. This caused the CombiMatches to not be correctly determined in many cases. Also added a unit test to detect these kinds of bugs in the future. --- .../eu/knowledge/engine/reasoner/Match.java | 5 +- .../engine/reasoner/api/MatchTest.java | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java b/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java index d7f083b47..1290fdd34 100644 --- a/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java +++ b/reasoner/src/main/java/eu/knowledge/engine/reasoner/Match.java @@ -157,10 +157,9 @@ private Map mergeContexts(Map ex return null; } } - // no conflict, so we can safely put it into the mergedContext. - mergedContext.put(newEntry.getKey(), newEntry.getValue()); - } + // no conflict, so we can safely put it into the mergedContext. + mergedContext.put(newEntry.getKey(), newEntry.getValue()); } return mergedContext; diff --git a/reasoner/src/test/java/eu/knowledge/engine/reasoner/api/MatchTest.java b/reasoner/src/test/java/eu/knowledge/engine/reasoner/api/MatchTest.java index e16ac5318..c5246d361 100644 --- a/reasoner/src/test/java/eu/knowledge/engine/reasoner/api/MatchTest.java +++ b/reasoner/src/test/java/eu/knowledge/engine/reasoner/api/MatchTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigInteger; @@ -21,6 +22,7 @@ import org.junit.jupiter.api.TestInstance.Lifecycle; import eu.knowledge.engine.reasoner.BaseRule; +import eu.knowledge.engine.reasoner.BaseRule.CombiMatch; import eu.knowledge.engine.reasoner.BaseRule.MatchFlag; import eu.knowledge.engine.reasoner.Match; import eu.knowledge.engine.reasoner.ProactiveRule; @@ -743,4 +745,95 @@ public void testNewGPMatcher2() { assertEquals(7, matches.size()); } + + @Test + public void testNewGPMatcher3() { + + String gp = """ + ?operation . + ?operation ?output . + ?operation ?parcel . + """; + + Set obj1 = Util.toGP(gp); + + String gp2 = """ + ?operation2 . + ?operation2 ?output2 . + ?operation2 ?parcel2 . + """; + + Set obj2 = Util.toGP(gp2); + + String gp3 = """ + ?operation2 . + ?operation2 ?output2 . + ?operation2 ?parcel2 . + """; + + Set obj3 = Util.toGP(gp3); + + BaseRule target = new ProactiveRule(new HashSet<>(), obj1); + + BaseRule candidate1 = new Rule("candidate1", obj2, new SinkBindingSetHandler() { + public java.util.concurrent.CompletableFuture handle(BindingSet aBindingSet) { + return null; + }; + }); + BaseRule candidate2 = new Rule("candidate2", obj3, new SinkBindingSetHandler() { + public java.util.concurrent.CompletableFuture handle(BindingSet aBindingSet) { + return null; + }; + }); + + HashSet someCandidateRules = new HashSet<>(Arrays.asList(candidate1, candidate2)); + Set combiMatches = BaseRule.getMatches(target, someCandidateRules, false, + EnumSet.of(MatchFlag.ONE_TO_ONE, MatchFlag.ONLY_BIGGEST, MatchFlag.SINGLE_RULE)); + + System.out.println("Size: " + combiMatches.size()); + assertEquals(2, combiMatches.size()); + } + + @Test + public void testNewGPMatcher4() { + + String gp = """ + ?operation . + ?operation ?output . + ?operation ?parcel . + """; + + Set obj1 = Util.toGP(gp); + + String gp2 = """ + ?operation2 . + ?operation2 ?output2 . + ?operation2 ?parcel2 . + """; + + Set obj2 = Util.toGP(gp2); + + BaseRule target = new ProactiveRule(new HashSet<>(), obj1); + + BaseRule candidate1 = new Rule("candidate1", obj2, new SinkBindingSetHandler() { + public java.util.concurrent.CompletableFuture handle(BindingSet aBindingSet) { + return null; + }; + }); + + HashSet someCandidateRules = new HashSet<>(Arrays.asList(candidate1)); + Set combiMatches = BaseRule.getMatches(target, someCandidateRules, false, + EnumSet.of(MatchFlag.ONE_TO_ONE, MatchFlag.ONLY_BIGGEST, MatchFlag.SINGLE_RULE)); + + CombiMatch cm = combiMatches.iterator().next(); + + Set matches = cm.get(candidate1); + assertNotNull(matches); + + Match match = matches.iterator().next(); + assertNotNull(match); + + assertEquals(5, match.getMappings().size()); + + } }