|
@@ -2,12 +2,14 @@ package com.pavis.backend.slim.project.system.service.impl;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.pavis.backend.slim.common.utils.CommonUtils;
|
|
|
import com.pavis.backend.slim.common.utils.SecurityUtils;
|
|
|
import com.pavis.backend.slim.project.system.client.AlgKgClient;
|
|
|
+import com.pavis.backend.slim.project.system.domain.EntityForAlg;
|
|
|
import com.pavis.backend.slim.project.system.domain.EntityLabelsRelation;
|
|
|
import com.pavis.backend.slim.project.system.domain.SysEntity;
|
|
|
import com.pavis.backend.slim.project.system.domain.SysRelation;
|
|
@@ -15,6 +17,8 @@ import com.pavis.backend.slim.project.system.domain.annotation.AnnotationData;
|
|
|
import com.pavis.backend.slim.project.system.domain.annotation.AnnotationEntity;
|
|
|
import com.pavis.backend.slim.project.system.domain.annotation.AnnotationEntityRelation;
|
|
|
import com.pavis.backend.slim.project.system.domain.annotation.AnnotationToAlg;
|
|
|
+import com.pavis.backend.slim.project.system.domain.annotation.RelationForAlg;
|
|
|
+import com.pavis.backend.slim.project.system.domain.annotation.SchemaImport;
|
|
|
import com.pavis.backend.slim.project.system.domain.annotation.ToAnnotation;
|
|
|
import com.pavis.backend.slim.project.system.domain.annotation.UniversalGgraphToAlg;
|
|
|
import com.pavis.backend.slim.project.system.mapper.EntityLabelsRelationMapper;
|
|
@@ -123,14 +127,12 @@ public class EntityLabelsRelationServiceImpl extends ServiceImpl<EntityLabelsRel
|
|
|
@Override
|
|
|
public AnnotationToAlg generateKgGraph(String kgId) {
|
|
|
// 1. 首先根据kgId获取图谱构建的实体、关系数据。
|
|
|
- List<SysEntity> entityByKgId = sysEntityService.getEntityByKgId(kgId);
|
|
|
+ List<SysEntity> selEntityByKgId = sysEntityService.getEntityByKgId(kgId);
|
|
|
// 根据kgId获取文档id列表。
|
|
|
List<String> fileIds = sysKgService.getFileIdsByKgId(kgId);
|
|
|
- // 2. 判断该图谱是否创建实体和关系数据。
|
|
|
- if (CollectionUtils.isEmpty(entityByKgId)) {
|
|
|
+ // 2. 判断该图谱是否创建实体和关系数据。即是否有图schema.
|
|
|
+ if (CollectionUtils.isEmpty(selEntityByKgId)) {
|
|
|
// 2.1 如果该图谱的未创建实体和关系数据,则调用算法另一个服务,入参为:fileIds列表、kgId。
|
|
|
- // 更新图模型状态。
|
|
|
-
|
|
|
// String universalGgraphToAlgRes = "";
|
|
|
try {
|
|
|
// todo 临时弃用,改用下面的接口,直接调用获取taskId。
|
|
@@ -138,57 +140,91 @@ public class EntityLabelsRelationServiceImpl extends ServiceImpl<EntityLabelsRel
|
|
|
// log.info("该图谱无实体、关系数据时,调用通用知识图谱算法接口返回res:{}", universalGgraphToAlgRes);
|
|
|
|
|
|
// todo 直接调用算法获取taskId,type=true。返回json{"taskId":""}。生成一条记录。
|
|
|
+ log.info("生成图谱接口 ---->无图schema 通用图谱调用中...");
|
|
|
UniversalGgraphToAlg universalGgraphToAlg = UniversalGgraphToAlg.builder().kgId(kgId).fileIds(fileIds).taskType(true).build();
|
|
|
String getTaskIdRes = algKgClient.afterSendAnnotationToGetTaskId(JSON.toJSONString(universalGgraphToAlg));
|
|
|
- createGenerateGraph(getTaskIdRes, kgId, true,0,0);
|
|
|
+ createGenerateGraph(getTaskIdRes, kgId, true, 0, 0);
|
|
|
} catch (Exception ex) {
|
|
|
ex.printStackTrace();
|
|
|
- log.info("该图谱无实体、关系数据时,调用通用知识图谱算法接口返回异常ex:{}", ex);
|
|
|
+ log.info("无图schema即该图谱无实体、关系数据时,调用通用知识图谱算法接口返回异常ex:{}", ex);
|
|
|
}
|
|
|
return new AnnotationToAlg();
|
|
|
} else {
|
|
|
- // 2.2 如果该图谱的创建了实体和关系数据,则调用算法的生成图谱服务,即入参为:text、实体标注数据列表、实体关系标注数据列表。
|
|
|
- // 如果图谱有实体关系数据,且标注了数据,则发送标注的数据给算法;如果该文档未被标注则不发送。如果仅有实体关系数据,所有文档均未被标注,也发送,只不过标注数据为空。
|
|
|
- // 图谱下的所有文档标注信息。
|
|
|
- List<AnnotationData> annotationDatas = new ArrayList<>();
|
|
|
- for (String fileId : fileIds) {
|
|
|
- String fileTextContent = sysFileService.getFileTextContent(fileId);
|
|
|
- if (null != fileTextContent && StringUtils.isNotBlank(fileTextContent)) {
|
|
|
- log.info("文本内容不为空:{}", fileTextContent);
|
|
|
- log.info("--->fileId:{}", fileId);
|
|
|
- // 获取历史标注实体信息数据列表,即知识库下的实体标注信息列表。
|
|
|
- List<AnnotationEntity> entities = entityLablesSpanService.getAnnotationEntityByKgIdAndFileId(kgId, fileId);
|
|
|
- // 如果标注实体数据不为空,则需要发送给算法,否则不发。
|
|
|
- if (!CollectionUtils.isEmpty(entities)) {
|
|
|
- log.info("--->entities:{}", entities.size());
|
|
|
- // 获取历史标注实体间关系数据列表,即图谱下的实体关系标注信息列表。
|
|
|
- List<AnnotationEntityRelation> relations = getAnnotationEntityRelationByKgIdAndFileId(kgId, fileId);
|
|
|
- log.info("--->relations:{}", entities.size());
|
|
|
- AnnotationData annotationData = AnnotationData.builder().text(fileTextContent).entities(entities).relations(relations).build();
|
|
|
- annotationDatas.add(annotationData);
|
|
|
+ log.info("生成图谱接口 ---->有图schema 判断是否存在标注数据...");
|
|
|
+ // 2.2 用户在图模型创建了实体和关系,即存在图schema数据。先请求schema/import,返回OK,再判断是否有标注数据。
|
|
|
+ List<EntityForAlg> entityForAlgs = JSONArray.parseArray(selEntityByKgId.get(0).getEntityAll(), EntityForAlg.class);
|
|
|
+ log.info("有图schema的实体信息:{}",JSON.toJSONString(entityForAlgs));
|
|
|
+ String relationAll = selEntityByKgId.get(0).getRelationAll();
|
|
|
+ List<RelationForAlg> relationForAlgs = JSONArray.parseArray(relationAll, RelationForAlg.class);
|
|
|
+ log.info("有图schema的实体间关系信息:{}",JSON.toJSONString(relationForAlgs));
|
|
|
+ SchemaImport schemaImport = SchemaImport.builder().kgId(kgId).entityList(entityForAlgs).relationList(relationForAlgs).build();
|
|
|
+ log.info("调用算法服务的schemaImport服务发送实体和关系属性数据:{}",JSON.toJSONString(schemaImport));
|
|
|
+ String schemaImportRes = algKgClient.sendEntityRelationToAlg(JSON.toJSONString(schemaImport));
|
|
|
+ log.info("调用算法服务的schemaImport服务发送实体和关系属性数据结果 schemaImportRes:{}",JSON.toJSONString(schemaImportRes));
|
|
|
+ if ("ok".equals(schemaImportRes)){
|
|
|
+ // 2.2 todo 当前版本已弃用: 如果该图谱的创建了实体和关系数据,则调用算法的生成图谱服务,即入参为:text、实体标注数据列表、实体关系标注数据列表。
|
|
|
+ // 获取标注数据: 如果图谱有实体关系数据,且标注了数据,则发送标注的数据给算法;如果该文档未被标注则不发送。如果仅有实体关系数据,所有文档均未被标注,也发送,只不过标注数据为空。
|
|
|
+ List<AnnotationData> annotationDatas = getAnnotationDatas(fileIds,kgId);
|
|
|
+ // 3.判断是否存在标注数据:有标注数据:请求labels/import,返回OK,继续调用获取taskId;
|
|
|
+ AnnotationToAlg annotationToAlg = AnnotationToAlg.builder().kgId(kgId).annotationDatas(annotationDatas).build();// 算法所需数据结构。
|
|
|
+ log.info("标注数据待发送至算法数据结构annotationToAlg:{}", JSON.toJSONString(annotationToAlg));
|
|
|
+ if (!CollectionUtils.isEmpty(annotationDatas) && annotationDatas.get(0).getEntities().size() > 0) {
|
|
|
+ log.info("有图schema 有标注数据");
|
|
|
+ // 3.1 有标注数据:请求/labels/import,返回OK,继续调用/graph/create_graph获取taskId;
|
|
|
+ // todo 3.1.1 发送数据给算法。
|
|
|
+ String algRes = null;
|
|
|
+ try {
|
|
|
+ algRes = algKgClient.sendToAlgForGenerateKgGraph(JSON.toJSONString(annotationToAlg));
|
|
|
+ log.info("该图谱存在实体关系数据时发送标注数据列表,调用算法生成知识图谱接口返回algRes:{}", algRes);
|
|
|
+ if (null != algRes && "ok".equals(algRes)) {
|
|
|
+ // todo 调用算法获取taskId,type=false。返回json{"taskId":""}。生成一条记录。状态为200.才调用。
|
|
|
+ UniversalGgraphToAlg universalGgraphToAlg = UniversalGgraphToAlg.builder().kgId(kgId).fileIds(fileIds).taskType(false).build();
|
|
|
+ String getTaskIdRes = algKgClient.afterSendAnnotationToGetTaskId(JSON.toJSONString(universalGgraphToAlg));
|
|
|
+ createGenerateGraph(getTaskIdRes, kgId, false, 1, annotationDatas.size() > 0 ? 1 : 0);
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ log.info("该图谱存在实体关系数据时发送标注数据列表,调用算法生成知识图谱接口返回异常ex:{}", ex);
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- // 3.算法所需数据结构。
|
|
|
- AnnotationToAlg annotationToAlg = AnnotationToAlg.builder().kgId(kgId).annotationDatas(annotationDatas).build();
|
|
|
- log.info("标注数据待发送至算法数据结构annotationToAlg:{}", JSON.toJSONString(annotationToAlg));
|
|
|
- // todo 4. 发送数据给算法。
|
|
|
- String algRes = null;
|
|
|
- try {
|
|
|
- algRes = algKgClient.sendToAlgForGenerateKgGraph(JSON.toJSONString(annotationToAlg));
|
|
|
- log.info("该图谱存在实体关系数据时发送标注数据列表,调用算法生成知识图谱接口返回algRes:{}", algRes);
|
|
|
- if (null != algRes && "ok".equals(algRes)) {
|
|
|
- // todo 调用算法获取taskId,type=false。返回json{"taskId":""}。生成一条记录。状态为200.才调用。
|
|
|
+ } else {
|
|
|
+ // 3.2 无标注数据:请求graph/create_graph获取taskId。
|
|
|
+ log.info("有图schema 但无标注数据");
|
|
|
UniversalGgraphToAlg universalGgraphToAlg = UniversalGgraphToAlg.builder().kgId(kgId).fileIds(fileIds).taskType(false).build();
|
|
|
String getTaskIdRes = algKgClient.afterSendAnnotationToGetTaskId(JSON.toJSONString(universalGgraphToAlg));
|
|
|
- createGenerateGraph(getTaskIdRes, kgId, false,1,annotationDatas.size() > 0 ? 1 : 0);
|
|
|
+ createGenerateGraph(getTaskIdRes, kgId, false, 0, 0);
|
|
|
+ }
|
|
|
+ return annotationToAlg;
|
|
|
+ }
|
|
|
+ return AnnotationToAlg.builder().kgId(kgId).annotationDatas(new ArrayList<>()).build();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取标注数据。
|
|
|
+ * @param fileIds
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<AnnotationData> getAnnotationDatas(List<String> fileIds,String kgId){
|
|
|
+ List<AnnotationData> annotationDatas = new ArrayList<>();// 图谱下的所有文档标注信息。
|
|
|
+ for (String fileId : fileIds) {
|
|
|
+ String fileTextContent = sysFileService.getFileTextContent(fileId);
|
|
|
+ if (null != fileTextContent && StringUtils.isNotBlank(fileTextContent)) {
|
|
|
+ log.info("文本内容不为空:{}", fileTextContent);
|
|
|
+ log.info("--->fileId:{}", fileId);
|
|
|
+ // 获取历史标注实体信息数据列表,即知识库下的实体标注信息列表。
|
|
|
+ List<AnnotationEntity> entities = entityLablesSpanService.getAnnotationEntityByKgIdAndFileId(kgId, fileId);
|
|
|
+ // 如果标注实体数据不为空,则需要发送给算法,否则不发。
|
|
|
+ if (!CollectionUtils.isEmpty(entities)) {
|
|
|
+ log.info("--->entities:{}", entities.size());
|
|
|
+ // 获取历史标注实体间关系数据列表,即图谱下的实体关系标注信息列表。
|
|
|
+ List<AnnotationEntityRelation> relations = getAnnotationEntityRelationByKgIdAndFileId(kgId, fileId);
|
|
|
+ log.info("--->relations:{}", entities.size());
|
|
|
+ AnnotationData annotationData = AnnotationData.builder().text(fileTextContent).entities(entities).relations(relations).build();
|
|
|
+ annotationDatas.add(annotationData);
|
|
|
}
|
|
|
- } catch (Exception ex) {
|
|
|
- ex.printStackTrace();
|
|
|
- log.info("该图谱存在实体关系数据时发送标注数据列表,调用算法生成知识图谱接口返回异常ex:{}", ex);
|
|
|
}
|
|
|
- return annotationToAlg;
|
|
|
}
|
|
|
+ return annotationDatas;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -209,10 +245,10 @@ public class EntityLabelsRelationServiceImpl extends ServiceImpl<EntityLabelsRel
|
|
|
* @param isGenerate
|
|
|
* @return
|
|
|
*/
|
|
|
- public Boolean createGenerateGraph(String getTaskIdRes, String kgId, Boolean isGenerate,Integer graphStatus,Integer annotationStatus) {
|
|
|
+ public Boolean createGenerateGraph(String getTaskIdRes, String kgId, Boolean isGenerate, Integer graphStatus, Integer annotationStatus) {
|
|
|
// 更新图谱的状态为进行中。
|
|
|
- Boolean upRes = sysKgService.updateGrapgStatus(kgId,graphStatus,annotationStatus,2, "进行中");
|
|
|
- log.info("更新图谱的状态为进行中:{}",upRes);
|
|
|
+ Boolean upRes = sysKgService.updateGrapgStatus(kgId, graphStatus, annotationStatus, 2, "进行中");
|
|
|
+ log.info("更新图谱的状态为进行中:{}", upRes);
|
|
|
log.info("调用算法获取taskId:{}", getTaskIdRes);
|
|
|
if (CommonUtils.isJson(getTaskIdRes)) {
|
|
|
JSONObject taskObj = JSON.parseObject(getTaskIdRes);
|