Skip to content

Commit f89a155

Browse files
authored
Merge pull request SciSharp#1339 from iceljc/features/fix-frontier-order
fix frontier order
2 parents 9e3fc04 + f1fb91f commit f89a155

3 files changed

Lines changed: 42 additions & 22 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Rules/Frontier.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,18 @@ public sealed class StackFrontier<T> : IFrontier<T>
3131

3232
public void DrainTo(IFrontier<T> other)
3333
{
34-
// Reverse so the item that was on top is added first and
35-
// therefore ends up at the same "priority" position in the target.
36-
var items = _stack.ToList();
37-
items.Reverse();
38-
_stack.Clear();
34+
// Pop gives items in priority order (highest-weight first).
35+
var items = new List<T>();
36+
while (_stack.Count > 0)
37+
{
38+
items.Add(_stack.Pop());
39+
}
40+
41+
if (other is StackFrontier<T>)
42+
{
43+
items.Reverse();
44+
}
45+
3946
foreach (var item in items)
4047
{
4148
other.Add(item);
@@ -58,9 +65,21 @@ public sealed class QueueFrontier<T> : IFrontier<T>
5865

5966
public void DrainTo(IFrontier<T> other)
6067
{
68+
// Dequeue gives items in priority order (highest-weight first).
69+
var items = new List<T>();
6170
while (_queue.Count > 0)
6271
{
63-
other.Add(_queue.Dequeue());
72+
items.Add(_queue.Dequeue());
73+
}
74+
75+
if (other is StackFrontier<T>)
76+
{
77+
items.Reverse();
78+
}
79+
80+
foreach (var item in items)
81+
{
82+
other.Add(item);
6483
}
6584
}
6685
}

src/Infrastructure/BotSharp.Abstraction/Rules/RuleGraph.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,24 @@ public void AddEdge(RuleNode from, RuleNode to, EdgeItemPayload payload)
121121
}
122122
}
123123

124-
public IEnumerable<(RuleNode, RuleEdge)> GetParentNodes(RuleNode node)
124+
public IEnumerable<(RuleNode, RuleEdge)> GetParentNodes(RuleNode node, bool ascending = false)
125125
{
126-
return _edges.Where(e => e.To != null && e.To.Id.IsEqualTo(node.Id))
127-
.OrderByDescending(e => e.Weight)
128-
.Select(e => (e.From, e))
129-
.ToList();
126+
var filtered = _edges.Where(e => e.To != null && e.To.Id.IsEqualTo(node.Id));
127+
var ordered = ascending
128+
? filtered.OrderBy(e => e.Weight)
129+
: filtered.OrderByDescending(e => e.Weight);
130+
131+
return ordered.Select(e => (e.From, e)).ToList();
130132
}
131133

132-
public IEnumerable<(RuleNode, RuleEdge)> GetChildrenNodes(RuleNode node)
134+
public IEnumerable<(RuleNode, RuleEdge)> GetChildrenNodes(RuleNode node, bool ascending = false)
133135
{
134-
return _edges.Where(e => e.From != null && e.From.Id.IsEqualTo(node.Id))
135-
.OrderByDescending(e => e.Weight)
136-
.Select(e => (e.To, e))
137-
.ToList();
136+
var filtered = _edges.Where(e => e.From != null && e.From.Id.IsEqualTo(node.Id));
137+
var ordered = ascending
138+
? filtered.OrderBy(e => e.Weight)
139+
: filtered.OrderByDescending(e => e.Weight);
140+
141+
return ordered.Select(e => (e.To, e)).ToList();
138142
}
139143

140144
public RuleGraphInfo GetGraphInfo()

src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,7 @@ private async Task ExecuteGraphTraversal(
211211
? new QueueFrontier<(RuleNode, RuleEdge)>()
212212
: new StackFrontier<(RuleNode, RuleEdge)>();
213213

214-
// Seed the frontier with root's children
215-
foreach (var child in graph.GetChildrenNodes(root))
216-
{
217-
frontier.Add(child);
218-
}
214+
EnqueueChildren(frontier, graph, root);
219215

220216
while (frontier.Count > 0)
221217
{
@@ -345,7 +341,8 @@ private static void EnqueueChildren(
345341
RuleGraph graph,
346342
RuleNode parent)
347343
{
348-
foreach (var child in graph.GetChildrenNodes(parent))
344+
var sortAscending = frontier is StackFrontier<(RuleNode, RuleEdge)>;
345+
foreach (var child in graph.GetChildrenNodes(parent, sortAscending))
349346
{
350347
frontier.Add(child);
351348
}

0 commit comments

Comments
 (0)