123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml;
- namespace AIPractice.Shared.Labels
- {
- public class RootLabel:LabelObject
- {
- private enum ReadingNode
- {
- /// <summary>
- /// 无意义节点
- /// </summary>
- None,
- /// <summary>
- /// 正在读取描述
- /// </summary>
- Descriptions,
- /// <summary>
- /// 正在读取结论
- /// </summary>
- Conclusions,
- /// <summary>
- /// 正在读取关联关系
- /// </summary>
- LabelAssociations
- }
- /// <summary>
- /// Gets all Descriptions belongs to this root label.
- /// </summary>
- public IReadOnlyList<LabelGroup> Descriptions { get; }
- /// <summary>
- /// Gets all conclusions belongs to this root label.
- /// </summary>
- public IReadOnlyList<LabelGroup> Conclusions { get; }
- /// <summary>
- /// Gets or label associations of this root label.
- /// </summary>
- public IReadOnlyList<LabelAssociation> LabelAssociations { get; }
- /// <summary>
- /// Gets or sets if this root label needs the parts conclusion.
- /// </summary>
- public bool NeedPartsConclusion { get; set; }
- public RootLabel(string id, string title, IEnumerable<LabelGroup> descriptions,
- IEnumerable<LabelGroup> conclusions, IEnumerable<LabelAssociation> labelAssociations) : base(id, title)
- {
- Descriptions = new List<LabelGroup>(descriptions);
- Conclusions = new List<LabelGroup>(conclusions);
- LabelAssociations = new List<LabelAssociation>(labelAssociations);
- foreach(var group in Descriptions)
- {
- group.Root = this;
- }
- foreach (var group in Conclusions)
- {
- group.Root = this;
- }
- }
- /// <summary>
- /// Add one group into the descriptions.
- /// </summary>
- /// <param name="group">The group to be added</param>
- public void AddDescriptionGroup(LabelGroup group)
- {
- var groupList = (List<LabelGroup>)Descriptions;
- if (!groupList.Contains(group))
- {
- groupList.Add(group);
- group.Root = this;
- }
- }
- /// <summary>
- /// Remove the group from the descriptions.
- /// </summary>
- /// <param name="group">The group to be removed.</param>
- public void RemoveDescriptionGroup(LabelGroup group)
- {
- var groupList = (List<LabelGroup>)Descriptions;
- if (groupList.Contains(group))
- {
- groupList.Remove(group);
- }
- }
- /// <summary>
- /// Insert one group into the descriptions on given index.
- /// </summary>
- /// <param name="index">The index to be insert.</param>
- /// <param name="group">The group to be added. and the root label will be assigned to the group.</param>
- public void InsertDescriptionGroup(int index, LabelGroup group)
- {
- var groupList = (List<LabelGroup>)Descriptions;
- if (!groupList.Contains(group))
- {
- groupList.Insert(index,group);
- group.Root = this;
- }
- }
- /// <summary>
- /// Add one group into the Conclusions.
- /// </summary>
- /// <param name="group">The group to be added</param>
- public void AddConclusionGroup(LabelGroup group)
- {
- var groupList = (List<LabelGroup>)Conclusions;
- if (!groupList.Contains(group))
- {
- groupList.Add(group);
- group.Root = this;
- }
- }
- /// <summary>
- /// Remove the group from the Conclusions.
- /// </summary>
- /// <param name="group">The group to be removed.</param>
- public void RemoveConclusionGroup(LabelGroup group)
- {
- var groupList = (List<LabelGroup>)Conclusions;
- if (groupList.Contains(group))
- {
- groupList.Remove(group);
- }
- }
- /// <summary>
- /// Insert one group into the Conclusions on given index.
- /// </summary>
- /// <param name="index">The index to be insert.</param>
- /// <param name="group">The group to be added. and the root label will be assigned to the group.</param>
- public void InsertConclusionGroup(int index, LabelGroup group)
- {
- var groupList = (List<LabelGroup>)Conclusions;
- if (!groupList.Contains(group))
- {
- groupList.Insert(index, group);
- group.Root = this;
- }
- }
- /// <summary>
- /// Add one association into the root.
- /// </summary>
- /// <param name="association">The association to be added</param>
- public void AddLabelAssociation(LabelAssociation association)
- {
- var associationList = (List<LabelAssociation>)LabelAssociations;
- var exists = associationList.FirstOrDefault(x => x.Left.Id == association.Left.Id && x.Right.Id == association.Right.Id);
- if (exists == null)
- {
- associationList.Add(association);
- }
- }
- /// <summary>
- /// Remove the association from the root.
- /// </summary>
- /// <param name="association">The association to be removed.</param>
- public void RemoveLabelAssociation(LabelAssociation association)
- {
- var associationList = (List<LabelAssociation>)LabelAssociations;
- var exists = associationList.FirstOrDefault(x => x.Left.Id == association.Left.Id && x.Right.Id == association.Right.Id);
- if (exists != null)
- {
- associationList.Remove(exists);
- }
- }
- /// <summary>
- /// Clear all associations.
- /// </summary>
- public void ClearLabelAssociations()
- {
- var associationList = (List<LabelAssociation>)LabelAssociations;
- associationList.Clear();
- }
- public static RootLabel FromXml(string xmlContent)
- {
- using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(xmlContent)))
- {
- using (XmlReader r = XmlReader.Create(ms))
- {
- r.Read();
- var readingNode = ReadingNode.None;
- var id = r.GetAttribute("id");
- var title = r.GetAttribute("title");
- var needPartsConclusionStr = r.GetAttribute("needPartsConclusion");
- bool.TryParse(needPartsConclusionStr, out bool needPartsConclusion);
- var descriptions = new List<LabelGroup>();
- var conclusions = new List<LabelGroup>();
- var labelAssociations = new List<LabelAssociation>();
- while (r.NodeType == XmlNodeType.Element &&
- (r.Name == nameof(SingleSelectionLabelGroup) ||
- r.Name == nameof(MultiSelectionLabelGroup) ||
- r.Name == nameof(LabelAssociation)) || r.Read())
- {
- if (r.NodeType == XmlNodeType.EndElement && r.Name == nameof(RootLabel))
- {
- break;
- }
- if (r.NodeType == XmlNodeType.Element && r.Name == nameof(Descriptions))
- {
- readingNode = ReadingNode.Descriptions;
- }
- if (r.NodeType == XmlNodeType.Element && r.Name == nameof(Conclusions))
- {
- readingNode = ReadingNode.Conclusions;
- }
- if (r.NodeType == XmlNodeType.Element && r.Name == nameof(LabelAssociations))
- {
- readingNode = ReadingNode.LabelAssociations;
- }
- if (r.NodeType == XmlNodeType.Element && r.Name == nameof(SingleSelectionLabelGroup))
- {
- if (readingNode == ReadingNode.Descriptions)
- {
- descriptions.Add(SingleSelectionLabelGroup.FromXml(r.ReadOuterXml()));
- }
- else if(readingNode == ReadingNode.Conclusions)
- {
- conclusions.Add(SingleSelectionLabelGroup.FromXml(r.ReadOuterXml()));
- }
- }
- if (r.NodeType == XmlNodeType.Element && r.Name == nameof(MultiSelectionLabelGroup))
- {
- if (readingNode == ReadingNode.Descriptions)
- {
- descriptions.Add(MultiSelectionLabelGroup.FromXml(r.ReadOuterXml()));
- }
- else if (readingNode == ReadingNode.Conclusions)
- {
- conclusions.Add(MultiSelectionLabelGroup.FromXml(r.ReadOuterXml()));
- }
- }
- if (r.NodeType == XmlNodeType.Element && r.Name == nameof(LabelAssociation))
- {
- if (readingNode == ReadingNode.LabelAssociations)
- {
- labelAssociations.Add(LabelAssociation.FromXml(r.ReadOuterXml()));
- }
- }
- }
- return new RootLabel(id, title, descriptions,conclusions, labelAssociations)
- {
- NeedPartsConclusion = needPartsConclusion,
- };
- }
- }
- }
- public override string ToXml()
- {
- var sb = new StringBuilder();
- sb.Append($"<{nameof(RootLabel)} id=\"{Id}\" title=\"{Title}\" needPartsConclusion=\"{NeedPartsConclusion}\">");
- sb.Append($"<{nameof(Descriptions)}>");
- if (Descriptions != null)
- {
- foreach (var description in Descriptions)
- {
- sb.Append(description.ToXml());
- }
- }
- sb.Append($"</{nameof(Descriptions)}>");
- sb.Append($"<{nameof(Conclusions)}>");
- if (Conclusions != null)
- {
- foreach (var conclusion in Conclusions)
- {
- sb.Append(conclusion.ToXml());
- }
- }
- sb.Append($"</{nameof(Conclusions)}>");
- sb.Append($"<{nameof(LabelAssociations)}>");
- if (LabelAssociations != null)
- {
- foreach (var labelAssociation in LabelAssociations)
- {
- sb.Append(labelAssociation.ToXml());
- }
- }
- sb.Append($"</{nameof(LabelAssociations)}>");
- sb.Append($"</{nameof(RootLabel)}>");
- return sb.ToString();
- }
- }
- }
|