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);
|
return _allUnits.get(unit);
|
||||||
}
|
}
|
||||||
|
|
||||||
String _converterFromCodeString(Number x, String codeString) {
|
String _converterFromCodeStringAsExpression(Number x, String codeString) {
|
||||||
final buffer = StringBuffer();
|
final buffer = StringBuffer();
|
||||||
buffer.writeln("final x = ${x};");
|
buffer.writeln("final x = ${x};");
|
||||||
buffer.writeln("main(){return $codeString;}");
|
buffer.writeln("main(){return $codeString;}");
|
||||||
|
|
@ -93,6 +93,14 @@ class Corpus{
|
||||||
return code;
|
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) {
|
Number _convertToBase(Number x, String fromUnit) {
|
||||||
final unit = getUnit(fromUnit);
|
final unit = getUnit(fromUnit);
|
||||||
|
|
||||||
|
|
@ -104,9 +112,7 @@ class Corpus{
|
||||||
throw ArgumentError("Unit has no codeFromUnitToBase: $unit");
|
throw ArgumentError("Unit has no codeFromUnitToBase: $unit");
|
||||||
}
|
}
|
||||||
|
|
||||||
final d4rt = D4rt();
|
final ret = _convertUsingCode(x, unit.codeFromUnitToBase as String);
|
||||||
final completeSource = _converterFromCodeString(x, unit.codeFromUnitToBase as String);
|
|
||||||
final ret = d4rt.execute(source: completeSource);
|
|
||||||
return ret as Number;
|
return ret as Number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,12 +127,44 @@ class Corpus{
|
||||||
throw ArgumentError("Unit has no codeFromBaseToUnit: $unit");
|
throw ArgumentError("Unit has no codeFromBaseToUnit: $unit");
|
||||||
}
|
}
|
||||||
|
|
||||||
final d4rt = D4rt();
|
final ret = _convertUsingCode(x, unit.codeFromBaseToUnit as String);
|
||||||
final completeSource = _converterFromCodeString(x, unit.codeFromBaseToUnit as String);
|
|
||||||
final ret = d4rt.execute(source: completeSource);
|
|
||||||
return ret as Number;
|
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) {
|
Number convert(Number x, String fromUnit, String toUnit) {
|
||||||
final xBase = _convertToBase(x, fromUnit);
|
final xBase = _convertToBase(x, fromUnit);
|
||||||
final xTo = _convertFromBase(xBase, toUnit);
|
final xTo = _convertFromBase(xBase, toUnit);
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@
|
||||||
"name": "Gas Mark",
|
"name": "Gas Mark",
|
||||||
"symbol": "GM",
|
"symbol": "GM",
|
||||||
"baseUnit": "Kelvin",
|
"baseUnit": "Kelvin",
|
||||||
"toBase": """
|
"toBase": r"""
|
||||||
if (x < 1) {
|
if (x < 1) {
|
||||||
double celsius = (243 - 25 * (log(1 / x) / log(2))) / 1.8;
|
double celsius = (243 - 25 * (log(1 / x) / log(2))) / 1.8;
|
||||||
return celsius + 273.15; // convert Celsius to Kelvin
|
return celsius + 273.15; // convert Celsius to Kelvin
|
||||||
|
|
|
||||||
|
|
@ -58,10 +58,9 @@ class FormulaEvaluator {
|
||||||
interpreter.registerBridgedClass(myMathDefinition, "package:d4rt_formulas.dart");
|
interpreter.registerBridgedClass(myMathDefinition, "package:d4rt_formulas.dart");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static dynamic evaluateExpression(String code, [D4rt? interpreter]) {
|
||||||
static dynamic evaluateExpression(String code) {
|
final d4rtInterpreter = interpreter ?? createDefaultInterpreter();
|
||||||
final interpreter = createDefaultInterpreter();
|
prepareInterpreter(d4rtInterpreter);
|
||||||
prepareInterpreter(interpreter);
|
|
||||||
final d4rtCode = """
|
final d4rtCode = """
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
import "package:d4rt_formulas.dart";
|
import "package:d4rt_formulas.dart";
|
||||||
|
|
@ -72,7 +71,7 @@ class FormulaEvaluator {
|
||||||
return result;
|
return result;
|
||||||
}""";
|
}""";
|
||||||
//print("evaluateExpression:\n$d4rtCode");
|
//print("evaluateExpression:\n$d4rtCode");
|
||||||
final result = interpreter.execute(source: d4rtCode);
|
final result = d4rtInterpreter.execute(source: d4rtCode);
|
||||||
return result.toDouble();
|
return result.toDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,13 +111,16 @@ class FormulaEvaluator {
|
||||||
return formula.inputVarNames()..sort();
|
return formula.inputVarNames()..sort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static final String d4rtImports = """
|
||||||
|
import 'dart:math';
|
||||||
|
import "package:d4rt_formulas.dart";
|
||||||
|
""";
|
||||||
|
|
||||||
String _buildCompleteSource(Formula formula, Map<String, dynamic> inputValues) {
|
String _buildCompleteSource(Formula formula, Map<String, dynamic> inputValues) {
|
||||||
final buffer = StringBuffer();
|
final buffer = StringBuffer();
|
||||||
|
|
||||||
buffer.writeln("""
|
buffer.writeln("""
|
||||||
import 'dart:math';
|
$d4rtImports
|
||||||
import "package:d4rt_formulas.dart";
|
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue