Skip to content

Commit

Permalink
adding preview of console logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Milian Lichere committed Aug 5, 2018
1 parent f1b231d commit f8640a1
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Linq;
using System.Linq;
using System.Text;
using BehaviourTree;
using BehaviourTree.Behaviours;
using BehaviourTree.Composites;
using BehaviourTree.Decorators;

namespace BehaviourTree
namespace BehaviourTreeBuilder.Tests
{
public sealed class BehaviourTreeExpressionPrinter<TContext> :
IVisitor<ActionBehaviour<TContext>>,
Expand Down
113 changes: 113 additions & 0 deletions src/BehaviourTree/BehaviourTreeConsoleLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Linq;
using BehaviourTree.Behaviours;
using BehaviourTree.Composites;
using BehaviourTree.Decorators;

namespace BehaviourTree
{
public sealed class BehaviourTreeConsoleLogger<TContext> :
IVisitor<Condition<TContext>>,
IVisitor<BaseBehaviour<TContext>>,
IVisitor<CompositeBehaviour<TContext>>,
IVisitor<DecoratorBehaviour<TContext>>,
IVisitor<IBehaviour<TContext>>
where TContext : IClock
{
private int _depth;

public void Visit(IBehaviour<TContext> obj)
{
Visit((dynamic)obj);
}

public void Visit(Condition<TContext> obj)
{
PrintNode(obj);
}

public void Visit(BaseBehaviour<TContext> obj)
{
PrintNode(obj);
}

public void Visit(CompositeBehaviour<TContext> obj)
{
PrintNode(obj);
VisitChildren(obj);
}

public void Visit(DecoratorBehaviour<TContext> obj)
{
PrintNode(obj);
VisitChild(obj);
}

private void VisitChildren(CompositeBehaviour<TContext> obj)
{
_depth++;

foreach (var child in obj.Children)
{
child.Accept(this);
}

_depth--;
}

private void VisitChild(DecoratorBehaviour<TContext> obj)
{
_depth++;

obj.Child.Accept(this);

_depth--;
}

private void PrintNode(IBehaviour<TContext> obj)
{
var indentation = GetIndentation();
var name = GetName(obj);
var color = GetColor(obj.Status);

Console.ForegroundColor = color;

var nodeExpression = $"{indentation}{name}";

Console.WriteLine(nodeExpression);

Console.ResetColor();
}

private static ConsoleColor GetColor(BehaviourStatus status)
{
switch (status)
{
case BehaviourStatus.Ready: return ConsoleColor.Blue;
case BehaviourStatus.Running: return ConsoleColor.Gray;
case BehaviourStatus.Succeeded: return ConsoleColor.Green;
case BehaviourStatus.Failed: return ConsoleColor.Red;
default: throw new ArgumentOutOfRangeException(nameof(status), status, null);
}
}

private string GetIndentation()
{
return string.Join(string.Empty, Enumerable.Repeat(" ", _depth));
}

private static string GetName(IBehaviour<TContext> obj)
{
if (!string.IsNullOrWhiteSpace(obj.Name))
{
return obj.Name;
}

var type = obj.GetType();

// TODO: check for generic

return type.Name;
}
}
}

0 comments on commit f8640a1

Please sign in to comment.