PageRenderTime 29ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/referencesource/System.Activities.Core.Presentation/System/Activities/Core/Presentation/StateMachineValidationErrorSourceLocator.cs

https://github.com/pruiz/mono
C# | 152 lines | 127 code | 17 blank | 8 comment | 32 complexity | 56a39a3bc4423e358053c4166bd50a5c MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // <copyright>
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // </copyright>
  4. namespace System.Activities.Core.Presentation
  5. {
  6. using System.Activities.Presentation.Validation;
  7. using System.Activities.Statements;
  8. using System.Collections.Generic;
  9. using System.Runtime;
  10. internal class StateMachineValidationErrorSourceLocator : IValidationErrorSourceLocator
  11. {
  12. public List<object> FindSourceDetailFromActivity(Activity errorSource, object errorSourceDetail)
  13. {
  14. if (errorSourceDetail == null)
  15. {
  16. return new List<object> { errorSource };
  17. }
  18. else
  19. {
  20. return FindRelativePath((StateMachine)errorSource, errorSourceDetail);
  21. }
  22. }
  23. // case 1: StateMachine -> Default expression of StateMachine's variable -> ...
  24. // case 2: StateMachine -> InternalState -> ...
  25. public void ReplaceParentChainWithSource(Activity parentActivity, List<object> parentChain)
  26. {
  27. Activity lastActivity = parentChain[parentChain.Count - 1] as Activity;
  28. StateMachine stateMachine = (StateMachine)parentActivity;
  29. foreach (Variable variable in stateMachine.Variables)
  30. {
  31. if (variable != null && variable.Default == lastActivity)
  32. {
  33. parentChain.Add(stateMachine);
  34. return;
  35. }
  36. }
  37. if (parentChain.Count > 1)
  38. {
  39. // assume lastActivity is InternalState
  40. // remove InternalState
  41. parentChain.RemoveAt(parentChain.Count - 1);
  42. Activity targetActivity = (Activity)parentChain[parentChain.Count - 1];
  43. // the targetActivity will be available in the path
  44. parentChain.RemoveAt(parentChain.Count - 1);
  45. List<object> path = FindRelativePath(stateMachine, targetActivity);
  46. foreach (object pathObject in path)
  47. {
  48. parentChain.Add(pathObject);
  49. }
  50. }
  51. }
  52. private static List<object> FindRelativePath(StateMachine machine, object descendent)
  53. {
  54. List<object> path = FindDescendentFromStateMachine(machine, descendent);
  55. path.Reverse();
  56. return path;
  57. }
  58. private static List<object> FindDescendentFromStateMachine(StateMachine machine, object descendent)
  59. {
  60. List<object> path = new List<object>();
  61. path.Add(machine);
  62. foreach (State state in machine.States)
  63. {
  64. if (state == descendent)
  65. {
  66. break;
  67. }
  68. else if (state.Entry == descendent)
  69. {
  70. path.Add(state);
  71. break;
  72. }
  73. else if (state.Exit == descendent)
  74. {
  75. path.Add(state);
  76. break;
  77. }
  78. else
  79. {
  80. Transition foundTransition = null;
  81. bool transitionAlone = false;
  82. foreach (Transition transition in state.Transitions)
  83. {
  84. foundTransition = transition;
  85. if (transition == descendent)
  86. {
  87. transitionAlone = true;
  88. break;
  89. }
  90. else if (transition.Trigger == descendent)
  91. {
  92. break;
  93. }
  94. else if (transition.Action == descendent)
  95. {
  96. break;
  97. }
  98. else if (transition.Condition == descendent)
  99. {
  100. break;
  101. }
  102. else
  103. {
  104. foundTransition = null;
  105. }
  106. }
  107. if (foundTransition != null)
  108. {
  109. path.Add(state);
  110. if (!transitionAlone)
  111. {
  112. path.Add(foundTransition);
  113. }
  114. break;
  115. }
  116. bool isVariableError = false;
  117. foreach (Variable variable in state.Variables)
  118. {
  119. if (variable.Default == descendent)
  120. {
  121. isVariableError = true;
  122. }
  123. }
  124. if (isVariableError)
  125. {
  126. path.Add(state);
  127. break;
  128. }
  129. }
  130. }
  131. path.Add(descendent);
  132. return path;
  133. }
  134. }
  135. }