public void SystemToXmlSerializeTest()
        {
            Project p = new Project();
            MathSystem s1 = p.CurrentSystem;

            // BUILD SYSTEM 1: sin(x^2)*2
            Signal x = Binder.CreateSignal(); x.Label = "x";
            Std.ConstrainAlwaysReal(x);
            Signal x2 = StdBuilder.Square(x); x2.Label = "x2";
            Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2";
            Signal sinx2t2 = sinx2 * IntegerValue.ConstantTwo;
            s1.AddSignalTree(sinx2t2, true, true);

            // EVALUATE SYSTEM 1 FOR x=1.5
            x.PostNewValue(new RealValue(1.5));
            p.SimulateInstant();
            Assert.AreEqual(0, s1.BusCount, "A0");
            Assert.AreEqual(5, s1.SignalCount, "A1");
            Assert.AreEqual(3, s1.PortCount, "A2");
            Assert.AreEqual("Std.Real(1.55614639377584)", sinx2t2.Value.ToString(), "A3");

            // SERIALIZE SYSTEM 1 TO XML
            /*
             * HINT: would be simpler to just call:
             * string s2xml = s1.WriteXml(false);
             */
            StringBuilder sb = new StringBuilder();
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.ConformanceLevel = ConformanceLevel.Document;
                settings.OmitXmlDeclaration = false;
                settings.Indent = true;
                settings.NewLineHandling = NewLineHandling.Entitize;
                settings.Encoding = Config.InternalEncoding;
                XmlWriter xwriter = XmlWriter.Create(sb, settings);
                xwriter.WriteStartElement("Systems");
                XmlSystemWriter writer = new XmlSystemWriter(xwriter);
                SystemReader reader = new SystemReader(writer);
                reader.ReadSystem(s1);
                xwriter.WriteEndElement();
                xwriter.Flush();
                xwriter.Close();
            }
            string s2xml = sb.ToString();
            Console.WriteLine(s2xml);

            // READER XML BACK TO SYSTEM 2
            /*
             * HINT: would be simpler to just call:
             * MathSystem s2 = MathSystem.ReadXml(s2xml, c);
             */
            IMathSystem s2;
            {
                StringReader sr = new StringReader(s2xml);
                XmlReader xreader = XmlReader.Create(sr);
                xreader.ReadToFollowing("Systems");
                xreader.Read();
                SystemWriter writer = new SystemWriter();
                XmlSystemReader reader = new XmlSystemReader(writer);
                reader.ReadSystems(xreader, false);
                xreader.ReadEndElement();
                s2 = writer.WrittenSystems.Dequeue();
            }

            Assert.AreEqual(0, s2.BusCount, "B0");
            Assert.AreEqual(5, s2.SignalCount, "B1");
            Assert.AreEqual(3, s2.PortCount, "B2");
            Assert.AreEqual("Std.Real(1.55614639377584)", s2.GetOutput(0).Value.ToString(), "B3");

            // EVALUATE SYSTEM 2 FOR x=2.5
            s2.GetInput(0).PostNewValue(new RealValue(2.5));
            p.SimulateInstant();
            Assert.AreEqual("Std.Real(-0.0663584330951136)", s2.GetOutput(0).Value.ToString(), "C0"); //-0.0331792165475568
        }
Пример #2
0
 public static IMathSystem ReadXml(XmlReader reader)
 {
     SystemWriter xwriter = new SystemWriter();
     XmlSystemReader xreader = new XmlSystemReader(xwriter);
     xreader.ReadSystems(reader, false);
     return xwriter.WrittenSystems.Dequeue();
 }