|
@@ -3,11 +3,16 @@ package com.kgraph.graph.neo4j.service.impl;
|
|
import com.kgraph.common.core.domain.BaseResponse;
|
|
import com.kgraph.common.core.domain.BaseResponse;
|
|
import com.kgraph.common.core.domain.entity.SysDictData;
|
|
import com.kgraph.common.core.domain.entity.SysDictData;
|
|
import com.kgraph.graph.neo4j.DTO.KgDTO;
|
|
import com.kgraph.graph.neo4j.DTO.KgDTO;
|
|
-import com.kgraph.graph.neo4j.domain.Neo4jEntity;
|
|
|
|
|
|
+import com.kgraph.graph.neo4j.domain.EntityClass;
|
|
|
|
+import com.kgraph.graph.neo4j.domain.EntityClassRelation;
|
|
|
|
+import com.kgraph.graph.neo4j.domain.Entity;
|
|
import com.kgraph.graph.neo4j.domain.Neo4jRelation;
|
|
import com.kgraph.graph.neo4j.domain.Neo4jRelation;
|
|
|
|
+import com.kgraph.graph.neo4j.mapper.EntityClassMapper;
|
|
|
|
+import com.kgraph.graph.neo4j.mapper.EntityClassRelationMapper;
|
|
import com.kgraph.graph.neo4j.mapper.EntityRepository;
|
|
import com.kgraph.graph.neo4j.mapper.EntityRepository;
|
|
import com.kgraph.graph.neo4j.mapper.RelationRepository;
|
|
import com.kgraph.graph.neo4j.mapper.RelationRepository;
|
|
import com.kgraph.graph.neo4j.service.IKgService;
|
|
import com.kgraph.graph.neo4j.service.IKgService;
|
|
|
|
+import com.kgraph.graph.suport.domain.BuildEntityRelation;
|
|
import com.kgraph.graph.suport.dto.BuildEntityRelationDTO;
|
|
import com.kgraph.graph.suport.dto.BuildEntityRelationDTO;
|
|
import com.kgraph.system.service.ISysDictDataService;
|
|
import com.kgraph.system.service.ISysDictDataService;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -35,20 +40,26 @@ public class KgServiceImpl implements IKgService {
|
|
@Autowired
|
|
@Autowired
|
|
private RelationRepository relationRepository;
|
|
private RelationRepository relationRepository;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private EntityClassRelationMapper entityClassRelationMapper;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private EntityClassMapper entityClassMapper;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void create(List<KgDTO> kgDTOList) {
|
|
public void create(List<KgDTO> kgDTOList) {
|
|
// build entity
|
|
// build entity
|
|
- List<Neo4jEntity> entityList = buildEntity(kgDTOList);
|
|
|
|
|
|
+ List<Entity> entityList = buildEntity(kgDTOList);
|
|
|
|
|
|
- Map<String, Neo4jEntity> entityMap = entityList.stream().collect(Collectors.toMap(Neo4jEntity::getName, Neo4jEntity -> Neo4jEntity));
|
|
|
|
|
|
+ Map<String, Entity> entityMap = entityList.stream().collect(Collectors.toMap(Entity::getName, Neo4jEntity -> Neo4jEntity));
|
|
|
|
|
|
- for (Neo4jEntity entity : entityMap.values()) {
|
|
|
|
- Neo4jEntity newEntity = entityRepository.findByName(entity.getName());
|
|
|
|
|
|
+ for (Entity entity : entityMap.values()) {
|
|
|
|
+ Entity newEntity = entityRepository.findByName(entity.getName());
|
|
if (newEntity != null) {
|
|
if (newEntity != null) {
|
|
// 如果已经在db中则替换
|
|
// 如果已经在db中则替换
|
|
entityMap.put(entity.getName(), newEntity);
|
|
entityMap.put(entity.getName(), newEntity);
|
|
} else {
|
|
} else {
|
|
- Neo4jEntity save = entityRepository.save(entity);
|
|
|
|
|
|
+ Entity save = entityRepository.save(entity);
|
|
entityMap.put(entity.getName(), save);
|
|
entityMap.put(entity.getName(), save);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -72,7 +83,7 @@ public class KgServiceImpl implements IKgService {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- private boolean relationExist(Neo4jEntity subEntity, Neo4jEntity objEntity, String relationName) {
|
|
|
|
|
|
+ private boolean relationExist(Entity subEntity, Entity objEntity, String relationName) {
|
|
Neo4jRelation relationDb = relationRepository.findRelation(subEntity.getId(), objEntity.getId(), relationName);
|
|
Neo4jRelation relationDb = relationRepository.findRelation(subEntity.getId(), objEntity.getId(), relationName);
|
|
return relationDb != null;
|
|
return relationDb != null;
|
|
}
|
|
}
|
|
@@ -87,22 +98,22 @@ public class KgServiceImpl implements IKgService {
|
|
return relationRepository.findAll();
|
|
return relationRepository.findAll();
|
|
}
|
|
}
|
|
|
|
|
|
- private List<Neo4jEntity> buildEntity(List<KgDTO> kgDTOList) {
|
|
|
|
|
|
+ private List<Entity> buildEntity(List<KgDTO> kgDTOList) {
|
|
Set<String> entitySet = new HashSet<>(kgDTOList.size() * 2);
|
|
Set<String> entitySet = new HashSet<>(kgDTOList.size() * 2);
|
|
for (KgDTO kgDTO : kgDTOList) {
|
|
for (KgDTO kgDTO : kgDTOList) {
|
|
entitySet.add(kgDTO.getStart());
|
|
entitySet.add(kgDTO.getStart());
|
|
entitySet.add(kgDTO.getEnd());
|
|
entitySet.add(kgDTO.getEnd());
|
|
}
|
|
}
|
|
- List<Neo4jEntity> entityList = new ArrayList<>(entitySet.size());
|
|
|
|
|
|
+ List<Entity> entityList = new ArrayList<>(entitySet.size());
|
|
for (String entityStr : entitySet) {
|
|
for (String entityStr : entitySet) {
|
|
- Neo4jEntity neo4jEntity = new Neo4jEntity();
|
|
|
|
- neo4jEntity.setName(entityStr);
|
|
|
|
- entityList.add(neo4jEntity);
|
|
|
|
|
|
+ Entity entity = new Entity();
|
|
|
|
+ entity.setName(entityStr);
|
|
|
|
+ entityList.add(entity);
|
|
}
|
|
}
|
|
return entityList;
|
|
return entityList;
|
|
}
|
|
}
|
|
|
|
|
|
- private List<Neo4jRelation> buildRelation(List<KgDTO> kgDTOList, Map<String, Neo4jEntity> entityMap) {
|
|
|
|
|
|
+ private List<Neo4jRelation> buildRelation(List<KgDTO> kgDTOList, Map<String, Entity> entityMap) {
|
|
List<Neo4jRelation> relationList = new ArrayList<>(kgDTOList.size());
|
|
List<Neo4jRelation> relationList = new ArrayList<>(kgDTOList.size());
|
|
for (KgDTO kgDTO : kgDTOList) {
|
|
for (KgDTO kgDTO : kgDTOList) {
|
|
Neo4jRelation neo4jRelation = new Neo4jRelation();
|
|
Neo4jRelation neo4jRelation = new Neo4jRelation();
|
|
@@ -218,11 +229,11 @@ public class KgServiceImpl implements IKgService {
|
|
public void buildEntityByClass(List<BuildEntityRelationDTO> bu1ildEntityRelationDTOList) {
|
|
public void buildEntityByClass(List<BuildEntityRelationDTO> bu1ildEntityRelationDTOList) {
|
|
bu1ildEntityRelationDTOList.forEach(buildEntityRelationDTO -> {
|
|
bu1ildEntityRelationDTOList.forEach(buildEntityRelationDTO -> {
|
|
String subEntityName = buildEntityRelationDTO.getSubEntity();
|
|
String subEntityName = buildEntityRelationDTO.getSubEntity();
|
|
- Neo4jEntity subEntity = entityRepository.findByName(subEntityName);
|
|
|
|
|
|
+ Entity subEntity = entityRepository.findByName(subEntityName);
|
|
String subClassName = buildEntityRelationDTO.getSubName();
|
|
String subClassName = buildEntityRelationDTO.getSubName();
|
|
// 如果实体不存在,新建实体
|
|
// 如果实体不存在,新建实体
|
|
if (subEntity == null) {
|
|
if (subEntity == null) {
|
|
- subEntity = new Neo4jEntity();
|
|
|
|
|
|
+ subEntity = new Entity();
|
|
subEntity.setName(subEntityName);
|
|
subEntity.setName(subEntityName);
|
|
subEntity.getLabels().add(subClassName);
|
|
subEntity.getLabels().add(subClassName);
|
|
entityRepository.save(subEntity);
|
|
entityRepository.save(subEntity);
|
|
@@ -233,11 +244,11 @@ public class KgServiceImpl implements IKgService {
|
|
}
|
|
}
|
|
|
|
|
|
String objEntityName = buildEntityRelationDTO.getObjEntity();
|
|
String objEntityName = buildEntityRelationDTO.getObjEntity();
|
|
- Neo4jEntity objEntity = entityRepository.findByName(objEntityName);
|
|
|
|
|
|
+ Entity objEntity = entityRepository.findByName(objEntityName);
|
|
String objClassName = buildEntityRelationDTO.getObjName();
|
|
String objClassName = buildEntityRelationDTO.getObjName();
|
|
// 如果实体不存在,新建实体
|
|
// 如果实体不存在,新建实体
|
|
if (objEntity == null) {
|
|
if (objEntity == null) {
|
|
- objEntity = new Neo4jEntity();
|
|
|
|
|
|
+ objEntity = new Entity();
|
|
objEntity.setName(objEntityName);
|
|
objEntity.setName(objEntityName);
|
|
objEntity.getLabels().add(objClassName);
|
|
objEntity.getLabels().add(objClassName);
|
|
entityRepository.save(objEntity);
|
|
entityRepository.save(objEntity);
|
|
@@ -258,4 +269,56 @@ public class KgServiceImpl implements IKgService {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public void addEntityRelation(BuildEntityRelation buildEntityRelation) {
|
|
|
|
+ EntityClass subEntityClass = entityClassMapper.selectEntityClassById(buildEntityRelation.getSubEntityClassId());
|
|
|
|
+ EntityClass objEntityClass = entityClassMapper.selectEntityClassById(buildEntityRelation.getObjEntityClassId());
|
|
|
|
+ EntityClassRelation entityClassRelation = entityClassRelationMapper.selectEntityClassRelationById(buildEntityRelation.getEntityClassRelId());
|
|
|
|
+
|
|
|
|
+ if(subEntityClass == null || objEntityClass == null || entityClassRelation == null){
|
|
|
|
+ throw new RuntimeException("实体类或实体关系未找到");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String subEntityName = buildEntityRelation.getSubEntity();
|
|
|
|
+ String subClassName = subEntityClass.getName();
|
|
|
|
+ Entity subEntity = entityRepository.findByName(subEntityName);
|
|
|
|
+
|
|
|
|
+ // 如果实体不存在,新建实体
|
|
|
|
+ if (subEntity == null) {
|
|
|
|
+ subEntity = new Entity();
|
|
|
|
+ subEntity.setName(subEntityName);
|
|
|
|
+ subEntity.getLabels().add(subClassName);
|
|
|
|
+ entityRepository.save(subEntity);
|
|
|
|
+ // 如果实体存在,实体类没有则添加实体类
|
|
|
|
+ } else if (!subEntity.getLabels().contains(subClassName)) {
|
|
|
|
+ subEntity.getLabels().add(subClassName);
|
|
|
|
+ entityRepository.save(subEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String objEntityName = buildEntityRelation.getObjEntity();
|
|
|
|
+ String objClassName = objEntityClass.getName();
|
|
|
|
+ Entity objEntity = entityRepository.findByName(objEntityName);
|
|
|
|
+
|
|
|
|
+ // 如果实体不存在,新建实体
|
|
|
|
+ if (objEntity == null) {
|
|
|
|
+ objEntity = new Entity();
|
|
|
|
+ objEntity.setName(objEntityName);
|
|
|
|
+ objEntity.getLabels().add(objClassName);
|
|
|
|
+ entityRepository.save(objEntity);
|
|
|
|
+ // 如果实体存在,实体类没有则添加实体类
|
|
|
|
+ } else if (!objEntity.getLabels().contains(objClassName)) {
|
|
|
|
+ objEntity.getLabels().add(objClassName);
|
|
|
|
+ entityRepository.save(objEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String relationName = entityClassRelation.getName();
|
|
|
|
+ if(!relationExist(subEntity,objEntity, relationName)){
|
|
|
|
+ Neo4jRelation relation = new Neo4jRelation();
|
|
|
|
+ relation.setName(relationName);
|
|
|
|
+ relation.setStartNode(subEntity);
|
|
|
|
+ relation.setEndNode(objEntity);
|
|
|
|
+ relationRepository.save(relation);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|