From 2742aa494f99c6ada9cf026dfa913b33e1152821 Mon Sep 17 00:00:00 2001 From: Marie Rinsche Date: Tue, 8 Aug 2023 13:44:41 +0200 Subject: [PATCH] Create class for parameters and optimize if-conditions --- .../Implementation/ComparingProperties.cs | 18 ++++ .../Workflows/Implementation/Workplan.cs | 96 +++++++++---------- 2 files changed, 64 insertions(+), 50 deletions(-) create mode 100644 src/Moryx/Workflows/Implementation/ComparingProperties.cs diff --git a/src/Moryx/Workflows/Implementation/ComparingProperties.cs b/src/Moryx/Workflows/Implementation/ComparingProperties.cs new file mode 100644 index 000000000..0acd48bbf --- /dev/null +++ b/src/Moryx/Workflows/Implementation/ComparingProperties.cs @@ -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 IsChecked { get; set; } + public List NeedToCheck { get; set; } + public List NewNeedToCheck { get; set; } + } + +} + diff --git a/src/Moryx/Workflows/Implementation/Workplan.cs b/src/Moryx/Workflows/Implementation/Workplan.cs index 9d0bb02a6..bf473a2bd 100644 --- a/src/Moryx/Workflows/Implementation/Workplan.cs +++ b/src/Moryx/Workflows/Implementation/Workplan.cs @@ -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 { @@ -94,41 +92,45 @@ public static Workplan Restore(List connectors, List return new Workplan(connectors, steps); } - public static bool NoteSameFollowringSteps(Workplan workplan, Workplan newWorkplan ,IWorkplanStep step, IWorkplanStep newStep, List needToCheck, List newNeedToCheck, List 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(); + } + /// /// Compare two workplans /// @@ -137,8 +139,8 @@ public static bool NoteSameFollowringSteps(Workplan workplan, Workplan newWorkpl /// 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 needToCheck = new List() { step }; List newNeedToCheck = new List() { newStep }; @@ -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; } - } } \ No newline at end of file