private void button1_Click(object sender, EventArgs e) { var parsedFunc = new ParsedFunction(); parsedFunc.StringToParse = " "; parsedFunc.VariableNames = new List <string> { "x", "y", "z" }; parsedFunc.ParameterNames = new List <string> { "p1", "p2" }; parsedFunc.ParameterValues = new[] { 0.0, 0.0 }; var errorData = new FuncParserErrorData(); parsedFunc.Parse(errorData); if (errorData.ErrorNumber == errNumber.err_OK) { MessageBox.Show(this, "That wasn't supposed to happen"); } else { MessageBox.Show(this, $"Error is on purpose. The error is {errorData.ErrorNumber}"); } }
private void checkExplicitFormula(IUsingFormula entityUsingFormula, string displayPath, BaseDimensionRepresentation baseDimensionRepresentation, ExplicitFormula explicitFormula) { if (explicitFormula == null) { return; } var dimensionInfos = new List <IQuantityDimensionInfo>(); if (isDoubleString(explicitFormula.FormulaString)) { return; } foreach (var objectReference in explicitFormula.ObjectReferences.Where(x => x.Object.Dimension != null)) { var pathDim = explicitFormula.ObjectPaths.Where(path => path.Alias.Equals(objectReference.Alias)).Select(path => path.Dimension).FirstOrDefault(); if (!Equals(objectReference.Object.Dimension, pathDim)) { addWarning(entityUsingFormula, AppConstants.Validation.ReferenceDimensionMissmatch(displayPath, objectReference, pathDim)); } dimensionInfos.Add(new QuantityDimensionInfo(objectReference.Alias, createDimensionInfoFromBaseRepresentation(objectReference.Object.Dimension.BaseRepresentation))); } IFuncParserErrorData ed = new FuncParserErrorData(); var verifyDimension = _dimensionParser.GetDimensionInfoFor(explicitFormula.FormulaString, dimensionInfos, ed); if (!ed.ErrorNumber.Equals(errNumber.err_OK)) { //ignore some dimension check errors. //Reason: some formulas are written so that dimension exponents cannot be calculated, e.g. //x^y where y is not dimensionless. // //In this case, err_CANNOTCALC_DIMENSION is returned. //In all other cases, generate error or warning, depending on the error number returned if (ed.ErrorNumber != errNumber.err_CANNOTCALC_DIMENSION || (_userSettings.ShowCannotCalcErrors && ed.ErrorNumber == errNumber.err_CANNOTCALC_DIMENSION)) { var notificationType = ed.ErrorNumber.IsOneOf(errNumber.err_DIMENSION, errNumber.err_CANNOTCALC_DIMENSION) ? NotificationType.Warning : NotificationType.Error; addNotification(notificationType, entityUsingFormula, ed.Description); } } else if (!verifyDimension.AreEquals(createDimensionInfoFromBaseRepresentation(baseDimensionRepresentation))) { addWarning(entityUsingFormula, AppConstants.Validation.FormulaDimensionMismatch(displayPath, explicitFormula.Dimension.Name)); } }
public void Parse() { try { var parserError = new FuncParserErrorData(); _parserFunc.Parse(parserError); if (parserError.ErrorNumber != 0) throw new FuncParserException(parserError); } catch (FuncParserException) { throw; } catch (Exception e) { throw new FuncParserException("Unable to parse expression", e); } }
public double Compute(double[] variableValues, double[] parameterValues) { try { var parserError = new FuncParserErrorData(); _parserFunc.ParameterValues = parameterValues; double value = _parserFunc.CalcExpression(variableValues, parserError); if (parserError.ErrorNumber != 0) throw new FuncParserException(parserError); return value; } catch (FuncParserException) { throw; } catch (Exception e) { throw new FuncParserException("Unable to compute expression", e); } }