public static IEnumerable <StructAtom <string> > LoadInputSeqFromFile() { List <StructAtom <string> > result = new List <StructAtom <string> >(); try { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "xml-файл (*.xml)|*.xml"; ofd.Multiselect = false; ofd.RestoreDirectory = true; if (ofd.ShowDialog() == DialogResult.OK) { var fi = new FileInfo(ofd.FileName); if (fi.Extension == ".xml") { XmlDocument doc = new XmlDocument(); doc.Load(fi.FullName); if (doc.ChildNodes.Count > 0) { var rootNode = doc.ChildNodes[0]; if (rootNode.Name.ToLower() == "inputsequence") { for (int i = 0; i < rootNode.ChildNodes.Count; ++i) { var childChildNode = rootNode.ChildNodes[i]; StructAtomStringXmlWorker w = new StructAtomStringXmlWorker(); w.ParseFromNode(childChildNode); if (w.value != null) { result.Add(w.value); } } } } else { MessageBox.Show("Файл пуст", "Загрузка входной последовательности", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Файл должен иметь расширение .xml", "Загрузка входной последовательности", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (Exception exc) { result.Clear(); } return(result); }
public void ParseFromNode(XmlNode node) { if (node == null) { throw new ArgumentNullException("node"); } value = new FiniteStateMachine <StructAtom <string>, StructAtom <string> >(); try { for (int i = 0; i < node.ChildNodes.Count; ++i) { var childNode = node.ChildNodes[i]; switch (childNode.Name.ToLower()) { case "stateset": { for (int j = 0; j < childNode.ChildNodes.Count; ++j) { var childChildNode = childNode.ChildNodes[j]; if (childChildNode.Name.ToLower() == "transition") { StateXmlWorker w = new StateXmlWorker(); w.FSM = value; w.ParseFromNode(childChildNode); var state = w.Value as FSMState <StructAtom <string>, StructAtom <string> >; if (state != null) { value.AddState(state); } } } } break; case "inputset": { for (int j = 0; j < childNode.ChildNodes.Count; ++j) { var childChildNode = childNode.ChildNodes[j]; if (childChildNode.Name.ToLower() == "input") { StructAtomStringXmlWorker w = new StructAtomStringXmlWorker(); w.ParseFromNode(childChildNode); var inp = w.Value as StructAtom <string>; if (inp != null) { value.AddInput(inp); } } } } break; case "outputset": { for (int j = 0; j < childNode.ChildNodes.Count; ++j) { var childChildNode = childNode.ChildNodes[j]; if (childChildNode.Name.ToLower() == "output") { StructAtomStringXmlWorker w = new StructAtomStringXmlWorker(); w.ParseFromNode(childChildNode); var inp = w.Value as StructAtom <string>; if (inp != null) { value.AddOutput(inp); } } } } break; case "keyname": value.KeyName = childNode.InnerText; break; case "initialstate": { StateXmlWorker w = new StateXmlWorker(); w.FSM = value; w.ParseFromNode(childNode); var state = w.Value as FSMState <StructAtom <string>, StructAtom <string> >; if (state != null) { value.InitialState = state; } } break; } } for (int i = 0; i < node.ChildNodes.Count; ++i) { var childNode = node.ChildNodes[i]; switch (childNode.Name.ToLower()) { case "transitions": { for (int j = 0; j < childNode.ChildNodes.Count; ++j) { var childChildNode = childNode.ChildNodes[j]; if (childChildNode.Name.ToLower() == "transition") { TransitionXmlWorker w = new TransitionXmlWorker(); w.FSM = value; w.ParseFromNode(childChildNode); var tr = w.Value as Transition <StructAtom <string>, StructAtom <string> >; if (tr != null) { foreach (var destinationState in tr.destinationStates) { value.AddOutgoing(tr.SourceState, tr.Input, destinationState.DestState, destinationState.Output, destinationState.Probability); } //value.Transitions.Add(tr.ToString(), tr); } } } } break; } } } catch (Exception exc) { value = null; throw exc; } }