Skip to content

Commit

Permalink
+ distance vector (for sankey)
Browse files Browse the repository at this point in the history
  • Loading branch information
eosfor committed Oct 8, 2024
1 parent 0e94fd0 commit ce4406f
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 189 deletions.
11 changes: 11 additions & 0 deletions PSGraph.Common/Model/PSDistanceVectorRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using PSGraph.Model;

namespace PSGraph.Common.Model;

public class PSDistanceVectorRecord
{
public required PSVertex Vertex;
public double Level;

}
84 changes: 84 additions & 0 deletions PSGraph.Tests/GetGraphDistanceVectorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Xunit;
using System;
using System.Management.Automation;
using PSGraph.Model;
using FluentAssertions;
using System.Collections.Generic;
using System.Linq;
using PSGraph.Common.Model;

namespace PSGraph.Tests
{
public class GetGraphDistanceVectorCmdletTests : IDisposable
{
private PowerShell _powershell;

public GetGraphDistanceVectorCmdletTests()
{
_powershell = PowerShell.Create();
_powershell.AddCommand("Import-Module")
.AddParameter("Assembly", typeof(PSGraph.Cmdlets.GetGraphDistanceVector).Assembly);
_powershell.Invoke();
_powershell.Commands.Clear();
}

public void Dispose()
{
_powershell.Dispose();
}

[Fact]
public void GetGraphDistanceVector_ValidAcyclicGraph_ReturnsDistances()
{
// Arrange
_powershell.AddCommand("New-Graph");
var graphResults = _powershell.Invoke();
var graph = graphResults[0].BaseObject as PsBidirectionalGraph;

_powershell.Commands.Clear();

var vertexA = new PSVertex("A");
var vertexB = new PSVertex("B");
var vertexC = new PSVertex("C");

graph.AddVertexRange(new[] { vertexA, vertexB, vertexC });
graph.AddEdge(new PSEdge(vertexA, vertexB, new PSEdgeTag()));
graph.AddEdge(new PSEdge(vertexB, vertexC, new PSEdgeTag()));

_powershell.AddCommand("Get-GraphDistanceVector")
.AddParameter("Graph", graph);

// Act
var results = _powershell.Invoke();

// Assert
results.Should().NotBeNullOrEmpty();

var distances = results.Select(r => r.BaseObject as PSDistanceVectorRecord).ToList();
distances.Should().NotBeNull();
distances.Should().Contain(d => d.Vertex == vertexA && d.Level == 0);
distances.Should().Contain(d => d.Vertex == vertexB && d.Level == 1);
distances.Should().Contain(d => d.Vertex == vertexC && d.Level == 2);
}

[Fact]
public void GetGraphDistanceVector_EmptyGraph_ReturnsEmptyDistances()
{
// Arrange
_powershell.AddCommand("New-Graph");
var graphResults = _powershell.Invoke();
var graph = graphResults[0].BaseObject as PsBidirectionalGraph;

_powershell.Commands.Clear();

_powershell.AddCommand("Get-GraphDistanceVector")
.AddParameter("Graph", graph);

// Act
var results = _powershell.Invoke();

// Assert
results.Should().BeEmpty("because the graph is empty and has no vertices to calculate distances");
}
}
}
101 changes: 0 additions & 101 deletions PSGraph.Tests/GetGraphTopologicSortCmdletTests.cs

This file was deleted.

48 changes: 48 additions & 0 deletions PSGraphv2/cmdlets/graph/GetGraphDistanceVector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Management.Automation;
using QuikGraph.Algorithms;
using PSGraph.Model;
using System.Collections.Generic;
using QuikGraph.Algorithms.TopologicalSort;
using QuikGraph.Algorithms.Search;
using QuikGraph.Algorithms.Observers;
using System.Diagnostics.CodeAnalysis;
using PSGraph.Common.Model;
namespace PSGraph.Cmdlets
{
[Cmdlet(VerbsCommon.Get, "GraphDistanceVector")]
public class GetGraphDistanceVector : PSCmdlet
{
[Parameter(Mandatory = true)]
[ValidateNotNullOrEmpty]
public PsBidirectionalGraph Graph;

protected override void EndProcessing()
{

var dfs = new DepthFirstSearchAlgorithm<PSVertex, PSEdge>(Graph);
var distanceRecorder = new VertexDistanceRecorderObserver<PSVertex, PSEdge>(v => 1);

var rootVertices = Graph.Vertices.Where(v => Graph.InDegree(v) == 0);

using (distanceRecorder.Attach(dfs))
{
foreach (var vertex in rootVertices)
{
dfs.SetRootVertex(vertex);
dfs.Compute();
}

}

var res = new List<PSDistanceVectorRecord>();
foreach (var record in distanceRecorder.Distances)
{
var rr = new PSDistanceVectorRecord() { Vertex = record.Key, Level = record.Value };
res.Add(rr);
}

WriteObject(res, enumerateCollection: true);
}
}
}
21 changes: 0 additions & 21 deletions PSGraphv2/cmdlets/graph/GetGraphTopologicSort.cs

This file was deleted.

57 changes: 57 additions & 0 deletions PsGraph.Pester.Tests/PSGraph.GraphDistanceVector.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
BeforeAll {
Import-Module "/workspaces/PSGraph/PSGraph.Tests/bin/Debug/net8.0/PSQuickGraph.psd1"
}

Describe 'Get-GraphDistanceVector' {
BeforeEach {
# Initialize a new graph before each test
$graph = New-Graph

# Add vertices
Add-Vertex -Graph $graph -Vertex 'A'
Add-Vertex -Graph $graph -Vertex 'B'
Add-Vertex -Graph $graph -Vertex 'C'
Add-Vertex -Graph $graph -Vertex 'D'

# Add edges
Add-Edge -From 'A' -To 'B' -Graph $graph | Out-Null
Add-Edge -From 'A' -To 'C' -Graph $graph | Out-Null
Add-Edge -From 'B' -To 'D' -Graph $graph | Out-Null
Add-Edge -From 'C' -To 'D' -Graph $graph | Out-Null
}

It 'Should return the correct distance vector for the graph' {
# Run the Get-GraphDistanceVector cmdlet
$distanceVector = Get-GraphDistanceVector -Graph $graph

# Check the results are not empty
$distanceVector | Should -Not -BeNullOrEmpty

# Expected results for distance from root vertices
$expectedResults = @(
@{ Vertex = 'A'; Level = 0 },
@{ Vertex = 'B'; Level = 1 },
@{ Vertex = 'C'; Level = 1 },
@{ Vertex = 'D'; Level = 2 }
)

# Validate each record in the distance vector
foreach ($record in $distanceVector) {
$vertex = $record.Vertex.Name
$level = $record.Level

$expected = $expectedResults | Where-Object { $_.Vertex -eq $vertex }

$expected | Should -Not -BeNullOrEmpty
$level | Should -BeExactly $expected.Level
}
}

It 'Should return an empty result if the graph has no vertices' {
$emptyGraph = New-Graph
$result = Get-GraphDistanceVector -Graph $emptyGraph

# The result should be empty
$result | Should -BeNullOrEmpty
}
}
67 changes: 0 additions & 67 deletions PsGraph.Pester.Tests/PSGraph.TopologySort.Tests.ps1

This file was deleted.

0 comments on commit ce4406f

Please sign in to comment.