Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/Infrastructure/BotSharp.Abstraction/Rules/Frontier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ public sealed class StackFrontier<T> : IFrontier<T>

public void DrainTo(IFrontier<T> other)
{
// Reverse so the item that was on top is added first and
// therefore ends up at the same "priority" position in the target.
var items = _stack.ToList();
items.Reverse();
_stack.Clear();
// Pop gives items in priority order (highest-weight first).
var items = new List<T>();
while (_stack.Count > 0)
{
items.Add(_stack.Pop());
}

if (other is StackFrontier<T>)
{
items.Reverse();
}

foreach (var item in items)
{
other.Add(item);
Expand All @@ -58,9 +65,21 @@ public sealed class QueueFrontier<T> : IFrontier<T>

public void DrainTo(IFrontier<T> other)
{
// Dequeue gives items in priority order (highest-weight first).
var items = new List<T>();
while (_queue.Count > 0)
{
other.Add(_queue.Dequeue());
items.Add(_queue.Dequeue());
}

if (other is StackFrontier<T>)
{
items.Reverse();
}

foreach (var item in items)
{
other.Add(item);
}
}
}
24 changes: 14 additions & 10 deletions src/Infrastructure/BotSharp.Abstraction/Rules/RuleGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,24 @@ public void AddEdge(RuleNode from, RuleNode to, EdgeItemPayload payload)
}
}

public IEnumerable<(RuleNode, RuleEdge)> GetParentNodes(RuleNode node)
public IEnumerable<(RuleNode, RuleEdge)> GetParentNodes(RuleNode node, bool ascending = false)
{
return _edges.Where(e => e.To != null && e.To.Id.IsEqualTo(node.Id))
.OrderByDescending(e => e.Weight)
.Select(e => (e.From, e))
.ToList();
var filtered = _edges.Where(e => e.To != null && e.To.Id.IsEqualTo(node.Id));
var ordered = ascending
? filtered.OrderBy(e => e.Weight)
: filtered.OrderByDescending(e => e.Weight);

return ordered.Select(e => (e.From, e)).ToList();
}

public IEnumerable<(RuleNode, RuleEdge)> GetChildrenNodes(RuleNode node)
public IEnumerable<(RuleNode, RuleEdge)> GetChildrenNodes(RuleNode node, bool ascending = false)
{
return _edges.Where(e => e.From != null && e.From.Id.IsEqualTo(node.Id))
.OrderByDescending(e => e.Weight)
.Select(e => (e.To, e))
.ToList();
var filtered = _edges.Where(e => e.From != null && e.From.Id.IsEqualTo(node.Id));
var ordered = ascending
? filtered.OrderBy(e => e.Weight)
: filtered.OrderByDescending(e => e.Weight);

return ordered.Select(e => (e.To, e)).ToList();
}

public RuleGraphInfo GetGraphInfo()
Expand Down
9 changes: 3 additions & 6 deletions src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,7 @@ private async Task ExecuteGraphTraversal(
? new QueueFrontier<(RuleNode, RuleEdge)>()
: new StackFrontier<(RuleNode, RuleEdge)>();

// Seed the frontier with root's children
foreach (var child in graph.GetChildrenNodes(root))
{
frontier.Add(child);
}
EnqueueChildren(frontier, graph, root);

while (frontier.Count > 0)
{
Expand Down Expand Up @@ -345,7 +341,8 @@ private static void EnqueueChildren(
RuleGraph graph,
RuleNode parent)
{
foreach (var child in graph.GetChildrenNodes(parent))
var sortAscending = frontier is StackFrontier<(RuleNode, RuleEdge)>;
foreach (var child in graph.GetChildrenNodes(parent, sortAscending))
{
frontier.Add(child);
}
Expand Down
Loading