// ----------------------------- private static IList <EqnCalc> TestEquationParser() { IList <EqnCalc> retList = new List <EqnCalc>(); IList <string> eqnStrings = new List <string>(); // Engineering: Heat: Heat Exchanger // Engineering: Equipment: Heat Exchanger eqnStrings.Add("Q = m*cp*(T1-T2)"); eqnStrings.Add("Q = U*A*DTLM"); eqnStrings.Add("DTLM = (DT1-DT2)/LOG(DT1/DT2)"); eqnStrings.Add("DTLM = ((Thi-Tco)-(Tho-Tci))/LOG((Thi-Tco)/(Tho-Tci))"); // Geometry: Solid Bodies: Cylinder eqnStrings.Add("V = PI*r^2*h"); eqnStrings.Add("Am = 2*PI*r*h"); eqnStrings.Add("A0 = 2*PI*r*(r+h)"); foreach (var item in eqnStrings) { JongErrWarn errW = null; FunctionCalc funCalc = _contentManager.CreateFunctionCalcFromExpression(item, null, null, out errW); if (funCalc != null) { retList.Add((EqnCalc)funCalc); } } return(retList); }
private static Result <bool> RearrangeOnce(EqnCalc eqCalc, int argIndexToPreserve, ContentManager cm) { FunctionCalc lhsFunCalc = eqCalc.Inputs[0] as FunctionCalc; Function lhsFunction = lhsFunCalc?.Function; if (lhsFunction == null) { return(Result <bool> .Bad($"Unexpected error: lhs is not a funcalc")); } SingleResult argToPreserve = lhsFunCalc.Inputs[argIndexToPreserve]; // Create placeholders that the function rearrangements will treat as variables IList <string> argPlaceholders = new List <string>(); IDictionary <string, SingleResult> placeFillers = new Dictionary <string, SingleResult>(); for (int i = 0; i < lhsFunCalc.Inputs.Count; i++) { string placeholder = $"v{i}"; argPlaceholders.Add(placeholder); if (i != argIndexToPreserve) { placeFillers.Add(placeholder, lhsFunCalc.Inputs[i]); } } string resPlaceHolder = "res"; placeFillers.Add(resPlaceHolder, eqCalc.Inputs[1]); // Ask the function definition for a rearrangement that isolates the required variable Result <string> r_rearrangement = lhsFunction.GetRearrangement(argPlaceholders, resPlaceHolder, argIndexToPreserve); if (r_rearrangement.IsNotGood() || string.IsNullOrEmpty(r_rearrangement.Value)) { return(Result <bool> .Bad($"Rearrangement failed: {r_rearrangement.Message}")); } // Parse the rearrangement expression JongErrWarn errW = null; FunctionCalc newRhs = cm.CreateFunctionCalcFromExpression(r_rearrangement.Value, "", null, out errW); if (newRhs == null) { return(Result <bool> .Bad($"Rearrangement cannot be used: {errW?.ErrWarnString}")); } // Subsitute for the placeholders Result <bool> r_ok = SubsituteForPlaceholders(newRhs, placeFillers); if (r_ok.IsNotGood()) { return(Result <bool> .Bad($"Error substituting for placeholders: {r_ok.Message}")); } // Update the eqCalc with the rearrangement eqCalc.Inputs[1] = newRhs; eqCalc.Inputs[0] = argToPreserve; return(Result <bool> .Good(true)); }
public async Task <EqnCalc> ShowCreateEquation(ContentManager cm) { Tuple <bool, string, string, string, IList <VarInfo> > tpl; tpl = await ShowAddEquation(cm); try { bool bCancelled = tpl.Item1; string equationString = null; string userEquationString = null; string eqDescription = null; IList <VarInfo> varInfos = null; if (!bCancelled) { equationString = tpl.Item2; userEquationString = tpl.Item3; eqDescription = tpl.Item4; varInfos = tpl.Item5; Debug.WriteLine("equationString={0}, userEquationString={1},eqDescription={2}"); Debug.WriteLine(varInfos); JongErrWarn errW = null; FunctionCalc funCalc = cm.CreateFunctionCalcFromExpression(equationString, eqDescription, varInfos, out errW); EqnCalc equationCalc = funCalc as EqnCalc; if (equationCalc == null) { // TODO } else { // TODO //EquationLibraries.Add(equationCalc); return(equationCalc); } } } catch (Exception ex) { Logging.LogException(ex); throw; } return(null); }
public static void AddEquationToSectionNode(ContentManager contentManager, SectionNode sectionNode, EquationDetails ed) { string description = ""; if (!string.IsNullOrEmpty(ed.Name)) { description += ((string.IsNullOrEmpty(description) ? "" : " ") + ed.Name); } if (!string.IsNullOrEmpty(ed.Reference)) { description += ((string.IsNullOrEmpty(description) ? "" : " ") + ed.Reference); } if (!string.IsNullOrEmpty(ed.Description)) { description += ((string.IsNullOrEmpty(description) ? "" : " ") + ed.Description); } // ---------------------- JongErrWarn errW = null; FunctionCalc funCalc = contentManager.CreateFunctionCalcFromExpression(ed.EquationAsText, description, ed.VariableInfos, out errW); // ---------------------- if (funCalc != null) { // ------------- Record the test values if there are any if ((ed.TestValues != null) && (ed.TestValues.Count > 0)) { funCalc.TestValues = new Dictionary </*variable name*/ string, double>(); for (int i = 0; i < ed.VariableInfos.Count; i++) { VarInfo vi = ed.VariableInfos[i]; funCalc.TestValues.Add(vi.Name, ed.TestValues[i]); } } funCalc.ExpectedDimensions = new Dictionary </*variable name*/ string, Dimensions>(); if (ed.VariableInfos != null) { foreach (VarInfo vi in ed.VariableInfos) { funCalc.ExpectedDimensions.Add(vi.Name, vi.ParamType?.Dimensions); } } // ------------- sectionNode.EqnCalcs.Add((EqnCalc)funCalc); } }
public void RefreshEquationCalc(string eqnString) { UserEquationString = eqnString; if (eqnString?.Length > 0) { JongErrWarn errW = null; FunctionCalc funCalc = ContentManager.CreateFunctionCalcFromExpression(eqnString, null, null, out errW); EquationCalc = funCalc as EqnCalc; EquationMsg = errW?.ErrWarnString; } else { EquationCalc = null; EquationMsg = DefaultEquationMsg; } OnPropertyChanged("UserEquationString"); OnPropertyChanged("EquationCalc"); OnPropertyChanged("EquationMsg"); }