Skip to content

Commit

Permalink
fix(reasoner): fix some bugs in thinker.
Browse files Browse the repository at this point in the history
  • Loading branch information
fishjoy committed Sep 27, 2024
1 parent 0219770 commit 4988f5d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ public List<Result> find(Element s, Element p, Element o) {
public List<Result> find(Element s, Element p, Element o, Map<String, Object> context) {
this.infGraph.clear();
Triple pattern = Triple.create(s, p, o);
this.infGraph.prepare(context);
List<Result> result = this.infGraph.find(pattern, context == null ? new HashMap<>() : context);
return result;
}

@Override
public List<Result> find(Node s, Map<String, Object> context) {
this.infGraph.clear();
this.infGraph.prepare(context);
List<Result> triples =
this.infGraph.find(Triple.create(s), context == null ? new HashMap<>() : context);
List<Result> results = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@
public interface Graph {
void init(Map<String, String> param);

void prepare(Map<String, Object> context);

List<Result> find(Triple pattern, Map<String, Object> context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public void init(Map<String, String> param) {
this.graphState.init(param);
}

@Override
public void prepare(Map<String, Object> context) {}

@Override
public List<Result> find(Triple pattern, Map<String, Object> context) {
List<Triple> data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ 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 context or tripleStore
Collection<Element> spo = this.tripleStore.find(pattern);
// Step1: find pattern in context
Collection<Element> spo = matchInContext(pattern, context);
if (CollectionUtils.isNotEmpty(spo)) {
result.addAll(
spo.stream().map(e -> new Result(e.bind(pattern), null)).collect(Collectors.toList()));
Expand All @@ -78,17 +78,19 @@ public List<Result> find(Triple pattern, Map<String, Object> context) {
return result;
}

@Override
public void prepare(Map<String, Object> context) {
private List<Element> matchInContext(Triple pattern, Map<String, Object> context) {
List<Element> rst = new ArrayList<>();
if (context != null) {
for (Object val : context.values()) {
if (val instanceof Entity) {
addEntity((Entity) val);
} else if (val instanceof Triple) {
addTriple((Triple) val);
if (val instanceof Entity || val instanceof Triple) {
Triple value = Triple.create((Element) val);
if (pattern.matches(value)) {
rst.add(value);
}
}
}
}
return rst;
}

private List<Result> inference(Triple pattern, Map<String, Object> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testCase1() {
triples.forEach(t -> context.put(t.toString(), t));
List<Result> result =
thinker.find(new Node("tiger"), new Predicate("iss"), new Node("young"), context);
Assert.assertTrue(result.size() == 1);
Assert.assertTrue(result.size() == 2);
}

@Test
Expand Down Expand Up @@ -188,4 +188,23 @@ public void testCase6() {
thinker.find(new Entity("Fiona", "Thing"), new Predicate("iss"), new Node("red"), context);
Assert.assertTrue(result.size() == 1);
}

@Test
public void testCase7() {
Thinker thinker = buildThinker("/ProofWriter7.txt");
Map<String, Object> context = new HashMap<>();
Triple t1 = makeTriple("lion", "Thing", "chases", "mouse", "Thing");
Triple t2 = makeTriple("mouse", "Thing", "iss", "cold");
Triple t3 = makeTriple("mouse", "Thing", "needs", "rabbit", "Thing");
Triple t4 = makeTriple("rabbit", "Thing", "chases", "squirrel", "Thing");
Triple t5 = makeTriple("rabbit", "Thing", "sees", "lion", "Thing");
Triple t6 = makeTriple("rabbit", "Thing", "sees", "squirrel", "Thing");
Triple t7 = makeTriple("squirrel", "Thing", "chases", "lion", "Thing");
List<Triple> triples = Arrays.asList(t1, t2, t3, t4, t5, t6, t7);
triples.forEach(t -> context.put(t.toString(), t));
List<Result> result =
thinker.find(
new Entity("squirrel", "Thing"), new Predicate("iss"), new Node("cold"), context);
Assert.assertTrue(result.size() == 1);
}
}
27 changes: 27 additions & 0 deletions reasoner/thinker/src/test/resources/ProofWriter7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Define (s:Thing)-[:sees]->(o:Thing/`rabbit`) {
(s)-[:iss]->(o1:cold)
}

Define (s:Thing)-[:sees]->(o:Thing/`squirrel`) {
(s)-[:sees]->(o1:Thing/`rabbit`)
}

Define (s:Thing/`squirrel`)-[:iss]->(o:cold) {
(s1:Thing)-[:iss]->(o) AND (s1)-[:sees]->(s)
}

Define (s:Thing)-[:needs]->(o:Thing/`squirrel`) {
(s)-[:chases]->(o)
}

Define (s:Thing/`squirrel`)-[:chases]->(o:Thing/`rabbit`) {
(s1:Thing)-[:chases]->(s) AND NOT(s1)-[:iss]->(o2:cold)
}

Define (s:Thing/`rabbit`)-[:chases]->(o:Thing/`squirrel`) {
(s)-[:sees]->(o) AND (o)-[:chases]->(s)
}

Define (s:Thing/`squirrel`)-[:needs]->(o:Thing/`mouse`) {
(s1:Thing/`rabbit`)-[:chases]->(s) AND NOT(s)-[:chases]->(s1)
}

0 comments on commit 4988f5d

Please sign in to comment.