Пример #1
0
 public SingleValue(CalcQuantity quantity, string name, string description, ParamType paramType)
 {
     CalcQuantity = quantity;
     Name         = name;
     Description  = description;
     ParamType    = paramType;
 }
 public void SetCalcStatus(CalcStatus calcStatus, string message = null)
 {
     if (CalcQuantity == null)
     {
         CalcQuantity = new CalcQuantity();
     }
     CalcQuantity.CalcStatus = calcStatus;
     if (message != null)
     {
         CalcQuantity.Message = message;
     }
 }
        /// <summary>
        /// Calculate this level as musch as possible, using whatever information is currently known.
        /// </summary>
        /// <returns></returns>
        private void Calculate_Here()
        {
            List <CalcQuantity> args = new List <CalcQuantity>();

            foreach (SingleResult item in Inputs)
            {
                args.Add(item.CalcQuantity);
            }

            CalcQuantity result = Function.Calculate(args);

            CalcQuantity = result;
        }
        // -----------------------------------------
        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);
        }
 public CalcQuantity(CalcQuantity source)
 {
     source.CopyMe(this);
 }
Пример #6
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);
        }