Skip to content

Commit

Permalink
fix(reasoner): bugfix when fact in context. (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
fishjoy authored Sep 24, 2024
1 parent 1b2eb00 commit 3652f01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ public void init(Map<String, String> param) {
public List<Result> find(Triple pattern, Map<String, Object> context) {
logger.info("InfGraph find pattern={}, context={}", pattern, context);
List<Result> result = new LinkedList<>();
// Step1: find pattern in graph

// Step1: find pattern in context or tripleStore
Collection<Element> spo = this.tripleStore.find(pattern);
if (CollectionUtils.isNotEmpty(spo)) {
result.addAll(
spo.stream().map(e -> new Result(e.bind(pattern), null)).collect(Collectors.toList()));
}
// Step2: find pattern in graph
List<Result> dataInGraph = graphStore.find(pattern, context);
logger.info("GraphStore find pattern={}, result={}", pattern, dataInGraph);
if (CollectionUtils.isNotEmpty(dataInGraph)) {
Expand All @@ -63,6 +70,7 @@ public List<Result> find(Triple pattern, Map<String, Object> context) {
}
}
recorder.add((Triple) pattern.cleanAlias());
// Step3: inference pattern
List<Result> infResult = inference(pattern, context);
logger.info("InfGraph infer pattern={}, result={}", pattern, infResult);
result.addAll(infResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,17 @@ public void testFindWithRule2Context() {
thinker.find(new Node("A"), new Predicate("ac"), new Entity("c", "C"), context);
Assert.assertTrue(triples.size() == 1);
}

@Test
public void testFactExists() {
MockLogicCatalog logicCatalog = new MockLogicCatalog(Arrays.asList(getR2()));
logicCatalog.init();
Thinker thinker = new DefaultThinker(buildEmptyGraphState(), logicCatalog);
Map<String, Object> context = new HashMap<>();
Triple triple = new Triple(new Entity("a1", "A"), new Predicate("ac"), new Node("C"));
context.put(triple.toString(), triple);
List<Result> triples =
thinker.find(triple.getSubject(), triple.getPredicate(), triple.getObject(), context);
Assert.assertTrue(triples.size() == 1);
}
}

0 comments on commit 3652f01

Please sign in to comment.