Skip to content

Commit

Permalink
Add basic node structure and test project
Browse files Browse the repository at this point in the history
  • Loading branch information
mhjort committed Nov 28, 2017
1 parent f5201db commit 549c596
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/src.sln → aivo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.0.0
MinimumVisualStudioVersion = 10.0.0.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aivo", "aivo.csproj", "{9593E956-9720-48E4-B1C3-6A906AAEE949}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aivo", "src/aivo.csproj", "{9593E956-9720-48E4-B1C3-6A906AAEE949}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{566B9528-29DA-4ED6-8E8F-040B9FAD273E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{9593E956-9720-48E4-B1C3-6A906AAEE949}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9593E956-9720-48E4-B1C3-6A906AAEE949}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9593E956-9720-48E4-B1C3-6A906AAEE949}.Release|Any CPU.Build.0 = Release|Any CPU
{566B9528-29DA-4ED6-8E8F-040B9FAD273E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{566B9528-29DA-4ED6-8E8F-040B9FAD273E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{566B9528-29DA-4ED6-8E8F-040B9FAD273E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{566B9528-29DA-4ED6-8E8F-040B9FAD273E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
19 changes: 19 additions & 0 deletions src/ActionNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace AivoTree
{
public class ActionNode<T> : TreeNode<T>
{
private readonly Func<int, T, AivoTreeStatus> _fn;

public ActionNode(Func<int, T, AivoTreeStatus> fn)
{
_fn = fn;
}

public AivoTreeStatus Tick(int frame, T context)
{
return _fn(frame, context);
}
}
}
26 changes: 26 additions & 0 deletions src/SequenceNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Linq;

namespace AivoTree
{
public class SequenceNode<T> : TreeNode<T>
{
private readonly TreeNode<T>[] _nodes;

public SequenceNode(params TreeNode<T>[] nodes)
{
_nodes = nodes;
}

public AivoTreeStatus Tick(int frame, T context)
{
return _nodes.Aggregate(AivoTreeStatus.Success, (acc, curr) =>
{
if (acc == AivoTreeStatus.Success)
{
return curr.Tick(frame, context);
}
return acc;
});
}
}
}
7 changes: 7 additions & 0 deletions src/TreeNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AivoTree
{
public interface TreeNode<T>
{
AivoTreeStatus Tick(int frame, T context);
}
}
3 changes: 3 additions & 0 deletions src/aivo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="ActionNode.cs" />
<Compile Include="AivoTreeStatus.cs" />
<Compile Include="SequenceNode.cs" />
<Compile Include="TreeNode.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
36 changes: 36 additions & 0 deletions test/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Test")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Test")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("566B9528-29DA-4ED6-8E8F-040B9FAD273E")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
67 changes: 67 additions & 0 deletions test/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using AivoTree;
using NUnit.Framework;

namespace Test
{
[TestFixture]
public class Tests
{
[Test]
public void RunsActioNode()
{
var model = new MyModel();
var action = new ActionNode<MyModel>((frame, ctx) =>
{
ctx.value = frame;
return AivoTreeStatus.Success;
});
Assert.AreEqual(AivoTreeStatus.Success, action.Tick(1, model));
Assert.AreEqual(1, model.value);
}

[Test]
public void SequenceNodeFailsWithFirstFailure()
{
var model = new MyModel();
var sequence = new SequenceNode<MyModel>(new SucceedingNode(), new FailingNode(), new ShouldNotRunNode());
Assert.AreEqual(AivoTreeStatus.Failure, sequence.Tick(1, model));
}

[Test]
public void SequenceNodeSucceedsIfAllNodesSucceed()
{
var model = new MyModel();
var sequence = new SequenceNode<MyModel>(new SucceedingNode(), new SucceedingNode());
Assert.AreEqual(AivoTreeStatus.Success, sequence.Tick(1, model));
}
}

public class MyModel
{
public int value;
}

public class FailingNode : TreeNode<MyModel>
{
public AivoTreeStatus Tick(int frame, MyModel context)
{
return AivoTreeStatus.Failure;
}
}

public class SucceedingNode : TreeNode<MyModel>
{
public AivoTreeStatus Tick(int frame, MyModel context)
{
return AivoTreeStatus.Success;
}
}

public class ShouldNotRunNode : TreeNode<MyModel>
{
public AivoTreeStatus Tick(int frame, MyModel context)
{
throw new AssertionException("Should not be called");
}
}
}
4 changes: 4 additions & 0 deletions test/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.5.0" targetFramework="net45" />
</packages>
62 changes: 62 additions & 0 deletions test/test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{566B9528-29DA-4ED6-8E8F-040B9FAD273E}</ProjectGuid>
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Test</RootNamespace>
<AssemblyName>Test</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="nunit.framework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb">
<HintPath>..\packages\NUnit.3.5.0\lib\net45\nunit.framework.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\aivo.csproj">
<Project>{9593E956-9720-48E4-B1C3-6A906AAEE949}</Project>
<Name>aivo</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

0 comments on commit 549c596

Please sign in to comment.