d4t_formulas/lib/formula_models.dart

248 lines
7.2 KiB
Dart
Raw Normal View History

2025-08-24 09:52:34 +00:00
import 'package:d4rt/d4rt.dart';
2025-08-28 10:34:49 +00:00
import 'package:collection/collection.dart';
2025-09-07 11:59:03 +00:00
abstract class SetUtils {
static Object safeGet(Map<Object?, Object?> map, String key) {
if (!map.containsKey(key)) {
throw ArgumentError("Key not found: $key -- $map");
}
return map[key] ?? "Not possible!!!";
}
static String stringValue(Map<Object?, Object?> map, String key) {
return safeGet(map, key).toString();
}
static List<Object?> listValue(Map<Object?, Object?> map, String key) {
return safeGet(map, key) as List<Object?>;
}
static Number numberValue(Map<Object?, Object?> map, String key) {
return double.parse(stringValue(map, key));
}
}
typedef Number = double;
class UnitSpec {
final String name;
final String baseUnit;
final String symbol;
final Number? factorFromUnitToBase;
final String? codeFromUnitToBase;
final String? codeFromBaseToUnit;
UnitSpec({
required this.name,
required this.baseUnit,
required this.symbol,
this.factorFromUnitToBase,
this.codeFromBaseToUnit,
this.codeFromUnitToBase,
});
factory UnitSpec.fromSet(Map<Object?, Object?> theSet) {
String name = SetUtils.stringValue(theSet, "name");
String symbol = SetUtils.stringValue(theSet, "symbol");
if( theSet.containsKey("isBase") ){
2025-09-07 12:04:42 +00:00
return UnitSpec(name: name, baseUnit: name, symbol: symbol, factorFromUnitToBase: 1);
2025-09-07 11:59:03 +00:00
}
String baseUnit = SetUtils.stringValue(theSet, "baseUnit");
if (theSet.containsKey("factor")) {
Number factorFromUnitToBase = SetUtils.numberValue(theSet, "factor");
return UnitSpec(
name: name,
baseUnit: baseUnit,
symbol: symbol,
factorFromUnitToBase: factorFromUnitToBase,
);
}
else if( theSet.containsKey("toBase")) {
String codeFromBaseToUnit = SetUtils.stringValue(
theSet,
"fromBase",
);
String codeFromUnitToBase = SetUtils.stringValue(
theSet,
"toBase",
);
return UnitSpec(name: name,
baseUnit: baseUnit,
symbol: symbol,
codeFromBaseToUnit: codeFromBaseToUnit,
codeFromUnitToBase: codeFromUnitToBase);
}
else{
throw ArgumentError( "Need factor or toBase/fromBase");
}
}
static List<UnitSpec> fromArrayStringLiteral(String arrayStringLiteral) {
var d4rt = D4rt();
final buffer = StringBuffer();
buffer.write("main(){ return $arrayStringLiteral; }");
final code = buffer.toString();
final List<Object?> list = d4rt.execute(source: code);
final units = list.map((set) => UnitSpec.fromSet(set as Map));
return units.toList(growable: false);
}
}
2025-08-24 09:52:34 +00:00
class VariableSpec {
final String name;
2025-11-09 19:29:58 +00:00
final String? unit;
final List<dynamic>? allowedValues;
2025-11-09 19:29:58 +00:00
VariableSpec({required this.name, this.unit, this.allowedValues}){
final valuesValid = allowedValues != null && allowedValues?.isNotEmpty == true;
if( unit == null && !valuesValid ){
throw new ArgumentError("$name: at least unit or allowedValues should be valid");
}
}
@override
2025-11-09 19:29:58 +00:00
String toString() => 'var($name: $unit${allowedValues != null ? ' allowed: $allowedValues' : ''})';
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is VariableSpec &&
runtimeType == other.runtimeType &&
2025-09-15 19:42:15 +00:00
unit == other.unit &&
2025-11-09 19:29:58 +00:00
name == other.name &&
const DeepCollectionEquality().equals(allowedValues, other.allowedValues);
@override
2025-11-09 19:29:58 +00:00
int get hashCode => Object.hash(unit, name, allowedValues != null ? const DeepCollectionEquality().hash(allowedValues!) : 0);
}
class Formula {
final String name;
final String? description;
2025-08-24 09:52:34 +00:00
final List<VariableSpec> input;
final VariableSpec output;
final String d4rtCode;
2025-09-16 16:22:29 +00:00
final List<String> tags;
2025-08-22 15:47:06 +00:00
Formula({
required this.name,
this.description,
required this.input,
required this.output,
required this.d4rtCode,
2025-09-16 16:22:29 +00:00
this.tags = const [],
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() =>
2025-09-16 16:22:29 +00:00
'Formula(name: $name, description: $description, input: $input, output: $output, d4rtCode: $d4rtCode, tags: $tags)';
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Formula &&
runtimeType == other.runtimeType &&
name == other.name &&
description == other.description &&
2025-08-24 09:52:34 +00:00
output == other.output &&
ListEquality().equals(input, other.input) &&
2025-09-16 16:22:29 +00:00
d4rtCode == other.d4rtCode &&
ListEquality().equals(tags, other.tags);
@override
int get hashCode =>
2025-09-16 16:22:29 +00:00
Object.hash(name, description, ListEquality().hash(input), output, d4rtCode, ListEquality().hash(tags));
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-09-07 11:59:03 +00:00
factory Formula.fromStringLiteral(String setStringLiteral) {
2025-08-26 14:54:35 +00:00
var d4rt = D4rt();
final buffer = StringBuffer();
2025-09-07 11:59:03 +00:00
buffer.write("main(){ return $setStringLiteral; }");
2025-08-26 14:54:35 +00:00
final code = buffer.toString();
final Map<Object?, Object?> setLiteral = d4rt.execute(source: code);
return Formula.fromSet(setLiteral);
}
2025-09-07 11:59:03 +00:00
static List<Formula> fromArrayStringLiteral(String arrayStringLiteral) {
2025-08-26 15:17:42 +00:00
var d4rt = D4rt();
final buffer = StringBuffer();
2025-09-07 11:59:03 +00:00
buffer.write("main(){ return $arrayStringLiteral; }");
2025-08-26 15:17:42 +00:00
final code = buffer.toString();
2025-10-05 14:53:46 +00:00
//print("fromArrayStringLiteral:$code");
2025-08-26 15:17:42 +00:00
final List<Object?> list = d4rt.execute(source: code);
2025-09-07 11:59:03 +00:00
final formulas = list.map((set) => Formula.fromSet(set as Map));
2025-08-26 15:17:42 +00:00
2025-09-05 16:53:06 +00:00
return formulas.toList(growable: false);
2025-08-26 15:17:42 +00:00
}
2025-08-26 14:37:28 +00:00
factory Formula.fromSet(Map<Object?, Object?> theSet) {
VariableSpec parseVar(Map<Object?, Object?> varSpec) {
2025-09-07 11:59:03 +00:00
String name = SetUtils.stringValue(varSpec, "name");
2025-11-09 19:29:58 +00:00
String? unit;
if (varSpec.containsKey("unit")) {
unit = SetUtils.stringValue(varSpec, "unit");
}
final allowed = varSpec['values'] as List<dynamic>?;
if (allowed != null) {
final types = allowed.map((v) => v.runtimeType).toSet();
if (types.length > 1) {
throw ArgumentError('Allowed values must be all Strings or all Numbers');
}
if (!types.contains(String) && !types.contains(double) && !types.contains(int)) {
throw ArgumentError('Allowed values must be Strings or Numbers');
}
}
return VariableSpec(
name: name,
unit: unit,
allowedValues: allowed?.toList(growable: false),
);
2025-08-26 14:37:28 +00:00
}
2025-09-07 11:59:03 +00:00
String name = SetUtils.stringValue(theSet, "name");
String? description = theSet ["description"] as String?;
2025-09-16 16:22:29 +00:00
List<String> tags = (theSet["tags"] as List<Object?>? ?? []).map((t) => t.toString()).toList();
2025-09-07 11:59:03 +00:00
final List<Object?> inputSet = SetUtils.listValue(theSet, "input");
List<VariableSpec> input = inputSet
.map((v) => parseVar(v as Map))
.toList(growable: false);
2025-08-26 14:37:28 +00:00
Map<Object?, Object?> outputSet = theSet.get("output");
VariableSpec output = parseVar(outputSet);
2025-09-07 11:59:03 +00:00
String d4rtCode = SetUtils.stringValue(theSet, "d4rtCode");
2025-08-26 14:37:28 +00:00
2025-09-05 16:53:06 +00:00
return Formula(
2025-08-26 14:37:28 +00:00
name: name,
description: description,
2025-10-05 14:53:46 +00:00
tags: tags,
2025-08-26 14:37:28 +00:00
input: input,
output: output,
d4rtCode: d4rtCode,
);
2025-08-24 10:33:21 +00:00
}
}