Skip to content

Commit

Permalink
update changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaRocks committed Sep 21, 2024
1 parent b84404d commit 3fa1405
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# <img src="https://github.com/NinjaRocks/abacus.net/blob/master/images/ninja-icon-16.png" alt="ninja" style="width:30px;"/> Abacus v1.0.0
# <img src="https://github.com/CodeShayk/abacus.net/blob/master/images/ninja-icon-16.png" alt="ninja" style="width:30px;"/> Abacus v1.0.0
[![NuGet version](https://badge.fury.io/nu/abacus.net.svg)](https://badge.fury.io/nu/abacus.net) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/NinjaRocks/abacus.net/blob/master/License.md) [![build-master](https://github.com/NinjaRocks/abacus.net/actions/workflows/Build-Master.yml/badge.svg)](https://github.com/NinjaRocks/abacus.net/actions/workflows/Build-Master.yml) [![GitHub Release](https://img.shields.io/github/v/release/ninjarocks/abacus.net?logo=github&sort=semver)](https://github.com/ninjarocks/abacus.net/releases/latest)
[![CodeQL](https://github.com/NinjaRocks/abacus.net/actions/workflows/codeql.yml/badge.svg)](https://github.com/NinjaRocks/abacus.net/actions/workflows/codeql.yml) [![.Net](https://img.shields.io/badge/.Net-8.0-blue)](https://dotnet.microsoft.com/en-us/download/dotnet/8.0)

Expand Down
111 changes: 97 additions & 14 deletions src/Abacus/Core/CreateWorkflow.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Net.NetworkInformation;

namespace Abacus.Core
{
public static class CreateWorkflow
Expand All @@ -9,25 +11,29 @@ public static ITemplate For<TEntity, TTrigger>(string name, string? version = nu
return new Template<TEntity, TTrigger>(name, version);
}

public static ITemplate Map<TTask>(this ITemplate workflow, Func<ITransition, ITransition> transition)
public static ITemplate StartWith<TTask>(this ITemplate workflow, Func<ITransition, ITransition> withTransition = null)
where TTask : ITaskType, new()
{
workflow.Root = new Transition<TTask>();
var child = transition(workflow.Root);
child.Parent = workflow.Root.Task;
workflow.Root.Transitions.Add(child);

if (withTransition != null)
{
var child = withTransition(workflow.Root);
child.Parent = workflow.Root.Task;
workflow.Root.Transitions.Add(child);
}

return workflow;
}

public static ITransition Map<TTriger, TTask>(this ITransition transtion, Func<ITransition, ITransition> dependent)
where TTriger : ITrigger
where TTask : ITaskType
public static ITransition CreateTask<TTask>(this ITransition transtion, ITrigger trigger, Func<ITransition, ITransition> withTransition = null)
where TTask : ITaskType, new()
{
var tran = new Transition<TTriger, TTask>() { Parent = transtion.Task };
var tran = new Transition<TTask>() { Trigger = trigger, Parent = transtion.Task };

if (dependent != null)
if (withTransition != null)
{
var child = dependent(tran);
var child = withTransition(tran);
child.Parent = tran.Task;
tran.Transitions.Add(child);
}
Expand All @@ -37,13 +43,90 @@ public static ITransition Map<TTriger, TTask>(this ITransition transtion, Func<I
return transtion;
}

public static ITransition Map<TTriger, TTask>(this ITransition transtion)
where TTriger : ITrigger
where TTask : ITaskType
public static ITransition Map<TTriger, TTask>(this ITransition transtion, Func<ITransition, ITransition> withTransition = null)
where TTriger : ITrigger, new()
where TTask : ITaskType, new()
{
var tran = new Transition<TTriger, TTask>() { Parent = transtion.Task };

if (withTransition != null)
{
var child = withTransition(tran);
child.Parent = tran.Task;
tran.Transitions.Add(child);
}

transtion.Transitions.Add(tran);

return transtion;
}

public static ITransition CreateTask<TTask>(this ITransition transtion, ITrigger trigger)
where TTask : ITaskType, new()
{
var tran = new Transition<TTask>() { Parent = transtion.Task };
transtion.Transitions.Add(tran);
return transtion;
}
}
}

public class With
{
public static ITrigger NoTrigger() => null;

// public static ITrigger Trigger<TTrigger>(IOutcomeType outcome=null)
// where TTrigger : ITaskTrigger, new() => new TTrigger();
//}

public static class On
{
public static ITransition Completion(ITransition parent, Func<ITransition> followtask)
{
var tran = followtask();
tran.Parent = parent.Task;

parent.Transitions.Add(tran);
return parent;
}
}

//public static class Create
//{
// public static ITransition Task<TTask>(ITrigger trigger, Func<ITransition, ITransition> withTransition = null)
// where TTask : ITaskType, new()
// {
// var tran = new Transition<TTask>() { Trigger = trigger, Parent = transtion.Task };

// if (withTransition != null)
// {
// var child = withTransition(tran);
// child.Parent = tran.Task;
// tran.Transitions.Add(child);
// }

// transtion.Transitions.Add(tran);

// return transtion;
// }
//}

public static class OnCompletion
{
public static ITransition CreateTask<TTask>(ITransition transtion, ITrigger trigger, Func<ITransition, ITransition> withTransition = null)
where TTask : ITaskType, new()
{
var tran = new Transition<TTask>() { Trigger = trigger, Parent = transtion.Task };

if (withTransition != null)
{
var child = withTransition(tran);
child.Parent = tran.Task;
tran.Transitions.Add(child);
}

transtion.Transitions.Add(tran);

return transtion;
}
}
}
6 changes: 6 additions & 0 deletions src/Abacus/Core/ITaskTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ public interface ITaskTrigger<in TTaskType, in TOutcomeType> : ITrigger
where TOutcomeType : IOutcomeType
{
}

public interface ITaskTrigger<in TTask> : ITrigger
where TTask : ITaskType, new()
{
IOutcomeType Outcome { get; set; }
}
}
19 changes: 16 additions & 3 deletions src/Abacus/Core/Transition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,31 @@ namespace Abacus.Core
/// </summary>
/// <typeparam name="TTask"></typeparam>
public class Transition<TTrigger, TTask> : ITransition
where TTask : ITaskType
where TTrigger : ITrigger
where TTask : ITaskType, new()
where TTrigger : ITrigger, new()
{
public Transition()
{
Trigger = new TTrigger();
Task = new TTask();
Transitions = new List<ITransition>();
}

public ITaskType Parent { get; set; }
public ITrigger Trigger { get; set; }
public ITaskType Task { get; set; }
public IList<ITransition> Transitions { get; set; }
}

public class Transition<TTask> : ITransition
where TTask : ITaskType
where TTask : ITaskType, new()
{
public Transition()
{
Task = new TTask();
Transitions = new List<ITransition>();
}

public ITaskType Parent { get; set; }
public ITrigger Trigger { get; set; }
public ITaskType Task { get; set; }
Expand Down
8 changes: 8 additions & 0 deletions test/Abacus.Tests/Tasks/Outcomes/Interested.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Abacus.Core;

namespace Abacus.Tests.Tasks.Outcomes
{
internal class Interested : IOutcomeType
{
}
}
4 changes: 0 additions & 4 deletions test/Abacus.Tests/Tasks/Outcomes/NotInterested.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ namespace Abacus.Tests.Tasks.Outcomes
internal class NotInterested : IOutcomeType
{
}

internal class Interested : IOutcomeType
{
}
}
9 changes: 9 additions & 0 deletions test/Abacus.Tests/Tasks/Triggers/LeadInterested.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Abacus.Core;
using Abacus.Tests.Tasks.Outcomes;

namespace Abacus.Tests.Tasks.Triggers
{
internal class LeadInterested : ITaskTrigger<ArrangeAppointment, Interested>
{
}
}
9 changes: 9 additions & 0 deletions test/Abacus.Tests/Tasks/Triggers/LeadNotInterested.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Abacus.Core;
using Abacus.Tests.Tasks.Outcomes;

namespace Abacus.Tests.Tasks.Triggers
{
internal class LeadNotInterested : ITaskTrigger<ArrangeAppointment, NotInterested>
{
}
}
10 changes: 10 additions & 0 deletions test/Abacus.Tests/Tasks/Triggers/LeadNotPurchased.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Abacus.Core;
using Abacus.Tests.Tasks.Outcomes;

namespace Abacus.Tests.Tasks.Triggers
{
internal class LeadNotPurchased : ITaskTrigger<SellProduct>
{
public IOutcomeType Outcome { get; set; } = new NotInterested();
}
}
12 changes: 0 additions & 12 deletions test/Abacus.Tests/Tasks/Triggers/LeadPurchased.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,4 @@ namespace Abacus.Tests.Tasks.Triggers
internal class LeadPurchased : ITaskTrigger<SellProduct, PurchasedProduct>
{
}

internal class LeadNotPurchased : ITaskTrigger<SellProduct, NotInterested>
{
}

internal class LeadNotInterested : ITaskTrigger<ArrangeAppointment, NotInterested>
{
}

internal class LeadInterested : ITaskTrigger<ArrangeAppointment, Interested>
{
}
}
57 changes: 47 additions & 10 deletions test/Abacus.Tests/WorkflowConfig.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,59 @@ public class WorkflowConfigTests

[SetUp]
public void Setup()
{
}

[Test]
public void TestConfigWithMapExtensions()
{
leadWorkflow = CreateWorkflow.For<Lead, LeadCreated>("LeadWorkflow")
.Map<CreateOpportunity>((root) => root
.Map<PassThrough, ArrangeAppointment>((appointment) => appointment
.Map<LeadNotInterested, RemoveOpportunity>()
.Map<LeadInterested, SellProduct>((sellproduct) => sellproduct
.Map<LeadNotPurchased, RemoveOpportunity>()
.Map<LeadPurchased, CreateCustomer>()
)
)
);
.StartWith<CreateOpportunity>((root) => root
.Map<PassThrough, ArrangeAppointment>((appointment) => appointment
.Map<LeadNotInterested, RemoveOpportunity>()
.Map<LeadInterested, SellProduct>((sellproduct) => sellproduct
.Map<LeadNotPurchased, RemoveOpportunity>()
.Map<LeadPurchased, CreateCustomer>()
)
)
);

//need to assert on config
Assert.IsNotNull(leadWorkflow);
}

[Test]
public void Test1()
public void TestConfigWithCreateTaskExtensions()
{
leadWorkflow = CreateWorkflow.For<Lead, LeadCreated>("LeadWorkflow")
.StartWith<CreateOpportunity>((root) => root
.CreateTask<ArrangeAppointment>(With.NoTrigger(), (appointment) => appointment
.CreateTask<RemoveOpportunity>(With.Trigger<LeadNotInterested>())
.CreateTask<SellProduct>(With.Trigger<LeadInterested>(), (sellproduct) => sellproduct
.CreateTask<RemoveOpportunity>(With.Trigger<LeadNotPurchased>())
.CreateTask<CreateCustomer>(With.Trigger<LeadPurchased>())
)
)
);

//need to assert on config
Assert.IsNotNull(leadWorkflow);
}

[Test]
public void TestConfigWithCreateTaskCompletionExtensions()
{
leadWorkflow = CreateWorkflow.For<Lead, LeadCreated>("LeadWorkflow")
.StartWith<CreateOpportunity>((root) => root
.CreateTask<ArrangeAppointment>(With.NoTrigger(), (appointment) => appointment
.CreateTask<RemoveOpportunity>(With.Trigger<LeadNotInterested>())
.CreateTask<SellProduct>(With.Trigger<LeadInterested>(), (sellproduct) => sellproduct
.CreateTask<RemoveOpportunity>(With.Trigger<LeadNotPurchased>())
.CreateTask<CreateCustomer>(With.Trigger<LeadPurchased>())
)
)
);

//need to assert on config
Assert.IsNotNull(leadWorkflow);
}
Expand Down

0 comments on commit 3fa1405

Please sign in to comment.