fix(reasoner): fix a bug in thinker service

This commit is contained in:
FishJoy 2024-11-05 10:31:23 +08:00
parent 222a02106d
commit 2327b31939
9 changed files with 72 additions and 76 deletions

View File

@ -192,15 +192,16 @@ public class LocalThinkerMain {
options.addOption(ParamsKey.MODE, ParamsKey.MODE, true, "infer mode, eg: spo or node");
return options;
}
public static GraphState<IVertexId> loadGraph(String graphStateClass, String graphStoreUrl) {
GraphState<IVertexId> graphState;
if (StringUtils.isNotEmpty(graphStateClass)) {
try {
graphState =
(GraphState<IVertexId>)
Class.forName(graphStateClass)
.getConstructor(String.class)
.newInstance(graphStoreUrl);
(GraphState<IVertexId>)
Class.forName(graphStateClass)
.getConstructor(String.class)
.newInstance(graphStoreUrl);
} catch (Exception e) {
throw new RuntimeException("can not create graph state from " + graphStateClass, e);
}

View File

@ -16,10 +16,10 @@ import lombok.Data;
@Data
public class ThinkerTaskRequest {
private Long projectId;
private String subject;
private String predicate;
private String object;
private String mode;
private String params;
}
private Long projectId;
private String subject;
private String predicate;
private String object;
private String mode;
private String params;
}

View File

@ -13,12 +13,11 @@
package com.antgroup.openspg.server.api.facade.dto.service.response;
import java.util.List;
import lombok.Data;
@Data
public class ThinkerTaskResponse {
private List<Object> result;
private String taskId;
private Long projectId;
}
private List<Object> result;
private String taskId;
private Long projectId;
}

View File

