Example #1
0
        private bool TryParseText(string valueText, out double numericValue)
        {
            PhysicalValue physicalValueResult;

            if (DisplayUnit.HasValue)
            {
                if (!valueText.Contains(DisplayUnit.Value.ToString()))
                {
                    valueText = valueText + " " + DisplayUnit.Value;
                }
                PhysicalValue.TryParse(valueText, DisplayUnit.Value, LastPrefix, out physicalValueResult);

                if (!double.IsNaN(physicalValueResult.Value))
                {
                    numericValue = physicalValueResult.ConvertTo(Unit).Value;
                }
                else
                {
                    numericValue = Value;
                }
            }
            else
            {
                if (!PhysicalValue.TryParse(valueText, Unit, LastPrefix, out physicalValueResult))
                {
                    physicalValueResult.Value = Double.NaN;
                }

                numericValue = double.IsNaN(physicalValueResult.Value) ? Value : physicalValueResult.Value;
            }

            return(!double.IsNaN(physicalValueResult.Value));
        }
 public MyViewModel()
 {
     _target         = new PhysicalValue(5, 10);
     TargetValidator = new PhysicalValueValidator(_target);
     // update validation Logic...
     TargetValidator.SetMaxValue(6000);
 }
Example #3
0
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Unit   unit;
            double doubleValue;

            if (value is PhysicalValue)
            {
                PhysicalValue physicalValue = (PhysicalValue)value;
                unit        = physicalValue.Unit;
                doubleValue = physicalValue.Value;
            }
            else if (value is Quantity)
            {
                Quantity quantity = (Quantity)value;
                unit        = quantity.Unit;
                doubleValue = quantity.Value;
            }
            else if (value is double)
            {
                unit        = DefaultUnit;
                doubleValue = (double)value;
            }
            else
            {
                return(null);
            }

            return(PhysicalValue.Format(doubleValue, unit, Format, MinimumPrefix, MaximumPrefix, DefaultPrefix, NumberOfDigits, Precision, MaximumMagnitude));
        }
Example #4
0
        public override PhysicalValue <HumidityUnit> LowValue()
        {
            var minHumidity = _humidityStorage.GetMinValueAsync(DateTime.UtcNow).Result;
            var humidity    = new PhysicalValue <HumidityUnit>(minHumidity.Value, HumidityUnit.Percent);

            return(humidity);
        }
        public override PhysicalValue <SpeedUnit> LowValue()
        {
            var minWind = _windStorage.GetMinValueAsync(DateTime.UtcNow).Result;
            var speed   = new PhysicalValue <SpeedUnit>(minWind.Speed, SpeedUnit.MetersPerSecond);

            return(speed);
        }
        public override PhysicalValue <PressureUnit> LowValue()
        {
            var minPressure = _pressureStorage.GetMinValueAsync(DateTime.UtcNow).Result;
            var pressure    = new PhysicalValue <PressureUnit>(minPressure.Value, PressureUnit.Hectopascal);

            return(pressure);
        }
Example #7
0
        public void EmitSelectionCriteria()
        {
            var n1 = new PhysicalValue()
            {
                Number = 51
            };
            var n2 = new PhysicalValue()
            {
                Number = 52
            };
            var sc = new SelectionCriteria()
            {
                BinaryRelation = ">",
                FirstArgument  = n1,
                SecondArgument = n2
            };
            var ms = new StringWriter();

            sc.Emit(ms);
            var text = ms.ToString().Split('\r').Select(l => l.Trim()).ToArray();

            Assert.AreEqual("atlas:number0 rdf:type qudt:QuantityValue ;", text[0]);
            Assert.AreEqual("qudt:numericValue \"51\"^^xsd:decimal .", text[1]);
            Assert.AreEqual("", text[2]);
            Assert.AreEqual("atlas:number1 rdf:type qudt:QuantityValue ;", text[3]);
            Assert.AreEqual("qudt:numericValue \"52\"^^xsd:decimal .", text[4]);
            Assert.AreEqual("", text[5]);

            Assert.AreEqual("atlas:selectionCriteria2 rdf:type dfs:SelectionCriteria ;", text[6]);
            Assert.AreEqual("dfs:usesBinaryRelation dfs:greaterThan ;", text[7]);
            Assert.AreEqual("dfs:hasFirstArgument atlas:number0 ;", text[8]);
            Assert.AreEqual("dfs:hasSecondArgument atlas:number1 .", text[9]);
        }
Example #8
0
        public override PhysicalValue <TemperatureUnit> LowValue()
        {
            var minTemperature = _temperatureStorage.GetMinValueAsync(DateTime.UtcNow).Result;
            var temperature    = new PhysicalValue <TemperatureUnit>(minTemperature.Value, TemperatureUnit.Kelvin);

            return(temperature);
        }
        public void TestUnitFromString()
        {
            var length1 = new PhysicalValue<Length>(1, Length.FromString("cm"));
            var length2 = new PhysicalValue<Length>(1, "cm");

            Assert.AreEqual(1d.cm(), length1);
            Assert.AreEqual(length1, length2);
        }
