From 6b480bf4882e97c5d46dfe2994e016f33fabf3e6 Mon Sep 17 00:00:00 2001 From: Marcel Vielhaus Date: Thu, 28 Nov 2024 06:46:23 +0100 Subject: [PATCH] Give structure to current extensions and update comments --- .../Processes/ProcessHolderExtensions.cs | 166 +++++++----------- 1 file changed, 68 insertions(+), 98 deletions(-) diff --git a/src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs b/src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs index b8dfc61..abd8d03 100644 --- a/src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs +++ b/src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs @@ -11,121 +11,71 @@ namespace Moryx.ControlSystem.Processes /// public static class ProcessHolderExtensions { + #region Get Position /// - /// Get the position by number + /// Get the position by its /// - public static TPosition GetPositionByIdentifier(this IProcessHolderGroup group, string identifier) - where TPosition : IProcessHolderPosition - { - return GetPosition(group.Positions, t => t.Identifier == identifier); - } + public static TPosition GetPositionByIdentifier(this IProcessHolderGroup group, string identifier) + where TPosition : IProcessHolderPosition => GetPosition(group.Positions, t => t.Identifier == identifier); /// - /// Get the position by number + /// Get the position by its /// public static TPosition GetPositionByIdentifier(this IEnumerable positions, string identifier) - where TPosition : IProcessHolderPosition - { - return GetPosition(positions, t => t.Identifier == identifier); - } + where TPosition : IProcessHolderPosition => GetPosition(positions, t => t.Identifier == identifier); /// - /// Get the position by session + /// Get the position by its /// public static TPosition GetPositionBySession(this IProcessHolderGroup group, Session session) - where TPosition : IProcessHolderPosition - { - return GetPosition(group.Positions, t => t.Session?.Id == session.Id); - } + where TPosition : IProcessHolderPosition => GetPosition(group.Positions, t => t.Session?.Id == session.Id); /// - /// Get the position by session + /// Get the position by its /// public static TPosition GetPositionBySession(this IEnumerable positions, Session session) - where TPosition : IProcessHolderPosition - { - return GetPosition(positions, t => t.Session?.Id == session.Id); - } + where TPosition : IProcessHolderPosition => GetPosition(positions, t => t.Session?.Id == session.Id); /// - /// Get the position by its process id + /// Get the position by its /// public static TPosition GetPositionByProcessId(this IProcessHolderGroup group, long processId) - where TPosition : IProcessHolderPosition - { - return GetPosition(group.Positions, t => t.Process?.Id == processId); - } + where TPosition : IProcessHolderPosition => GetPosition(group.Positions, t => t.Process?.Id == processId); /// - /// Get the position by its process id + /// Get the position by its /// public static TPosition GetPositionByProcessId(this IEnumerable positions, long processId) - where TPosition : IProcessHolderPosition - { - return GetPosition(positions, t => t.Process?.Id == processId); - } + where TPosition : IProcessHolderPosition => GetPosition(positions, t => t.Process?.Id == processId); /// /// Get the position by id of the running activity /// public static TPosition GetPositionByActivityId(this IProcessHolderGroup group, long activityId) - where TPosition : IProcessHolderPosition - { - return GetPosition(group.Positions, t => (t.Session as ActivityStart)?.Activity.Id == activityId); - } - - /// - /// Checks if the group holds a process with a finished activity having the matching result - /// - public static bool HasFinishedActivity(this IProcessHolderGroup group, long activityId, long activityResult) - where TPosition : IProcessHolderPosition - { - return group.Positions.Any(position => position.HasFinishedActivity(activityId, activityResult)); - } - /// - /// Checks if the position holds a process with a finished activity having the matching result. - /// - public static bool HasFinishedActivity(this IProcessHolderPosition holderPosition, long activityId, long activityResult) - { - return holderPosition.Process?.GetActivities(activity => activity.Id == activityId && activity.Result?.Numeric == activityResult).Any() == true; - } + where TPosition : IProcessHolderPosition => GetPosition(group.Positions, t => (t.Session as ActivityStart)?.Activity.Id == activityId); /// /// Get the position by id of the running activity /// public static TPosition GetPositionByActivityId(this IEnumerable positions, long activityId) - where TPosition : IProcessHolderPosition - { - return GetPosition(positions, t => (t.Session as ActivityStart)?.Activity.Id == activityId); - } + where TPosition : IProcessHolderPosition => GetPosition(positions, t => (t.Session as ActivityStart)?.Activity.Id == activityId); private static TPosition GetPosition(IEnumerable positions, Func filter) - where TPosition : IProcessHolderPosition - { - return positions.SingleOrDefault(filter); - } + where TPosition : IProcessHolderPosition => positions.SingleOrDefault(filter); + + #endregion + + #region Get or Create Sessions /// /// Try cast and return the holders session property /// public static TSession ConvertSession(this IProcessHolderPosition holderPosition) - where TSession : Session - { - return holderPosition.Session as TSession; - } + where TSession : Session => holderPosition.Session as TSession; /// - /// Access tracing of the current activity - /// - public static TTracing Tracing(this IProcessHolderPosition position) - where TTracing : Tracing, new() - { - var currentActivity = (position.Session as ActivityStart)?.Activity; - return currentActivity?.TransformTracing(); - } - - /// - /// Create sessions for all positions on a holder group, that have a process + /// Get or create a session for the , if it holds a process. Otherwise returns an empty enumerable. + /// This is usually used when attaching to the control system. /// public static IEnumerable Attach(this ProcessHolderPosition position) { @@ -136,43 +86,63 @@ public static IEnumerable Attach(this ProcessHolderPosition position) } /// - /// Create sessions for all positions on a holder group, that have a process + /// Get or create sessions for all that have a process. + /// This is usually used when attaching to the control system. /// - public static IEnumerable Attach(this IEnumerable positions) - { - return positions.SelectMany(p => p.Attach()); - } + public static IEnumerable Attach(this IEnumerable positions) + => positions.SelectMany(p => p.Attach()); /// - /// Create sessions for all positions on a holder group, that have a process + /// Get a session if has one. Otherwise returns an empty enumerable. + /// This is usually used when detaching from the control system. /// - public static IEnumerable Detach(this ProcessHolderPosition position) - { - return position.Session != null ? new[] { position.Session } : Enumerable.Empty(); - } + public static IEnumerable Detach(this ProcessHolderPosition position) + => position.Session != null ? new[] { position.Session } : Enumerable.Empty(); /// /// Create sessions for all positions on a holder group, that have a process /// - public static IEnumerable Detach(this IEnumerable positions) - { - return positions.Where(p => p.Session != null).Select(p => p.Session); - } + public static IEnumerable Detach(this IEnumerable positions) + => positions.Where(p => p.Session != null).Select(p => p.Session); + + #endregion + + #region Mounting /// - /// Assign process and session to this position + /// Assign a to this position /// - public static void Mount(this IProcessHolderPosition position, IProcess process) - { - position.Mount(new MountInformation(process, null)); - } + public static void Mount(this IProcessHolderPosition position, IProcess process) + => position.Mount(new MountInformation(process, null)); /// - /// Assign process and session to this position + /// Assign and to this position /// - public static void Mount(this IProcessHolderPosition position, IProcess process, Session session) - { - position.Mount(new MountInformation(process, session)); - } + public static void Mount(this IProcessHolderPosition position, IProcess process, Session session) + => position.Mount(new MountInformation(process, session)); + + #endregion + + #region Has + + /// + /// Checks if the group holds a process with a finished activity having the matching result + /// + public static bool HasFinishedActivity(this IProcessHolderGroup group, long activityId, long activityResult) + where TPosition : IProcessHolderPosition => group.Positions.Any(position => position.HasFinishedActivity(activityId, activityResult)); + + /// + /// Checks if the position holds a process with a finished activity having the matching result. + /// + public static bool HasFinishedActivity(this IProcessHolderPosition holderPosition, long activityId, long activityResult) + => holderPosition.Process?.GetActivities(activity => activity.Id == activityId && activity.Result?.Numeric == activityResult).Any() == true; + + #endregion + + /// + /// Access tracing of the current activity + /// + public static TTracing Tracing(this IProcessHolderPosition position) + where TTracing : Tracing, new() => (position.Session as ActivityStart)?.Activity?.TransformTracing(); } } \ No newline at end of file