PageRenderTime 850ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Misc_Microsoft_MPL_Libs/XObjects/Src/FSM.cs

http://github.com/o2platform/O2.Platform.Projects
C# | 324 lines | 249 code | 66 blank | 9 comment | 49 complexity | 1f21c0f47e6432de8d1c2891a533e722 MD5 | raw file
  1. //Copyright (c) Microsoft Corporation. All rights reserved.
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Xml;
  6. using System.Xml.Schema;
  7. using System.Xml.Linq;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Diagnostics;
  12. using System.Reflection;
  13. using Xml.Schema.Linq.CodeGen;
  14. using System.Text;
  15. namespace Xml.Schema.Linq {
  16. public class FSM {
  17. internal static int InvalidState = default(int);
  18. private readonly int startState;
  19. private readonly Set<int> acceptStates;
  20. private readonly IDictionary<int, Transitions> trans;
  21. public FSM(int startState, Set<int> acceptStates,
  22. IDictionary<int, Transitions> trans) {
  23. this.startState = startState;
  24. this.acceptStates = acceptStates;
  25. this.trans = trans;
  26. }
  27. public int Start { get { return startState; } }
  28. public Set<int> Accept { get { return acceptStates; } }
  29. public IDictionary<int, Transitions> Trans {
  30. get { return trans; }
  31. }
  32. public override String ToString() {
  33. return "DFA start=" + startState + "\naccept=" + acceptStates;
  34. }
  35. public bool isAccept(int state) {
  36. return this.acceptStates.Contains(state);
  37. }
  38. internal void AddTransitions(FSM otherFSM) {
  39. foreach(KeyValuePair<int, Transitions> pair in otherFSM.Trans) {
  40. this.trans.Add(pair);
  41. }
  42. }
  43. internal static void CloneTransitions(FSM srcFsm, int srcState, FSM destFsm, int destState) {
  44. //Clone the transitions from srcState to destState
  45. Transitions srcTrans = null;
  46. srcFsm.Trans.TryGetValue(srcState, out srcTrans);
  47. if (srcTrans == null) return;
  48. Transitions destTrans = null;
  49. destFsm.Trans.TryGetValue(destState, out destTrans);
  50. if (destTrans == null) {
  51. destTrans = new Transitions();
  52. destFsm.Trans.Add(destState, destTrans);
  53. }
  54. destTrans.CloneTransitions(srcTrans, srcState, destState);
  55. }
  56. }
  57. public class SingleTransition {
  58. internal XName nameLabel;
  59. internal WildCard wcLabel;
  60. internal int nextState;
  61. public SingleTransition(XName name, int newState) {
  62. nameLabel = name;
  63. wcLabel = null;
  64. nextState = newState;
  65. }
  66. public SingleTransition(WildCard wildCard, int newState) {
  67. wcLabel = wildCard;
  68. nameLabel = null;
  69. nextState = newState;
  70. }
  71. }
  72. public class Transitions {
  73. internal Dictionary<XName, int> nameTransitions;
  74. internal Dictionary<WildCard, int> wildCardTransitions;
  75. internal bool IsEmpty {
  76. get {
  77. return Count == 0;
  78. }
  79. }
  80. internal int Count {
  81. get {
  82. int count = 0;
  83. if (nameTransitions != null) count += nameTransitions.Count;
  84. if (wildCardTransitions != null) count += wildCardTransitions.Count;
  85. return count;
  86. }
  87. }
  88. public Transitions() {
  89. }
  90. public Transitions(params SingleTransition[] transitions) {
  91. if (transitions != null)
  92. foreach(SingleTransition st in transitions) {
  93. if (st.nameLabel != null) {
  94. if (nameTransitions == null) nameTransitions = new Dictionary<XName, int>();
  95. nameTransitions.Add(st.nameLabel, st.nextState);
  96. }
  97. else {
  98. if (wildCardTransitions == null) wildCardTransitions = new Dictionary<WildCard, int>();
  99. wildCardTransitions.Add(st.wcLabel, st.nextState);
  100. }
  101. }
  102. }
  103. public Transitions(Dictionary<XName, int> nameTrans,
  104. Dictionary<WildCard, int> wildCardTrans) {
  105. this.nameTransitions = nameTrans;
  106. this.wildCardTransitions = wildCardTrans;
  107. }
  108. internal static void Add<T>(ref Dictionary<T, int> d, T id, int nextState)
  109. {
  110. if (d == null)
  111. {
  112. d = new Dictionary<T, int>();
  113. }
  114. d[id] = nextState;
  115. }
  116. internal void Add(XName name, int nextState) {
  117. Add(ref this.nameTransitions, name, nextState);
  118. }
  119. internal void Add(WildCard wildCard, int nextState) {
  120. Add(ref this.wildCardTransitions, wildCard, nextState);
  121. }
  122. internal int GetNextState(XName inputSymbol, out XName matchingName, out WildCard matchingWildCard) {
  123. matchingWildCard = null;
  124. matchingName = null;
  125. //first try name table, then match any
  126. int state = FSM.InvalidState;
  127. if (nameTransitions!=null && nameTransitions.TryGetValue(inputSymbol, out state)) {
  128. matchingName = inputSymbol;
  129. }
  130. else if (wildCardTransitions != null) {//We need to scan the wildcard dictionary because this is not "equality" based checking
  131. foreach(KeyValuePair<WildCard, int> pair in wildCardTransitions) {
  132. if (pair.Key.Allows(inputSymbol)) {
  133. matchingWildCard = pair.Key;
  134. state = pair.Value;
  135. }
  136. }
  137. }
  138. return state;
  139. }
  140. internal void Clear(){
  141. if (nameTransitions != null) nameTransitions.Clear();
  142. if (wildCardTransitions!=null) wildCardTransitions.Clear();
  143. }
  144. internal void CloneTransitions(Transitions otherTransitions, int srcState, int destState) {
  145. bool isEmpty = IsEmpty;
  146. if (otherTransitions.nameTransitions!=null) {
  147. if (nameTransitions==null) {
  148. nameTransitions = new Dictionary<XName,int>();
  149. }
  150. foreach(KeyValuePair<XName, int> pair in otherTransitions.nameTransitions) {
  151. int nextState = pair.Value;
  152. //An optimization: if my transition is empty, even copy self-loop
  153. if (isEmpty && (nextState == srcState)) nextState = destState;
  154. // This would silently overwrite the value for an existing key
  155. // The assumption is due to UPA, if there are two same transitions
  156. // they should lead to the same state
  157. nameTransitions[pair.Key] = nextState;
  158. }
  159. }
  160. if (otherTransitions.wildCardTransitions != null) {
  161. if (wildCardTransitions == null) {
  162. wildCardTransitions = new Dictionary<WildCard,int>();
  163. }
  164. foreach(KeyValuePair<WildCard, int> pair in otherTransitions.wildCardTransitions) {
  165. int nextState = pair.Value;
  166. //An optimization: if my transition is empty, even copy self-loop
  167. if (isEmpty && (nextState == srcState)) nextState = destState;
  168. wildCardTransitions[pair.Key] = nextState;
  169. }
  170. }
  171. }
  172. }
  173. public class WildCard {
  174. public readonly static WildCard DefaultWildCard = new WildCard("##any","");
  175. XObjectsNamespaceList nsList;
  176. internal XObjectsNamespaceList NsList {
  177. get { return nsList; }
  178. }
  179. public WildCard(string namespaces, string targetNamespace) {
  180. if (targetNamespace == null) targetNamespace = "";
  181. if (namespaces == null) namespaces = "##any";
  182. this.nsList = new XObjectsNamespaceList(namespaces, targetNamespace);
  183. }
  184. public override bool Equals(object obj) {
  185. WildCard symbol = obj as WildCard;
  186. if (symbol != null) return symbol.NsList.Equals(this.NsList);
  187. return false;
  188. }
  189. public override int GetHashCode() {
  190. return NsList.GetHashCode();
  191. }
  192. internal bool Allows(XName symbol) {
  193. return this.NsList.Allows(symbol.Namespace.ToString());
  194. }
  195. public override string ToString() {
  196. return "<ANY> : " + this.NsList.ToString();
  197. }
  198. }
  199. public class Set<T> : ICollection<T> {
  200. // Only the keys matter; the type bool used for the value is arbitrary
  201. private Dictionary<T, bool> dictionary;
  202. public Set() {
  203. dictionary = new Dictionary<T, bool>();
  204. }
  205. public Set(T x) : this() {
  206. Add(x);
  207. }
  208. public Set(IEnumerable<T> collection) : this() {
  209. foreach (T x in collection)
  210. Add(x);
  211. }
  212. public Set(T[] array) : this() {
  213. foreach (T x in array) Add(x);
  214. }
  215. public bool Contains(T x) {
  216. return dictionary.ContainsKey(x);
  217. }
  218. public void Add(T x) {
  219. if (!Contains(x))
  220. dictionary.Add(x, false);
  221. }
  222. public bool Remove(T x) {
  223. return dictionary.Remove(x);
  224. }
  225. public void Clear() {
  226. dictionary.Clear();
  227. }
  228. public bool IsReadOnly {
  229. get { return false; }
  230. }
  231. public IEnumerator<T> GetEnumerator() {
  232. return dictionary.Keys.GetEnumerator();
  233. }
  234. IEnumerator IEnumerable.GetEnumerator() {
  235. return GetEnumerator();
  236. }
  237. public int Count {
  238. get { return dictionary.Count; }
  239. }
  240. public void CopyTo(T[] arr, int i) {
  241. dictionary.Keys.CopyTo(arr, i);
  242. }
  243. public override String ToString() {
  244. StringBuilder str = new StringBuilder();
  245. str.Append("{ ");
  246. bool first = true;
  247. foreach (T x in this)
  248. {
  249. if (!first)
  250. str.Append(", ");
  251. str.Append(x);
  252. first = false;
  253. }
  254. str.Append(" }");
  255. return str.ToString();
  256. }
  257. }
  258. }