dart Set literals instead of json
This commit is contained in:
parent
06be580dff
commit
dcf409cbf7
2 changed files with 76 additions and 20 deletions
|
|
@ -1,8 +1,6 @@
|
|||
|
||||
import 'package:d4rt/d4rt.dart';
|
||||
|
||||
class VariableSpec {
|
||||
|
||||
final String name;
|
||||
final String magnitude;
|
||||
static final MAGNITUDELESS = "magnitudeless";
|
||||
|
|
@ -35,7 +33,7 @@ class Formula {
|
|||
required this.input,
|
||||
required this.output,
|
||||
required this.d4rtCode,
|
||||
}){
|
||||
}) {
|
||||
validate();
|
||||
}
|
||||
|
||||
|
|
@ -45,7 +43,6 @@ class Formula {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'Formula(name: $name, input: $input, output: $output, d4rtCode: $d4rtCode)';
|
||||
|
|
@ -64,25 +61,48 @@ class Formula {
|
|||
int get hashCode =>
|
||||
Object.hash(name, ListEquality().hash(input), output, d4rtCode);
|
||||
|
||||
List<String> inputVarNames() => input.map( (v) => v.name ).toList(growable: false);
|
||||
List<String> inputVarNames() =>
|
||||
input.map((v) => v.name).toList(growable: false);
|
||||
|
||||
factory Formula.fromSetLiteral(Map<String, Object> set ) {
|
||||
factory Formula.fromSet(Map<Object?, Object?> theSet) {
|
||||
|
||||
VariableSpec parseVar(Map<String, String> varSpec ){
|
||||
String name = varSpec.get("name");
|
||||
String magnitude = varSpec.get("magnitude");
|
||||
Object safeGet(Map<Object?, Object?> map, String key){
|
||||
if( !map.containsKey(key) ){
|
||||
throw ArgumentError( "Key not found: $key -- $map" );
|
||||
}
|
||||
return map[key] ?? "Not possible!!!";
|
||||
}
|
||||
|
||||
String stringValue(Map<Object?, Object?> map, String key){
|
||||
return safeGet(map, key).toString();
|
||||
}
|
||||
|
||||
List<Object?> listValue(Map<Object?, Object?> map, String key){
|
||||
return safeGet(map,key) as List<Object?>;
|
||||
}
|
||||
|
||||
Map<String, Object?> mapValue(Map<Object?, Object?> map, String key){
|
||||
return safeGet(map,key) as Map<String, Object?>;
|
||||
}
|
||||
|
||||
VariableSpec parseVar(Map<Object?, Object?> varSpec) {
|
||||
String name = stringValue(varSpec, "name");
|
||||
String magnitude = stringValue(varSpec, "magnitude");
|
||||
return VariableSpec(name: name, magnitude: magnitude);
|
||||
}
|
||||
|
||||
String name = set.get("name");
|
||||
List<Map<String,String>> inputSet = set.get("input");
|
||||
List<VariableSpec> input = inputSet.map(parseVar).toList(growable:false);
|
||||
Map<String,String> outputSet = set.get("output");
|
||||
String name = stringValue( theSet, "name" );
|
||||
final List<Object?> inputSet = listValue( theSet, "input");
|
||||
List<VariableSpec> input = inputSet.map( (v) => parseVar(v as Map)).toList(growable: false);
|
||||
Map<Object?, Object?> outputSet = theSet.get("output");
|
||||
VariableSpec output = parseVar(outputSet);
|
||||
String d4rtCode = set.get("d4rtCode");
|
||||
|
||||
return new Formula(name:name, input:input, output:output, d4rtCode:d4rtCode );
|
||||
String d4rtCode = theSet.get("d4rtCode");
|
||||
|
||||
return new Formula(
|
||||
name: name,
|
||||
input: input,
|
||||
output: output,
|
||||
d4rtCode: d4rtCode,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:d4rt/d4rt.dart';
|
||||
import 'package:d4rt_formulas/formula_evaluator.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:d4rt_formulas/formula_models.dart';
|
||||
|
|
@ -19,8 +20,8 @@ void main() {
|
|||
'''
|
||||
};
|
||||
|
||||
final formula = Formula.fromSetLiteral(setLiteral);
|
||||
final evaluator = new FormulaEvaluator();
|
||||
final formula = Formula.fromSet(setLiteral);
|
||||
final evaluator = FormulaEvaluator();
|
||||
|
||||
final result = evaluator.evaluate(formula, {
|
||||
'm': 10.0, // 10 kg
|
||||
|
|
@ -30,4 +31,39 @@ void main() {
|
|||
expect(result, 98.0); // F = m * a = 10 * 9.8 = 98 N
|
||||
});
|
||||
|
||||
test( 'd4rt parses formula from literal', (){
|
||||
final literal = """
|
||||
{
|
||||
"name": "Newton's second law",
|
||||
"input": [
|
||||
{ "name": 'm', "magnitude": 'mass'},
|
||||
{ "name": 'a', "magnitude": 'acceleration'}
|
||||
],
|
||||
"output": { "name": 'F', "magnitude": 'force'},
|
||||
"d4rtCode": '''
|
||||
return a * m;
|
||||
'''
|
||||
}
|
||||
""";
|
||||
|
||||
var d4rt = D4rt();
|
||||
final buffer = StringBuffer();
|
||||
buffer.write( "main(){ return $literal; }");
|
||||
final code = buffer.toString();
|
||||
|
||||
final Map<Object?, Object?> setLiteral = d4rt.execute(source: code);
|
||||
|
||||
final formula = Formula.fromSet(setLiteral);
|
||||
final evaluator = FormulaEvaluator();
|
||||
|
||||
final result = evaluator.evaluate(formula, {
|
||||
'm': 10.0, // 10 kg
|
||||
'a': 9.8, // 9.8 m/s²
|
||||
});
|
||||
|
||||
expect(result, 98.0); // F = m * a = 10 * 9.8 = 98 N
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue