//------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //------------------------------------------------------------------------------ namespace System.Activities.Statements { using System; using System.Globalization; using System.Linq; using System.Runtime; using System.Text; using System.Text.RegularExpressions; /// /// Helper methods which are used by both StateMachine and State. /// static class StateMachineIdHelper { internal const char StateIdSeparator = ':'; /// /// Given current stateId and descendant Id, this method returns Id of direct child state of current state. /// This direct child state is either the state which descendantId represents or one of ancestor states of it. /// /// Internal StateId of StateMachine. /// Internal StateId of the state. /// Index position of the state in the state machine. public static int GetChildStateIndex(string stateId, string descendantId) { Fx.Assert(!string.IsNullOrEmpty(descendantId), "descendantId should not be null or empty."); Fx.Assert(!string.IsNullOrEmpty(stateId), "stateId should not be null or empty."); string[] child = descendantId.Split(StateIdSeparator); string[] parent = stateId.Split(StateIdSeparator); Fx.Assert(parent.Length < child.Length, "stateId should not be null or empty."); return int.Parse(child[parent.Length], CultureInfo.InvariantCulture); } /// /// Return the StateId, which is the identifier of a state. /// /// Internal StateId of the parent activity, which is StateMachine. /// Internal index of the state within StateMachine. /// Unique identifier of a state within StateMachine. public static string GenerateStateId(string parentId, int index) { return parentId + StateIdSeparator + index.ToString(CultureInfo.InvariantCulture); } /// /// Return the TransitionId, which is the identifier of a transition. /// /// Internal StateId of the state. /// Internal index of the transition within state. /// Unique identifier of a transition within a state. public static string GenerateTransitionId(string stateid, int transitionIndex) { return stateid + StateIdSeparator + transitionIndex.ToString(CultureInfo.InvariantCulture); } /// /// This method is used to see whether state1 is one of ancestors of state2. /// /// Internal StateId of the state1. /// Internal StateId of the state2. /// True if the state2.Id is identified as a child for state1. public static bool IsAncestor(string state1Id, string state2Id) { if (string.IsNullOrEmpty(state2Id)) { return false; } return state2Id.StartsWith(state1Id + StateIdSeparator, StringComparison.Ordinal); } } }