fix(reasoner): support add facts in thinker. (#396)

Co-authored-by: matthewhyx <matthew.hyx@antgroup.com>
This commit is contained in:
FishJoy 2024-11-08 17:26:16 +08:00 committed by GitHub
parent 84b9209ed9
commit 23cd365ff9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 4 deletions

View File

@ -191,12 +191,18 @@ class SPGConceptRuleMarkLang:
if self.is_reasoning: if self.is_reasoning:
predicate_name = self.predicate predicate_name = self.predicate
subject_type = ( subject_type = (
self.src_concept[0] if len(self.src_concept) > 0 else None f"{self.namespace}.{self.src_concept[0]}"
if len(self.src_concept) > 0
else None
) )
subject_name = ( subject_name = (
self.src_concept[1] if len(self.src_concept) > 0 else None self.src_concept[1] if len(self.src_concept) > 0 else None
) )
object_type = self.dst_concept[0] if len(self.dst_concept) > 0 else None object_type = (
f"{self.namespace}.{self.dst_concept[0]}"
if len(self.dst_concept) > 0
else None
)
object_name = self.dst_concept[1] if len(self.dst_concept) > 0 else None object_name = self.dst_concept[1] if len(self.dst_concept) > 0 else None
elif self.dst_concept[0] is not None: elif self.dst_concept[0] is not None:
predicate_name = "leadTo" predicate_name = "leadTo"

View File

@ -13,15 +13,20 @@
package com.antgroup.openspg.server.core.reasoner.service.runner; package com.antgroup.openspg.server.core.reasoner.service.runner;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.antgroup.kg.reasoner.thinker.Thinker; import com.antgroup.kg.reasoner.thinker.Thinker;
import com.antgroup.kg.reasoner.thinker.catalog.LogicCatalog; import com.antgroup.kg.reasoner.thinker.catalog.LogicCatalog;
import com.antgroup.kg.reasoner.thinker.engine.DefaultThinker; import com.antgroup.kg.reasoner.thinker.engine.DefaultThinker;
import com.antgroup.kg.reasoner.thinker.logic.Result; import com.antgroup.kg.reasoner.thinker.logic.Result;
import com.antgroup.kg.reasoner.thinker.logic.graph.Entity;
import com.antgroup.kg.reasoner.thinker.logic.graph.Node; import com.antgroup.kg.reasoner.thinker.logic.graph.Node;
import com.antgroup.openspg.reasoner.runner.local.thinker.LocalThinkerMain; import com.antgroup.openspg.reasoner.runner.local.thinker.LocalThinkerMain;
import com.antgroup.openspg.reasoner.runner.local.thinker.OpenSPGLogicCatalog; import com.antgroup.openspg.reasoner.runner.local.thinker.OpenSPGLogicCatalog;
import com.antgroup.openspg.reasoner.runner.local.thinker.ThinkerParams; import com.antgroup.openspg.reasoner.runner.local.thinker.ThinkerParams;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
@ -42,15 +47,30 @@ public class ThinkerRunner {
LocalThinkerMain.loadGraph(graphStateClass, task.getGraphStateInitString()), LocalThinkerMain.loadGraph(graphStateClass, task.getGraphStateInitString()),
logicCatalog); logicCatalog);
List<Result> result; List<Result> result;
Map<String, Object> params = new HashMap<>();
for (Map.Entry<String, Object> entry : task.getParams().entrySet()) {
if (entry.getKey().equals("entities")) {
List<Map<String, String>> entities =
JSON.parseObject(
String.valueOf(entry.getValue()),
new TypeReference<List<Map<String, String>>>() {});
for (Map<String, String> map : entities) {
Entity entity = new Entity(map.get("id"), map.get("type"));
params.put(entity.toString(), entity);
}
} else {
params.put(entry.getKey(), entry.getValue());
}
}
if (task.getMode().toLowerCase().equals("spo")) { if (task.getMode().toLowerCase().equals("spo")) {
result = result =
thinker.find( thinker.find(
task.getTriple().getSubject(), task.getTriple().getSubject(),
task.getTriple().getPredicate(), task.getTriple().getPredicate(),
task.getTriple().getObject(), task.getTriple().getObject(),
task.getParams()); params);
} else { } else {
result = thinker.find((Node) task.getTriple().getObject(), task.getParams()); result = thinker.find((Node) task.getTriple().getObject(), params);
} }
return result; return result;
} }