/// <summary> /// In order to add a child to this node, it must not be sealed /// </summary> /// <param name="reportedProcess"></param> public void AddChildren(ProcessNode reportedProcess) { Contract.Assert(!IsSealed); Contract.Requires(reportedProcess != null); m_children.Add(reportedProcess); }
private ProcessTree ComputeTree(IReadOnlyCollection <ReportedProcess> reportedProcesses) { // Process Ids can be reused, but let's assume uniqueness, just for demo purposes var allProcessNodes = reportedProcesses.ToDictionary(proc => proc.ProcessId, proc => new ProcessNode(proc)); ProcessNode root = null; foreach (var reportedProcess in reportedProcesses) { // If the parent id is part of the reported processes, update the children. Otherwise, that should be the root process if (allProcessNodes.TryGetValue(reportedProcess.ParentProcessId, out ProcessNode parentProcessNode)) { parentProcessNode.AddChildren(allProcessNodes[reportedProcess.ProcessId]); } else if (reportedProcess.ParentProcessId == 0) { // This case means that the parent process id could not be retrieved. Just ignore the process for sake of the demo continue; } else { // There should be only one process that has a parent id that is not part of the set of reported processes: the main one root = allProcessNodes[reportedProcess.ProcessId]; } } // Seal all nodes so children become immutable foreach (var processNode in allProcessNodes.Values) { processNode.Seal(); } Contract.Assert(root != null); return(new ProcessTree(root)); }
/// <nodoc/> public ProcessTree(ProcessNode root) { Contract.Requires(root != null); Root = root; }