Skip to content

Commit

Permalink
Update after merge request feedback
Browse files Browse the repository at this point in the history
- create an additional method to check connactions and following steps
- reduce unnecessary variables
- changing the sequence
- rename methods in the EqualTest class
  • Loading branch information
marierin committed Aug 7, 2023
1 parent 28eaa67 commit 8aadc89
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 82 deletions.
122 changes: 64 additions & 58 deletions src/Moryx/Workflows/Implementation/Workplan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Linq;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;

namespace Moryx.Workplans
{
Expand Down Expand Up @@ -95,85 +95,92 @@ public static Workplan Restore(List<IConnector> connectors, List<IWorkplanStep>
return new Workplan(connectors, steps);
}

/// <summary>
/// Compare two workplans
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
public static bool noteSameFollowringSteps(Workplan workplan, Workplan newWorkplan ,IWorkplanStep step, IWorkplanStep newStep, List<IWorkplanStep> needToCheck, List<IWorkplanStep> newNeedToCheck, List<IWorkplanStep> isChecked)
{

if (!(obj is Workplan))
{
return false;
}
Workplan newPlan = (Workplan)obj;

var start = this.Connectors.First(x => x.Name.Equals("Start"));
var end = this.Connectors.First(x => x.Name.Equals("End"));
var failed = this.Connectors.First(x => x.Name.Equals("Failed"));

var newStart = newPlan.Connectors.First(x => x.Name.Equals("Start"));
var newEnd = newPlan.Connectors.First(x => x.Name.Equals("End"));
var newFailed = newPlan.Connectors.First(x => x.Name.Equals("Failed"));

var step = this.Steps.First(x => x.Inputs.Any(y => y.Equals(start)));
var newStep = newPlan.Steps.First(x => x.Inputs.Any(y => y.Equals(newStart)));

List<IWorkplanStep> needToCheck = new List<IWorkplanStep>();
List<IWorkplanStep> newNeedToCheck = new List<IWorkplanStep>();

List<IWorkplanStep> check = new List<IWorkplanStep>();

needToCheck.Add(step);
newNeedToCheck.Add(newStep);

while (needToCheck.Count != 0 && newNeedToCheck.Count != 0)

for (int a = 0; a < step.Outputs.Length; a++)
{
var connector = step.Outputs[a];
var newConnector = newStep.Outputs[a];


for (int a = 0; a < step.Outputs.Length; a++)
if (connector.Id == newConnector.Id)
{

var connector = step.Outputs[a];
var newConnector = newStep.Outputs[a];

bool isNotEndConnector = (connector != end && newConnector != newEnd);
bool isNotFailedConnector = (connector != failed && newConnector != newFailed);
bool isNotEndConnector = !(connector.Classification.Equals(NodeClassification.End)) && !(newConnector.Classification.Equals(NodeClassification.End));
bool isNotFailedConnector = !(connector.Classification.Equals(NodeClassification.Failed)) && !(newConnector.Classification.Equals(NodeClassification.Failed));

if (isNotEndConnector && isNotFailedConnector)
{
var follower = this.Steps.FirstOrDefault(x => x.Inputs.Any(y => y.Equals(connector)));
var newFollower = newPlan.Steps.FirstOrDefault(x => x.Inputs.Any(y => y.Equals(newConnector)));
var follower = workplan.Steps.FirstOrDefault(x => x.Inputs.Any(y => y.Equals(connector)));
var newFollower = newWorkplan.Steps.FirstOrDefault(x => x.Inputs.Any(y => y.Equals(newConnector)));

bool isAlreadyChecked = (isChecked.Contains(follower) || isChecked.Contains(newFollower));

bool isAlreadyChecked = (check.Contains(follower) || check.Contains(newFollower));

if (!(isAlreadyChecked))
{
needToCheck.Add(follower);
newNeedToCheck.Add(newFollower);
}
}
else if (connector.Classification != newConnector.Classification)
{
return false;
}
}
else
{
return false;
}
}
return true;
}

/// <summary>
/// Compare two workplans
/// </summary>
/// <param name="workplan"></param>
/// <param name="newWorkplan"></param>
/// <returns></returns>
public static bool Equals(Workplan workplan, Workplan newWorkplan)
{

//var start = workplan.Connectors.First(x => x.Classification.Equals(NodeClassification.Start));
//var end = workplan.Connectors.First(x => x.Classification.Equals(NodeClassification.End));
//var failed = workplan.Connectors.First(x => x.Classification.Equals(NodeClassification.Failed));

//var newStart = newWorkplan.Connectors.First(x => x.Classification.Equals(NodeClassification.Start));
//var newEnd = newWorkplan.Connectors.First(x => x.Classification.Equals(NodeClassification.End));
//var newFailed = newWorkplan.Connectors.First(x => x.Classification.Equals(NodeClassification.Failed));

var step = workplan.Steps.First(x => x.Inputs.Any(y => y.Classification.Equals(NodeClassification.Start)));
var newStep = newWorkplan.Steps.First(x => x.Inputs.Any(y => y.Classification.Equals(NodeClassification.Start)));

List<IWorkplanStep> needToCheck = new List<IWorkplanStep>() { step };
List<IWorkplanStep> newNeedToCheck = new List<IWorkplanStep>() { newStep };

List<IWorkplanStep> isChecked = new List<IWorkplanStep>();

while (needToCheck.Count != 0 && newNeedToCheck.Count != 0)
{
//noteAllFollowringSteps(workplan, newWorkplan, step, newStep, needToCheck, newNeedToCheck, isChecked);

bool isSameStep = (step.GetType() == newStep.GetType());
if (isSameStep)
{
needToCheck.Remove(step);
newNeedToCheck.Remove(newStep);

check.Add(step);
bool sameConnections = noteSameFollowringSteps(workplan, newWorkplan, step, newStep, needToCheck, newNeedToCheck, isChecked);
if (sameConnections)
{
needToCheck.Remove(step);
newNeedToCheck.Remove(newStep);

isChecked.Add(step);

if (needToCheck.Count != 0 && newNeedToCheck.Count != 0)
if (needToCheck.Count != 0 && newNeedToCheck.Count != 0)
{
step = needToCheck[0];
newStep = newNeedToCheck[0];
}
}
else
{
step = needToCheck[0];
newStep = newNeedToCheck[0];
return false;
}
}
else
Expand All @@ -184,8 +191,7 @@ public override bool Equals(object obj)

return true;
}




}
}
Expand Down
11 changes: 0 additions & 11 deletions src/Tests/Moryx.Tests/Workplans/Dummies/AssemblingActivity.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using Moryx.AbstractionLayer;
using Moryx.AbstractionLayer.Capabilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Moryx.Tests.Workplans.Dummies
{
[ActivityResults(typeof(DefaultActivityResult))]
public class AssemblingActivity : Activity<AssemblingParameters>
{


public override ProcessRequirement ProcessRequirement => ProcessRequirement.NotRequired;

public override ICapabilities RequiredCapabilities => new AssemblingCapabilities();
Expand All @@ -26,9 +19,5 @@ protected override ActivityResult CreateFailureResult()
{
return ActivityResult.Create(DefaultActivityResult.Failed);
}




}
}
26 changes: 13 additions & 13 deletions src/Tests/Moryx.Tests/Workplans/EqualTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class EqualTest
private Workplan fifthWorkplan;
private Workplan sixthWorkplan;

