-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3695 from cisagov/feature/CR-36b
Feature/cr 36b
- Loading branch information
Showing
31 changed files
with
6,497 additions
and
8 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Malcolm/MalcolmBusiness.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using CSETWebCore.DataLayer.Model; | ||
using CSETWebCore.Interfaces.Malcolm; | ||
using CSETWebCore.Model.Malcolm; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace CSETWebCore.Business.Malcolm | ||
{ | ||
public class MalcolmBusiness : IMalcolmBusiness | ||
{ | ||
private CSETContext _context; | ||
|
||
|
||
private Dictionary<string, TempNode> networkOfNodes = new Dictionary<string, TempNode>(); | ||
|
||
|
||
|
||
public MalcolmBusiness(CSETContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public List<MalcolmData> GetMalcolmJsonData(List<MalcolmData> datalist) | ||
{ | ||
foreach (MalcolmData malcolmData in datalist) | ||
{ | ||
if (malcolmData != null) | ||
{ | ||
foreach (var bucket in malcolmData.Values.Buckets) | ||
{ | ||
//if (!networkOfNodes.ContainsKey(bucket.Key)) | ||
//{ | ||
var buckets = new List<Buckets>() { bucket }; | ||
BuildNetwork(null, buckets); | ||
//} | ||
} | ||
malcolmData.Graphs = networkOfNodes; | ||
networkOfNodes = new Dictionary<string, TempNode>(); | ||
} | ||
} | ||
return datalist; | ||
|
||
} | ||
public List<MalcolmData> GetTreesFromMalcolmData(List<MalcolmData> datalist) | ||
{ | ||
var malcolmDataList = new List<MalcolmData>(); | ||
foreach (MalcolmData malcolmData in datalist) | ||
{ | ||
if (malcolmData != null) | ||
{ | ||
MalcolmTree trees = new MalcolmTree(); | ||
malcolmData.Trees =trees.StartTheTreeWalk(malcolmData.Graphs); | ||
malcolmDataList.Add(malcolmData); | ||
} | ||
} | ||
return malcolmDataList; | ||
|
||
} | ||
|
||
private void BuildNetwork(TempNode parent, List<Buckets> buckets) | ||
{ | ||
foreach (var bucket in buckets) | ||
{ | ||
TempNode tnode; | ||
if (String.IsNullOrEmpty(bucket.Key)) | ||
{ | ||
continue; | ||
} | ||
if (networkOfNodes.TryGetValue(bucket.Key, out tnode)) | ||
{ | ||
if (parent != null) | ||
parent.AddChildGraphOnly(tnode); | ||
} | ||
else | ||
{ | ||
tnode = new TempNode(bucket.Key); | ||
networkOfNodes.TryAdd(bucket.Key, tnode); | ||
if (parent != null) | ||
parent.AddChildGraphOnly(tnode); | ||
} | ||
if (bucket.Values != null) | ||
{ | ||
BuildNetwork(tnode, bucket.Values.Buckets); | ||
} | ||
} | ||
} | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Malcolm/MalcolmTree.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using CSETWebCore.Model.Malcolm; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
||
namespace CSETWebCore.Business.Malcolm | ||
{ | ||
public class MalcolmTree | ||
{ | ||
|
||
private HashSet<string> alreadySeenList = new HashSet<string>(); | ||
|
||
|
||
|
||
public MalcolmTree() | ||
{ | ||
|
||
} | ||
|
||
private Dictionary<string, TempNode> listOfAll = new Dictionary<string, TempNode>(); | ||
|
||
public List<TempNode> StartTheTreeWalk(Dictionary<string, TempNode> childrenDict) | ||
{ | ||
List<TempNode> RootNodes = new List<TempNode>(); | ||
foreach (TempNode t in childrenDict.Values) | ||
{ | ||
if (!alreadySeenList.Contains(t.Key)) | ||
{ | ||
TempNode root = new TempNode(t.Key); | ||
listOfAll.Add(root.Key, root); | ||
RootNodes.Add(root); | ||
HashSet<string> seen = new HashSet<string>() { root.Key }; | ||
WalkTree(root,t,seen); | ||
} | ||
|
||
} | ||
return RootNodes; | ||
} | ||
|
||
private void WalkTree(TempNode parent, TempNode graphNode, HashSet<string> alreadySeen) | ||
{ | ||
//trying to do breadth first | ||
//add all the children to the parent | ||
//then recurse down the children | ||
HashSet<string> visited = new HashSet<string>(alreadySeen); | ||
visited.Add(parent.Key); | ||
visited.Add(graphNode.Key); | ||
//Trace.WriteLine("W: "+parent.Key+ "G: "+graphNode.Key); | ||
|
||
Dictionary<TempNode, bool> children = new Dictionary<TempNode, bool>(); | ||
foreach (var c in graphNode.Children) | ||
{ | ||
|
||
TempNode cnode; | ||
if (listOfAll.TryGetValue(c.Key, out cnode)) | ||
{ | ||
if (graphNode.AlreadyWalked(cnode,visited)) | ||
{ | ||
children.TryAdd(c, false); | ||
} | ||
else | ||
{ | ||
visited.Add(cnode.Key); | ||
AddNode(parent, cnode); | ||
children.TryAdd(c, true); | ||
} | ||
|
||
} | ||
else | ||
{ | ||
var newT = new TempNode(c.Key); | ||
if (graphNode.AlreadyWalked(newT, visited)) | ||
{ | ||
children.TryAdd(c, false); | ||
} | ||
else | ||
{ | ||
visited.Add(newT.Key); | ||
listOfAll.TryAdd(newT.Key, newT); | ||
AddNode(parent, newT); | ||
children.TryAdd(c, true); | ||
} | ||
} | ||
} | ||
List<TempNode> childrenToWalk = children.Where(x => x.Value).Select(x=> x.Key).ToList(); | ||
foreach (var c in childrenToWalk) | ||
{ | ||
TempNode cnode; | ||
if (listOfAll.TryGetValue(c.Key, out cnode)) | ||
{ | ||
WalkTree(cnode, c,visited); | ||
//Trace.WriteLine("rf:" + parent.Key + "-" + graphNode.Key); | ||
} | ||
else | ||
{ | ||
var newT= new TempNode(c.Key); | ||
listOfAll.Add(c.Key, newT); | ||
WalkTree(newT, c, visited); | ||
//Trace.WriteLine("rn:" + parent.Key + "-" + graphNode.Key); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="parent"></param> | ||
/// <param name="treeNode"></param> | ||
/// <param name="graph"></param> | ||
/// <returns>true if needs processed false if not</returns> | ||
private bool AddNode(TempNode parent, TempNode treeNode) | ||
{ | ||
if(alreadySeenList.Contains(treeNode.Key) && alreadySeenList.Contains(parent.Key)) | ||
{ | ||
return true; | ||
} | ||
//Trace.WriteLine("Adding: " + parent.Key+" ->" +treeNode.Key); | ||
parent.Children.Add(treeNode); | ||
alreadySeenList.Add(treeNode.Key); | ||
alreadySeenList.Add(parent.Key); | ||
return true; | ||
|
||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.