feat: add description field to Formula and example formulas

This commit is contained in:
Álvaro González 2025-09-15 21:58:11 +02:00 committed by Álvaro González (aider)
parent e08474a7eb
commit ba5175b6be
2 changed files with 53 additions and 2 deletions

View file

@ -123,12 +123,14 @@ class VariableSpec {
class Formula { class Formula {
final String name; final String name;
final String? description;
final List<VariableSpec> input; final List<VariableSpec> input;
final VariableSpec output; final VariableSpec output;
final String d4rtCode; final String d4rtCode;
Formula({ Formula({
required this.name, required this.name,
this.description,
required this.input, required this.input,
required this.output, required this.output,
required this.d4rtCode, required this.d4rtCode,
@ -144,7 +146,7 @@ class Formula {
@override @override
String toString() => String toString() =>
'Formula(name: $name, input: $input, output: $output, d4rtCode: $d4rtCode)'; 'Formula(name: $name, description: $description, input: $input, output: $output, d4rtCode: $d4rtCode)';
@override @override
bool operator ==(Object other) => bool operator ==(Object other) =>
@ -152,13 +154,14 @@ class Formula {
other is Formula && other is Formula &&
runtimeType == other.runtimeType && runtimeType == other.runtimeType &&
name == other.name && name == other.name &&
description == other.description &&
output == other.output && output == other.output &&
ListEquality().equals(input, other.input) && ListEquality().equals(input, other.input) &&
d4rtCode == other.d4rtCode; d4rtCode == other.d4rtCode;
@override @override
int get hashCode => int get hashCode =>
Object.hash(name, ListEquality().hash(input), output, d4rtCode); Object.hash(name, description, ListEquality().hash(input), output, d4rtCode);
List<String> inputVarNames() => List<String> inputVarNames() =>
input.map((v) => v.name).toList(growable: false); input.map((v) => v.name).toList(growable: false);
@ -195,6 +198,7 @@ class Formula {
} }
String name = SetUtils.stringValue(theSet, "name"); String name = SetUtils.stringValue(theSet, "name");
String? description = theSet ["description"] as String?;
final List<Object?> inputSet = SetUtils.listValue(theSet, "input"); final List<Object?> inputSet = SetUtils.listValue(theSet, "input");
List<VariableSpec> input = inputSet List<VariableSpec> input = inputSet
.map((v) => parseVar(v as Map)) .map((v) => parseVar(v as Map))
@ -205,6 +209,7 @@ class Formula {
return Formula( return Formula(
name: name, name: name,
description: description,
input: input, input: input,
output: output, output: output,
d4rtCode: d4rtCode, d4rtCode: d4rtCode,

46
lib/formulas.d4rt Normal file
View file

@ -0,0 +1,46 @@
[
// Free fall distance (vertical)
{
name: "Free Fall Distance",
input: [
{name: "t", magnitude: "s"}, // Time in seconds
{name: "g", magnitude: "m/s²"} // Gravitational acceleration
],
output: {name: "h", magnitude: "m"}, // Height in meters
d4rtCode: "0.5 * g * pow(t, 2)"
},
// Newton's Law of Universal Gravitation
{
name: "Gravitational Force",
input: [
{name: "m1", magnitude: "kg"}, // Mass 1
{name: "m2", magnitude: "kg"}, // Mass 2
{name: "r", magnitude: "m"} // Distance between masses
],
output: {name: "F", magnitude: "N"}, // Force in newtons
d4rtCode: "(6.67430e-11 * m1 * m2) / pow(r, 2)"
},
// Kinetic Energy
{
name: "Kinetic Energy",
input: [
{name: "m", magnitude: "kg"}, // Mass
{name: "v", magnitude: "m/s"} // Velocity
],
output: {name: "KE", magnitude: "J"}, // Energy in joules
d4rtCode: "0.5 * m * pow(v, 2)"
},
// Projectile Motion Range
{
name: "Projectile Range",
input: [
{name: "v", magnitude: "m/s"}, // Initial velocity
{name: "θ", magnitude: "deg"} // Launch angle
],
output: {name: "R", magnitude: "m"}, // Horizontal distance
d4rtCode: "(pow(v, 2) * sin(2 * radians(θ))) / 9.80665"
}
]