public async Task <TopologyModel> CreateGraphTopology(long firstElementGid, string whoIsCalling) { Logger.LogVerbose($"{baseLogString} CreateGraphTopology method called, by {whoIsCalling}."); ITopologyElement currentFider = null; Logger.LogDebug($"{baseLogString} CreateGraphTopology => Calling GetElementModels method from model provider."); var modelProviderClient = CeModelProviderClient.CreateClient(); var dict = await modelProviderClient.GetElementModels(); elements = TransformDictionary(dict); Logger.LogDebug($"{baseLogString} CreateGraphTopology => GetElementModels method from model provider has been called successfully."); Logger.LogDebug($"{baseLogString} CreateGraphTopology => Calling GetConnections method from model provider."); modelProviderClient = CeModelProviderClient.CreateClient(); connections = await modelProviderClient.GetConnections(); Logger.LogDebug($"{baseLogString} CreateGraphTopology => GetConnections method from model provider has been called successfully."); Logger.LogDebug($"{baseLogString} CreateGraphTopology => Calling GetReclosers method from model provider."); modelProviderClient = CeModelProviderClient.CreateClient(); reclosers = await modelProviderClient.GetReclosers(); Logger.LogDebug($"{baseLogString} CreateGraphTopology => GetReclosers method from model provider has been called successfully."); Logger.LogDebug($"{baseLogString} CreateGraphTopology => Creating topology from first element with GID {firstElementGid:X16} started."); visited = new HashSet <long>(); stack = new Stack <long>(); fields = new List <Field>(); TopologyModel topology = new TopologyModel { FirstNode = firstElementGid }; try { stack.Push(firstElementGid); ITopologyElement currentElement; long currentElementId = 0; while (stack.Count > 0) { currentElementId = stack.Pop(); if (!visited.Contains(currentElementId)) { visited.Add(currentElementId); } if (!elements.TryGetValue(currentElementId, out currentElement)) { string message = $"{baseLogString} CreateGraphTopology => Failed to build topology.Topology does not contain element with GID {currentElementId:X16}."; Logger.LogError(message); return(topology); //throw new Exception(message); } List <long> referencedElements = GetReferencedElementsWithoutIgnorables(currentElementId); foreach (var element in referencedElements) { if (elements.TryGetValue(element, out ITopologyElement newNode)) { if (!reclosers.Contains(element)) { ConnectTwoNodes(newNode, currentElement); stack.Push(element); } else { currentElement.SecondEnd.Add(newNode); if (newNode.FirstEnd == null) { newNode.FirstEnd = currentElement; } else { newNode.SecondEnd.Add(currentElement); } if (!topology.TopologyElements.ContainsKey(newNode.Id)) { topology.AddElement(newNode); } } } else { Logger.LogError($"{baseLogString} CreateGraphTopology => Element with GID {element:X16} does not exist in collection of elements."); } } if (currentElement is Feeder) { currentFider = currentElement; } else { currentElement.Feeder = currentFider; } topology.AddElement(currentElement); } foreach (var field in fields) { topology.AddElement(field); } //long size = 0; //using (Stream stream = new MemoryStream()) //{ // BinaryFormatter formatter = new BinaryFormatter(); // formatter.Serialize(stream, topology); // size = stream.Length; //} //using (FileStream writer = new FileStream(@"E:\LogFiles\Topology.txt", FileMode.OpenOrCreate)) //{ // DataContractSerializer serializer = new DataContractSerializer(topology.GetType()); // serializer.WriteObject(writer, topology); //} } catch (Exception e) { Logger.LogError($"{baseLogString} Uhvacen eksepsn. {e.Message} {e.StackTrace}"); } Logger.LogDebug($"{baseLogString} CreateGraphTopology => Topology successfully created."); return(topology); }
public ITopology CreateGraphTopology(long firstElementGid) { ITopologyElement currentFider = null; elements = Provider.Instance.ModelProvider.GetElementModels(); connections = Provider.Instance.ModelProvider.GetConnections(); reclosers = Provider.Instance.ModelProvider.GetReclosers(); logger.LogInfo($"Creating topology from first element with GID 0x{firstElementGid.ToString("X16")}."); visited = new HashSet <long>(); stack = new Stack <long>(); fields = new List <Field>(); ITopology topology = new TopologyModel { FirstNode = firstElementGid }; stack.Push(firstElementGid); ITopologyElement currentElement; long currentElementId = 0; while (stack.Count > 0) { currentElementId = stack.Pop(); if (!visited.Contains(currentElementId)) { visited.Add(currentElementId); } if (!elements.TryGetValue(currentElementId, out currentElement)) { logger.LogFatal($"Failed to build topology.Topology elements do not contain element with GID 0x{currentElementId.ToString("X16")}."); } foreach (var element in GetReferencedElementsWithoutIgnorables(currentElementId)) { if (elements.TryGetValue(element, out ITopologyElement newNode)) { if (!reclosers.Contains(element)) { ConnectTwoNodes(newNode, currentElement); stack.Push(element); } else { currentElement.SecondEnd.Add(newNode); if (newNode.FirstEnd == null) { newNode.FirstEnd = currentElement; } else { newNode.SecondEnd.Add(currentElement); } if (!topology.TopologyElements.ContainsKey(newNode.Id)) { topology.AddElement(newNode); } } } else { logger.LogError($"[GraphBuilder] Element with GID 0x{element.ToString("X16")} does not exist in collection of elements."); } } if (currentElement is Feeder) { currentFider = currentElement; } else { currentElement.Feeder = currentFider; } topology.AddElement(currentElement); } foreach (var field in fields) { topology.AddElement(field); } logger.LogInfo("Topology successfully created."); return(topology); }