1
0

RootLabel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. namespace AIPractice.Shared.Labels
  7. {
  8. public class RootLabel:LabelObject
  9. {
  10. private enum ReadingNode
  11. {
  12. /// <summary>
  13. /// 无意义节点
  14. /// </summary>
  15. None,
  16. /// <summary>
  17. /// 正在读取描述
  18. /// </summary>
  19. Descriptions,
  20. /// <summary>
  21. /// 正在读取结论
  22. /// </summary>
  23. Conclusions,
  24. /// <summary>
  25. /// 正在读取关联关系
  26. /// </summary>
  27. LabelAssociations
  28. }
  29. /// <summary>
  30. /// Gets all Descriptions belongs to this root label.
  31. /// </summary>
  32. public IReadOnlyList<LabelGroup> Descriptions { get; }
  33. /// <summary>
  34. /// Gets all conclusions belongs to this root label.
  35. /// </summary>
  36. public IReadOnlyList<LabelGroup> Conclusions { get; }
  37. /// <summary>
  38. /// Gets or label associations of this root label.
  39. /// </summary>
  40. public IReadOnlyList<LabelAssociation> LabelAssociations { get; }
  41. /// <summary>
  42. /// Gets or sets if this root label needs the parts conclusion.
  43. /// </summary>
  44. public bool NeedPartsConclusion { get; set; }
  45. public RootLabel(string id, string title, IEnumerable<LabelGroup> descriptions,
  46. IEnumerable<LabelGroup> conclusions, IEnumerable<LabelAssociation> labelAssociations) : base(id, title)
  47. {
  48. Descriptions = new List<LabelGroup>(descriptions);
  49. Conclusions = new List<LabelGroup>(conclusions);
  50. LabelAssociations = new List<LabelAssociation>(labelAssociations);
  51. foreach(var group in Descriptions)
  52. {
  53. group.Root = this;
  54. }
  55. foreach (var group in Conclusions)
  56. {
  57. group.Root = this;
  58. }
  59. }
  60. /// <summary>
  61. /// Add one group into the descriptions.
  62. /// </summary>
  63. /// <param name="group">The group to be added</param>
  64. public void AddDescriptionGroup(LabelGroup group)
  65. {
  66. var groupList = (List<LabelGroup>)Descriptions;
  67. if (!groupList.Contains(group))
  68. {
  69. groupList.Add(group);
  70. group.Root = this;
  71. }
  72. }
  73. /// <summary>
  74. /// Remove the group from the descriptions.
  75. /// </summary>
  76. /// <param name="group">The group to be removed.</param>
  77. public void RemoveDescriptionGroup(LabelGroup group)
  78. {
  79. var groupList = (List<LabelGroup>)Descriptions;
  80. if (groupList.Contains(group))
  81. {
  82. groupList.Remove(group);
  83. }
  84. }
  85. /// <summary>
  86. /// Insert one group into the descriptions on given index.
  87. /// </summary>
  88. /// <param name="index">The index to be insert.</param>
  89. /// <param name="group">The group to be added. and the root label will be assigned to the group.</param>
  90. public void InsertDescriptionGroup(int index, LabelGroup group)
  91. {
  92. var groupList = (List<LabelGroup>)Descriptions;
  93. if (!groupList.Contains(group))
  94. {
  95. groupList.Insert(index,group);
  96. group.Root = this;
  97. }
  98. }
  99. /// <summary>
  100. /// Add one group into the Conclusions.
  101. /// </summary>
  102. /// <param name="group">The group to be added</param>
  103. public void AddConclusionGroup(LabelGroup group)
  104. {
  105. var groupList = (List<LabelGroup>)Conclusions;
  106. if (!groupList.Contains(group))
  107. {
  108. groupList.Add(group);
  109. group.Root = this;
  110. }
  111. }
  112. /// <summary>
  113. /// Remove the group from the Conclusions.
  114. /// </summary>
  115. /// <param name="group">The group to be removed.</param>
  116. public void RemoveConclusionGroup(LabelGroup group)
  117. {
  118. var groupList = (List<LabelGroup>)Conclusions;
  119. if (groupList.Contains(group))
  120. {
  121. groupList.Remove(group);
  122. }
  123. }
  124. /// <summary>
  125. /// Insert one group into the Conclusions on given index.
  126. /// </summary>
  127. /// <param name="index">The index to be insert.</param>
  128. /// <param name="group">The group to be added. and the root label will be assigned to the group.</param>
  129. public void InsertConclusionGroup(int index, LabelGroup group)
  130. {
  131. var groupList = (List<LabelGroup>)Conclusions;
  132. if (!groupList.Contains(group))
  133. {
  134. groupList.Insert(index, group);
  135. group.Root = this;
  136. }
  137. }
  138. /// <summary>
  139. /// Add one association into the root.
  140. /// </summary>
  141. /// <param name="association">The association to be added</param>
  142. public void AddLabelAssociation(LabelAssociation association)
  143. {
  144. var associationList = (List<LabelAssociation>)LabelAssociations;
  145. var exists = associationList.FirstOrDefault(x => x.Left.Id == association.Left.Id && x.Right.Id == association.Right.Id);
  146. if (exists == null)
  147. {
  148. associationList.Add(association);
  149. }
  150. }
  151. /// <summary>
  152. /// Remove the association from the root.
  153. /// </summary>
  154. /// <param name="association">The association to be removed.</param>
  155. public void RemoveLabelAssociation(LabelAssociation association)
  156. {
  157. var associationList = (List<LabelAssociation>)LabelAssociations;
  158. var exists = associationList.FirstOrDefault(x => x.Left.Id == association.Left.Id && x.Right.Id == association.Right.Id);
  159. if (exists != null)
  160. {
  161. associationList.Remove(exists);
  162. }
  163. }
  164. /// <summary>
  165. /// Clear all associations.
  166. /// </summary>
  167. public void ClearLabelAssociations()
  168. {
  169. var associationList = (List<LabelAssociation>)LabelAssociations;
  170. associationList.Clear();
  171. }
  172. public static RootLabel FromXml(string xmlContent)
  173. {
  174. using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xmlContent)))
  175. {
  176. using (XmlReader r = XmlReader.Create(ms))
  177. {
  178. r.Read();
  179. var readingNode = ReadingNode.None;
  180. var id = r.GetAttribute("id");
  181. var title = r.GetAttribute("title");
  182. var needPartsConclusionStr = r.GetAttribute("needPartsConclusion");
  183. bool.TryParse(needPartsConclusionStr, out bool needPartsConclusion);
  184. var descriptions = new List<LabelGroup>();
  185. var conclusions = new List<LabelGroup>();
  186. var labelAssociations = new List<LabelAssociation>();
  187. while (r.NodeType == XmlNodeType.Element &&
  188. (r.Name == nameof(SingleSelectionLabelGroup) ||
  189. r.Name == nameof(MultiSelectionLabelGroup) ||
  190. r.Name == nameof(LabelAssociation)) || r.Read())
  191. {
  192. if (r.NodeType == XmlNodeType.EndElement && r.Name == nameof(RootLabel))
  193. {
  194. break;
  195. }
  196. if (r.NodeType == XmlNodeType.Element && r.Name == nameof(Descriptions))
  197. {
  198. readingNode = ReadingNode.Descriptions;
  199. }
  200. if (r.NodeType == XmlNodeType.Element && r.Name == nameof(Conclusions))
  201. {
  202. readingNode = ReadingNode.Conclusions;
  203. }
  204. if (r.NodeType == XmlNodeType.Element && r.Name == nameof(LabelAssociations))
  205. {
  206. readingNode = ReadingNode.LabelAssociations;
  207. }
  208. if (r.NodeType == XmlNodeType.Element && r.Name == nameof(SingleSelectionLabelGroup))
  209. {
  210. if (readingNode == ReadingNode.Descriptions)
  211. {
  212. descriptions.Add(SingleSelectionLabelGroup.FromXml(r.ReadOuterXml()));
  213. }
  214. else if(readingNode == ReadingNode.Conclusions)
  215. {
  216. conclusions.Add(SingleSelectionLabelGroup.FromXml(r.ReadOuterXml()));
  217. }
  218. }
  219. if (r.NodeType == XmlNodeType.Element && r.Name == nameof(MultiSelectionLabelGroup))
  220. {
  221. if (readingNode == ReadingNode.Descriptions)
  222. {
  223. descriptions.Add(MultiSelectionLabelGroup.FromXml(r.ReadOuterXml()));
  224. }
  225. else if (readingNode == ReadingNode.Conclusions)
  226. {
  227. conclusions.Add(MultiSelectionLabelGroup.FromXml(r.ReadOuterXml()));
  228. }
  229. }
  230. if (r.NodeType == XmlNodeType.Element && r.Name == nameof(LabelAssociation))
  231. {
  232. if (readingNode == ReadingNode.LabelAssociations)
  233. {
  234. labelAssociations.Add(LabelAssociation.FromXml(r.ReadOuterXml()));
  235. }
  236. }
  237. }
  238. return new RootLabel(id, title, descriptions,conclusions, labelAssociations)
  239. {
  240. NeedPartsConclusion = needPartsConclusion,
  241. };
  242. }
  243. }
  244. }
  245. public override string ToXml()
  246. {
  247. var sb = new StringBuilder();
  248. sb.Append($"<{nameof(RootLabel)} id=\"{Id}\" title=\"{Title}\" needPartsConclusion=\"{NeedPartsConclusion}\">");
  249. sb.Append($"<{nameof(Descriptions)}>");
  250. if (Descriptions != null)
  251. {
  252. foreach (var description in Descriptions)
  253. {
  254. sb.Append(description.ToXml());
  255. }
  256. }
  257. sb.Append($"</{nameof(Descriptions)}>");
  258. sb.Append($"<{nameof(Conclusions)}>");
  259. if (Conclusions != null)
  260. {
  261. foreach (var conclusion in Conclusions)
  262. {
  263. sb.Append(conclusion.ToXml());
  264. }
  265. }
  266. sb.Append($"</{nameof(Conclusions)}>");
  267. sb.Append($"<{nameof(LabelAssociations)}>");
  268. if (LabelAssociations != null)
  269. {
  270. foreach (var labelAssociation in LabelAssociations)
  271. {
  272. sb.Append(labelAssociation.ToXml());
  273. }
  274. }
  275. sb.Append($"</{nameof(LabelAssociations)}>");
  276. sb.Append($"</{nameof(RootLabel)}>");
  277. return sb.ToString();
  278. }
  279. }
  280. }