Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rdf)Fix rdf expand #435

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.antgroup.openspg.reasoner.rdg.common;

import com.antgroup.openspg.reasoner.common.constants.Constants;
import com.antgroup.openspg.reasoner.common.graph.edge.Direction;
import com.antgroup.openspg.reasoner.common.graph.edge.IEdge;
import com.antgroup.openspg.reasoner.common.graph.edge.impl.Edge;
import com.antgroup.openspg.reasoner.common.graph.property.IProperty;
Expand Down Expand Up @@ -151,6 +152,10 @@ public List<KgGraph<IVertexId>> link(KgGraph<IVertexId> kgGraph) {
genTargetVertexTypes =
Lists.newArrayList(linkedUdtfResult.getTargetVertexTypeList().get(i));
}
String direction = Direction.OUT.name();
if (CollectionUtils.isNotEmpty(linkedUdtfResult.getDirection()) && i < linkedUdtfResult.getDirection().size()) {
direction = linkedUdtfResult.getDirection().get(i);
}
if (genTargetVertexTypes.size() == 0) {
throw new RuntimeException(
"Linked edge target vertex type must contains at least one type");
Expand All @@ -170,21 +175,32 @@ public List<KgGraph<IVertexId>> link(KgGraph<IVertexId> kgGraph) {
newAliasVertexMap.computeIfAbsent(targetAlias, k -> new HashSet<>());
newVertexSet.add(new Vertex<>(targetId, vertexProperty));


Map<String, Object> props = new HashMap<>(linkedUdtfResult.getEdgePropertyMap());
props.put(Constants.EDGE_TO_ID_KEY, targetIdStr);
Object from_id = sourceVertex.getValue().get(Constants.NODE_ID_KEY);
Object to_id = targetIdStr;
if (Objects.equals(direction, Direction.IN.name())) {
to_id = from_id;
from_id = targetId;
}
props.put(Constants.EDGE_TO_ID_KEY, to_id);
if (sourceVertex.getValue().isKeyExist(Constants.NODE_ID_KEY)) {
props.put(
Constants.EDGE_FROM_ID_KEY, sourceVertex.getValue().get(Constants.NODE_ID_KEY));
Constants.EDGE_FROM_ID_KEY, from_id);
}
IProperty property = new EdgeProperty(props);

// construct new edge
IEdge<IVertexId, IProperty> linkedEdge = new Edge<>(sourceId, targetId, property);

String edgeType =
StringUtils.isNotEmpty(linkedUdtfResult.getEdgeType())
? linkedUdtfResult.getEdgeType()
: linkedEdgePattern.edge().funcName();
linkedEdge.setType(sourceId.getType() + "_" + edgeType + "_" + targetVertexType);
if (Objects.equals(direction, Direction.IN.name())) {
linkedEdge.setType(targetVertexType + "_" + edgeType + "_" + sourceId.getType());
}
String edgeAlias = pc.alias();

Set<IEdge<IVertexId, IProperty>> newEdgeSet =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class LinkedUdtfResult {
/** The target vertex id of linked edge */
private List<String> targetVertexIdList = new ArrayList<>();

/** The linked direction */
private List<String> direction = new ArrayList<>();

/** The target vertex type of linked edge */
private List<String> targetVertexTypeList = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
public class Utils {

public static List<LinkedUdtfResult> getAllRdfEntity(
GraphState<IVertexId> graphState, IVertexId id) {
GraphState<IVertexId> graphState, IVertexId id, String rdfType) {

List<LinkedUdtfResult> result = new ArrayList<>();

// find vertex prop
IVertex<IVertexId, IProperty> vertex = graphState.getVertex(id, null, null);
if (null != vertex && null != vertex.getValue()) {
if (null != vertex && null != vertex.getValue() && !"relation".equals(rdfType)) {
// 提取属性
log.info("vertex_property,{}", vertex);
for (String propertyName : vertex.getValue().getKeySet()) {
Expand All @@ -56,6 +56,7 @@ public static List<LinkedUdtfResult> getAllRdfEntity(
"name", String.valueOf(pValue)));
graphState.addVertex(propVertex);
LinkedUdtfResult udtfRes = new LinkedUdtfResult();
udtfRes.getDirection().add(Direction.OUT.name());
udtfRes.setEdgeType(propertyName);
udtfRes.getTargetVertexIdList().add(String.valueOf(pValue));
if (pValue instanceof Integer) {
Expand All @@ -76,6 +77,14 @@ public static List<LinkedUdtfResult> getAllRdfEntity(
if (CollectionUtils.isNotEmpty(edgeList)) {
for (IEdge<IVertexId, IProperty> edge : edgeList) {
Object toIdObj = edge.getValue().get(Constants.EDGE_TO_ID_KEY);
String dir = Direction.OUT.name();
Object nodeIdObj = vertex.getValue().get(Constants.NODE_ID_KEY);
String targetType = edge.getTargetId().getType();
if (nodeIdObj.equals(toIdObj)) {
toIdObj = edge.getValue().get(Constants.EDGE_FROM_ID_KEY);
dir = Direction.IN.name();
targetType = String.valueOf(edge.getValue().get(Constants.EDGE_FROM_ID_TYPE_KEY));
}
if (null == toIdObj) {
continue;
}
Expand All @@ -84,7 +93,8 @@ public static List<LinkedUdtfResult> getAllRdfEntity(
LinkedUdtfResult udtfRes = new LinkedUdtfResult();
udtfRes.setEdgeType(spo.getP());
udtfRes.getTargetVertexIdList().add(String.valueOf(toIdObj));
udtfRes.getTargetVertexTypeList().add(edge.getTargetId().getType());
udtfRes.getTargetVertexTypeList().add(targetType);
udtfRes.getDirection().add(dir);
for (String propKey : edge.getValue().getKeySet()) {
if (propKey.startsWith("_")) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,18 @@ public void process(List<Object> args) {
String vertexType = null;
String bizId = null;
Object s = context.get(srcAlias);
String rdfType = null;
if (args.size() > 0) {
rdfType = (String) args.get(0);
}
if (s instanceof Map) {
Map<String, Object> sMap = (Map<String, Object>) s;
bizId = (String) sMap.get(Constants.NODE_ID_KEY);
vertexType = (String) sMap.get(Constants.CONTEXT_LABEL);
}
IVertexId id = new VertexBizId(bizId, vertexType);
// 结果
List<LinkedUdtfResult> validBizIds = Utils.getAllRdfEntity(graphState, id);
List<LinkedUdtfResult> validBizIds = Utils.getAllRdfEntity(graphState, id, rdfType);
for (LinkedUdtfResult udtfResult : validBizIds) {
forward(Lists.newArrayList(udtfResult));
}
Expand Down
Loading