public (bool, List <VisulizedVertex>, List <VisulizedEdge>) GetFirstLevelRelationships(string vId) { if (this.kgDF == null) { return(false, null, null); } Vertex vertex = this.kgDF.GetVertexById(vId); if (vertex == null) { return(true, null, null); } List <VisulizedVertex> rVVs = new List <VisulizedVertex>(); List <VisulizedEdge> rVEs = new List <VisulizedEdge>(); if (vertex.properties != null && vertex.properties.Count > 0) { foreach (VertexProperty property in vertex.properties) { VisulizedVertex vP = KGUtility.GeneratePropertyVVertex(vertex.label, property.name, property.value); rVVs.Add(vP); VisulizedEdge vEdge = new VisulizedEdge(); vEdge.value = vP.displayName; vEdge.sourceId = vertex.id; vEdge.targetId = vP.id; rVEs.Add(vEdge); } } List <VisulizedVertex> tmpRVVs; List <VisulizedEdge> tmpRVEs; Dictionary <RelationLink, List <string> > childrenLinkDict = this.kgDF.GetChildrenLinkDict(vId); HashSet <string> addedIDs = new HashSet <string>(); if (childrenLinkDict != null) { (tmpRVVs, tmpRVEs) = GetConnectedVertexesAndEdges(addedIDs, vId, childrenLinkDict, true); rVVs.AddRange(tmpRVVs); rVEs.AddRange(tmpRVEs); } Dictionary <RelationLink, List <string> > parentLinkDict = this.kgDF.GetParentLinkDict(vId); if (parentLinkDict != null) { (tmpRVVs, tmpRVEs) = GetConnectedVertexesAndEdges(addedIDs, vId, parentLinkDict, false); rVVs.AddRange(tmpRVVs); rVEs.AddRange(tmpRVEs); } return(true, rVVs, rVEs); }
public async Task <ActionResult <IResult> > Filter(string datastoreName, string name, string value) { RelationResult filterResult; if (string.IsNullOrWhiteSpace(datastoreName) || string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(value)) { filterResult = new RelationResult(false, "datastoreName, 属性名和属性值都不能为空"); } else { name = name.ToLower(); value = value.ToLower(); GraphExecutor executor = new GraphExecutor(datastoreName); (bool isDSExist, List <VisulizedVertex> vvs) = executor.FilterVertexesByProperty(name, value); if (!isDSExist) { filterResult = new RelationResult(false, "Datastore " + datastoreName + "不存在,或没有数据导入。"); } else { if (vvs == null || vvs.Count == 0) { filterResult = new RelationResult(true, "根据\"" + name + " = " + value + ",\"未能找到任何符合条件的节点。"); } else { VisulizedVertex propertyVV = KGUtility.GeneratePropertyVVertex("", name, value); filterResult = new RelationResult(true, "根据\"" + name + " = " + value + ",\"为您搜索到以下节点:"); List <VisulizedEdge> ves = new List <VisulizedEdge>(); foreach (VisulizedVertex vv in vvs) { VisulizedEdge ve = new VisulizedEdge(); ve.value = name; ve.sourceId = propertyVV.id; ve.targetId = vv.id; ves.Add(ve); } vvs.Insert(0, propertyVV); filterResult.nodes = vvs; filterResult.relations = ves; } } } log.Here().Information("[Response]: " + JsonConvert.SerializeObject(filterResult)); return(Ok(filterResult)); }
private (List <VisulizedVertex>, List <VisulizedEdge>) GetConnectedVertexesAndEdges(HashSet <string> addedIDs, string vId, Dictionary <RelationLink, List <string> > relationDict, bool vIsSrc) { List <VisulizedVertex> rVVs = new List <VisulizedVertex>(); List <VisulizedEdge> rVEs = new List <VisulizedEdge>(); HashSet <string> cIdSet = new HashSet <string>(); if (relationDict != null && relationDict.Count > 0) { foreach (RelationLink link in relationDict.Keys) { string relationType = link.relationType; foreach (string cId in relationDict[link]) { if (cIdSet.Contains(cId)) { continue; } cIdSet.Add(cId); VisulizedVertex vRV = ConvertVertex(this.kgDF.GetVertexById(cId)); VisulizedEdge vRE = new VisulizedEdge(); vRE.value = relationType; if (vIsSrc) { vRE.sourceId = vId; vRE.targetId = vRV.id; } else { vRE.targetId = vId; vRE.sourceId = vRV.id; } if (!addedIDs.Contains(vRV.id)) { rVVs.Add(vRV); addedIDs.Add(vRV.id); } rVEs.Add(vRE); } } } return(rVVs, rVEs); }
private VisulizedVertex ConvertVertex(Vertex vertex) { if (vertex == null) { return(null); } VisulizedVertex vv = new VisulizedVertex(); vv.id = vertex.id; vv.name = vertex.name; vv.displayName = vertex.name; vv.label = vertex.label; return(vv); }
public static VisulizedVertex GeneratePropertyVVertex(string vertexLabel, string pName, string pValue) { if (string.IsNullOrWhiteSpace(vertexLabel)) { vertexLabel = ""; } else { vertexLabel = "_" + vertexLabel; } VisulizedVertex propertyVV = new VisulizedVertex(); propertyVV.name = pValue; propertyVV.displayName = pName; propertyVV.label = vertexLabel + "属性"; propertyVV.id = GenerateIdForProperty(pName, pValue); return(propertyVV); }
public async Task <ActionResult <IResult> > GetRelations(string id) { RelationResult relationResult; if (string.IsNullOrWhiteSpace(id)) { relationResult = new RelationResult(false, "id不能为空"); } else { GraphExecutor executor = new GraphExecutor(); VisulizedVertex theVVertex = executor.GetVertexById(id); if (theVVertex == null) { relationResult = new RelationResult(true, "无法找到节点 " + id + "。"); } else { (List <VisulizedVertex> vvs, List <VisulizedEdge> ves) = executor.GetFirstLevelRelationships(id); relationResult = new RelationResult(true, "节点 id =" + id + " 的一阶关系如下:"); relationResult.nodes = vvs; if (relationResult.nodes == null) { relationResult.nodes = new List <VisulizedVertex> { theVVertex }; } else { relationResult.nodes.Add(theVVertex); } relationResult.relations = ves; } } log.Here().Information("[Response]: " + JsonConvert.SerializeObject(relationResult)); return(Ok(relationResult)); }