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';
|
import 'package:d4rt/d4rt.dart';
|
||||||
|
|
||||||
class VariableSpec {
|
class VariableSpec {
|
||||||
|
|
||||||
final String name;
|
final String name;
|
||||||
final String magnitude;
|
final String magnitude;
|
||||||
static final MAGNITUDELESS = "magnitudeless";
|
static final MAGNITUDELESS = "magnitudeless";
|
||||||
|
|
@ -35,7 +33,7 @@ class Formula {
|
||||||
required this.input,
|
required this.input,
|
||||||
required this.output,
|
required this.output,
|
||||||
required this.d4rtCode,
|
required this.d4rtCode,
|
||||||
}){
|
}) {
|
||||||
validate();
|
validate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,7 +43,6 @@ class Formula {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() =>
|
String toString() =>
|
||||||
'Formula(name: $name, input: $input, output: $output, d4rtCode: $d4rtCode)';
|
'Formula(name: $name, input: $input, output: $output, d4rtCode: $d4rtCode)';
|
||||||
|
|
@ -64,25 +61,48 @@ class Formula {
|
||||||
int get hashCode =>
|
int get hashCode =>
|
||||||
Object.hash(name, ListEquality().hash(input), output, d4rtCode);
|
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 ){
|
Object safeGet(Map<Object?, Object?> map, String key){
|
||||||
String name = varSpec.get("name");
|
if( !map.containsKey(key) ){
|
||||||
String magnitude = varSpec.get("magnitude");
|
throw ArgumentError( "Key not found: $key -- $map" );
|
||||||
return VariableSpec(name: name, magnitude: magnitude);
|
|
||||||
}
|
}
|
||||||
|
return map[key] ?? "Not possible!!!";
|
||||||
|
}
|
||||||
|
|
||||||
String name = set.get("name");
|
String stringValue(Map<Object?, Object?> map, String key){
|
||||||
List<Map<String,String>> inputSet = set.get("input");
|
return safeGet(map, key).toString();
|
||||||
List<VariableSpec> input = inputSet.map(parseVar).toList(growable:false);
|
}
|
||||||
Map<String,String> outputSet = set.get("output");
|
|
||||||
VariableSpec output = parseVar(outputSet);
|
|
||||||
String d4rtCode = set.get("d4rtCode");
|
|
||||||
|
|
||||||
return new Formula(name:name, input:input, output:output, d4rtCode:d4rtCode );
|
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 = 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 = theSet.get("d4rtCode");
|
||||||
|
|
||||||
|
return new Formula(
|
||||||
|
name: name,
|
||||||
|
input: input,
|
||||||
|
output: output,
|
||||||
|
d4rtCode: d4rtCode,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:d4rt/d4rt.dart';
|
||||||
import 'package:d4rt_formulas/formula_evaluator.dart';
|
import 'package:d4rt_formulas/formula_evaluator.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
import 'package:d4rt_formulas/formula_models.dart';
|
import 'package:d4rt_formulas/formula_models.dart';
|
||||||
|
|
@ -19,8 +20,8 @@ void main() {
|
||||||
'''
|
'''
|
||||||
};
|
};
|
||||||
|
|
||||||
final formula = Formula.fromSetLiteral(setLiteral);
|
final formula = Formula.fromSet(setLiteral);
|
||||||
final evaluator = new FormulaEvaluator();
|
final evaluator = FormulaEvaluator();
|
||||||
|
|
||||||
final result = evaluator.evaluate(formula, {
|
final result = evaluator.evaluate(formula, {
|
||||||
'm': 10.0, // 10 kg
|
'm': 10.0, // 10 kg
|
||||||
|
|
@ -30,4 +31,39 @@ void main() {
|
||||||
expect(result, 98.0); // F = m * a = 10 * 9.8 = 98 N
|
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