Example #10
0
        /// <summary>
        ///     Selects the numeric part of the text.
        /// </summary>
        public void SelectNumericText()
        {
            var numeric         = PhysicalValue.ExtractNumericPart(Text);
            var selectionStart  = Text.IndexOf(numeric, StringComparison.Ordinal);
            var selectionLength = numeric.Length;

            Select(selectionStart, selectionLength);
        }
        public void Test()
        {
            var length = new PhysicalValue<Length>(100, Length.Meter);

            var length2 = 100d.m();

            Assert.AreEqual(length, length2);
        }
Example #12
0
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            PhysicalValue result;

            if (PhysicalValue.TryParse(value.ToString(), DefaultUnit, out result))
            {
                return(result.Value);
            }
            else
            {
                return(double.NaN);
            }
        }
Example #13
0
        public void EmitPhysicalNumberUnit()
        {
            var pn = new PhysicalValue()
            {
                Number = 55.0, Unit = "GeV"
            };
            var ms = new StringWriter();

            pn.Emit(ms);
            var text = ms.ToString().Trim();

            Assert.AreEqual("atlas:number0 rdf:type qudt:QuantityValue ;\r\n  qudt:numericValue \"55\"^^xsd:decimal ;\r\n  qudt:unit unit:GigaElectronVolt .", text);
        }
Example #14
0
        public PhysicalValue <TemperatureUnit> CurrentTemperature()
        {
            double tempMin = 5;
            double tempMax = 15;

            double sensorMin = LowValue().Value;
            double sensorMax = HighValue().Value;
            double curVal    = CurrentValue();

            var result  = (curVal - sensorMin) * (tempMax - tempMin) / (sensorMax - sensorMin) + tempMin;
            var result2 = (result - tempMin) * (sensorMax - sensorMin) / (tempMax - tempMin) + sensorMin;

            var temperature = new PhysicalValue <TemperatureUnit>(_temperatureStorage.GetLastValueAsync().Result.Value, TemperatureUnit.Kelvin);

            return(temperature);
        }
Example #15
0
        /// <summary>
        /// Some units are not valid, but can easily be made valid. So we will make this work
        /// correctly.
        /// </summary>
        /// <param name="pv"></param>
        /// <returns></returns>
        /// <remarks>
        /// TODO: It could be that some fo these hould be added to the "unit" database to make them more easy to deal
        /// with when humans look at them.
        /// </remarks>
        private static PhysicalValue SanitizeUnits(PhysicalValue pv)
        {
            if (pv.Unit == null)
            {
                return(pv);
            }

            var unit = pv.Unit.ToLower();

            if (unit == "ns")
            {
                return(new PhysicalValue()
                {
                    Unit = "s", Number = pv.Number * 1.0E-9
                });
            }
            return(pv);
        }
Example #16
0
        /// <summary>
        /// Dump out a name
        /// </summary>
        /// <param name="pv"></param>
        /// <param name="wr"></param>
        /// <returns></returns>
        public static string Emit(this PhysicalValue pv, TextWriter wr)
        {
            pv = SanitizeUnits(pv);

            var name = MakeName("number");

            wr.WriteLine($"{OWLNamespace}:{name} rdf:type qudt:QuantityValue ;");
            wr.Write($"  qudt:numericValue \"{pv.Number}\"^^xsd:decimal ");
            if (pv.Unit == null)
            {
                wr.WriteLine(".");
            }
            else
            {
                wr.WriteLine(";");
                wr.WriteLine($"  qudt:unit {QUDTUnit(pv.Unit)} .");
            }
            return(name);
        }
Example #17
0
        /// <summary>
        ///     Formats the value with the format options of the control.
        /// </summary>
        /// <param name="value">The value.</param>
        protected internal string FormatValue(double value)
        {
            var displayedUnit = Unit;
            var precision     = Precision;
            var maxMagnitude  = MaximumMagnitude;

            if (DisplayUnit.HasValue && DisplayUnit.Value != Unit)
            {
                value         = Conversions.Convert(value, Unit, DisplayUnit.Value);
                precision     = Conversions.Convert(Precision, Unit, DisplayUnit.Value);
                maxMagnitude  = Conversions.Convert(MaximumMagnitude, Unit, DisplayUnit.Value);
                displayedUnit = DisplayUnit.Value;
            }
            var digits = NumberOfFractionDigits;

            if (PhysicalValueFormat == PhysicalValueFormat.SIUsingSignificantDigits)
            {
                digits = NumberOfSignificantDigits;
            }

            return(PhysicalValue.Format(value, displayedUnit, PhysicalValueFormat, MinimumSIPrefix, MaximumSIPrefix, ParsePrefix, digits, precision, maxMagnitude));
        }
