/// This is the function implemented by each concrete class to actually do the calculation. /// Args will be converted to a consistent UOMSet, unless there are all UOMless, before calling the actual function calc. /// If the function requires any other specific UOM it must do the conversion itself. public override CalcQuantity Calculate_Here(IList <CalcQuantity> args, UOMSet uomSet) { if (args.Count != 2) { return(new BadCalcQuantity(Name + " must have 2 arguments")); } if ((args[0].CalcStatus == CalcStatus.Bad) || (args[1].CalcStatus == CalcStatus.Bad)) { return(new BadCalcQuantity("Bad arguments")); } if ((args[0].CalcStatus == CalcStatus.Good) && (args[1].CalcStatus == CalcStatus.Good)) { if (args[0].Value == args[1].Value) // TODO: Equations: Tolerance? { return(new CalcQuantity(0, CalcStatus.Good, "", args[0].AnonUOM)); } else { return(new CalcQuantity((args[0].Value - args[1].Value), CalcStatus.Bad, "Both sides are known but not equal", args[0].AnonUOM)); // Return imbalance } } else if ((args[0].CalcStatus == CalcStatus.Good) && (args[1].CalcStatus == CalcStatus.Uknown)) { args[1].CalcStatus = args[0].CalcStatus; args[1].AnonUOM = args[0].AnonUOM; args[1].Value = args[0].Value; return(new CalcQuantity(0, CalcStatus.Good, "", args[0].AnonUOM)); } else if ((args[1].CalcStatus == CalcStatus.Good) && (args[0].CalcStatus == CalcStatus.Uknown)) { args[0].CalcStatus = args[1].CalcStatus; args[0].AnonUOM = args[1].AnonUOM; args[0].Value = args[1].Value; return(new CalcQuantity(0, CalcStatus.Good, "", args[0].AnonUOM)); } else if ((args[0].CalcStatus == CalcStatus.Uknown) && (args[1].CalcStatus == CalcStatus.Uknown)) { return(new CalcQuantity(0, CalcStatus.Uknown, "", args[0].AnonUOM)); } return(new BadCalcQuantity("Unexpected failure")); // Should never happen }
/// <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); }
} // 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); }
/// This is the function implemented by each concrete class to actually do the calculation. /// Args will be converted to a consistent UOMSet, unless there are all UOMless, before calling the actual function calc. /// If the function requires any other specific UOM it must do the conversion itself. public abstract CalcQuantity Calculate_Here(IList <CalcQuantity> args, UOMSet uomSet);
public AnonUOM(Dimensions dimensions, UOMSet uOMSet) : base(dimensions) { _uOMSet = uOMSet; }