Skip to content

Commit

Permalink
Give structure to current extensions and update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
1nf0rmagician committed Nov 28, 2024
1 parent 0f8aa29 commit 6b480bf
Showing 1 changed file with 68 additions and 98 deletions.
166 changes: 68 additions & 98 deletions src/Moryx.ControlSystem/Processes/ProcessHolderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,121 +11,71 @@ namespace Moryx.ControlSystem.Processes
/// </summary>
public static class ProcessHolderExtensions
{
#region Get Position
/// <summary>
/// Get the position by number
/// Get the position by its <see cref="IProcessHolderPosition.Identifier"/>
/// </summary>
public static TPosition GetPositionByIdentifier<TPosition>(this IProcessHolderGroup<TPosition> group, string identifier)
where TPosition : IProcessHolderPosition
{
return GetPosition(group.Positions, t => t.Identifier == identifier);
}
public static TPosition GetPositionByIdentifier<TPosition>(this IProcessHolderGroup<TPosition> group, string identifier)
where TPosition : IProcessHolderPosition => GetPosition(group.Positions, t => t.Identifier == identifier);

/// <summary>
/// Get the position by number
/// Get the position by its <see cref="IProcessHolderPosition.Identifier"/>
/// </summary>
public static TPosition GetPositionByIdentifier<TPosition>(this IEnumerable<TPosition> positions, string identifier)
where TPosition : IProcessHolderPosition
{
return GetPosition(positions, t => t.Identifier == identifier);
}
where TPosition : IProcessHolderPosition => GetPosition(positions, t => t.Identifier == identifier);

/// <summary>
/// Get the position by session
/// Get the position by its <see cref="IProcessHolderPosition.Session"/>
/// </summary>
public static TPosition GetPositionBySession<TPosition>(this IProcessHolderGroup<TPosition> 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);

/// <summary>
/// Get the position by session
/// Get the position by its <see cref="IProcessHolderPosition.Session"/>
/// </summary>
public static TPosition GetPositionBySession<TPosition>(this IEnumerable<TPosition> 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);

/// <summary>
/// Get the position by its process id
/// Get the position by its <see cref="IProcessHolderPosition.Process"/>
/// </summary>
public static TPosition GetPositionByProcessId<TPosition>(this IProcessHolderGroup<TPosition> 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);

/// <summary>
/// Get the position by its process id
/// Get the position by its <see cref="IProcessHolderPosition.Process"/>
/// </summary>
public static TPosition GetPositionByProcessId<TPosition>(this IEnumerable<TPosition> positions, long processId)
where TPosition : IProcessHolderPosition
{
return GetPosition(positions, t => t.Process?.Id == processId);
}
where TPosition : IProcessHolderPosition => GetPosition(positions, t => t.Process?.Id == processId);

/// <summary>
/// Get the position by id of the running activity
/// </summary>
public static TPosition GetPositionByActivityId<TPosition>(this IProcessHolderGroup<TPosition> group, long activityId)
where TPosition : IProcessHolderPosition
{
return GetPosition(group.Positions, t => (t.Session as ActivityStart)?.Activity.Id == activityId);
}

/// <summary>
/// Checks if the group holds a process with a finished activity having the matching result
/// </summary>
public static bool HasFinishedActivity<TPosition>(this IProcessHolderGroup<TPosition> group, long activityId, long activityResult)
where TPosition : IProcessHolderPosition
{
return group.Positions.Any(position => position.HasFinishedActivity(activityId, activityResult));
}
/// <summary>
/// Checks if the position holds a process with a finished activity having the matching result.
/// </summary>
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);

/// <summary>
/// Get the position by id of the running activity
/// </summary>
public static TPosition GetPositionByActivityId<TPosition>(this IEnumerable<TPosition> 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<TPosition>(IEnumerable<TPosition> positions, Func<TPosition, bool> filter)
where TPosition : IProcessHolderPosition
{
return positions.SingleOrDefault(filter);
}
where TPosition : IProcessHolderPosition => positions.SingleOrDefault(filter);

#endregion

#region Get or Create Sessions

/// <summary>
/// Try cast and return the holders session property
/// </summary>
public static TSession ConvertSession<TSession>(this IProcessHolderPosition holderPosition)
where TSession : Session
{
return holderPosition.Session as TSession;
}
where TSession : Session => holderPosition.Session as TSession;

/// <summary>
/// Access tracing of the current activity
/// </summary>
public static TTracing Tracing<TTracing>(this IProcessHolderPosition position)
where TTracing : Tracing, new()
{
var currentActivity = (position.Session as ActivityStart)?.Activity;
return currentActivity?.TransformTracing<TTracing>();
}

/// <summary>
/// Create sessions for all positions on a holder group, that have a process
/// Get or create a session for the <paramref name="position"/>, if it holds a process. Otherwise returns an empty enumerable.
/// This is usually used when attaching to the control system.
/// </summary>
public static IEnumerable<Session> Attach(this ProcessHolderPosition position)
{
Expand All @@ -136,43 +86,63 @@ public static IEnumerable<Session> Attach(this ProcessHolderPosition position)
}

/// <summary>
/// Create sessions for all positions on a holder group, that have a process
/// Get or create sessions for all <paramref name="positions"/> that have a process.
/// This is usually used when attaching to the control system.
/// </summary>
public static IEnumerable<Session> Attach(this IEnumerable<ProcessHolderPosition> positions)
{
return positions.SelectMany(p => p.Attach());
}
public static IEnumerable<Session> Attach(this IEnumerable<ProcessHolderPosition> positions)
=> positions.SelectMany(p => p.Attach());

/// <summary>
/// Create sessions for all positions on a holder group, that have a process
/// Get a session if <paramref name="position"/> has one. Otherwise returns an empty enumerable.
/// This is usually used when detaching from the control system.
/// </summary>
public static IEnumerable<Session> Detach(this ProcessHolderPosition position)
{
return position.Session != null ? new[] { position.Session } : Enumerable.Empty<Session>();
}
public static IEnumerable<Session> Detach(this ProcessHolderPosition position)
=> position.Session != null ? new[] { position.Session } : Enumerable.Empty<Session>();

/// <summary>
/// Create sessions for all positions on a holder group, that have a process
/// </summary>
public static IEnumerable<Session> Detach(this IEnumerable<ProcessHolderPosition> positions)
{
return positions.Where(p => p.Session != null).Select(p => p.Session);
}
public static IEnumerable<Session> Detach(this IEnumerable<ProcessHolderPosition> positions)
=> positions.Where(p => p.Session != null).Select(p => p.Session);

#endregion

#region Mounting

/// <summary>
/// Assign process and session to this position
/// Assign a <paramref name="process"/> to this position
/// </summary>
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));

/// <summary>
/// Assign process and session to this position
/// Assign <paramref name="process"/> and <paramref name="session"/> to this position
/// </summary>
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

/// <summary>
/// Checks if the group holds a process with a finished activity having the matching result
/// </summary>
public static bool HasFinishedActivity<TPosition>(this IProcessHolderGroup<TPosition> group, long activityId, long activityResult)
where TPosition : IProcessHolderPosition => group.Positions.Any(position => position.HasFinishedActivity(activityId, activityResult));

/// <summary>
/// Checks if the position holds a process with a finished activity having the matching result.
/// </summary>
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

/// <summary>
/// Access tracing of the current activity
/// </summary>
public static TTracing Tracing<TTracing>(this IProcessHolderPosition position)
where TTracing : Tracing, new() => (position.Session as ActivityStart)?.Activity?.TransformTracing<TTracing>();
}
}

0 comments on commit 6b480bf

Please sign in to comment.