Prechádzať zdrojové kódy

Merge remote-tracking branch 'origin/gz-en' into enen

zhangenzhi 1 rok pred
rodič
commit
463728b923

+ 1 - 1
src/main/java/com/pavis/backend/slim/framework/config/SecurityConfig.java

@@ -128,7 +128,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
                 .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
                 // todo ghj 算法接收标注信息测试接口,临时使用,后期需要删除。
-                .antMatchers("/annotation/testGenerateKgGraph","/annotation/convertToTxt","/annotation/universalGgraphToAlg").permitAll()
+                .antMatchers("/annotation/testGenerateKgGraph","/annotation/convertToTxt","/annotation/universalGgraphToAlg","/annotation/generateKgGraphResult").permitAll()
                 // 除上面外的所有请求全部需要鉴权认证
                 .anyRequest().authenticated()
                 .and()

+ 15 - 1
src/main/java/com/pavis/backend/slim/project/system/client/AlgKgClient.java

@@ -17,6 +17,8 @@ import java.util.Map;
 @FeignClient(value = "algKgClient", url = "${algorithm.convert.url}", configuration = AlgKgConfig.class)
 public interface AlgKgClient {
 
+
+
     /**
      * 发送标注数据给算法服务,生成图谱。状态为200,调用下一个接口:入参kgId、fileIds等。
      * url: http://192.168.1.150:12000/labels/import
@@ -30,7 +32,7 @@ public interface AlgKgClient {
     String sendToAlgForGenerateKgGraph(String data);
 
     /**
-     * todo 将标注数据给算法后,状态为200再次调用算法获取taskId。
+     * todo 调用算法服务获取taskId。
      * 入参:kgId、fielIds、type[true 通用;false 标注。]
      * @param data
      * @return
@@ -39,6 +41,18 @@ public interface AlgKgClient {
             consumes = "application/json")
     String afterSendAnnotationToGetTaskId(String data);
 
+    /**
+     * todo 调用此算法服务获取taskId。
+     * 入参:kgId、fielIds、type[true 通用;false 标注。]
+     * @param data
+     * @return
+     */
+    @PostMapping(value = "/schema/import", produces = {MediaType.APPLICATION_JSON_VALUE},
+            consumes = "application/json")
+    String sendEntityRelationToAlg(String data);
+
+
+
     /**
      * 发送文档需转换为txt的数据给算法服务。
      *

+ 74 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/EntityForAlg.java

@@ -0,0 +1,74 @@
+package com.pavis.backend.slim.project.system.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.experimental.SuperBuilder;
+
+import java.util.List;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: 实体属性数据,给算法使用。
+ * @date: 2023-11-13 13:55
+ */
+@SuperBuilder
+@Getter
+@Setter
+@AllArgsConstructor
+@NoArgsConstructor
+public class EntityForAlg {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 实体ID
+     */
+    @ApiModelProperty("图谱实体id")
+    @TableId(value = "entity_id",type = IdType.ASSIGN_UUID)
+    private String entityId;
+
+    @ApiModelProperty("实体ID")
+    private String id;
+    /**
+     * 图谱ID
+     */
+    @ApiModelProperty("图谱ID")
+    private String kgId;
+    /**
+     * 实体名称
+     */
+    @ApiModelProperty("实体名称")
+    private String name;
+    /**
+     * 实体名称
+     */
+    @ApiModelProperty("默认名称")
+    private String defaultName;
+    /**
+     * 实体别称
+     */
+    @ApiModelProperty("实体别称")
+    private String nickName;
+
+    /**
+     * 颜色
+     */
+    @ApiModelProperty("颜色")
+    private String color;
+
+    /**
+     * 前端需要——无需更改
+     */
+    @ApiModelProperty("前端需要——无需更改")
+    private Integer frontTime;
+
+    /**
+     * 默认值
+     */
+    @ApiModelProperty("默认值")
+    private List describe;
+}

+ 90 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/RelationForAlg.java

@@ -0,0 +1,90 @@
+package com.pavis.backend.slim.project.system.domain.annotation;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.experimental.SuperBuilder;
+
+import java.util.List;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: 实体间关系属性数据,给算法使用。
+ * @date: 2023-11-13 13:55
+ */
+@SuperBuilder
+@Getter
+@Setter
+@AllArgsConstructor
+@NoArgsConstructor
+public class RelationForAlg {
+    // /**
+    //  * 关系ID
+    //  */
+    // @ApiModelProperty("关系ID")
+    // private String relationId;
+    /**
+     * 关系ID
+     */
+    @ApiModelProperty("关系ID")
+    private Long id;
+
+    /**
+     * 类目/起始点类型
+     */
+    @ApiModelProperty("类目/起始点类型")
+    private String startName;
+
+    /**
+     * 起始点id
+     */
+    @ApiModelProperty("起始点id")
+    private String start;
+
+    /**
+     * 属性/关系
+     */
+    @ApiModelProperty("属性/关系")
+    private String relation;
+
+    /**
+     * 值类型/终止点类型
+     */
+    @ApiModelProperty("值类型/终止点类型")
+    private String endName;
+
+    /**
+     * 终止点id
+     */
+    @ApiModelProperty("终止点id")
+    private String end;
+
+    /**
+     * 方向:有向边/无向边。
+     */
+    @ApiModelProperty("方向")
+    private Integer direction;
+
+    /**
+     * 颜色。
+     */
+    @ApiModelProperty("颜色")
+    private String styleEntity;
+
+    /**
+     * 默认值
+     */
+    @ApiModelProperty("默认值")
+    private List describe;
+    /**
+     * 去重。
+     */
+    @ApiModelProperty("去重")
+    private Integer isAt;
+
+    @ApiModelProperty("关系ID")
+    private String relationId;
+}

+ 33 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/SchemaImport.java

@@ -0,0 +1,33 @@
+package com.pavis.backend.slim.project.system.domain.annotation;
+
+import com.pavis.backend.slim.project.system.domain.EntityForAlg;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.experimental.SuperBuilder;
+
+import java.util.List;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: 实体和关系图模型数据给算法。
+ * @date: 2023-10-26 9:47
+ */
+@SuperBuilder
+@Getter
+@Setter
+@AllArgsConstructor
+@NoArgsConstructor
+public class SchemaImport{
+    @ApiModelProperty("图谱ID")
+    private String kgId;
+    // 该知识库下的所有实体信息。
+    @ApiModelProperty("该知识库下的所有实体信息")
+    private List<EntityForAlg> entityList;
+    // 该知识库下的所有实体间关系信息。
+    @ApiModelProperty("该知识库下的所有实体间关系信息")
+    private List<RelationForAlg> relationList;
+}

+ 82 - 46
src/main/java/com/pavis/backend/slim/project/system/service/impl/EntityLabelsRelationServiceImpl.java

@@ -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);