public void ShouldWriteEpochGroupMetadata()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            XmlWriter         sxw        = XmlWriter.Create(sb);
            EpochXMLPersistor exp        = new EpochXMLPersistor(sxw);

            string label  = "EpochGroup_label";
            string source = "source";

            string[] keywords = new string[] { "keyword1", "keyword2" };
            Dictionary <string, object> properties = new Dictionary <string, object> {
                { "key1", "value1" }, { "key2", 12 }
            };
            Guid guid = Guid.NewGuid();

            exp.BeginEpochGroup(label, source, keywords, properties, guid, DateTimeOffset.Now);
            exp.EndEpochGroup();
            exp.Close();

            string      xml = sb.ToString();
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            XmlElement egElement = (XmlElement)doc.DocumentElement.ChildNodes[0];

            Assert.That(egElement.Name, Is.EqualTo("epochGroup"));

            Assert.That(egElement.Attributes["source"].Value, Is.EqualTo(source));

            Assert.DoesNotThrow(() => DateTimeOffset.Parse(egElement.Attributes["startTime"].InnerText));

            var actualStart = DateTimeOffset.Parse(egElement.Attributes["startTime"].InnerText);

            Assert.That(actualStart.UtcDateTime.Ticks, Is.EqualTo(actualStart.Ticks));

            Assert.That(egElement.Attributes["timeZoneUTCOffsetHours"].InnerText, Is.EqualTo(
                            TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours.ToString()));
        }
        //[UseReporter(typeof(DiffReporter))]
        public void ShouldAllowNestedEpochGroupsInXML()
        {
            StringBuilder sb  = new System.Text.StringBuilder();
            var           sxw = XmlWriter.Create(sb);
            var           exp = new EpochXMLPersistor(sxw);

            const string label1     = "label1";
            const string label2     = "label2";
            var          g1ID       = new Guid("a5839fe9-90ef-4e39-bf26-8f75048306f3");
            var          g2ID       = new Guid("a5839fe9-90ef-4e39-bf26-8f75048306f4");
            var          startTime1 = new DateTimeOffset(2011, 8, 22, 11, 12, 0, 0, TimeSpan.FromHours(-6));
            var          startTime2 = new DateTimeOffset(2011, 8, 22, 11, 15, 0, 0, TimeSpan.FromHours(-6));

            exp.BeginEpochGroup(label1, "", new string[0], new Dictionary <string, object>(), g1ID, startTime1);

            exp.BeginEpochGroup(label2, "source", new string[0], new Dictionary <string, object>(), g2ID, startTime2);
            exp.EndEpochGroup();

            exp.Close();

            Approvals.VerifyXml(sb.ToString());
        }
