Пример #1
0
        public XmlNode CreateXmlNode(XmlDocument doc, string nodeName)
        {
            if (value == null)
            {
                throw new NullReferenceException();
            }

            XmlNode result = doc.CreateElement(nodeName);

            StateXmlWorker w = new StateXmlWorker(value.SourceState);

            result.AppendChild(w.CreateXmlNode(doc, "SourceState"));

            XmlElement element = doc.CreateElement("Input");

            element.InnerText = value.Input.Value;
            result.AppendChild(element);

            element = doc.CreateElement("TransitionResults");
            foreach (var destinationState in value.destinationStates)
            {
                TransitionResXmlWorker worker = new TransitionResXmlWorker(destinationState);
                element.AppendChild(worker.CreateXmlNode(doc));
            }
            result.AppendChild(element);

            return(result);
        }
        public void ParseFromNode(XmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (FSM == null)
            {
                throw new NullReferenceException("FSM");
            }

            value = new TransitionRes <StructAtom <string>, StructAtom <string> >();

            try
            {
                for (int i = 0; i < node.ChildNodes.Count; ++i)
                {
                    var childNode = node.ChildNodes[i];
                    switch (childNode.Name.ToLower())
                    {
                    case "deststate":
                    {
                        StateXmlWorker w = new StateXmlWorker();
                        w.FSM = this.FSM;
                        w.ParseFromNode(node.ChildNodes[i]);
                        var state = w.Value as FSMState <StructAtom <string>, StructAtom <string> >;
                        if (state != null)
                        {
                            var exState = FSM.StateSet.First(s => s.KeyName == state.KeyName);
                            if (exState != null)
                            {
                                state = exState;
                            }
                            value.DestState = state;
                        }
                    }
                    break;

                    case "output":
                    {
                        value.Output = new StructAtom <string>(childNode.InnerText);
                    }
                    break;

                    case "probability":
                    {
                        value.Probability = double.Parse(childNode.InnerText);
                    }
                    break;
                    }
                }
            }
            catch (Exception exc)
            {
                value = null;
                throw exc;
            }
        }