public Workplan CreateFirstWorkplan()
public Workplan CreateSimpleWorkplan()
{
var plan = new Workplan();

Expand All @@ -38,7 +38,7 @@ public Workplan CreateFirstWorkplan()
return plan;
}

public Workplan CreateSecondWorkplan()
public Workplan CreateWorkplanWithLoop()
{
var plan = new Workplan();

Expand All @@ -58,7 +58,7 @@ public Workplan CreateSecondWorkplan()
return plan;
}

public Workplan CreateThirdWorkplan()
public Workplan CreateWorkplanWithLoopsAndBranches()
{
var plan = new Workplan();

Expand All @@ -85,41 +85,41 @@ public Workplan CreateThirdWorkplan()
[SetUp]
public void SetUp()
{
firstWorkplan = CreateFirstWorkplan();
secondWorkplan = CreateFirstWorkplan();
firstWorkplan = CreateSimpleWorkplan();
secondWorkplan = CreateSimpleWorkplan();

thirdWorkplan = CreateSecondWorkplan();
fourthWorkplan = CreateSecondWorkplan();
thirdWorkplan = CreateWorkplanWithLoop();
fourthWorkplan = CreateWorkplanWithLoop();

fifthWorkplan = CreateThirdWorkplan();
sixthWorkplan = CreateThirdWorkplan();
fifthWorkplan = CreateWorkplanWithLoopsAndBranches();
sixthWorkplan = CreateWorkplanWithLoopsAndBranches();
}

[Test]
public void TestEqualWorkplans()
{
bool result = firstWorkplan.Equals(secondWorkplan);
bool result = Workplan.Equals(firstWorkplan, secondWorkplan);
Assert.That(result, Is.True);
}

[Test]
public void TestEqualWorkplansSimpleLoop()
{
bool result = thirdWorkplan.Equals(fourthWorkplan);
bool result = Workplan.Equals(thirdWorkplan, fourthWorkplan);
Assert.That(result, Is.True);
}

[Test]
public void TestEqualWorkplansDoubleLoop()
{
bool result = fifthWorkplan.Equals(sixthWorkplan);
bool result = Workplan.Equals(fifthWorkplan, sixthWorkplan);
Assert.That(result, Is.True);
}

[Test]
public void TestUnequalWorkplans()
{
bool r = thirdWorkplan.Equals(fifthWorkplan);
bool r = Workplan.Equals(firstWorkplan, sixthWorkplan);
Assert.That(r, Is.False);
}
}
Expand Down

0 comments on commit 8aadc89

Please sign in to comment.