feat: add tags support to formulas

This commit is contained in:
Álvaro González 2025-09-16 18:22:29 +02:00 committed by Álvaro González (aider)
parent d396d71afc
commit 1a7fd0c884
3 changed files with 22 additions and 8 deletions

View file

@ -22,6 +22,11 @@ class Multimap<K, V> extends DelegatingMap<K, List<V>> {
}
}
class FormulaCorpus{
final Multimap<String, Formula> _tags = Multimap.create();
final Map<String, Formula> _allFormulas = {};
}
class UnitCorpus {
final Multimap<String, String> _baseToUnits = Multimap.create();
final Map<String, UnitSpec> _allUnits = {};

View file

@ -127,6 +127,7 @@ class Formula {
final List<VariableSpec> input;
final VariableSpec output;
final String d4rtCode;
final List<String> 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<String> 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<String> tags = (theSet["tags"] as List<Object?>? ?? []).map((t) => t.toString()).toList();
final List<Object?> inputSet = SetUtils.listValue(theSet, "input");
List<VariableSpec> input = inputSet
.map((v) => parseVar(v as Map))

View file

@ -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"]
},
]