/// <summary> /// 实例化一个图谱阅读器 /// </summary> /// <param name="realtion"></param> public MapReader(CompanyRelation realtion, bool enableAreaName) { this.rawRelation = realtion; this.enableAreaName = enableAreaName; elements = new Dictionary <int, Node>(0x400); if (!enableAreaName) { nameMap = new Dictionary <string, List <Node> >(0x400); } Init(); }
/// <summary> /// 遍历节点 /// </summary> /// <param name="relation"></param> /// <param name="direction"></param> /// <returns></returns> private Node ToNode(CompanyRelation relation, int direction) { this.totalLoops++; Node node = new Node(); node.type = relation.term.companyTermId > 0 ? 1 : 2; if (node.type == 1) { node.id = -relation.term.companyTermId; } else { node.id = relation.memoryStoreId; } if (elements.ContainsKey(node.id)) { Node storeNode = elements[node.id]; if (storeNode.name != null && storeNode.name.Length > 0) { return(storeNode); } node = storeNode; } else { elements.Add(node.id, node); node.nodeIndex = currIndex++; //if (node.index == 767) //{ // Console.WriteLine("here comes"); //} } node.index = relation.index; node.direction = direction; //if (stages.ContainsKey(node.index)) //{ // stages[node.index].Add(node); //} //else //{ // stages.Add(node.index, new List<Node>() { node}); //} if (node.id < 0) { node.longName = relation.name; //node.name = string.Format(string.Format("{0}{1}{2}", relation.areaSegment, relation.nameSegment, relation.tradeSegment)); node.areaName = relation.areaSegment; node.sgName = relation.nameSegment; node.sgTrade = relation.tradeSegment; node.sgBranch = relation.branchSegment; if (node.longName != null && node.areaName != null && node.longName.IndexOf(node.areaName) == -1) { //说明是大公司或者其他机构 if (relation.typeSegment != null && relation.typeSegment.Length < 4) { node.name = string.Format("{0}{1}{2}", relation.nameSegment, relation.tradeSegment, relation.typeSegment); } else { node.name = string.Format("{0}{1}", relation.nameSegment, relation.tradeSegment); } } else { string checkName = string.Format("{0}{1}", relation.nameSegment, relation.tradeSegment); if (!enableAreaName && nameMap.ContainsKey(checkName)) { if (relation.typeSegment != null && relation.typeSegment.Length < 4) { node.name = string.Format("{0}{1}{2}{3}", relation.areaSegment, relation.nameSegment, relation.tradeSegment, relation.typeSegment); } else { node.name = string.Format("{0}{1}{2}", relation.areaSegment, relation.nameSegment, relation.tradeSegment); } nameMap[checkName].Add(node); } else { if (!enableAreaName) { if (relation.typeSegment != null && relation.typeSegment.Length < 4) { node.name = string.Format("{0}{1}{2}", relation.nameSegment, relation.tradeSegment, relation.typeSegment); } else { node.name = string.Format("{0}{1}", relation.nameSegment, relation.tradeSegment); } nameMap.Add(checkName, new List <Node>() { node }); } else { if (relation.typeSegment != null && relation.typeSegment.Length < 4) { node.name = string.Format("{0}{1}{2}{3}", relation.areaSegment, relation.nameSegment, relation.tradeSegment, relation.typeSegment); } else { node.name = string.Format("{0}{1}{2}", relation.areaSegment, relation.nameSegment, relation.tradeSegment); } } } } node.code = relation.code; if (node.code == null) { this.elements.Remove(node.id); this.currIndex--; } } else { node.name = relation.name; node.longName = node.name; } if (node.index > maxIndex) { maxIndex = node.index; } node.weight = relation.weight; node.areaCode = relation.areaCode; if (relation.nextRelations != null) { Node tmp; foreach (CompanyRelation nextRelation in relation.nextRelations) { tmp = ToNode(nextRelation, 1); if (tmp != null) { tmp.relationType = 1; node.AddChildren(tmp); } } } if (relation.invisibleRelations != null) { Node tmp; int i = 0, len = relation.invisibleRelations.Count; CompanyRelation inviRelation; while (i < len) { inviRelation = relation.invisibleRelations[i]; tmp = ToNode(inviRelation, -2); if (tmp != null) { tmp.relationType = 2; } i++; } //foreach (CompanyRelation invisibleRelation in relation.invisibleRelations) //{ // tmp=ToNode(invisibleRelation); // if (tmp != null) // { // } //} } if (relation.previousRelations != null) { Node tmp; foreach (CompanyRelation gdRelation in relation.previousRelations) { //if (gdRelation.name!=null &&gdRelation.name.StartsWith("浙江蚂蚁小微")) // Console.WriteLine("here comes."); tmp = ToNode(gdRelation, -1); if (tmp != null) { //if(tmp.name!=null&&tmp.name.StartsWith("浙江蚂蚁小微")) // Console.WriteLine("here comes."); //tmp.AddChildren(node); tmp.AddChildren(node); tmp.relationType = -1; } } } return(node); }