Пример #3
0
        public XmlNode CreateXmlNode(XmlDocument doc, string nodeName)
        {
            if (value == null)
            {
                throw new NullReferenceException();
            }

            XmlNode result = doc.CreateElement(nodeName);

            XmlElement element = doc.CreateElement("StateSet");

            foreach (var state in value.StateSet)
            {
                StateXmlWorker worker = new StateXmlWorker(state);
                element.AppendChild(worker.CreateXmlNode(doc));
            }
            result.AppendChild(element);

            element = doc.CreateElement("InputSet");
            foreach (var input in value.InputSet)
            {
                StructAtomStringXmlWorker worker = new StructAtomStringXmlWorker(input);
                element.AppendChild(worker.CreateXmlNode(doc, "Input"));
            }
            result.AppendChild(element);

            element = doc.CreateElement("OutputSet");
            foreach (var output in value.OutputSet)
            {
                StructAtomStringXmlWorker worker = new StructAtomStringXmlWorker(output);
                element.AppendChild(worker.CreateXmlNode(doc, "Output"));
            }
            result.AppendChild(element);

            element           = doc.CreateElement("KeyName");
            element.InnerText = value.KeyName;
            result.AppendChild(element);

            StateXmlWorker w = new StateXmlWorker(value.InitialState);

            result.AppendChild(w.CreateXmlNode(doc, "InitialState"));

            element = doc.CreateElement("Transitions");
            foreach (var transition in value.Transitions.Values)
            {
                TransitionXmlWorker worker = new TransitionXmlWorker(transition);
                element.AppendChild(worker.CreateXmlNode(doc));
            }
            result.AppendChild(element);

            return(result);
        }
        public void ParseFromNode(XmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (this.FSM == null)
            {
                throw new NullReferenceException("FSM");
            }

            value = new Partition <FSMState <StructAtom <string>, StructAtom <string> > >();

            try
            {
                for (int i = 0; i < node.ChildNodes.Count; ++i)
                {
                    var childNode = node.ChildNodes[i];
                    if (childNode.Name.ToLower() == "block")
                    {
                        HashSet <FSMState <StructAtom <string>, StructAtom <string> > > block = new HashSet <FSMState <StructAtom <string>, StructAtom <string> > >();
                        for (int j = 0; j < childNode.ChildNodes.Count; ++j)
                        {
                            var            childChildNode = childNode.ChildNodes[j];
                            StateXmlWorker w = new StateXmlWorker();
                            w.FSM = this.FSM;
                            if (childChildNode.Name == w.NodeName)
                            {
                                w.ParseFromNode(childChildNode);
                                var state = w.Value as FSMState <StructAtom <string>, StructAtom <string> >;
                                if (state != null)
                                {
                                    var exState = FSM.StateSet.First(s => s.KeyName == state.KeyName);
                                    if (exState == null)
                                    {
                                        state = exState;
                                    }
                                    block.Add(state);
                                }
                            }
                        }
                        value.Add(block);
                    }
                }
            }
            catch (Exception exc)
            {
                value = null;
                throw exc;
            }
        }
        public XmlNode CreateXmlNode(XmlDocument doc, string nodeName)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }

            XmlNode result = doc.CreateElement(nodeName);

            foreach (var block in value)
            {
                var blockNode = doc.CreateElement("Block");
                foreach (var state in block)
                {
                    StateXmlWorker w = new StateXmlWorker(state);
                    blockNode.AppendChild(w.CreateXmlNode(doc));
                }
                result.AppendChild(blockNode);
            }

            return(result);
        }
        public XmlNode CreateXmlNode(XmlDocument doc, string nodeName)
        {
            if (value == null)
            {
                throw new NullReferenceException();
            }

            XmlNode result = doc.CreateElement(nodeName);

            StateXmlWorker w = new StateXmlWorker(value.DestState);

            result.AppendChild(w.CreateXmlNode(doc, "DestState"));

            XmlElement element = doc.CreateElement("Output");

            element.InnerText = value.Output.Value;
            result.AppendChild(element);

            element           = doc.CreateElement("Probability");
            element.InnerText = value.Probability.ToString();
            result.AppendChild(element);

            return(result);
        }
Пример #7
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;
            }
        }
Пример #8
0
        public void ParseFromNode(XmlNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (FSM == null)
            {
                throw new NullReferenceException("FSM");
            }

            value = new Transition <StructAtom <string>, StructAtom <string> >(FSM);

            try
            {
                for (int i = 0; i < node.ChildNodes.Count; ++i)
                {
                    var childNode = node.ChildNodes[i];
                    switch (childNode.Name.ToLower())
                    {
                    case "sourcestate":
                    {
                        StateXmlWorker w = new StateXmlWorker();
                        w.FSM = this.FSM;
                        w.ParseFromNode(childNode);
                        var state = w.Value as FSMState <StructAtom <string>, StructAtom <string> >;
                        if (state != null)
                        {
                            var exState = this.FSM.StateSet.FirstOrDefault(s => s.KeyName == state.KeyName);
                            if (exState != null)
                            {
                                state = exState;
                            }
                            value.SourceState = state;
                        }
                    }
                    break;

                    case "input":
                    {
                        value.Input = new StructAtom <string>(childNode.InnerText);
                    }
                    break;

                    case "transitionresults":
                    {
                        for (int j = 0; j < childNode.ChildNodes.Count; ++j)
                        {
                            var childChildNodes = childNode.ChildNodes[j];
                            if (childChildNodes.Name.ToLower() == "transitionresult")
                            {
                                TransitionResXmlWorker w = new TransitionResXmlWorker();
                                w.FSM = this.FSM;
                                w.ParseFromNode(childChildNodes);
                                if (w.Value is TransitionRes <StructAtom <string>, StructAtom <string> > )
                                {
                                    value.destinationStates.Add(w.Value as TransitionRes <StructAtom <string>, StructAtom <string> >);
                                }
                            }
                        }
                    }
                    break;
                    }
                }
            }
            catch (Exception exc)
            {
                value = null;
                throw exc;
            }
        }