Beispiel #1
0
            void Init(PhysicalQuantity physicalQuantity, string name, MeasureUnit convRefUnit = null)
            {
                PhysicalQuantity = physicalQuantity;

                if (AllMeasureUnits
                    .Where(r => r.PhysicalQuantity.id == physicalQuantity.id)
                    .Any(r => r.Name == name))
                {
                    throw new Exception($"A registered measure unit [{name}] already exists for the physical quantity [{physicalQuantity.Name}]");
                }

                if (physicalQuantity.MUConversionType == MeasureUnitConversionTypeEnum.Linear &&
                    convRefUnit == null && physicalQuantity.LinearConversionRefMU != null)
                {
                    throw new Exception(
                              $"A reference measure unit [{physicalQuantity.LinearConversionRefMU}] already exists for the physical quantity [{physicalQuantity.Name}]" +
                              $"Need to specify a valid existing convRefUnit with related convRefFactor to specify measure unit scale factor");
                }

                if (global_static_id_counter.ContainsKey(physicalQuantity.id))
                {
                    id = ++global_static_id_counter[physicalQuantity.id];
                }
                else
                {
                    global_static_id_counter.Add(physicalQuantity.id, id = 0);
                }

                Name             = name;
                PhysicalQuantity = PhysicalQuantity;
            }
Beispiel #2
0
        public static PhysicalQuantity ByName(string pq_name)
        {
            PhysicalQuantity pq = null;

            dict_pq.TryGetValue(pq_name, out pq);

            return(pq);
        }
Beispiel #3
0
            public MeasureUnit(PhysicalQuantity physicalQuantity, string name, Func <MeasureUnit, MeasureUnit, double, double> convRefFunctor)
            {
                Init(physicalQuantity, name);

                physicalQuantity.RegisterMeasureUnit(this, convRefFunctor);
            }
Beispiel #4
0
            public MeasureUnit(PhysicalQuantity physicalQuantity, string name, MeasureUnit convRefUnit = null, double convRefFactor = 0)
            {
                Init(physicalQuantity, name, convRefUnit);

                physicalQuantity.RegisterMeasureUnit(this, convRefUnit, convRefFactor);
            }
Beispiel #5
0
        public static Measure TryParse(string text, PhysicalQuantity pq = null, CultureInfo culture = null)
        {
            if (culture == null)
            {
                culture = CultureInfo.InvariantCulture;
            }

            if (pq == null)
            {
                var pqstart = text.LastIndexOf('[') + 1;
                if (pqstart != 0)
                {
                    var pqname = text.Substring(pqstart, text.Length - pqstart - 1);
                    pq = PQCollection.PhysicalQuantities.First(w => w.Name == pqname);

                    text = text.Substring(0, pqstart - 1);
                }
            }

            if (pq != null && pq.Equals(PQCollection.Adimensional))
            {
                double n;
                if (double.TryParse(text, NumberStyles.Number | NumberStyles.AllowExponent, culture, out n))
                {
                    var res   = new Measure(n, MUCollection.Adimensional.adim);
                    var regex = new Regex("([0-9.]*)([eE])(.*)");
                    var q     = regex.Match(text);
                    if (q.Success)
                    {
                        res.ExpPref = int.Parse(q.Groups[3].Value);
                    }

                    return(res);
                }
            }
            else
            {
                var s = text.Trim();

                MeasureUnit mu = null;

                if (pq == null)
                {
                    var all_mus = MUCollection.MeasureUnits;

                    foreach (var _mu in all_mus.OrderByDescending(w => w.Name.Length))
                    {
                        if (s.EndsWith(_mu.ToString()))
                        {
                            mu = _mu;
                            break;
                        }
                    }

                    if (mu == null)
                    {
                        return(null);
                    }

                    // ambiguity between different pq with same mu name
                    if (all_mus.Count(r => r.ToString() == mu.ToString()) != 1)
                    {
                        return(null);
                    }
                }
                else
                {
                    foreach (var _mu in pq.MeasureUnits.OrderByDescending(w => w.Name.Length))
                    {
                        if (s.EndsWith(_mu.ToString()))
                        {
                            mu = _mu;
                            break;
                        }
                    }
                }

                if (mu == null)
                {
                    return(null);
                }

                s = s.StripEnd(mu.ToString());

                double n;
                if (double.TryParse(s, NumberStyles.Number | NumberStyles.AllowExponent, culture, out n))
                {
                    var res = new Measure(n, mu);

                    var regex = new Regex("([0-9.]*)([eE])(.*)");
                    var q     = regex.Match(s);
                    if (q.Success)
                    {
                        res.ExpPref = int.Parse(q.Groups[3].Value);
                    }

                    return(res);
                }
            }

            return(null);
        }