d4t_formulas/lib/formula_models.dart

109 lines
2.9 KiB
Dart
Raw Normal View History

2025-08-24 09:52:34 +00:00
import 'package:d4rt/d4rt.dart';
2025-08-24 09:52:34 +00:00
class VariableSpec {
final String name;
final String magnitude;
static final MAGNITUDELESS = "magnitudeless";
2025-08-24 09:52:34 +00:00
VariableSpec({required this.name, required this.magnitude});
@override
2025-08-24 09:52:34 +00:00
String toString() => 'var($name: $magnitude)';
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is VariableSpec &&
runtimeType == other.runtimeType &&
2025-08-24 09:52:34 +00:00
magnitude == other.magnitude &&
name == other.name;
@override
2025-08-24 09:52:34 +00:00
int get hashCode => Object.hash(magnitude, name);
}
class Formula {
final String name;
2025-08-24 09:52:34 +00:00
final List<VariableSpec> input;
final VariableSpec output;
final String d4rtCode;
2025-08-22 15:47:06 +00:00
Formula({
required this.name,
required this.input,
required this.output,
required this.d4rtCode,
2025-08-26 14:37:28 +00:00
}) {
2025-08-22 15:47:06 +00:00
validate();
}
validate() {
if (name.trim().isEmpty) {
throw ArgumentError('Formula name cannot be empty');
}
}
@override
String toString() =>
'Formula(name: $name, input: $input, output: $output, d4rtCode: $d4rtCode)';
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Formula &&
runtimeType == other.runtimeType &&
name == other.name &&
2025-08-24 09:52:34 +00:00
output == other.output &&
ListEquality().equals(input, other.input) &&
d4rtCode == other.d4rtCode;
@override
int get hashCode =>
2025-08-24 09:52:34 +00:00
Object.hash(name, ListEquality().hash(input), output, d4rtCode);
2025-08-26 14:37:28 +00:00
List<String> inputVarNames() =>
input.map((v) => v.name).toList(growable: false);
2025-08-24 10:33:21 +00:00
2025-08-26 14:37:28 +00:00
factory Formula.fromSet(Map<Object?, Object?> theSet) {
2025-08-24 10:33:21 +00:00
2025-08-26 14:37:28 +00:00
Object safeGet(Map<Object?, Object?> map, String key){
if( !map.containsKey(key) ){
throw ArgumentError( "Key not found: $key -- $map" );
2025-08-24 10:33:21 +00:00
}
2025-08-26 14:37:28 +00:00
return map[key] ?? "Not possible!!!";
}
2025-08-24 10:33:21 +00:00
2025-08-26 14:37:28 +00:00
String stringValue(Map<Object?, Object?> map, String key){
return safeGet(map, key).toString();
}
2025-08-24 10:33:21 +00:00
2025-08-26 14:37:28 +00:00
List<Object?> listValue(Map<Object?, Object?> map, String key){
return safeGet(map,key) as List<Object?>;
}
2025-08-24 10:33:21 +00:00
2025-08-26 14:37:28 +00:00
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,
);
2025-08-24 10:33:21 +00:00
}
}