コード例 #1
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));
        }
コード例 #2
0
        // -----------------------------------------
        public static CalcQuantity NewMathResult(double value, AnonUOM anonUOM)
        {
            CalcStatus calcStatus = CalcStatus.Good;
            string     message    = "";

            if (double.IsNaN(value) || double.IsInfinity(value))
            {
                calcStatus = CalcStatus.Bad;
                message    = "Math error";
            }

            CalcQuantity result = new CalcQuantity(
                value: value,
                calcStatus: calcStatus, message: message, anonUOM: anonUOM);

            return(result);
        }
コード例 #3
0
        public static bool EqualOrNull(AnonUOM an1, AnonUOM an2)
        {
            if ((an1 == null) && (an2 == null))
            {
                return(true);
            }

            if (an2 == null)
            {
                return(an1.Equals(an2));
            }
            if (an1 == null)
            {
                return(an2.Equals(an1));
            }

            return(an1.Equals(an2));
        }
コード例 #4
0
        public static bool HaveConsistentUomSets(AnonUOM an1, AnonUOM an2)
        {
            if ((an1 == null) && (an2 == null))
            {
                return(true);
            }

            if (IsDimenionless(an1?.Dimensions) || IsDimenionless(an2?.Dimensions))
            {
                return(true);
            }

            if ((an1 == null) || (an2 == null))
            {
                return(false);
            }

            return(an1.UOMSet == an2.UOMSet);
        }
コード例 #5
0
        public override bool Equals(Object obj)
        {
            AnonUOM anonUOM = obj as AnonUOM;

            if ((anonUOM == null) || (this.Dimensions == null) || (anonUOM.Dimensions == null))
            {
                return(false);                                                                                      // throw new InvalidOperationException("AnonUOM equality check involving null dimensions");
            }
            if (IsDimenionless(this.Dimensions) && IsDimenionless(anonUOM.Dimensions))
            {
                return(true);                                                                            // UOMSet doesn't matter
            }
            if (this.UOMSet != anonUOM.UOMSet)
            {
                return(false);
            }
            if (!this.Dimensions.Equals(anonUOM.Dimensions))
            {
                return(false);
            }

            return(true);
        }
コード例 #6
0
        /// <summary>
        /// Create a new content item from the database row
        /// </summary>
        public static Constant NewContentItem(string packageName, TblConstant_Row t1, IList <ParamType> paramTypes, IList <UOMSet> uomSets)
        {
            ParamType paramType = FindInList <ParamType>(paramTypes, (item) => item.Name.Equals(t1.ParamType));

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

            // -------------
            UOMSet uomSet = FindInList <UOMSet>(uomSets, (item) => item.Name.Equals(t1.UnitSet));

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

            AnonUOM anonUOM = new AnonUOM(paramType.Dimensions, uomSet);

            // -------------
            Constant constant = new Constant(packageName, t1.Name, t1.Description, paramType, t1.Value, anonUOM);

            return(constant);
        }
コード例 #7
0
 public CalcQuantity(double value, CalcStatus calcStatus, string message, AnonUOM anonUOM) :
     base(value, calcStatus, message)
 {
     AnonUOM = anonUOM;
 }
コード例 #8
0
        }                                           // Safe symbol to use in pure text scenarios

        // ---------------------------
        /// <summary>
        /// Calculate the function as much as possible.
        /// For 'NormalFunction' objects we expect that the result is calculated from the args, and we do not change the args.
        /// However, this is not enforced at this time.
        /// Special sub-types, such as 'FnEquals', behave differently.
        ///
        /// Args will be converted to a consistent UOMSet, unless they are all UOMless, before calling the actual function calc.
        /// If the function requires any other specific UOM it must do the conversion itself.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public CalcQuantity Calculate(IList <CalcQuantity> args)
        {
            CalcQuantity result = new CalcQuantity();

            try
            {
                // ------------------- Loop through all the args, checking and converting
                UOMSet uomSet = null;

                IList <CalcQuantity> convertedArgs = new List <CalcQuantity>();

                bool bStarted = false;
                for (int i = 0; i < args.Count; i++)
                {
                    CalcQuantity arg = args[i];

                    // ------------------- Check for a Bad or missing value
                    if (IsNormalFunction)       // Otherwise more flexibility is allowed so we can't demand that all args are known before calculation
                    {
                        switch (arg.CalcStatus)
                        {
                        case CalcStatus.Uknown:
                        case CalcStatus.Bad:
                            result.CalcStatus = arg.CalcStatus; result.Message = "Missing or bad arguments";
                            return(result);

                        default:
                            break;
                        }
                    }


                    // ------------------- Convert the arg to the same UOMSET
                    CalcQuantity convertedArg = new CalcQuantity(arg);
                    bool         bDimLess     = Dimensions.IsDimenionless(convertedArg?.AnonUOM?.Dimensions);

                    if (!bDimLess)
                    {
                        if ((uomSet == null) & (!bStarted || !IsNormalFunction))
                        {
                            bStarted = true;
                            uomSet   = convertedArg?.AnonUOM?.UOMSet;
                        }
                        else if (IsNormalFunction && (convertedArg?.AnonUOM?.UOMSet != uomSet))
                        {
                            if ((uomSet == null) || (convertedArg?.AnonUOM?.UOMSet == null))
                            {
                                result.CalcStatus = CalcStatus.Bad; result.Message = "Some arguments have units while others do not";       // They must all have a specified UOMSet, or none of them have
                                return(result);
                            }
                            else
                            {
                                // Convert to the uomSet used by other args
                                AnonUOM argUOM = convertedArg.AnonUOM;
                                convertedArg.Value   = argUOM.UOMSet.Convert(arg.Value, argUOM.Dimensions, uomSet);
                                convertedArg.AnonUOM = new AnonUOM(argUOM.Dimensions, uomSet);
                            }
                        }
                    }

                    convertedArgs.Add(convertedArg);
                }


                // ------------------- Call the concrete implementations to actually do the calcualtion
                result = Calculate_Here(convertedArgs, uomSet);

                if (!IsNormalFunction)      // Then some args might have been changed by the calculation (e.g. assignment in FnEquals)
                {
                    for (int i = 0; i < convertedArgs.Count; i++)
                    {
                        convertedArgs[i].CopyMe(args[i]);
                    }
                }
            }
            catch (Exception)
            {
                return(new CalcQuantity(0, CalcStatus.Bad, "Unexpected failure", null));
            }

            return(result);
        }
コード例 #9
0
 public Constant(string packageName, string name, string description, ParamType paramType, double value, AnonUOM anonUOM, bool enforceNameFormat = true) :
     base(new CalcQuantity(value, CalcStatus.Good, "", anonUOM), name, description, paramType)
 {
     if (enforceNameFormat && !name.Substring(0, 1).Equals(ConstantPrefix))
     {
         throw new UnspecifiedException($"Constant names must begin with '{ConstantPrefix}' but {name} does not");
     }
     PackageName = packageName;
 }