Skip to content

Commit

Permalink
feat(reasoner): support ProntoQA and ProofWriter. (#352)
Browse files Browse the repository at this point in the history
Co-authored-by: kejian <[email protected]>
  • Loading branch information
fishjoy and wangsff authored Sep 18, 2024
1 parent 579be8b commit 1b2eb00
Show file tree
Hide file tree
Showing 29 changed files with 720 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
package com.antgroup.openspg.reasoner.thinker;

import com.antgroup.openspg.reasoner.thinker.logic.graph.Element;
import com.antgroup.openspg.reasoner.thinker.logic.graph.Entity;
import com.antgroup.openspg.reasoner.thinker.logic.graph.Triple;
import java.util.Collection;
import java.util.Map;

public interface TripleStore {
void init(Map<String, String> param);

Collection<Element> find(final Element pattern);

void addEntity(Entity entity);
Collection<Element> find(final Triple triple);

void addTriple(Triple triple);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import com.antgroup.openspg.reasoner.thinker.Thinker;
import com.antgroup.openspg.reasoner.thinker.catalog.LogicCatalog;
import com.antgroup.openspg.reasoner.thinker.logic.Result;
import com.antgroup.openspg.reasoner.thinker.logic.graph.Element;
import com.antgroup.openspg.reasoner.thinker.logic.graph.Node;
import com.antgroup.openspg.reasoner.thinker.logic.graph.Triple;
import com.antgroup.openspg.reasoner.thinker.logic.graph.*;
import java.util.*;

public class DefaultThinker implements Thinker {
Expand All @@ -46,13 +44,21 @@ 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();
return this.infGraph.find(s, context == null ? new HashMap<>() : context);
this.infGraph.prepare(context);
List<Result> triples =
this.infGraph.find(Triple.create(s), context == null ? new HashMap<>() : context);
List<Result> results = new LinkedList<>();
for (Result triple : triples) {
results.add(new Result(((Triple) triple.getData()).getObject(), triple.getTraceLog()));
}
return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
package com.antgroup.openspg.reasoner.thinker.engine;

import com.antgroup.openspg.reasoner.thinker.logic.Result;
import com.antgroup.openspg.reasoner.thinker.logic.graph.Node;
import com.antgroup.openspg.reasoner.thinker.logic.graph.Triple;
import java.util.List;
import java.util.Map;

public interface Graph {
void init(Map<String, String> param);

List<Result> find(Triple pattern, Map<String, Object> context);
void prepare(Map<String, Object> context);

List<Result> find(Node s, 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,6 +37,9 @@ 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 All @@ -62,11 +65,6 @@ public List<Result> find(Triple pattern, Map<String, Object> context) {
return matchInGraph(pattern, data);
}

@Override
public List<Result> find(Node s, Map<String, Object> context) {
return Collections.emptyList();
}

protected List<Triple> getTriple(Entity s, Element predicate, Element o, Direction direction) {
List<Triple> triples = new LinkedList<>();
if (direction == Direction.OUT) {
Expand All @@ -80,7 +78,7 @@ protected List<Triple> getTriple(Entity s, Element predicate, Element o, Directi
}
}
List<IEdge<IVertexId, IProperty>> edges;
String spo = toSPO(s, predicate, o);
String spo = toSPO(s, predicate, o, direction);
if (StringUtils.isNotBlank(spo)) {
edges =
this.graphState.getEdges(
Expand All @@ -101,17 +99,25 @@ protected List<Triple> getTriple(Entity s, Element predicate, Element o, Directi
return triples;
}

private String toSPO(Entity s, Element predicate, Element o) {
private String toSPO(Entity s, Element predicate, Element o, Direction direction) {
if (!(predicate instanceof Predicate)
|| StringUtils.isBlank(((Predicate) predicate).getName())) {
return null;
}
SPO spo;
if (o instanceof Node) {
spo = new SPO(s.getType(), ((Predicate) predicate).getName(), ((Node) o).getType());
if (direction == Direction.OUT) {
spo = new SPO(s.getType(), ((Predicate) predicate).getName(), ((Node) o).getType());
} else {
spo = new SPO(((Node) o).getType(), ((Predicate) predicate).getName(), s.getType());
}
return spo.toString();
} else if (o instanceof Entity) {
spo = new SPO(s.getType(), ((Predicate) predicate).getName(), ((Entity) o).getType());
if (direction == Direction.OUT) {
spo = new SPO(s.getType(), ((Predicate) predicate).getName(), ((Entity) o).getType());
} else {
spo = new SPO(((Entity) o).getType(), ((Predicate) predicate).getName(), s.getType());
}
return spo.toString();
} else {
return null;
Expand All @@ -124,7 +130,7 @@ protected List<Triple> getTriple(Triple triple, Predicate predicate) {
Predicate p = (Predicate) triple.getPredicate();
Entity o = (Entity) triple.getObject();
List<IEdge<IVertexId, IProperty>> edges;
String edgeType = toSPO(s, p, o);
String edgeType = toSPO(s, p, o, Direction.OUT);
if (StringUtils.isNotBlank(edgeType)) {
edges =
this.graphState.getEdges(
Expand Down
Loading

0 comments on commit 1b2eb00

Please sign in to comment.