Skip to content

Commit

Permalink
Create class for parameters and optimize if-conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
marierin committed Aug 8, 2023
1 parent 59fd6d1 commit 2742aa4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 50 deletions.
18 changes: 18 additions & 0 deletions src/Moryx/Workflows/Implementation/ComparingProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Moryx.Workplans;
using System.Collections.Generic;

namespace Moryx.Workflows.Implementation
{
public class ComparingProperties
{
public IWorkplanStep Step { get; set;}
public IWorkplanStep NewStep { get; set; }
public Workplan Workplan { get; set; }
public Workplan NewWorkplan { get; set; }
public List<IWorkplanStep> IsChecked { get; set; }
public List<IWorkplanStep> NeedToCheck { get; set; }
public List<IWorkplanStep> NewNeedToCheck { get; set; }
}

}

96 changes: 46 additions & 50 deletions src/Moryx/Workflows/Implementation/Workplan.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Linq;
using Moryx.Workflows.Implementation;

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

public static bool NoteSameFollowringSteps(Workplan workplan, Workplan newWorkplan ,IWorkplanStep step, IWorkplanStep newStep, List<IWorkplanStep> needToCheck, List<IWorkplanStep> newNeedToCheck, List<IWorkplanStep> isChecked)
public static bool CompareOutputs(ComparingProperties outputProperties)
{
for (int a = 0; a < step.Outputs.Length; a++)

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

if (connector.Id == newConnector.Id)
{
bool isNotEndConnector = !(connector.Classification.Equals(NodeClassification.End)) && !(newConnector.Classification.Equals(NodeClassification.End));
bool isNotFailedConnector = !(connector.Classification.Equals(NodeClassification.Failed)) && !(newConnector.Classification.Equals(NodeClassification.Failed));

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 = outputProperties.Workplan.Steps.FirstOrDefault(x => x.Inputs.Any(y => y.Equals(connector)));
var newFollower = outputProperties.NewWorkplan.Steps.FirstOrDefault(x => x.Inputs.Any(y => y.Equals(newConnector)));

if (isNotEndConnector && isNotFailedConnector)
bool isSameStep = CompareSteps(follower, newFollower);
if (!isSameStep)
{
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)));
return false;
}

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

if (!(isAlreadyChecked))
{
needToCheck.Add(follower);
newNeedToCheck.Add(newFollower);
}
if (!(isAlreadyChecked))
{
outputProperties.NeedToCheck.Add(follower);
outputProperties.NewNeedToCheck.Add(newFollower);
}
}
else
{
return false;
}
}
return true;
}

public static bool CompareSteps(IWorkplanStep step1, IWorkplanStep step2)
{
return step1.GetType() == step2.GetType();
}

/// <summary>
/// Compare two workplans
/// </summary>
Expand All @@ -137,8 +139,8 @@ public static bool NoteSameFollowringSteps(Workplan workplan, Workplan newWorkpl
/// <returns></returns>
public static bool Equals(Workplan workplan, Workplan newWorkplan)
{
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)));
var step = workplan.Steps.FirstOrDefault(x => x.Inputs.Any(y => y.Classification.Equals(NodeClassification.Start)));
var newStep = newWorkplan.Steps.FirstOrDefault(x => x.Inputs.Any(y => y.Classification.Equals(NodeClassification.Start)));

List<IWorkplanStep> needToCheck = new List<IWorkplanStep>() { step };
List<IWorkplanStep> newNeedToCheck = new List<IWorkplanStep>() { newStep };
Expand All @@ -148,38 +150,32 @@ public static bool Equals(Workplan workplan, Workplan newWorkplan)
while (needToCheck.Count != 0 && newNeedToCheck.Count != 0)
{

bool isSameStep = (step.GetType() == newStep.GetType());
if (isSameStep)
bool isSameStep = CompareSteps(step, newStep);
if (!isSameStep)
{

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)
{
step = needToCheck[0];
newStep = newNeedToCheck[0];
}
}
else
{
return false;
}
return false;
}
else
var properties = new ComparingProperties() {Step = step, NewStep = newStep, Workplan = workplan, NewWorkplan = newWorkplan,IsChecked = isChecked, NeedToCheck = needToCheck, NewNeedToCheck = newNeedToCheck };
bool sameConnections = CompareOutputs(properties);
if (!sameConnections)
{
return false;
}
needToCheck.Remove(step);
newNeedToCheck.Remove(newStep);

isChecked.Add(step);

if (needToCheck.Count != 0 && newNeedToCheck.Count != 0)
{
step = needToCheck[0];
newStep = newNeedToCheck[0];
}

}

return true;
}


}
}

0 comments on commit 2742aa4

Please sign in to comment.