コード例 #1
0
        public KnownUOM FindFromDimensions(Dimensions dimensions, bool bAllowCreate, out bool wasCreated, string name = null)
        {
            if (bAllowCreate && !IsConsistent())
            {
                bAllowCreate = false;       // Only makes sense to create base units in a consistent set
            }

            wasCreated = false;

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

            foreach (var item in KnownUOMs)
            {
                if (item.Dimensions.Equals(dimensions))
                {
                    return(item);
                }
            }

            if (bAllowCreate && IsConsistent() && !Dimensions.IsDimenionless(dimensions))
            {
                wasCreated = true;

                string sName = name;

                if (string.IsNullOrEmpty(sName))
                {
                    string sTop    = "";
                    string sBottom = "";

                    AddUomStringPart(DimType.Mass, dimensions.Mass, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.Length, dimensions.Length, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.Time, dimensions.Time, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.ElectricCurrent, dimensions.ElectricCurrent, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.Temperature, dimensions.Temperature, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.QuantityOfSubstance, dimensions.QuantityOfSubstance, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.Luminosity, dimensions.Luminosity, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.PlaneAngle, dimensions.PlaneAngle, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.SolidAngle, dimensions.SolidAngle, ref sTop, ref sBottom);
                    AddUomStringPart(DimType.Currency, dimensions.Currency, ref sTop, ref sBottom);

                    sName = string.Format("{0}{1}{2}", (sTop.Length == 0 ? "1" : sTop), (sBottom.Length == 0 ? "" : "/"), (sBottom.Length == 0 ? "" : sBottom));
                }

                KnownUOM uom = new KnownUOM("", dimensions, Name: sName, Description: sName, Symbol: sName,
                                            IsBase: true, BaseUOM: null, AFactor: 0, BFactor: 1, CFactor: 1, DFactor: 0);

                KnownUOMs.Add(uom);
                return(uom);
            }
            else
            {
                return(ParentUomSet?.FindFromDimensions(dimensions, bAllowCreate, out wasCreated));
            }

            return(null);
        }
コード例 #2
0
        /// <summary>
        /// Create a new content item from the database row
        /// </summary>
        /// <param name="knownSoFar">Used to find the baseUOM, not to check for consistency (which is done outside this routine)</param>
        public static KnownUOM NewContentItem(string packageName, TblUOM_Row t1, Dictionary <string, KnownUOM> knownSoFar)
        {
            bool     isBase  = t1.IsBase;
            KnownUOM baseUOM = null;

            if (!isBase)
            {
                foreach (var item in knownSoFar)
                {
                    if (Dimensions.AreEqual(item.Value.Dimensions, t1.Dimensions))
                    {
                        baseUOM = item.Value;
                        break;
                    }
                }
                if (baseUOM == null)
                {
                    int iDbg = 0;
                    throw new UnspecifiedException();
                }
            }

            KnownUOM knownUOM = new KnownUOM(packageName, t1.Dimensions,
                                             t1.Name, t1.Description, t1.Symbol,
                                             isBase, baseUOM,
                                             t1.Factor_A, t1.Factor_B, t1.Factor_C, t1.Factor_D);

            return(knownUOM);
        }
コード例 #3
0
        /// Convert the value with the dimensions from this AnyUOMSet to tgtUOMSet
        public double Convert(double theValue, Dimensions dimensions, AnyUOMSet tgtUOMSet)
        {
            KnownUOM srcKownUOM = this.FindFromDimensions(dimensions);
            KnownUOM tgtKownUOM = tgtUOMSet.FindFromDimensions(dimensions);

            return(UOM.Convert(theValue, srcKownUOM, tgtKownUOM));
        }
コード例 #4
0
        public static double Convert(double theValue, AnonUOM anonFromUOM, AnonUOM anonToUOM)
        {
            KnownUOM from_UOM = anonFromUOM?.UOMSet?.FindFromDimensions(anonFromUOM.Dimensions);
            KnownUOM to_UOM   = anonToUOM?.UOMSet?.FindFromDimensions(anonToUOM.Dimensions);

            return(Convert(theValue, from_UOM, to_UOM));
        }
