Skip to content

Commit

Permalink
Graph GetNodeByIdentifier and readme fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vfrz committed Aug 23, 2023
1 parent 498990e commit 8b5d8c6
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 6 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ graph.Add(myEdge);

```csharp
// Subgraph identifier need to start with "cluster" to be identified as a cluster
var mySubGraph = new DotSubGraph().WithIdentifier("cluster_0");
var mySubgraph = new DotSubgraph().WithIdentifier("cluster_0");

// Create a subgraph with attributes (only used for cluster)
var mySubGraph = new DotSubGraph()
.WithIdentifier("cluster_0")
var mySubgraph2 = new DotSubgraph()
.WithIdentifier("cluster_1")
.WithColor(DotColor.Red)
.WithStyle(DotSubGraphStyle.Dashed)
.WithLabel("My subgraph!");

// Add node, edge, subgraph
subGraph.Add(myNode);
subGraph.Add(myEdge);
subGraph.Add(mySubGraph2);
subGraph.Add(mySubgraph2);

// Add subgraph to main graph
graph.Add(mySubGraph);
graph.Add(mySubgraph);
```

## Compile to DOT format
Expand Down
32 changes: 32 additions & 0 deletions Sources/DotNetGraph.Tests/Core/DotGraphTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,36 @@ public async Task CompileDirectedGraphWithOneNode()
var result = writer.GetStringBuilder().ToString();
result.Should().Be("digraph Test {\n\tTestNode\n}\n");
}

[TestMethod]
public void GetNodeByIdentifier()
{
var node = new DotNode()
.WithIdentifier("TestNode");

var graph = new DotGraph()
.WithIdentifier("Test")
.Directed()
.Add(node);

var nodeFromGraph = graph.GetNodeByIdentifier("TestNode");

nodeFromGraph.Should().Be(node);
}

[TestMethod]
public void GetNodeByIdentifier_MissingNode()
{
var node = new DotNode()
.WithIdentifier("TestNode");

var graph = new DotGraph()
.WithIdentifier("Test")
.Directed()
.Add(node);

var nodeFromGraph = graph.GetNodeByIdentifier("NotTestNode");

nodeFromGraph.Should().BeNull();
}
}
9 changes: 9 additions & 0 deletions Sources/DotNetGraph/Core/DotBaseGraph.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DotNetGraph.Attributes;
using DotNetGraph.Compilation;
Expand All @@ -15,6 +16,14 @@ public DotRankDirAttribute RankDir
set => SetAttribute("rankdir", value);
}

public DotNode GetNodeByIdentifier(string identifier, bool isHtml = false)
{
return Elements
.Where(e => e is DotNode)
.Cast<DotNode>()
.FirstOrDefault(node => node.Identifier == new DotIdentifier(identifier, isHtml));
}

public List<IDotElement> Elements { get; } = new List<IDotElement>();

public abstract override Task CompileAsync(CompilationContext context);
Expand Down
40 changes: 39 additions & 1 deletion Sources/DotNetGraph/Core/DotIdentifier.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand All @@ -6,7 +7,7 @@

namespace DotNetGraph.Core
{
public class DotIdentifier : IDotElement
public class DotIdentifier : IDotElement, IEquatable<DotIdentifier>
{
private static readonly Regex NoQuotesRequiredRegex
= new Regex("^([a-zA-Z\\200-\\377_][a-zA-Z\\200-\\3770-9_]*|[-]?(.[0-9]+|[0-9]+(.[0-9]+)?))$");
Expand Down Expand Up @@ -50,5 +51,42 @@ private static bool RequiresDoubleQuotes(string value)
{
return ReservedWords.Contains(value) || !NoQuotesRequiredRegex.IsMatch(value);
}

public bool Equals(DotIdentifier other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return Value == other.Value && IsHtml == other.IsHtml;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != GetType())
return false;
return Equals((DotIdentifier) obj);
}

public override int GetHashCode()
{
unchecked
{
return ((Value != null ? Value.GetHashCode() : 0) * 397) ^ IsHtml.GetHashCode();
}
}

public static bool operator ==(DotIdentifier identifier1, DotIdentifier identifier2)
{
if (identifier1 is null)
return identifier2 is null;
return identifier1.Equals(identifier2);
}

public static bool operator !=(DotIdentifier identifier1, DotIdentifier identifier2) => !(identifier1 == identifier2);
}
}

0 comments on commit 8b5d8c6

Please sign in to comment.