parent
ee4b4adb37
commit
eaf043aa07
@ -0,0 +1,117 @@
|
||||
package com.supervision.pdfqaserver.dto;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* CypherSchemaDTO
|
||||
*/
|
||||
public class CypherSchemaDTO {
|
||||
|
||||
private List<EntityExtractionDTO> nodes = new ArrayList<>();
|
||||
|
||||
private List<RelationExtractionDTO> relations = new ArrayList<>();
|
||||
|
||||
public CypherSchemaDTO(List<EntityExtractionDTO> nodes, List<RelationExtractionDTO> relations) {
|
||||
this.nodes = nodes;
|
||||
this.relations = relations;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据头节点、尾节点、关系获取关系抽取DTO
|
||||
* @param sourceType 源节点类型
|
||||
* @param relation 关系
|
||||
* @param targetType 尾节点类型
|
||||
* @return
|
||||
*/
|
||||
public RelationExtractionDTO getRelation(String sourceType, String relation,String targetType) {
|
||||
for (RelationExtractionDTO relationDTO : relations) {
|
||||
if (StrUtil.equals(relationDTO.getSourceType(), sourceType) &&
|
||||
StrUtil.equals(relationDTO.getRelation(), relation) &&
|
||||
StrUtil.equals(relationDTO.getTargetType(), targetType)) {
|
||||
return relationDTO;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据源节点类型或目标节点类型获取关系抽取DTO列表
|
||||
* @param sourceOrTargetType
|
||||
* @return
|
||||
*/
|
||||
public List<RelationExtractionDTO> getRelations(String sourceOrTargetType) {
|
||||
List<RelationExtractionDTO> result = new ArrayList<>();
|
||||
for (RelationExtractionDTO relationDTO : relations) {
|
||||
if (StrUtil.equals(relationDTO.getSourceType(), sourceOrTargetType) ||
|
||||
StrUtil.equals(relationDTO.getTargetType(), sourceOrTargetType)) {
|
||||
result.add(relationDTO);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据实体名获取关系抽取DTO列表
|
||||
* @param entity
|
||||
* @return
|
||||
*/
|
||||
public EntityExtractionDTO getNode(String entity) {
|
||||
for (EntityExtractionDTO node : nodes) {
|
||||
if (StrUtil.equals(node.getEntity(), entity)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String format(){
|
||||
JSONObject nodeJson = new JSONObject();
|
||||
for (EntityExtractionDTO node : nodes) {
|
||||
String entity = node.getEntity();
|
||||
List<TruncationERAttributeDTO> attributes = node.getAttributes();
|
||||
JSONObject nodeAttr = nodeJson.getJSONObject(entity);
|
||||
if (nodeAttr == null) {
|
||||
nodeAttr = new JSONObject();
|
||||
nodeJson.set(entity, nodeAttr);
|
||||
}
|
||||
for (TruncationERAttributeDTO attribute : attributes) {
|
||||
boolean none = nodeAttr.entrySet().stream().noneMatch(
|
||||
entry -> StrUtil.equals(entry.getKey(), attribute.getAttribute()));
|
||||
if (none){
|
||||
nodeAttr.set(attribute.getAttribute(), attribute.getDataType());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
JSONObject relJson = new JSONObject();
|
||||
for (RelationExtractionDTO relation : relations) {
|
||||
String sourceType = relation.getSourceType();
|
||||
String targetType = relation.getTargetType();
|
||||
String rela = relation.getRelation();
|
||||
JSONObject json = relJson.getJSONObject(rela);
|
||||
if (null == json) {
|
||||
json = new JSONObject();
|
||||
relJson.set(rela, json);
|
||||
}
|
||||
json.set("_endpoints", new JSONArray(new String[]{sourceType, targetType}));
|
||||
for (TruncationERAttributeDTO attribute : relation.getAttributes()) {
|
||||
boolean none = json.entrySet().stream().noneMatch(
|
||||
entry -> StrUtil.equals(entry.getKey(), attribute.getAttribute())
|
||||
);
|
||||
if (none) {
|
||||
json.set(attribute.getAttribute(), attribute.getDataType());
|
||||
}
|
||||
}
|
||||
}
|
||||
JSONObject object = new JSONObject()
|
||||
.set("nodetypes", nodeJson)
|
||||
.set("relationshiptypes", relJson);
|
||||
return object.toString();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.supervision.pdfqaserver.dto.neo4j;
|
||||
|
||||
import lombok.Data;
|
||||
import org.neo4j.driver.internal.InternalNode;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class NodeDTO {
|
||||
|
||||
private long id;
|
||||
|
||||
private String elementId;
|
||||
|
||||
private Map<String, Object> properties;
|
||||
|
||||
private Collection<String> labels;
|
||||
|
||||
|
||||
public NodeDTO() {
|
||||
}
|
||||
|
||||
public NodeDTO(InternalNode internalNode) {
|
||||
this.id = internalNode.id();
|
||||
this.elementId = internalNode.elementId();
|
||||
this.properties = internalNode.asMap();
|
||||
this.labels = internalNode.labels();
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.supervision.pdfqaserver.dto.neo4j;
|
||||
|
||||
import lombok.Data;
|
||||
import org.neo4j.driver.internal.InternalNode;
|
||||
import org.neo4j.driver.internal.InternalRelationship;
|
||||
import org.neo4j.driver.types.Node;
|
||||
import org.neo4j.driver.types.Path;
|
||||
import org.neo4j.driver.types.Relationship;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PathDTO {
|
||||
|
||||
private List<NodeDTO> nodes;
|
||||
|
||||
private List<RelationshipValueDTO> relationships;
|
||||
|
||||
public PathDTO() {
|
||||
}
|
||||
|
||||
public PathDTO(Path path) {
|
||||
Iterator<Node> nodeIterator = path.nodes().iterator();
|
||||
List<NodeDTO> nodes = new ArrayList<>();
|
||||
while (nodeIterator.hasNext()){
|
||||
Node next = nodeIterator.next();
|
||||
nodes.add(new NodeDTO((InternalNode) next));
|
||||
}
|
||||
this.nodes = nodes;
|
||||
|
||||
|
||||
Iterator<Relationship> iterator = path.relationships().iterator();
|
||||
List<RelationshipValueDTO> relationships = new ArrayList<>();
|
||||
while (iterator.hasNext()){
|
||||
relationships.add(new RelationshipValueDTO((InternalRelationship) iterator.next()));
|
||||
}
|
||||
this.relationships = relationships;
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.supervision.pdfqaserver.dto.neo4j;
|
||||
|
||||
import lombok.Data;
|
||||
import org.neo4j.driver.internal.InternalRelationship;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class RelationshipValueDTO {
|
||||
|
||||
|
||||
private long start;
|
||||
|
||||
private String startElementId;
|
||||
|
||||
private long end;
|
||||
|
||||
private String endElementId;
|
||||
|
||||
private String type;
|
||||
|
||||
private long id;
|
||||
|
||||
private String elementId;
|
||||
|
||||
private Map<String,Object> properties;
|
||||
|
||||
|
||||
public RelationshipValueDTO() {
|
||||
}
|
||||
|
||||
public RelationshipValueDTO(InternalRelationship relationship) {
|
||||
this.start = (int) relationship.startNodeId();
|
||||
this.startElementId = relationship.startNodeElementId();
|
||||
this.end = relationship.endNodeId();
|
||||
this.endElementId = relationship.endNodeElementId();
|
||||
this.type = relationship.type();
|
||||
this.id = relationship.id();
|
||||
this.elementId = relationship.elementId();
|
||||
this.properties = relationship.asMap();
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue