Skip to content

Commit

Permalink
old code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mihainradu committed Apr 11, 2024
1 parent 518d15f commit 5a81556
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 352 deletions.
20 changes: 3 additions & 17 deletions src/UiPath.Workflow.Runtime/Statements/FlowDecision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,17 @@ public string DisplayName
set => _displayName = value;
}

protected override void OnEndCacheMetadata()
protected override void OnCacheMetadata()
{
if (Condition == null)
{
Metadata.AddValidationError(SR.FlowDecisionRequiresCondition(Flowchart.DisplayName));
}
}

internal override IReadOnlyList<FlowNode> GetSuccessors()
{
var result = new List<FlowNode>(2);
if (True != null)
{
result.Add(True);
}

if (False != null)
{
result.Add(False);
}
return result;
}
internal override IReadOnlyList<FlowNode> GetSuccessors() => new[] { True, False };

internal override IEnumerable<Activity> GetChildActivities()
=> new[] { Condition };
internal override IEnumerable<Activity> GetChildActivities() => new[] { Condition };

internal override void Execute()
{
Expand Down
4 changes: 2 additions & 2 deletions src/UiPath.Workflow.Runtime/Statements/FlowMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ internal override void Execute()

DoNotComplete = false;
Debug.WriteLine($"{Node}: Next queued");
Flowchart.EnqueueNodeExecution(Node.Next, Flowchart.CurrentBranch.Pop());
Flowchart.EnqueueNodeExecution(Node.Next, EnqueueType.Pop);
}
}

protected override void OnEndCacheMetadata()
{
var connectedBranches = Flowchart.GetStaticStack(this).GetTop();
var connectedBranches = Flowchart.GetStaticSplitsStack(this).GetTop();
var splits = connectedBranches.Select(bl => bl).Distinct().ToList();
if (splits.Count > 1)
AddValidationError("All merge branches should start in the same Split node.", splits);
Expand Down
60 changes: 14 additions & 46 deletions src/UiPath.Workflow.Runtime/Statements/FlowNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Activities.Validation;
using System.Dynamic;
using System.Linq;
// This file is part of Core WF which is licensed under the MIT license.
// See LICENSE file in the project root for full license information.
Expand All @@ -9,67 +8,36 @@ namespace System.Activities.Statements;
public abstract class FlowNode
{
private Flowchart _owner;
private int _cacheId;


internal FlowNode()
{
Index = -1;
}

internal int Index { get; set; }

internal bool IsOpen => _owner != null;

internal Flowchart Flowchart => _owner;
protected NativeActivityMetadata Metadata { get; private set; }

// Returns true if this is the first time we've visited this node during this pass
internal bool Open(Flowchart owner, NativeActivityMetadata metadata)
{
if (_cacheId == owner.CacheId)
{
// We've already visited this node during this pass
if (!ReferenceEquals(_owner, owner))
{
metadata.AddValidationError(SR.FlowNodeCannotBeShared(_owner.DisplayName, owner.DisplayName));
}

// Whether we found an issue or not we don't want to change
// the metadata during this pass.
return false;
}

// if owner.ValidateUnconnectedNodes - Flowchart will be responsible for calling OnOpen for all the Nodes (connected and unconnected)
if (!owner.ValidateUnconnectedNodes)
{
OnOpen(owner, metadata);
}

internal void CacheMetadata(Flowchart owner, NativeActivityMetadata metadata)
{
_owner = owner;
_cacheId = owner.CacheId;
Index = -1;

return true;
Metadata = metadata;
OnCacheMetadata();
}

internal virtual void OnOpen(Flowchart owner, NativeActivityMetadata metadata)
{ }

internal virtual IEnumerable<Activity> GetChildActivities()
=> null;


internal abstract IReadOnlyList<FlowNode> GetSuccessors();

internal void EndCacheMetadata(NativeActivityMetadata metadata)
internal void EndCacheMetadata(NativeActivityMetadata metadata)
{
Metadata = metadata;
OnEndCacheMetadata();
}

protected virtual void OnCacheMetadata() { }
protected virtual void OnEndCacheMetadata() { }

internal virtual Flowchart.NodeInstance CreateInstance()
=> new();
internal virtual IEnumerable<Activity> GetChildActivities() => null;
internal abstract IReadOnlyList<FlowNode> GetSuccessors();


internal virtual Flowchart.NodeInstance CreateInstance() => new();
internal virtual void Execute() { }

protected virtual void OnCompletionCallback() { }
Expand All @@ -95,7 +63,7 @@ protected void AddValidationError(string message, IEnumerable<FlowNode> nodes =
{
Metadata.AddValidationError(new ValidationError(message)
{
SourceDetail = (nodes ?? Array.Empty<FlowNode>()).Concat(new[] { this }).ToArray()
SourceDetail = new[] { this }.Concat(nodes ?? Array.Empty<FlowNode>()).ToArray()
});
}

Expand Down
4 changes: 1 addition & 3 deletions src/UiPath.Workflow.Runtime/Statements/FlowSplit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ internal override void Execute()
for (int i = Node.Branches.Count - 1; i >= 0; i--)
{
var branch = Node.Branches[i];
Flowchart.EnqueueNodeExecution(branch, Flowchart.CurrentBranch.Push(
branchId: $"{branch.Index}",
splitId: Flowchart.CurrentNodeId));
Flowchart.EnqueueNodeExecution(branch, Flowchart.EnqueueType.Push);
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/UiPath.Workflow.Runtime/Statements/FlowStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ public FlowStep() { }
[DependsOn("Action")]
public FlowNode Next { get; set; }

internal override IReadOnlyList<FlowNode> GetSuccessors()
{
if (Next != null)
{
return new[] { Next };
}
return Array.Empty<FlowNode>();
}
internal override IReadOnlyList<FlowNode> GetSuccessors() => new[] { Next };

internal override void Execute()
{
Expand Down
28 changes: 8 additions & 20 deletions src/UiPath.Workflow.Runtime/Statements/FlowSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,23 @@ public FlowSwitch()

internal override IReadOnlyList<FlowNode> GetSuccessors()
{
var connections = new List<FlowNode>(Cases.Count + 1);
foreach (KeyValuePair<T, FlowNode> item in Cases)
{
connections.Add(item.Value);
}
var connections = new List<FlowNode>(Cases.Values);
if (Default != null)
{
connections.Add(Default);
}
return connections;
}

protected override void OnEndCacheMetadata()
protected override void OnCacheMetadata()
{
if (Expression == null)
{
Metadata.AddValidationError(SR.FlowSwitchRequiresExpression(Flowchart.DisplayName));
}
}

internal override IEnumerable<Activity> GetChildActivities()
=> new[] { Expression };
internal override IEnumerable<Activity> GetChildActivities() => new[] { Expression };

internal override void Execute()
{
Expand All @@ -63,19 +58,12 @@ internal override void Execute()

internal override void OnCompletionCallback<TResult>(TResult value)
{
T newValue;

switch (value)
var newValue = value switch
{
case T castedValue:
newValue = castedValue;
break;
case null:
newValue = default;
break;
default:
throw new InvalidCastException();
}
T castedValue => castedValue,
null => default,
_ => throw new InvalidCastException(),
};

if (Cases.TryGetValue(newValue, out FlowNode result))
{
Expand Down
Loading

0 comments on commit 5a81556

Please sign in to comment.