conversión de unidades con expresiones y con sentencias
This commit is contained in:
parent
a48182ffbf
commit
d58a6cda8f
3 changed files with 57 additions and 17 deletions
|
|
@ -85,7 +85,7 @@ class Corpus{
|
|||
return _allUnits.get(unit);
|
||||
}
|
||||
|
||||
String _converterFromCodeString(Number x, String codeString) {
|
||||
String _converterFromCodeStringAsExpression(Number x, String codeString) {
|
||||
final buffer = StringBuffer();
|
||||
buffer.writeln("final x = ${x};");
|
||||
buffer.writeln("main(){return $codeString;}");
|
||||
|
|
@ -93,6 +93,14 @@ class Corpus{
|
|||
return code;
|
||||
}
|
||||
|
||||
String _converterFromCodeStringAsStatement(Number x, String codeString) {
|
||||
final buffer = StringBuffer();
|
||||
buffer.writeln("final x = ${x};");
|
||||
buffer.writeln("main(){ $codeString; return x; }");
|
||||
final code = buffer.toString();
|
||||
return code;
|
||||
}
|
||||
|
||||
Number _convertToBase(Number x, String fromUnit) {
|
||||
final unit = getUnit(fromUnit);
|
||||
|
||||
|
|
@ -104,9 +112,7 @@ class Corpus{
|
|||
throw ArgumentError("Unit has no codeFromUnitToBase: $unit");
|
||||
}
|
||||
|
||||
final d4rt = D4rt();
|
||||
final completeSource = _converterFromCodeString(x, unit.codeFromUnitToBase as String);
|
||||
final ret = d4rt.execute(source: completeSource);
|
||||
final ret = _convertUsingCode(x, unit.codeFromUnitToBase as String);
|
||||
return ret as Number;
|
||||
}
|
||||
|
||||
|
|
@ -121,12 +127,44 @@ class Corpus{
|
|||
throw ArgumentError("Unit has no codeFromBaseToUnit: $unit");
|
||||
}
|
||||
|
||||
final d4rt = D4rt();
|
||||
final completeSource = _converterFromCodeString(x, unit.codeFromBaseToUnit as String);
|
||||
final ret = d4rt.execute(source: completeSource);
|
||||
final ret = _convertUsingCode(x, unit.codeFromBaseToUnit as String);
|
||||
return ret as Number;
|
||||
}
|
||||
|
||||
|
||||
Number _convertUsingCode(Number x, String code ){
|
||||
late String completeSourceExpression;
|
||||
late String completeSourceStatement;
|
||||
try {
|
||||
completeSourceExpression = _converterFromCodeStringAsExpression(x, code);
|
||||
final ret = _evaluate(completeSourceExpression);
|
||||
return ret;
|
||||
}
|
||||
catch(e1,stack){
|
||||
try{
|
||||
completeSourceStatement = _converterFromCodeStringAsStatement(x, code);
|
||||
final ret = _evaluate(completeSourceStatement);
|
||||
return ret;
|
||||
}
|
||||
catch( e2, stack ){
|
||||
print(completeSourceExpression);
|
||||
print(e1);
|
||||
print(completeSourceStatement);
|
||||
print(e2);
|
||||
throw FormulaEvaluationException( "Evaluation as statement and expression failed" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static dynamic _evaluate(String code, [D4rt? interpreter]) {
|
||||
final d4rtInterpreter = interpreter ?? FormulaEvaluator.createDefaultInterpreter();
|
||||
FormulaEvaluator.prepareInterpreter(d4rtInterpreter);
|
||||
final completeCode = "${FormulaEvaluator.d4rtImports}\n$code";
|
||||
final result = d4rtInterpreter.execute(source: completeCode);
|
||||
return result.toDouble();
|
||||
}
|
||||
|
||||
|
||||
Number convert(Number x, String fromUnit, String toUnit) {
|
||||
final xBase = _convertToBase(x, fromUnit);
|
||||
final xTo = _convertFromBase(xBase, toUnit);
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@
|
|||
"name": "Gas Mark",
|
||||
"symbol": "GM",
|
||||
"baseUnit": "Kelvin",
|
||||
"toBase": """
|
||||
"toBase": r"""
|
||||
if (x < 1) {
|
||||
double celsius = (243 - 25 * (log(1 / x) / log(2))) / 1.8;
|
||||
return celsius + 273.15; // convert Celsius to Kelvin
|
||||
|
|
|
|||
|
|
@ -58,10 +58,9 @@ class FormulaEvaluator {
|
|||
interpreter.registerBridgedClass(myMathDefinition, "package:d4rt_formulas.dart");
|
||||
}
|
||||
|
||||
|
||||
static dynamic evaluateExpression(String code) {
|
||||
final interpreter = createDefaultInterpreter();
|
||||
prepareInterpreter(interpreter);
|
||||
static dynamic evaluateExpression(String code, [D4rt? interpreter]) {
|
||||
final d4rtInterpreter = interpreter ?? createDefaultInterpreter();
|
||||
prepareInterpreter(d4rtInterpreter);
|
||||
final d4rtCode = """
|
||||
import 'dart:math';
|
||||
import "package:d4rt_formulas.dart";
|
||||
|
|
@ -72,7 +71,7 @@ class FormulaEvaluator {
|
|||
return result;
|
||||
}""";
|
||||
//print("evaluateExpression:\n$d4rtCode");
|
||||
final result = interpreter.execute(source: d4rtCode);
|
||||
final result = d4rtInterpreter.execute(source: d4rtCode);
|
||||
return result.toDouble();
|
||||
}
|
||||
|
||||
|
|
@ -112,19 +111,22 @@ class FormulaEvaluator {
|
|||
return formula.inputVarNames()..sort();
|
||||
}
|
||||
|
||||
static final String d4rtImports = """
|
||||
import 'dart:math';
|
||||
import "package:d4rt_formulas.dart";
|
||||
""";
|
||||
|
||||
String _buildCompleteSource(Formula formula, Map<String, dynamic> inputValues) {
|
||||
final buffer = StringBuffer();
|
||||
|
||||
buffer.writeln("""
|
||||
import 'dart:math';
|
||||
import "package:d4rt_formulas.dart";
|
||||
|
||||
$d4rtImports
|
||||
|
||||
main()
|
||||
{
|
||||
"""
|
||||
);
|
||||
|
||||
|
||||
|
||||
for (final entry in inputValues.entries) {
|
||||
final varName = entry.key;
|
||||
|
|
|
|||
Loading…
Reference in a new issue