Skip to content

Commit b539c94

Browse files
committed
+ additional parameter
1 parent ac96201 commit b539c94

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

PSGraph.Common/Model/PSBidirectionalGraph.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using QuikGraph;
2+
using QuikGraph.Algorithms;
23

34
namespace PSGraph.Model
45
{
@@ -11,6 +12,16 @@ public PsBidirectionalGraph(bool allowParallelEdges = false) : base(allowParalle
1112

1213
public PsBidirectionalGraph(PsBidirectionalGraph g) : base(g) { }
1314

15+
// Strongly-typed clone that preserves the derived type
16+
public new PsBidirectionalGraph Clone()
17+
{
18+
return new PsBidirectionalGraph(this);
19+
}
20+
21+
public bool IsDag => this.IsDirectedAcyclicGraph();
22+
23+
public IEnumerable<PSVertex> Roots => this.Roots();
24+
1425
// B: explicit duplicate prevention + vertex reuse by label
1526
public override bool AddEdge(PSEdge edge)
1627
{

PSGraph/cmdlets/graph/ExportGraphViewCmdLet.cs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public class ExportGraphViewCmdLet : PSCmdlet
3434
[ValidateNotNullOrEmpty]
3535
public string Path;
3636

37+
[Parameter(Mandatory = false)]
38+
public SwitchParameter UseVirtualTreeRoot;
39+
3740
private VegaExportTypes _vegaExportType = VegaExportTypes.JSON;
3841

3942

@@ -114,24 +117,58 @@ protected override void ProcessRecord()
114117
private string ExportVegaTreeLayout(VegaExportTypes exportType)
115118
{
116119
var modulePath = MyInvocation.MyCommand.Module?.ModuleBase;
117-
var records = Graph.ConvertToParentChildList();
118120
var vega = VegaHelper.GetVegaTemplateObjectFromModulePath(modulePath, "vega.tree.layout.json");
119-
vega.Data[0].Values = records.ToList<object>();
120121

121-
switch (exportType)
122+
if (Graph.Roots().Count() == 0)
122123
{
123-
case VegaExportTypes.HTML:
124-
return VegaHelper.RenderHtml(vega);
125-
default:
126-
case VegaExportTypes.JSON:
127-
return vega.ToJson();
128-
}
124+
var records = Graph.ConvertToParentChildList();
125+
vega.Data[0].Values = records.ToList<object>();
129126

130-
//string html = VegaHelper.RenderHtml(vega);
131-
132-
//return html;
127+
switch (exportType)
128+
{
129+
case VegaExportTypes.HTML:
130+
return VegaHelper.RenderHtml(vega);
131+
default:
132+
case VegaExportTypes.JSON:
133+
return vega.ToJson();
134+
}
135+
}
136+
else
137+
{
138+
if (!UseVirtualTreeRoot.IsPresent)
139+
{
140+
WriteError(new ErrorRecord(
141+
new InvalidOperationException("The graph cannot be exported as a tree. It has multiple roots."),
142+
"PSGraph.ExportVegaTreeLayout.MultipleRoots",
143+
ErrorCategory.InvalidOperation,
144+
Graph));
145+
return string.Empty;
146+
}
147+
else
148+
{
149+
var tmpGraph = Graph.Clone();
150+
var roots = tmpGraph.Roots();
151+
var virtualRoot = new PSVertex("VirtualRoot Node");
152+
tmpGraph.AddVertex(virtualRoot);
153+
foreach (var root in roots)
154+
{
155+
tmpGraph.AddVerticesAndEdge(new PSEdge(virtualRoot, root, new PSEdgeTag(string.Empty)));
156+
}
157+
158+
var records = tmpGraph.ConvertToParentChildList();
159+
vega.Data[0].Values = records.ToList<object>();
160+
161+
switch (exportType)
162+
{
163+
case VegaExportTypes.HTML:
164+
return VegaHelper.RenderHtml(vega);
165+
default:
166+
case VegaExportTypes.JSON:
167+
return vega.ToJson();
168+
}
133169

134-
//File.WriteAllText(Path, html);
170+
}
171+
}
135172
}
136173

137174
private string ExportVegaAdjacencyMatrix(VegaExportTypes exportType)

0 commit comments

Comments
 (0)