コード例 #5
0
        public KnownUOM(string packageName, Dimensions Dimensions,
                        string Name, string Description, string Symbol,
                        bool IsBase, KnownUOM BaseUOM,
                        double AFactor, double BFactor, double CFactor, double DFactor) :
            base(Dimensions)
        {
            PackageName = packageName;

            _Name        = Name;
            _Description = Description;
            _Symbol      = Symbol;
            _IsBase      = IsBase;

            _BaseUOM = BaseUOM;

            _AFactor = AFactor;
            _BFactor = BFactor;
            _CFactor = CFactor;
            _DFactor = DFactor;
        }
コード例 #6
0
        // ---------------------------
        public static bool EqualDimensions(KnownUOM uom1, KnownUOM uom2)
        {
            if ((uom1 == null) && (uom2 == null))
            {
                return(true);
            }
            if ((uom1 == null) || (uom2 == null))
            {
                return(false);
            }

            if ((uom1.Dimensions == null) && (uom2.Dimensions == null))
            {
                return(true);
            }
            if ((uom1.Dimensions == null) || (uom2.Dimensions == null))
            {
                return(false);
            }

            return(uom1.Dimensions.Equals(uom2.Dimensions));
        }
コード例 #7
0
        /// Convert value from the UOM of this class to those of toUOM.
        /// Return the converted value, or Double.NaN on failure.
        //public abstract double Convert(double value, UOM toUOM);

        // ---------------------------

        public static double Convert(double theValue, KnownUOM fromUOM, KnownUOM toUOM)
        {
            /*
             * Conversion To Base Unit: (A + BX) / (C + DX)
             *
             * To convert from unit1 (value x1) to unit2 (value x2):
             *
             * BaseVal: b = (A1 + B1.X1) / (C1 + D1.X1) = (A2 + B2.X2) / (C2 + D2.X2)
             *
             * So:
             *
             * b = (A1 + B1.X1) / (C1 + D1.X1)
             * x2 = (A2 - C2.b) / (D2.b - B2)
             *
             */

            if (fromUOM == null)
            {
                return(double.NaN);
            }
            if (toUOM == null)
            {
                return(double.NaN);
            }

            if (!fromUOM.Dimensions.Equals(toUOM.Dimensions))
            {
                return(double.NaN);
            }

            double x1 = theValue;
            double b  = (fromUOM.AFactor + (fromUOM.BFactor * x1)) / (fromUOM.CFactor + (fromUOM.DFactor * x1));
            double x2 = (toUOM.AFactor - (toUOM.CFactor * b)) / ((toUOM.DFactor * b) - toUOM.BFactor);

            return(x2);
        }
コード例 #8
0
 public DisplayQuantity(double value, CalcStatus calcStatus, string message, KnownUOM kownUOM) :
     base(value, calcStatus, message)
 {
     KownUOM = kownUOM;
 }
コード例 #9
0
        public static UOMConflictInfo TestConflict(string packageName, TblUOM_Row t1, KnownUOM t2)
        {
            if (!t1.Name.Equals(t2.Name))
            {
                return(null);
            }

            if (!t1.Dimensions.Equals(t2.Dimensions))
            {
                return(new UOMConflictInfo(packageName, t1, ConflictType.InConsistent, UOMConflictReason.Dimensions, "Dimensions", "Inconsistent dimensions"));
            }

            // TODO: Tolerance:
            if ((t1.Factor_A != t2.AFactor) || (t1.Factor_B != t2.BFactor) || (t1.Factor_C != t2.CFactor) || (t1.Factor_D != t2.DFactor))
            {
                return(new UOMConflictInfo(packageName, t1, ConflictType.InConsistent, UOMConflictReason.Factors, "Factors", "Inconsistent factors"));
            }

            if (!t1.Description.Equals(t2.Description))
            {
                return(new UOMConflictInfo(packageName, t1, ConflictType.Consistent, UOMConflictReason.Description, "Description", "Different description"));
            }

            return(new UOMConflictInfo(packageName, t1, ConflictType.Duplicate, UOMConflictReason.Duplicate, "Duplicate", "Duplicate"));
        }