示例#3
0
        /// <summary>
        /// Begin a new Epoch Group (i.e. a logical block of Epochs). As each Epoch Group is persisted
        /// to a separate data file, this method creates the appropriate output file and
        /// EpochPersistor instance.
        /// </summary>
        /// <param name="path">The name of the file into which to store the epoch; if the name
        ///   ends in ".xml", it will store the file using the EpochXMLPersistor, and if the name
        ///   ends in ".hdf5", it will store the file using the EpochHDF5Persistor. This file will
        ///   be overwritten if it already exists at this location.</param>
        /// <param name="epochGroupLabel">Label for the new Epoch Group</param>
        /// <param name="source">Identifier for EpochGroup's Source</param>
        /// <param name="keywords"></param>
        /// <param name="properties"></param>
        /// <returns>The EpochPersistor instance to be used for saving Epochs</returns>
        /// <see cref="RunEpoch"/>
        public EpochPersistor BeginEpochGroup(string path, string epochGroupLabel, string source, IEnumerable <string> keywords, IDictionary <string, object> properties)
        {
            EpochPersistor result = null;

            if (path.EndsWith(".xml"))
            {
                result = new EpochXMLPersistor(path);
            }
            else if (path.EndsWith(".hdf5"))
            {
                result = new EpochHDF5Persistor(path, null);
            }
            else
            {
                throw new ArgumentException(String.Format("{0} doesn't look like a legit Epoch filename", path));
            }

            var kws   = keywords == null ? new string[0] : keywords.ToArray();
            var props = properties ?? new Dictionary <string, object>();

            result.BeginEpochGroup(epochGroupLabel, source, kws, props, Guid.NewGuid(), DateTime.Now);

            return(result);
        }
        public void SingleEpochXMLPersistence()
        {
            var sb = new System.Text.StringBuilder();
            var sxw = XmlWriter.Create(sb);
            var gID = new Guid("a5839fe9-90ef-4e39-bf26-8f75048306a3");
            var exp = new EpochXMLPersistor(sxw, () => gID);

            var g1ID = new Guid("a5839fe9-90ef-4e39-bf26-8f75048306f3");
            var startTime1 = new DateTimeOffset(2011, 8, 22, 11, 12, 0, 0, TimeSpan.FromHours(-6));

            exp.BeginEpochGroup("label", "source", new string[0], new Dictionary<string, object>(), g1ID, startTime1);
            RunSingleEpoch(5, 2, exp);
            exp.EndEpochGroup();

            exp.Close();

            Approvals.VerifyXml(sb.ToString());
        }
        public void EpochXmlFixture()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            XmlWriter sxw = XmlWriter.Create(sb);
            EpochXMLPersistor exp = new EpochXMLPersistor(sxw);
            exp.BeginEpochGroup("", "", new string[0], new Dictionary<string, object>(), Guid.NewGuid(), DateTimeOffset.Now);
            exp.Serialize(testEpoch);
            exp.EndEpochGroup();
            exp.Close();

            string xml = sb.ToString();
            //Console.WriteLine("XML = {0}", xml);
            //Assert.AreEqual(xml, (Symphony.Core.Properties.Resources.EpochXML).Trim());
            // This is a bit of a fragile unit test--if the file's XML is pretty-printed, for example,
            // the CR/LF will be in the string coming from the file and cause this test to fail.
            // Comment out the test if that seems too fragile. (It does to me, but this also a good
            // way to red-flag unexpected XML changes.)

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlElement epochEl = doc["experiment"]["epochGroup"]["epoch"];
            Assert.AreEqual("epoch", epochEl.Name);
            Assert.AreEqual(testEpoch.ProtocolID, epochEl.GetAttribute("protocolID"));

            var kwsEl = (XmlElement)epochEl.GetElementsByTagName("keywords")[0];
            Assert.That(kwsEl.ChildNodes.Count == 2);

            var keywords = new HashSet<string>();
            for (int i = 0; i < kwsEl.ChildNodes.Count; i++)
            {
                var kwEl = kwsEl.ChildNodes[i];
                keywords.Add(kwEl.Attributes["tag"].Value);
            }

            Assert.That(keywords, Has.Member(kw1));
            Assert.That(keywords, Has.Member(kw2));

            Assert.AreEqual(1, epochEl.GetElementsByTagName("background").Count);
            XmlElement backgroundEl = (XmlElement)epochEl.GetElementsByTagName("background")[0];

            // There should be 2 devices in the Background
            Assert.AreEqual(2, backgroundEl.ChildNodes.Count);
            bool device1Found = false;
            bool device2Found = false;
            XmlNodeList backgroundList = backgroundEl.ChildNodes;
            for (int i = 0; i < backgroundList.Count; i++)
            {
                XmlElement backgroundChild = (XmlElement)backgroundList[i];
                if (backgroundChild.Name == dev1Name)
                {
                    device1Found = true;
                    bool bgFound = false;
                    bool srFound = false;
                    for (int j = 0; j < backgroundChild.ChildNodes.Count; j++)
                    {
                        XmlElement m = (XmlElement)backgroundChild.ChildNodes[j];
                        if (m.Name == "backgroundMeasurement")
                        {
                            XmlElement b = (XmlElement)m.ChildNodes[0];
                            Assert.That(b.GetAttribute("qty"), Is.EqualTo("0"));
                            Assert.That(b.GetAttribute("unit"), Is.EqualTo("V"));
                            bgFound = true;
                        }
                        else if (m.Name == "sampleRate")
                        {
                            XmlElement sr = (XmlElement)m.ChildNodes[0];
                            Assert.That(sr.GetAttribute("unit"), Is.EqualTo("Hz"));
                            srFound = true;
                        }
                        else
                        {
                            Assert.Fail("Unexpected background element name {0}", m.Name);
                        }

                    }

                    Assert.That(bgFound && srFound);

                }
                if (backgroundChild.Name == dev2Name)
                {
                    device2Found = true;
                    bool bgFound = false;
                    bool srFound = false;
                    for (int j = 0; j < backgroundChild.ChildNodes.Count; j++)
                    {
                        XmlElement m = (XmlElement)backgroundChild.ChildNodes[j];
                        XmlElement c = (XmlElement)m.ChildNodes[0];
                        if (m.Name == "backgroundMeasurement")
                        {
                            Assert.That(c.GetAttribute("qty"), Is.EqualTo("1"));
                            Assert.That(c.GetAttribute("unit"), Is.EqualTo("V"));
                            bgFound = true;
                        }
                        else if (m.Name == "sampleRate")
                        {
                            Assert.That(c.GetAttribute("unit") == "Hz");
                            srFound = true;
                        }
                        else
                        {
                            Assert.Fail("Unexpected background element name {0}", m.Name);
                        }

                    }
                    Assert.That(bgFound && srFound);
                }
            }
            Assert.IsTrue(device1Found);
            Assert.IsTrue(device2Found);

            Assert.AreEqual(1, epochEl.GetElementsByTagName("protocolParameters").Count);
            XmlElement protoParamsEl = (XmlElement)epochEl.GetElementsByTagName("protocolParameters")[0];

            // There should be 2 key/value pairs in protocol parameters
            Assert.AreEqual(2, protoParamsEl.ChildNodes.Count); // for key1/value1 and key2/value2
            for (int i = 0; i < protoParamsEl.ChildNodes.Count; i++)
            {
                XmlElement proParamChild = (XmlElement)protoParamsEl.ChildNodes[i];
                if (proParamChild.Name == param1)
                    Assert.AreEqual(value1.ToString(), proParamChild.InnerText);
                if (proParamChild.Name == param2)
                    Assert.AreEqual(value2, proParamChild.InnerText);
            }

            // still need to verify stimuli and responses; leave that as a TODO, in favor
            // of getting HDF5 work started in the meantime. --TKN
        }
        public void ShouldWriteEpochGroupMetadata()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            XmlWriter sxw = XmlWriter.Create(sb);
            EpochXMLPersistor exp = new EpochXMLPersistor(sxw);

            string label = "EpochGroup_label";
            string source = "source";
            string[] keywords = new string[] { "keyword1", "keyword2" };
            Dictionary<string, object> properties = new Dictionary<string, object> { { "key1", "value1" }, { "key2", 12 } };
            Guid guid = Guid.NewGuid();
            exp.BeginEpochGroup(label, source, keywords, properties, guid, DateTimeOffset.Now);
            exp.EndEpochGroup();
            exp.Close();

            string xml = sb.ToString();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlElement egElement = (XmlElement)doc.DocumentElement.ChildNodes[0];

            Assert.That(egElement.Name, Is.EqualTo("epochGroup"));

            Assert.That(egElement.Attributes["source"].Value, Is.EqualTo(source));

            Assert.DoesNotThrow(() => DateTimeOffset.Parse(egElement.Attributes["startTime"].InnerText));

            var actualStart = DateTimeOffset.Parse(egElement.Attributes["startTime"].InnerText);
            Assert.That(actualStart.UtcDateTime.Ticks, Is.EqualTo(actualStart.Ticks));

            Assert.That(egElement.Attributes["timeZoneUTCOffsetHours"].InnerText, Is.EqualTo(
                TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours.ToString()));
        }
        public void ShouldCloseEnclosingEpochGroups()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            XmlWriter sxw = XmlWriter.Create(sb);
            EpochXMLPersistor exp = new EpochXMLPersistor(sxw);

            const string label1 = "label1";
            const string label2 = "label2";
            Guid g1ID = new Guid("a5839fe9-90ef-4e39-bf26-8f75048306f3");
            Guid g2ID = new Guid("a5839fe9-90ef-4e39-bf26-8f75048306f4");
            var startTime1 = new DateTimeOffset(2011, 8, 22, 11, 12, 0, 0, TimeSpan.FromHours(-6));
            var startTime2 = new DateTimeOffset(2011, 8, 22, 11, 15, 0, 0, TimeSpan.FromHours(-6));

            exp.BeginEpochGroup(label1, "", new string[0], new Dictionary<string, object>(), g1ID, startTime1);

            exp.BeginEpochGroup(label2, "source", new string[0], new Dictionary<string, object>(), g2ID, startTime2);
            exp.EndEpochGroup();

            exp.EndEpochGroup();

            exp.Close();

            Approvals.VerifyXml(sb.ToString());
        }
        public void EpochXmlFixture()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            XmlWriter         sxw        = XmlWriter.Create(sb);
            EpochXMLPersistor exp        = new EpochXMLPersistor(sxw);

            exp.BeginEpochGroup("", "", new string[0], new Dictionary <string, object>(), Guid.NewGuid(), DateTimeOffset.Now);
            exp.Serialize(testEpoch);
            exp.EndEpochGroup();
            exp.Close();

            string xml = sb.ToString();
            //Console.WriteLine("XML = {0}", xml);
            //Assert.AreEqual(xml, (Symphony.Core.Properties.Resources.EpochXML).Trim());
            // This is a bit of a fragile unit test--if the file's XML is pretty-printed, for example,
            // the CR/LF will be in the string coming from the file and cause this test to fail.
            // Comment out the test if that seems too fragile. (It does to me, but this also a good
            // way to red-flag unexpected XML changes.)

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            XmlElement epochEl = doc["experiment"]["epochGroup"]["epoch"];

            Assert.AreEqual("epoch", epochEl.Name);
            Assert.AreEqual(testEpoch.ProtocolID, epochEl.GetAttribute("protocolID"));

            var kwsEl = (XmlElement)epochEl.GetElementsByTagName("keywords")[0];

            Assert.That(kwsEl.ChildNodes.Count == 2);

            var keywords = new HashSet <string>();

            for (int i = 0; i < kwsEl.ChildNodes.Count; i++)
            {
                var kwEl = kwsEl.ChildNodes[i];
                keywords.Add(kwEl.Attributes["tag"].Value);
            }

            Assert.That(keywords, Has.Member(kw1));
            Assert.That(keywords, Has.Member(kw2));


            Assert.AreEqual(1, epochEl.GetElementsByTagName("background").Count);
            XmlElement backgroundEl = (XmlElement)epochEl.GetElementsByTagName("background")[0];

            // There should be 2 devices in the Background
            Assert.AreEqual(2, backgroundEl.ChildNodes.Count);
            bool        device1Found   = false;
            bool        device2Found   = false;
            XmlNodeList backgroundList = backgroundEl.ChildNodes;

            for (int i = 0; i < backgroundList.Count; i++)
            {
                XmlElement backgroundChild = (XmlElement)backgroundList[i];
                if (backgroundChild.Name == dev1Name)
                {
                    device1Found = true;
                    bool bgFound = false;
                    bool srFound = false;
                    for (int j = 0; j < backgroundChild.ChildNodes.Count; j++)
                    {
                        XmlElement m = (XmlElement)backgroundChild.ChildNodes[j];
                        if (m.Name == "backgroundMeasurement")
                        {
                            XmlElement b = (XmlElement)m.ChildNodes[0];
                            Assert.That(b.GetAttribute("qty"), Is.EqualTo("0"));
                            Assert.That(b.GetAttribute("unit"), Is.EqualTo("V"));
                            bgFound = true;
                        }
                        else if (m.Name == "sampleRate")
                        {
                            XmlElement sr = (XmlElement)m.ChildNodes[0];
                            Assert.That(sr.GetAttribute("unit"), Is.EqualTo("Hz"));
                            srFound = true;
                        }
                        else
                        {
                            Assert.Fail("Unexpected background element name {0}", m.Name);
                        }
                    }

                    Assert.That(bgFound && srFound);
                }
                if (backgroundChild.Name == dev2Name)
                {
                    device2Found = true;
                    bool bgFound = false;
                    bool srFound = false;
                    for (int j = 0; j < backgroundChild.ChildNodes.Count; j++)
                    {
                        XmlElement m = (XmlElement)backgroundChild.ChildNodes[j];
                        XmlElement c = (XmlElement)m.ChildNodes[0];
                        if (m.Name == "backgroundMeasurement")
                        {
                            Assert.That(c.GetAttribute("qty"), Is.EqualTo("1"));
                            Assert.That(c.GetAttribute("unit"), Is.EqualTo("V"));
                            bgFound = true;
                        }
                        else if (m.Name == "sampleRate")
                        {
                            Assert.That(c.GetAttribute("unit") == "Hz");
                            srFound = true;
                        }
                        else
                        {
                            Assert.Fail("Unexpected background element name {0}", m.Name);
                        }
                    }
                    Assert.That(bgFound && srFound);
                }
            }
            Assert.IsTrue(device1Found);
            Assert.IsTrue(device2Found);

            Assert.AreEqual(1, epochEl.GetElementsByTagName("protocolParameters").Count);
            XmlElement protoParamsEl = (XmlElement)epochEl.GetElementsByTagName("protocolParameters")[0];

            // There should be 2 key/value pairs in protocol parameters
            Assert.AreEqual(2, protoParamsEl.ChildNodes.Count); // for key1/value1 and key2/value2
            for (int i = 0; i < protoParamsEl.ChildNodes.Count; i++)
            {
                XmlElement proParamChild = (XmlElement)protoParamsEl.ChildNodes[i];
                if (proParamChild.Name == param1)
                {
                    Assert.AreEqual(value1.ToString(), proParamChild.InnerText);
                }
                if (proParamChild.Name == param2)
                {
                    Assert.AreEqual(value2, proParamChild.InnerText);
                }
            }

            // still need to verify stimuli and responses; leave that as a TODO, in favor
            // of getting HDF5 work started in the meantime. --TKN
        }