line wrap at 120
This commit is contained in:
parent
e3c5d9cef9
commit
78d7b190db
1 changed files with 22 additions and 46 deletions
|
|
@ -47,8 +47,7 @@ class FormulaEvaluator {
|
||||||
|
|
||||||
static D4rt createDefaultInterpreter() => D4rt();
|
static D4rt createDefaultInterpreter() => D4rt();
|
||||||
|
|
||||||
FormulaEvaluator([D4rt? interpreter])
|
FormulaEvaluator([D4rt? interpreter]) : _interpreter = interpreter ?? createDefaultInterpreter() {
|
||||||
: _interpreter = interpreter ?? createDefaultInterpreter() {
|
|
||||||
prepareInterpreter(_interpreter);
|
prepareInterpreter(_interpreter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,10 +72,7 @@ class FormulaEvaluator {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
interpreter.registerBridgedClass(
|
interpreter.registerBridgedClass(myMathDefinition, "package:d4rt_formulas.dart");
|
||||||
myMathDefinition,
|
|
||||||
"package:d4rt_formulas.dart",
|
|
||||||
);
|
|
||||||
registerD4rtBridgeBridges(interpreter);
|
registerD4rtBridgeBridges(interpreter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,9 +98,7 @@ class FormulaEvaluator {
|
||||||
case String value:
|
case String value:
|
||||||
return StringResult(value);
|
return StringResult(value);
|
||||||
default:
|
default:
|
||||||
throw FormulaEvaluationException(
|
throw FormulaEvaluationException("Unexpected result type: ${result.runtimeType} -- $result");
|
||||||
"Unexpected result type: ${result.runtimeType} -- $result",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -123,10 +117,7 @@ class FormulaEvaluator {
|
||||||
}
|
}
|
||||||
|
|
||||||
errorHandler.notify("$e\n$completeSource", stack);
|
errorHandler.notify("$e\n$completeSource", stack);
|
||||||
throw FormulaEvaluationException(
|
throw FormulaEvaluationException('Error evaluating formula "${formula.name}": $e', e);
|
||||||
'Error evaluating formula "${formula.name}": $e',
|
|
||||||
e,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -154,9 +145,7 @@ class FormulaEvaluator {
|
||||||
if (inputValue != null) {
|
if (inputValue != null) {
|
||||||
// Convert input value to string for comparison since allowed values are stored as strings
|
// Convert input value to string for comparison since allowed values are stored as strings
|
||||||
final inputValueAsString = inputValue.toString();
|
final inputValueAsString = inputValue.toString();
|
||||||
final containsValue = values.any(
|
final containsValue = values.any((allowedValue) => allowedValue.toString() == inputValueAsString);
|
||||||
(allowedValue) => allowedValue.toString() == inputValueAsString,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!containsValue) {
|
if (!containsValue) {
|
||||||
throw FormulaEvaluationException(
|
throw FormulaEvaluationException(
|
||||||
|
|
@ -185,16 +174,9 @@ class FormulaEvaluator {
|
||||||
|
|
||||||
""";
|
""";
|
||||||
|
|
||||||
static const reservedVariableNames = {
|
static const reservedVariableNames = {"variableValues", "indexOf", "variableAllowedValues"};
|
||||||
"variableValues",
|
|
||||||
"indexOf",
|
|
||||||
"variableAllowedValues",
|
|
||||||
};
|
|
||||||
|
|
||||||
String _buildCompleteSource(
|
String _buildCompleteSource(Formula formula, Map<String, dynamic> inputValues) {
|
||||||
Formula formula,
|
|
||||||
Map<String, dynamic> inputValues,
|
|
||||||
) {
|
|
||||||
final buffer = StringBuffer();
|
final buffer = StringBuffer();
|
||||||
|
|
||||||
buffer.writeln("""
|
buffer.writeln("""
|
||||||
|
|
@ -250,17 +232,13 @@ class FormulaEvaluator {
|
||||||
for (final vs in formula.input) {
|
for (final vs in formula.input) {
|
||||||
final values = vs.values;
|
final values = vs.values;
|
||||||
if (values != null && values.isNotEmpty) {
|
if (values != null && values.isNotEmpty) {
|
||||||
variableValuesMap[vs.name] = values
|
variableValuesMap[vs.name] = values.map((v) => v.toString()).toList(growable: false);
|
||||||
.map((v) => v.toString())
|
|
||||||
.toList(growable: false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Explicitly include the output VariableSpec if it has allowed values
|
// Explicitly include the output VariableSpec if it has allowed values
|
||||||
final outValues = formula.output.values;
|
final outValues = formula.output.values;
|
||||||
if (outValues != null && outValues.isNotEmpty) {
|
if (outValues != null && outValues.isNotEmpty) {
|
||||||
variableValuesMap[formula.output.name] = outValues
|
variableValuesMap[formula.output.name] = outValues.map((v) => v.toString()).toList(growable: false);
|
||||||
.map((v) => v.toString())
|
|
||||||
.toList(growable: false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the variableValues map into the generated source without escaping names/values
|
// Write the variableValues map into the generated source without escaping names/values
|
||||||
|
|
@ -293,27 +271,25 @@ class FormulaEvaluator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Number formulaSolver(Formula formula,
|
Number formulaSolver(
|
||||||
|
Formula formula,
|
||||||
String variableToSolve,
|
String variableToSolve,
|
||||||
Map<String, dynamic> fixedInputValues, {
|
Map<String, dynamic> fixedInputValues, {
|
||||||
Number hint = 0,
|
Number hint = 0,
|
||||||
Number step = 10,
|
Number step = 10,
|
||||||
Number maxDelta = 0.01,
|
Number maxDelta = 0.01,
|
||||||
int maxTries = 100,
|
int maxTries = 100,
|
||||||
}) {
|
}) {
|
||||||
|
if (variableToSolve == formula.output.name) {
|
||||||
if( variableToSolve == formula.output.name ){
|
|
||||||
return FormulaEvaluator().evaluate(formula, fixedInputValues);
|
return FormulaEvaluator().evaluate(formula, fixedInputValues);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!formula.inputVarNames().contains(variableToSolve) ){
|
if (!formula.inputVarNames().contains(variableToSolve)) {
|
||||||
throw ArgumentError(
|
throw ArgumentError(
|
||||||
'Variable "$variableToSolve" is not an input or output variable of the formula "${formula
|
'Variable "$variableToSolve" is not an input or output variable of the formula "${formula.name}".',
|
||||||
.name}".',
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
final modifiedInputValues = Map<String, dynamic>.from(fixedInputValues);
|
final modifiedInputValues = Map<String, dynamic>.from(fixedInputValues);
|
||||||
var evaluator = FormulaEvaluator();
|
var evaluator = FormulaEvaluator();
|
||||||
Number f(Number x) {
|
Number f(Number x) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue