private FormulaResult GetFormulaResult(Formula formula, bool evaluate) { var e = this.CreateExpression(formula); var formulaResult = new FormulaResult(); formulaResult.IsValid = !e.HasErrors(); if (!formulaResult.IsValid) { formulaResult.Error = this.FormatSyntaxError(e.Error); } else { var parametersFormula = new List <string>(); parametersFormula.AddRange(this.systemWildcards); if (formula.Parameters != null) { parametersFormula.AddRange(formula.Parameters.Select(x => x.Name)); } formulaResult.Parameters = this.ExtractParameters(formula); var missingParameters = formulaResult.Parameters.Where( p => parametersFormula.All(x => !x.Equals(p.Name, StringComparison.InvariantCultureIgnoreCase))) .ToArray(); if (missingParameters.Length > 0) { formulaResult.IsValid = false; var sb = new StringBuilder(); foreach (var missingParameter in missingParameters) { sb.AppendLine($"Parametro no encontrado {missingParameter.Name}"); } formulaResult.Error = sb.ToString(); } else if (evaluate) { formulaResult.Result = e.Evaluate()?.ToString() ?? null; } } return(formulaResult); }
/// <summary> /// 論理式を解析する /// </summary> /// <param name="expressType">論理演算式の種別</param> /// <param name="expression">論理演算式</param> /// <param name="ocrEdit">検査対象</param> /// <returns>解析した結果</returns> private FormulaResult createFormula(ExpressionChecker.ExpressType expressType, string expression, OcrEdit ocrEdit) { FormulaResult result = new FormulaResult(); ExpressionChecker expressChecker = new ExpressionChecker(expression, expressType, new ExpressionChecker.GetValueHandler(groupId => { return(this.getGroupValue(ocrEdit.PageNo, ocrEdit.ClId, groupId)); })); try { result.Value = expressChecker.Compute(); result.HasInput = expressChecker.HasInput; return(result); } catch (Exception) { //エラーの場合NULLで設定する result.Value = null; result.HasInput = expressChecker.HasInput; return(result); } }
/// <summary> /// 論理演算検査 /// </summary> /// <param name="chkInfo">検査情報</param> /// <param name="ocrEdit">検査対象</param> private bool checkLogicalOperation(ChecklistInfo chkInfo, OcrEdit ocrEdit) { FormulaResult expressResult = null; if (string.IsNullOrEmpty(chkInfo.DefinitionExpression)) { return(true); } // 日付妥当性検査 if (chkInfo.DateValidationFlag.HasValue && chkInfo.DateValidationFlag.Value) { //定義式(日付計算) expressResult = this.createFormula(ExpressionChecker.ExpressType.CalcDate, chkInfo.DefinitionExpression, ocrEdit); // 結果式を解析 FormulaResult resultExpression = createFormula(ExpressionChecker.ExpressType.CalcNumeric, chkInfo.ResultExpression, ocrEdit); if (!expressResult.HasInput && !resultExpression.HasInput) { //未入力場合通る return(true); } //演算結果null及び一致しない場合エラーになる if ((expressResult.Value == null) || (resultExpression == null) || (expressResult.Value != resultExpression.Value)) { string result = string.Format(ValidateResultMsg, expressResult.Value, resultExpression.Value); string errMsg = string.Format(DenshowCommon.Message.EP091, result); setErrMessage(ocrEdit, errMsg); return(false); } return(true); } // 時間妥当性検査 if (chkInfo.TimeValidationFlag.HasValue && chkInfo.TimeValidationFlag.Value) { //定義式(時間計算) expressResult = this.createFormula(ExpressionChecker.ExpressType.CalcTime, chkInfo.DefinitionExpression, ocrEdit); // 結果式を解析 FormulaResult resultExpression = createFormula(ExpressionChecker.ExpressType.CalcNumeric, chkInfo.ResultExpression, ocrEdit); if (!expressResult.HasInput && !resultExpression.HasInput) { //未入力場合通る return(true); } //演算結果null及び一致しない場合エラーになる if ((expressResult.Value == null) || (resultExpression == null) || (expressResult.Value != resultExpression.Value)) { string result = string.Format(ValidateResultMsg, expressResult.Value, resultExpression.Value); string errMsg = string.Format(DenshowCommon.Message.EP091, result); setErrMessage(ocrEdit, errMsg); return(false); } return(true); } // 数値妥当性検査 if (chkInfo.NumberValidationFlag.HasValue && chkInfo.NumberValidationFlag.Value) { //定義式(時間計算) expressResult = this.createFormula(ExpressionChecker.ExpressType.CalcNumeric, chkInfo.DefinitionExpression, ocrEdit); // 結果式を解析 FormulaResult resultExpression = createFormula(ExpressionChecker.ExpressType.CalcNumeric, chkInfo.ResultExpression, ocrEdit); if (!expressResult.HasInput && !resultExpression.HasInput) { //未入力場合通る return(true); } //演算結果null及び一致しない場合エラーになる if ((expressResult.Value == null) || (resultExpression == null) || (expressResult.Value != resultExpression.Value)) { string result = string.Format(ValidateResultMsg, expressResult.Value, resultExpression.Value); string errMsg = string.Format(DenshowCommon.Message.EP091, result); setErrMessage(ocrEdit, errMsg); return(false); } return(true); } // 日付形式検査 if (chkInfo.DateFormatCheckFlag.HasValue && chkInfo.DateFormatCheckFlag.Value) { expressResult = this.createFormula(ExpressionChecker.ExpressType.CheckDate, chkInfo.DefinitionExpression, ocrEdit); if (expressResult.HasInput && !checkDate(expressResult.Value)) { string result = string.Format(CheckExpResultMsg, expressResult.Value); string errMsg = string.Format(DenshowCommon.Message.EP091, result); setErrMessage(ocrEdit, errMsg); return(false); } return(true); } // 時刻形式検査 if (chkInfo.TimeFormatCheckFlag.HasValue && chkInfo.TimeFormatCheckFlag.Value) { expressResult = this.createFormula(ExpressionChecker.ExpressType.CheckTime, chkInfo.DefinitionExpression, ocrEdit); if (expressResult.HasInput && !checkTime(expressResult.Value)) { string result = string.Format(CheckExpResultMsg, expressResult.Value); string errMsg = string.Format(DenshowCommon.Message.EP091, result); setErrMessage(ocrEdit, errMsg); return(false); } return(true); } // 数値形式検査 if (chkInfo.NumberFormatCheckFlag.HasValue && chkInfo.NumberFormatCheckFlag.Value) { expressResult = this.createFormula(ExpressionChecker.ExpressType.CheckNumeric, chkInfo.DefinitionExpression, ocrEdit); if (expressResult.HasInput && !checkNum(expressResult.Value)) { string result = string.Format(CheckExpResultMsg, expressResult.Value); string errMsg = string.Format(DenshowCommon.Message.EP091, result); setErrMessage(ocrEdit, errMsg); return(false); } return(true); } return(true); }
protected abstract void FuseDoneHandler(Fusion fusion, FusionTrigger trigger, FormulaResult results);
protected override bool Execute(CodeActivityContext context) { var formulaUn = Formula_UN.Get(context); if (string.IsNullOrEmpty(formulaUn)) { Error.Set(context, "Не определен идентификатор формулы"); return(false); } var fId = new TFormulaParam { FormulaID = formulaUn, FormulasTable = enumFormulasTable.Info_Formula_Description, IsFormulaHasCorrectDescription = true, }; TFormulasResult result = null; try { var res = ARM_Service.FR_GetFormulasResults(new List <TFormulaParam> { fId }, StartDateTime.Get(context), EndDateTime.Get(context), DiscreteType, DataSourceType, 1, IsCoeffEnabled, false, isPower, false, UnitDigit, null, false, false); if (res != null && res.Result_Values != null && res.Result_Values.Count > 0) { var rv = res.Result_Values[0]; if (rv != null && rv.Result_Values != null && rv.Result_Values.Count > 0) { result = rv.Result_Values[0]; if (result.Val_List != null) { var r = result.Val_List.Accomulate(); SummValues.Set(context, r.F_VALUE); Status.Set(context, (int)r.F_FLAG); StatusStr.Set(context, TVALUES_DB.FLAG_to_String(r.F_FLAG, ";")); } } } if (res.Errors != null) { Error.Set(context, res.Errors.ToString()); } FormulaResult.Set(context, result); } catch (Exception ex) { Error.Set(context, ex.Message); if (!HideException.Get(context)) { throw ex; } } return(string.IsNullOrEmpty(Error.Get(context))); }
public decimal Calculate(string formula, OrderLine orderLine) { try { dynamic inputs = new ExpandoObject(); IDictionary <string, object> dictionary = (IDictionary <string, object>)inputs; dictionary.Add("OrderLineQty", orderLine.Quantity); IEnumerable <OrderLineOption> olOptions = orderLine.OrderLineOptions; if (olOptions != null) { foreach (OrderLineOption olOption in olOptions) { string varName = GuidToVar(olOption.ProductOptionId); if (dictionary.ContainsKey(varName)) { continue; // duplicate product option values: should not happen (but it did)!! } if (!string.IsNullOrWhiteSpace(olOption.ProductOptionValueId)) { string varValue = GuidToVar(olOption.ProductOptionValueId); dictionary.Add(varName, varValue); } else { dictionary.Add(varName, olOption.ProductOptionValue); } } } FormulaRequest req = new FormulaRequest() { Formula = formula, Inputs = inputs }; using (var client = new System.Net.Http.HttpClient()) { // client.BaseAddress = new Uri("http://localhost:5051/"); string webApiUrl = Properties.Settings.Default.WebApiUrl; client.BaseAddress = new Uri(webApiUrl); // "http://webapi-openbiz-dev.azurewebsites.net/" client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.PostAsJsonAsync("nodejs/execFormula.js", req).Result; if (response.IsSuccessStatusCode) { var headers = response.Headers; FormulaResult returnValue = response.Content.ReadAsAsync <FormulaResult>().Result; return(returnValue.Result); } else { return(0); } } } catch (Exception ex) { throw; } }