Skip to content

Commit

Permalink
Ensure successor edges follow topological sort
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed May 12, 2023
1 parent 1c12ed6 commit a07f93b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
24 changes: 21 additions & 3 deletions Bonsai.Core/Expressions/ExpressionBuilderGraphDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Xml.Serialization;

namespace Bonsai.Expressions
Expand All @@ -8,15 +9,32 @@ namespace Bonsai.Expressions
/// </summary>
public class ExpressionBuilderGraphDescriptor
{
/// <summary>
/// Initializes a new instance of the <see cref="ExpressionBuilderGraphDescriptor"/> class.
/// </summary>
public ExpressionBuilderGraphDescriptor()
{
Nodes = new Collection<ExpressionBuilder>();
Edges = new Collection<ExpressionBuilderArgumentDescriptor>();
}

internal ExpressionBuilderGraphDescriptor(
IList<ExpressionBuilder> nodes,
IList<ExpressionBuilderArgumentDescriptor> edges)
{
Nodes = new Collection<ExpressionBuilder>(nodes);
Edges = new Collection<ExpressionBuilderArgumentDescriptor>(edges);
}

/// <summary>
/// Gets the collection of labels associated with each node in the expression builder graph.
/// </summary>
public Collection<ExpressionBuilder> Nodes { get; } = new Collection<ExpressionBuilder>();
public Collection<ExpressionBuilder> Nodes { get; }

/// <summary>
/// Gets a collection of descriptors corresponding to each edge in the expression builder graph.
/// </summary>
[XmlArrayItem("Edge")]
public Collection<ExpressionBuilderArgumentDescriptor> Edges { get; } = new Collection<ExpressionBuilderArgumentDescriptor>();
public Collection<ExpressionBuilderArgumentDescriptor> Edges { get; }
}
}
34 changes: 20 additions & 14 deletions Bonsai.Core/Expressions/ExpressionBuilderGraphExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,26 +1249,32 @@ public static ExpressionBuilderGraphDescriptor ToDescriptor(this ExpressionBuild
throw new ArgumentException("Cannot serialize a workflow with cyclical dependencies.", nameof(source));
}

var nodes = buildOrder.SelectMany(component => component).ToArray();
var descriptor = new ExpressionBuilderGraphDescriptor();
int index = 0;
var nodeMap = buildOrder
.SelectMany(component => component)
.ToDictionary(node => node, node => index++);

foreach (var node in nodes)
{
descriptor.Nodes.Add(node.Value);
}
var nodes = new List<ExpressionBuilder>(nodeMap.Count);
nodes.AddRange(nodeMap.Keys.Select(node => node.Value));

var from = 0;
foreach (var node in nodes)
var edges = new List<ExpressionBuilderArgumentDescriptor>();
foreach (var entry in nodeMap)
{
foreach (var successor in node.Successors)
var from = entry.Value;
foreach (var successor in entry.Key.Successors)
{
var to = Array.IndexOf(nodes, successor.Target);
descriptor.Edges.Add(new ExpressionBuilderArgumentDescriptor(from, to, successor.Label.Name));
var to = nodeMap[successor.Target];
edges.Add(new ExpressionBuilderArgumentDescriptor(from, to, successor.Label.Name));
}

from++;
}
return descriptor;

edges.Sort((x, y) =>
{
var from = x.From.CompareTo(y.From);
if (from != 0) return from;
else return x.To.CompareTo(y.To);
});
return new ExpressionBuilderGraphDescriptor(nodes, edges);
}

/// <summary>
Expand Down

0 comments on commit a07f93b

Please sign in to comment.