2025-08-21 15:15:00 +00:00
|
|
|
|
2025-08-24 09:52:34 +00:00
|
|
|
import 'package:d4rt/d4rt.dart';
|
2025-08-21 15:15:00 +00:00
|
|
|
|
2025-08-24 09:52:34 +00:00
|
|
|
class VariableSpec {
|
2025-08-21 15:15:00 +00:00
|
|
|
|
2025-08-24 09:52:34 +00:00
|
|
|
final String name;
|
|
|
|
|
final String magnitude;
|
|
|
|
|
static final MAGNITUDELESS = "magnitudeless";
|
2025-08-21 15:15:00 +00:00
|
|
|
|
2025-08-24 09:52:34 +00:00
|
|
|
VariableSpec({required this.name, required this.magnitude});
|
2025-08-21 15:15:00 +00:00
|
|
|
|
|
|
|
|
@override
|
2025-08-24 09:52:34 +00:00
|
|
|
String toString() => 'var($name: $magnitude)';
|
2025-08-21 15:15:00 +00:00
|
|
|
|
|
|
|
|
@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;
|
2025-08-21 15:15:00 +00:00
|
|
|
|
|
|
|
|
@override
|
2025-08-24 09:52:34 +00:00
|
|
|
int get hashCode => Object.hash(magnitude, name);
|
2025-08-21 15:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Formula {
|
|
|
|
|
final String name;
|
2025-08-24 09:52:34 +00:00
|
|
|
final List<VariableSpec> input;
|
|
|
|
|
final VariableSpec output;
|
2025-08-21 15:15:00 +00:00
|
|
|
final String d4rtCode;
|
|
|
|
|
|
2025-08-22 15:47:06 +00:00
|
|
|
Formula({
|
2025-08-21 15:15:00 +00:00
|
|
|
required this.name,
|
|
|
|
|
required this.input,
|
|
|
|
|
required this.output,
|
|
|
|
|
required this.d4rtCode,
|
2025-08-22 15:47:06 +00:00
|
|
|
}){
|
|
|
|
|
validate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate() {
|
|
|
|
|
if (name.trim().isEmpty) {
|
|
|
|
|
throw ArgumentError('Formula name cannot be empty');
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-21 15:15:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@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) &&
|
2025-08-21 15:15:00 +00:00
|
|
|
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-21 15:15:00 +00:00
|
|
|
|
2025-08-24 09:52:34 +00:00
|
|
|
List<String> inputVarNames() => input.map( (v) => v.name ).toList(growable: false);
|
2025-08-24 10:33:21 +00:00
|
|
|
|
|
|
|
|
factory Formula.fromSetLiteral(Map<String, Object> set ) {
|
|
|
|
|
|
|
|
|
|
VariableSpec parseVar(Map<String, String> varSpec ){
|
|
|
|
|
String name = varSpec.get("name");
|
|
|
|
|
String magnitude = varSpec.get("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");
|
|
|
|
|
VariableSpec output = parseVar(outputSet);
|
|
|
|
|
String d4rtCode = set.get("d4rtCode");
|
|
|
|
|
|
|
|
|
|
return new Formula(name:name, input:input, output:output, d4rtCode:d4rtCode );
|
|
|
|
|
|
|
|
|
|
}
|
2025-08-21 15:15:00 +00:00
|
|
|
}
|
|
|
|
|
|