From 1a7fd0c88497ef3c7cc89c40b31f06bce6d91b51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Gonz=C3=A1lez?= Date: Tue, 16 Sep 2025 18:22:29 +0200 Subject: [PATCH] feat: add tags support to formulas --- lib/corpus.dart | 5 +++++ lib/formula_models.dart | 10 +++++++--- lib/formulas.d4rt | 15 ++++++++++----- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/corpus.dart b/lib/corpus.dart index 3510927..16ec384 100644 --- a/lib/corpus.dart +++ b/lib/corpus.dart @@ -22,6 +22,11 @@ class Multimap extends DelegatingMap> { } } +class FormulaCorpus{ + final Multimap _tags = Multimap.create(); + final Map _allFormulas = {}; +} + class UnitCorpus { final Multimap _baseToUnits = Multimap.create(); final Map _allUnits = {}; diff --git a/lib/formula_models.dart b/lib/formula_models.dart index bec3b4c..5d38601 100644 --- a/lib/formula_models.dart +++ b/lib/formula_models.dart @@ -127,6 +127,7 @@ class Formula { final List input; final VariableSpec output; final String d4rtCode; + final List tags; Formula({ required this.name, @@ -134,6 +135,7 @@ class Formula { required this.input, required this.output, required this.d4rtCode, + this.tags = const [], }) { validate(); } @@ -146,7 +148,7 @@ class Formula { @override String toString() => - 'Formula(name: $name, description: $description, input: $input, output: $output, d4rtCode: $d4rtCode)'; + 'Formula(name: $name, description: $description, input: $input, output: $output, d4rtCode: $d4rtCode, tags: $tags)'; @override bool operator ==(Object other) => @@ -157,11 +159,12 @@ class Formula { description == other.description && output == other.output && ListEquality().equals(input, other.input) && - d4rtCode == other.d4rtCode; + d4rtCode == other.d4rtCode && + ListEquality().equals(tags, other.tags); @override int get hashCode => - Object.hash(name, description, ListEquality().hash(input), output, d4rtCode); + Object.hash(name, description, ListEquality().hash(input), output, d4rtCode, ListEquality().hash(tags)); List inputVarNames() => input.map((v) => v.name).toList(growable: false); @@ -199,6 +202,7 @@ class Formula { String name = SetUtils.stringValue(theSet, "name"); String? description = theSet ["description"] as String?; + List tags = (theSet["tags"] as List? ?? []).map((t) => t.toString()).toList(); final List inputSet = SetUtils.listValue(theSet, "input"); List input = inputSet .map((v) => parseVar(v as Map)) diff --git a/lib/formulas.d4rt b/lib/formulas.d4rt index 872e70e..c5e3913 100644 --- a/lib/formulas.d4rt +++ b/lib/formulas.d4rt @@ -17,7 +17,8 @@ Where: {name: "g", unit: "m/s²"} // Gravitational acceleration ], output: {name: "h", unit: "m"}, // Height in meters - d4rtCode: "h = 0.5 * g * pow(t, 2)" + d4rtCode: "h = 0.5 * g * pow(t, 2)", + tags: ["physics", "kinematics"] }, // Newton's Law of Universal Gravitation @@ -40,7 +41,8 @@ Where: {name: "r", unit: "m"} // Distance between masses ], output: {name: "F", unit: "N"}, // Force in newtons - d4rtCode: "F = (6.67430e-11 * m1 * m2) / pow(r, 2)" + d4rtCode: "F = (6.67430e-11 * m1 * m2) / pow(r, 2)", + tags: ["physics", "astronomy", "gravity"] }, // Kinetic Energy @@ -61,7 +63,8 @@ Where: {name: "v", unit: "m/s"} // Velocity ], output: {name: "KE", unit: "J"}, // Energy in joules - d4rtCode: "KE = 0.5 * m * pow(v, 2)" + d4rtCode: "KE = 0.5 * m * pow(v, 2)", + tags: ["physics", "energy", "mechanics"] }, // Projectile Motion Range @@ -79,7 +82,8 @@ Where: {name: "θ", unit: "deg"} // Launch angle ], output: {name: "R", unit: "m"}, // Horizontal distance - d4rtCode: "R = (pow(v, 2) * sin(2 * radians(θ))) / 9.80665" + d4rtCode: "R = (pow(v, 2) * sin(2 * radians(θ))) / 9.80665", + tags: ["physics", "kinematics", "projectile"] }, { @@ -99,6 +103,7 @@ Where: {name: "a", unit: "m/s²"} // Acceleration ], output: {name: "F", unit: "N"}, // Force in newtons - d4rtCode: "F = m * a" + d4rtCode: "F = m * a", + tags: ["physics", "mechanics", "newton"] }, ]