Ejemplo n.º 1
0
        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;
            }
        }