@ -50,22 +50,21 @@ public class ReasonController extends BaseController {
});
}
@RequestMapping(method = RequestMethod.POST, value = "/thinker")
public ResponseEntity<Object> reason(@RequestBody ThinkerTaskRequest request) {
return HttpBizTemplate.execute(
new HttpBizCallback<ThinkerTaskResponse>() {
@Override
public void check() {}
@RequestMapping(method = RequestMethod.POST, value = "/thinker")
public ResponseEntity<Object> reason(@RequestBody ThinkerTaskRequest request) {
return HttpBizTemplate.execute(
new HttpBizCallback<ThinkerTaskResponse>() {
@Override
public void check() {}
@Override
public ThinkerTaskResponse action() {
return reasonerManager.thinker(request);
}
});
}
@Override
public ThinkerTaskResponse action() {
return reasonerManager.thinker(request);
}
});
}
@RequestMapping(method = RequestMethod.GET, value = "/schema")
@RequestMapping(method = RequestMethod.GET, value = "/schema")
public ResponseEntity<Object> getReasonSchema(@RequestParam Long projectId) {
return HttpBizTemplate.execute(
new HttpBizCallback<ProjectSchema>() {

View File

@ -24,7 +24,6 @@ import com.antgroup.openspg.server.common.model.reasoner.ReasonerTask;
import com.antgroup.openspg.server.common.model.reasoner.ThinkerTask;
import com.antgroup.openspg.server.core.reasoner.service.CatalogService;
import com.antgroup.openspg.server.core.reasoner.service.ReasonerService;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@ -49,7 +48,7 @@ public class ReasonerManagerImpl implements ReasonerManager {
task.setParams(request.getParams());
String graphStoreUrl = getGraphStoreUrl(request.getProjectId());
task.setGraphStoreUrl(graphStoreUrl);
List<Result> res = reasonerService.thinker(task);
List<Result> res = reasonerService.thinker(task);
ThinkerTaskResponse response = new ThinkerTaskResponse();
response.setProjectId(request.getProjectId());
response.setTaskId(task.getTaskId());

View File

@ -16,12 +16,12 @@ import lombok.Data;
@Data
public class ThinkerTask {
private String taskId;
private Long projectId;
private String subject;
private String predicate;
private String object;
private String mode;
private String params;
private String graphStoreUrl;
}
private String taskId;
private Long projectId;
private String subject;
private String predicate;
private String object;
private String mode;
private String params;
private String graphStoreUrl;
}

View File

@ -12,13 +12,13 @@
*/
package com.antgroup.openspg.server.core.reasoner.service;
import java.util.List;
import com.antgroup.kg.reasoner.thinker.logic.Result;
import com.antgroup.openspg.server.common.model.reasoner.ReasonerTask;
import com.antgroup.openspg.server.common.model.reasoner.ThinkerTask;
import java.util.List;
public interface ReasonerService {
ReasonerTask runTask(ReasonerTask request);
List<Result> thinker(ThinkerTask request);
}

View File

@ -12,22 +12,15 @@
*/
package com.antgroup.openspg.server.core.reasoner.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.antgroup.kg.reasoner.thinker.logic.Result;
import com.antgroup.kg.reasoner.thinker.logic.graph.Element;
import com.antgroup.kg.reasoner.thinker.logic.graph.Triple;
import com.antgroup.openspg.reasoner.catalog.impl.KgSchemaConnectionInfo;
import com.antgroup.openspg.reasoner.lube.catalog.Catalog;
import com.antgroup.openspg.reasoner.runner.local.ParamsKey;
import com.antgroup.openspg.reasoner.runner.local.thinker.LocalThinkerMain;
import com.antgroup.openspg.reasoner.runner.local.thinker.ThinkerParams;
import com.antgroup.openspg.reasoner.udf.impl.UdfMngImpl;
import com.antgroup.openspg.server.api.facade.dto.service.request.ThinkerTaskRequest;
import com.antgroup.openspg.server.common.model.reasoner.ReasonerTask;
import com.antgroup.openspg.server.common.model.reasoner.StatusEnum;
import com.antgroup.openspg.server.common.model.reasoner.ThinkerTask;
@ -37,6 +30,9 @@ import com.antgroup.openspg.server.core.reasoner.service.ReasonerService;
import com.antgroup.openspg.server.core.reasoner.service.runner.ReasonerRunner;
import com.antgroup.openspg.server.core.reasoner.service.runner.ThinkerRunner;
import com.google.common.collect.Lists;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -103,7 +99,7 @@ public class ReasonerServiceImpl implements ReasonerService {
}
if (s == Element.ANY && p == Element.ANY && o == Element.ANY) {
throw new RuntimeException(
"subject, predicate, object cannot all be empty at the same time.");
"subject, predicate, object cannot all be empty at the same time.");
}
String m = request.getMode();

View File

@ -13,8 +13,6 @@
package com.antgroup.openspg.server.core.reasoner.service.runner;
import java.util.List;
import com.antgroup.kg.reasoner.thinker.Thinker;
import com.antgroup.kg.reasoner.thinker.catalog.LogicCatalog;
import com.antgroup.kg.reasoner.thinker.engine.DefaultThinker;
@ -23,33 +21,37 @@ 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.OpenSPGLogicCatalog;
import com.antgroup.openspg.reasoner.runner.local.thinker.ThinkerParams;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ThinkerRunner {
private final ThinkerParams task;
private final String graphStateClass = "com.antgroup.openspg.reasoner.warehouse.cloudext.CloudExtGraphState";
private final ThinkerParams task;
private final String graphStateClass =
"com.antgroup.openspg.reasoner.warehouse.cloudext.CloudExtGraphState";
public ThinkerRunner(ThinkerParams task) {
this.task = task;
}
public ThinkerRunner(ThinkerParams task) {
this.task = task;
public List<Result> run() {
LogicCatalog logicCatalog = new OpenSPGLogicCatalog(task.getProjectId(), task.getConnInfo());
logicCatalog.init();
Thinker thinker =
new DefaultThinker(
LocalThinkerMain.loadGraph(graphStateClass, task.getGraphStateInitString()),
logicCatalog);
List<Result> result;
if (task.getMode().toLowerCase().equals("spo")) {
result =
thinker.find(
task.getTriple().getSubject(),
task.getTriple().getPredicate(),
task.getTriple().getObject(),
task.getParams());
} else {
result = thinker.find((Node) task.getTriple().getObject(), task.getParams());
}
public List<Result> run() {
LogicCatalog logicCatalog = new OpenSPGLogicCatalog(task.getProjectId(), task.getConnInfo());
logicCatalog.init();
Thinker thinker = new DefaultThinker(LocalThinkerMain.loadGraph(graphStateClass, task.getGraphStateInitString()), logicCatalog);
List<Result> result;
if (task.getMode().toLowerCase().equals("spo")) {
result =
thinker.find(
task.getTriple().getSubject(),
task.getTriple().getPredicate(),
task.getTriple().getObject(),
task.getParams());
} else {
result = thinker.find((Node) task.getTriple().getObject(), task.getParams());
}
return result;
}
}
return result;
}
}