public void InputDataConversion() { IDictionary <string, object> config = new Dictionary <string, object>(2); config["param1"] = 10; config["param2"] = 1; IMeasurement srate = new Measurement(1000, "Hz"); IInputData data = new InputData(this.Data, srate, DateTimeOffset.Now, new PipelineNodeConfiguration(IOData.STREAM_CONFIGURATION_NAME, new Dictionary <string, object>())) .DataWithExternalDeviceConfiguration(devFake, new Dictionary <string, object>()); Converters.Clear(); ConvertProc fooConversion = (m) => new Measurement(m.QuantityInBaseUnit * 10, 1, "foo"); Converters.Register("V", "foo", fooConversion); IInputData expected = new InputData(data, data.Data.Select(m => new Measurement(m.QuantityInBaseUnit * 10, 1, "foo")).ToList()); Assert.NotNull(expected.NodeConfigurationWithName(IOData.STREAM_CONFIGURATION_NAME)); IInputData actual = data.DataWithUnits("foo"); Assert.AreEqual(expected.Data, actual.Data); Assert.That(actual.NodeConfigurationWithName(IOData.EXTERNAL_DEVICE_CONFIGURATION_NAME), Is.EqualTo(expected.NodeConfigurationWithName(IOData.EXTERNAL_DEVICE_CONFIGURATION_NAME))); Assert.That(actual.NodeConfigurationWithName(IOData.STREAM_CONFIGURATION_NAME), Is.EqualTo(expected.NodeConfigurationWithName(IOData.STREAM_CONFIGURATION_NAME))); Assert.AreEqual(expected.InputTime, actual.InputTime); }
public void RegisterConverters() { Converters.Clear(); Converters.Register("V", "V", // just an identity conversion for now, to pass Validate() (IMeasurement m) => m); }
public void TestSimpleConversion() { Converters.Register("ImpFeet", "inches", delegate(IMeasurement ft) { if (ft.BaseUnit == "ImpFeet") { return(new Measurement(ft.Quantity * 12, "inches")); } throw new Exception(String.Format("Illegal conversion: {0} to inches", ft.BaseUnit)); }); Converters.Register("ImpYards", "inches", delegate(IMeasurement yd) { if (yd.BaseUnit == "ImpYards") { return(new Measurement(yd.Quantity * 12 * 3, "inches")); } throw new Exception(String.Format("Illegal conversion: {0} to inches", yd.BaseUnit)); }); Measurement tedsHeight = new Measurement(6, "ImpFeet"); Assert.That(Converters.Convert(tedsHeight, "inches"), Is.EqualTo(new Measurement(72, "inches"))); tedsHeight = new Measurement(2, "ImpYards"); Assert.That(Converters.Convert(tedsHeight, "inches"), Is.EqualTo(new Measurement(72, "inches"))); }
public void MinimalRigValidates() { // How do Converters get registered? Converters.Clear(); Converters.Register("units", "units", // just an identity conversion for now, to pass Validate() (IMeasurement m) => m); Converters.Register("Hz", "Hz", // just an identity conversion for now, to pass Validate() (IMeasurement m) => m); Converters.Register("V", "units", // just an identity conversion for now, to pass Validate() (IMeasurement m) => m); ValidateObjectGraph(Resources.MinimalRigConfig); }
public void RunEpochShouldDiscardEpochWhenCancelEpochCalled() { Converters.Register("V", "V", // just an identity conversion for now, to pass Validate() (IMeasurement m) => m); var c = new Controller { DAQController = new SimpleDAQController2() }; c.DAQController.Clock = c.DAQController as IClock; var e = new Epoch(UNUSED_PROTOCOL); var dev1 = new UnitConvertingExternalDevice("dev1", "co", c, new Measurement(0, "V")) { MeasurementConversionTarget = "V", Clock = c.Clock }; var sampleRate = new Measurement(1, "Hz"); e.Stimuli[dev1] = new DelegatedStimulus("ID1", "units", new Dictionary <string, object>(), (parameters, duration) => new OutputData(new List <IMeasurement>(), sampleRate, false), objects => Option <TimeSpan> .None()); bool epochDiscarded = false; c.DiscardedEpoch += (sender, args) => { epochDiscarded = true; }; c.DAQController.ProcessIteration += (o, eventArgs) => { Console.WriteLine("Process iteration"); c.CancelEpoch(); }; c.RunEpoch(e, new FakeEpochPersistor()); Assert.True(epochDiscarded); }
public void ShouldConvertBackgoundUnits() { Converters.Register("xfromUnits", "toUnits", m => new Measurement(2 * m.Quantity, m.Exponent, m.BaseUnit)); var bg = new Measurement(1, "xfromUnits"); var e = new UnitConvertingExternalDevice(UNUSED_NAME, null, bg) { MeasurementConversionTarget = "toUnits" }; var stream = new DAQOutputStream(UNUSED_NAME); e.BindStream(stream); var expected = new Measurement(bg.Quantity * 2, bg.Exponent, bg.BaseUnit); Assert.That(e.OutputBackground, Is.EqualTo(expected)); }
public void InputDataConversionViaProc() { IDictionary <string, object> config = new Dictionary <string, object>(2); config["param1"] = 10; config["param2"] = 1; IMeasurement srate = new Measurement(1000, "Hz"); IOutputData outData = new OutputData(this.Data, srate, false) .DataWithExternalDeviceConfiguration(devFake, new Dictionary <string, object>()) .DataWithStreamConfiguration(streamFake, new Dictionary <string, object>()); outData.OutputTime = DateTimeOffset.Now; Converters.Clear(); ConvertProc fooConversion = (m) => new Measurement(m.QuantityInBaseUnit * 10, 1, "foo"); Converters.Register("V", "foo", fooConversion); IOutputData expected = new OutputData(outData, outData.Data.Select(m => new Measurement(m.QuantityInBaseUnit * 10, 1, "foo")).ToList()) { OutputTime = outData.OutputTime }; IOutputData actual = outData.DataWithConversion((m) => new Measurement(m.QuantityInBaseUnit * 10, 1, "foo")); Assert.AreEqual(expected.Data, actual.Data); Assert.AreEqual(expected.NodeConfigurationWithName(IOData.EXTERNAL_DEVICE_CONFIGURATION_NAME), actual.NodeConfigurationWithName(IOData.EXTERNAL_DEVICE_CONFIGURATION_NAME)); Assert.AreEqual(expected.NodeConfigurationWithName(IOData.STREAM_CONFIGURATION_NAME), actual.NodeConfigurationWithName(IOData.STREAM_CONFIGURATION_NAME)); Assert.AreEqual(expected.OutputTime, actual.OutputTime); }
public void CreatePipeline() { // Based on the "Minimal Rig.pdf" in the docs folder Converters.Clear(); // We need an IClock IClock clock = new FakeClock(); // We need a controller ... Controller con = new Controller(); Converters.Register("units", "units", // just an identity conversion for now, to pass Validate() (IMeasurement m) => m); Converters.Register("V", "units", // just an identity conversion for now, to pass Validate() (IMeasurement m) => m); con.Clock = clock; // Three ExternalDevices CoalescingDevice amp = new CoalescingDevice("Amp", UNUSED_DEVICE_MANUFACTURER, con, UNUSED_BACKGROUND) { MeasurementConversionTarget = "units" }; var LED = new UnitConvertingExternalDevice("LED", UNUSED_DEVICE_MANUFACTURER, UNUSED_BACKGROUND) { MeasurementConversionTarget = "units" }; var temp = new UnitConvertingExternalDevice("Temp", UNUSED_DEVICE_MANUFACTURER, UNUSED_BACKGROUND) { MeasurementConversionTarget = "units" }; amp.Clock = clock; LED.Clock = clock; temp.Clock = clock; con.AddDevice(LED).AddDevice(temp); // There should be no difference whether we use the // ExternalDevice constructor to wire up the Controller // to the ExternalDevice, or the explicit Add() call Assert.IsNotNull(amp.Controller); Assert.IsNotNull(LED.Controller); Assert.IsNotNull(temp.Controller); Assert.IsTrue(amp.Controller == con); Assert.IsTrue(LED.Controller == con); Assert.IsTrue(temp.Controller == con); // Five DAQStreams DAQInputStream in0 = new DAQInputStream("In-0"); in0.Clock = clock; DAQInputStream in1 = new DAQInputStream("In-1"); in1.Clock = clock; DAQInputStream in2 = new DAQInputStream("In-2"); in2.Clock = clock; DAQOutputStream out0 = new DAQOutputStream("Out-0"); out0.Clock = clock; DAQOutputStream out1 = new DAQOutputStream("Out-1"); out1.Clock = clock; in0.MeasurementConversionTarget = "units"; in1.MeasurementConversionTarget = "units"; in2.MeasurementConversionTarget = "units"; out0.MeasurementConversionTarget = "units"; out1.MeasurementConversionTarget = "units"; //amp.Coalesce = CoalescingDevice.OneItemCoalesce; amp.Configuration["CoalesceProc"] = "Symphony.Core.CoalescingDevice.OneItemCoalesce"; LED.BindStream(out0); amp.BindStream(out1).BindStream(in0).BindStream(in1); amp.Connect(in0, in1); temp.BindStream(in2); Assert.IsTrue(LED.Streams.Count == 1); Assert.IsTrue(amp.Streams.Count == 3); Assert.IsTrue(temp.Streams.Count == 1); Assert.IsTrue(in0.Devices.Contains(amp)); Assert.IsTrue(in1.Devices.Contains(amp)); Assert.IsTrue(in2.Devices.Contains(temp)); Assert.IsTrue(out0.Device == LED); Assert.IsTrue(out1.Device == amp); // One DAQController IDAQController dc = new SimpleDAQController(new IDAQStream[] { in0, in1, in2, out0, out1 }); con.DAQController = dc; // DAQController-to-streams Assert.IsTrue(dc.InputStreams.Contains(in0)); Assert.IsTrue(dc.InputStreams.Contains(in1)); Assert.IsTrue(dc.InputStreams.Contains(in2)); Assert.IsTrue(dc.OutputStreams.Contains(out0)); Assert.IsTrue(dc.OutputStreams.Contains(out0)); // Validate and report the validation results Maybe <string> conVal = con.Validate(); Assert.IsTrue(conVal, conVal.Item2); Assert.IsTrue(amp.Coalesce == CoalescingDevice.OneItemCoalesce); }
public void TestExponentialConversion() { Converters.Register("m", "cm", delegate(IMeasurement m) { if (m.BaseUnit == "m") { // m are 100 cm double q = (double)(m.Quantity * 100); // but only if the exponents match; if they don't, then we need to adjust int exp = m.Exponent; if (exp < 0) { while (exp < 0) { q = q / 10; exp++; } } else if (exp > 0) { while (exp > 0) { q = q * 10; exp--; } } return(new Measurement((decimal)q, "cm")); } throw new Exception(String.Format("Illegal conversion: {0} to cm", m.BaseUnit)); }); Measurement oneMeter = new Measurement(1, "m"); // no exponent (oneMeter.Exponent = 0) Assert.That(Converters.Convert(oneMeter, "cm"), Is.EqualTo(new Measurement(100, "cm"))); Measurement tenMeter = new Measurement(1, 1, "m"); // exponent (tenMeter.Exponent = 1) Assert.That(Converters.Convert(tenMeter, "cm"), Is.EqualTo(new Measurement(1000, "cm"))); // Now go the other way Converters.Register("cm", "m", delegate(IMeasurement m) { if (m.BaseUnit == "cm") { // m are 100 cm double q = (double)(m.Quantity / 100); // but only if the exponents match; if they don't, then we need to adjust int exp = m.Exponent; if (exp < 0) { while (exp < 0) { q = q / 10; exp++; } } else if (exp > 0) { while (exp > 0) { q = q * 10; exp--; } } return(new Measurement((decimal)q, "m")); } throw new Exception(String.Format("Illegal conversion: {0} cm to m", m.BaseUnit)); }); var oneCentimeter = new Measurement(1, "cm"); // no exponent Assert.That(Converters.Convert(oneCentimeter, "m"), Is.EqualTo(new Measurement(0.01m, "m"))); var tenCentimeter = new Measurement(1, 1, "cm"); // no exponent (tenCentimeter.Exponent = 1) Assert.That(Converters.Convert(tenCentimeter, "m"), Is.EqualTo(new Measurement(0.1m, "m"))); }