Example #18
0
        private ProgrammingChallenge2.Codecs.Matthid.Protobuf.StupidPhysicalValue EncodePhysical(PhysicalValue p)
        {
            var pressureProto = new ProgrammingChallenge2.Codecs.Matthid.Protobuf.StupidPhysicalValue();

            pressureProto.Unit  = p.Unit;
            pressureProto.Value = p.Value;
            return(pressureProto);
        }
 /// <summary>
 /// A physical value has nothing.
 /// </summary>
 /// <param name="physicalValue"></param>
 /// <returns></returns>
 private IEnumerable <FinalStateObject> ExtractFSOReferences(PhysicalValue physicalValue)
 {
     return(Enumerable.Empty <FinalStateObject>());
 }
Example #20
0
        public void EmitANDOR()
        {
            var n1 = new PhysicalValue()
            {
                Number = 51
            };
            var n2 = new PhysicalValue()
            {
                Number = 52
            };
            var sc1 = new SelectionCriteria()
            {
                BinaryRelation = ">",
                FirstArgument  = n1,
                SecondArgument = n2
            };

            var n3 = new PhysicalValue()
            {
                Number = 53
            };
            var n4 = new PhysicalValue()
            {
                Number = 54
            };
            var sc2 = new SelectionCriteria()
            {
                BinaryRelation = "<",
                FirstArgument  = n3,
                SecondArgument = n4
            };

            var andor = new ANDOR()
            {
                AOType = ANDORType.kAnd
            };

            andor.Arguments.Add(sc1);
            andor.Arguments.Add(sc2);

            var ms = new StringWriter();

            andor.Emit(ms);
            var text = ms.ToString().Split('\r').Select(l => l.Trim()).ToArray();

            Assert.AreEqual("atlas:number0 rdf:type qudt:QuantityValue ;", text[0]);
            Assert.AreEqual("qudt:numericValue \"51\"^^xsd:decimal .", text[1]);
            Assert.AreEqual("", text[2]);
            Assert.AreEqual("atlas:number1 rdf:type qudt:QuantityValue ;", text[3]);
            Assert.AreEqual("qudt:numericValue \"52\"^^xsd:decimal .", text[4]);
            Assert.AreEqual("", text[5]);

            Assert.AreEqual("atlas:selectionCriteria2 rdf:type dfs:SelectionCriteria ;", text[6]);
            Assert.AreEqual("dfs:usesBinaryRelation dfs:greaterThan ;", text[7]);
            Assert.AreEqual("dfs:hasFirstArgument atlas:number0 ;", text[8]);
            Assert.AreEqual("dfs:hasSecondArgument atlas:number1 .", text[9]);
            Assert.AreEqual("", text[10]);

            Assert.AreEqual("atlas:number3 rdf:type qudt:QuantityValue ;", text[11]);
            Assert.AreEqual("qudt:numericValue \"53\"^^xsd:decimal .", text[12]);
            Assert.AreEqual("", text[13]);
            Assert.AreEqual("atlas:number4 rdf:type qudt:QuantityValue ;", text[14]);
            Assert.AreEqual("qudt:numericValue \"54\"^^xsd:decimal .", text[15]);
            Assert.AreEqual("", text[16]);

            Assert.AreEqual("atlas:selectionCriteria5 rdf:type dfs:SelectionCriteria ;", text[17]);
            Assert.AreEqual("dfs:usesBinaryRelation dfs:lessThan ;", text[18]);
            Assert.AreEqual("dfs:hasFirstArgument atlas:number3 ;", text[19]);
            Assert.AreEqual("dfs:hasSecondArgument atlas:number4 .", text[20]);
            Assert.AreEqual("", text[21]);

            Assert.AreEqual("atlas:andor6 rdf:type dfs:And ;", text[22]);
            Assert.AreEqual("dfs:hasOperand atlas:selectionCriteria2 , atlas:selectionCriteria5 .", text[23]);
        }
        public PhysicalValue <SpeedUnit> CurrentSpeed()
        {
            var speed = new PhysicalValue <SpeedUnit>(_windStorage.GetLastValueAsync().Result.Speed, SpeedUnit.MetersPerSecond);

            return(speed);
        }
        public PhysicalValue <PressureUnit> CurrentPressure()
        {
            var pressure = new PhysicalValue <PressureUnit>(_pressureStorage.GetLastValueAsync().Result.Value, PressureUnit.Hectopascal);

            return(pressure);
        }
Example #23
0
        public PhysicalValue <HumidityUnit> CurrentHumidity()
        {
            var humidity = new PhysicalValue <HumidityUnit>(_humidityStorage.GetLastValueAsync().Result.Value, HumidityUnit.Percent);

            return(humidity);
        }
        public void TestConstruction()
        {
            var length = new PhysicalValue<Length>();

            Assert.AreEqual(length, 0d.m());
        }
 public PhysicalValueValidator(PhysicalValue pv)
 {
     _physicalValue = pv;
     _maxValue      = 5000;
 }