formula works, mergin in main

This commit is contained in:
Álvaro González 2026-01-25 19:03:57 +01:00
parent fd5999d586
commit b2ffea801a
6 changed files with 55 additions and 122 deletions

View file

@ -135,11 +135,16 @@ Where:
"output": {"name": "Result", "unit": "scalar"},
"d4rtCode": """
var total = HeartRate + Breathing + MuscleTone + Reflexes + SkinColor;
var interpretation = switch (total) {
>= 7 => 'Normal',
4-6 => 'Requires attention',
_ => 'Emergency care needed'
};
late var interpretation;
if( total < 4 ) {
interpretation = 'Critical condition';
}
else if( total < 7 ){
interpretation = 'Needs assistance';
}
else {
interpretation = 'Normal';
}
Result = 'Score: \$total - \$interpretation';
""",
"tags": ["medical", "pediatrics", "assessment"]

View file

@ -58,6 +58,10 @@ class Corpus{
return _allFormulas.values.toList(growable:false);
}
Formula? getFormula(String name) {
return _allFormulas.get(name);
}
final Multimap<String, String> _baseToUnits = Multimap.create();
final Map<String, UnitSpec> _allUnits = {};

View file

@ -100,9 +100,18 @@ class FormulaEvaluator {
dynamic evaluate(Formula formula, Map<String, dynamic> inputValues) {
_validateInputValues(formula, inputValues);
final completeSource = _buildCompleteSource(formula, inputValues);
try {
final result = _interpreter.execute(source: completeSource);
return result;
}
catch (e) {
print( "Error evaluating formula source:\n$completeSource" );
throw FormulaEvaluationException(
'Error evaluating formula "${formula.name}": $e',
e,
);
}
}
void _validateInputValues(Formula formula, Map<String, dynamic> inputValues) {
final missingVars = <String>[];

View file

@ -19,7 +19,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ^3.8.1
sdk: ^3.10.7
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions

View file

@ -208,112 +208,5 @@ void main() {
expect(result, closeTo(9.8596, 0.0001));
});
});
group('APGAR Score', () {
test('evaluates APGAR score formula - Normal case', () {
final formula = Formula(
name: "Apgar Score",
description: "Newborn health assessment scoring system",
input: [
VariableSpec(name: "HeartRate", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "Breathing", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "MuscleTone", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "Reflexes", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "SkinColor", values: ["hr1", "hr2", "hr3"])
],
output: VariableSpec(name: "Result", unit: "stringscalar"),
d4rtCode: """
var total = HeartRate + Breathing + MuscleTone + Reflexes + SkinColor;
var interpretation = switch (total) {
>= 7 => 'Normal',
4-6 => 'Requires attention',
_ => 'Emergency care needed'
};
Result = 'Score: \$total - \$interpretation';
""",
);
// Test normal case (score 7-10)
final result = evaluator.evaluate(formula, {
'HeartRate': 2,
'Breathing': 2,
'MuscleTone': 2,
'Reflexes': 2,
'SkinColor': 2
});
expect(result, 'Score: 10 - Normal');
});
test('evaluates APGAR score formula - Requires attention case', () {
final formula = Formula(
name: "Apgar Score",
description: "Newborn health assessment scoring system",
input: [
VariableSpec(name: "HeartRate", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "Breathing", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "MuscleTone", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "Reflexes", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "SkinColor", values: ["hr1", "hr2", "hr3"])
],
output: VariableSpec(name: "Result", unit: "stringscalar"),
d4rtCode: """
var total = HeartRate + Breathing + MuscleTone + Reflexes + SkinColor;
var interpretation = switch (total) {
>= 7 => 'Normal',
4-6 => 'Requires attention',
_ => 'Emergency care needed'
};
Result = 'Score: \$total - \$interpretation';
""",
);
// Test requires attention case (score 4-6)
final result = evaluator.evaluate(formula, {
'HeartRate': 1,
'Breathing': 1,
'MuscleTone': 1,
'Reflexes': 1,
'SkinColor': 2
});
expect(result, 'Score: 6 - Requires attention');
});
test('evaluates APGAR score formula - Emergency case', () {
final formula = Formula(
name: "Apgar Score",
description: "Newborn health assessment scoring system",
input: [
VariableSpec(name: "HeartRate", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "Breathing", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "MuscleTone", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "Reflexes", values: ["hr1", "hr2", "hr3"]),
VariableSpec(name: "SkinColor", values: ["hr1", "hr2", "hr3"])
],
output: VariableSpec(name: "Result", unit: "stringscalar"),
d4rtCode: """
var total = HeartRate + Breathing + MuscleTone + Reflexes + SkinColor;
var interpretation = switch (total) {
>= 7 => 'Normal',
4-6 => 'Requires attention',
_ => 'Emergency care needed'
};
Result = 'Score: \$total - \$interpretation';
""",
);
// Test emergency case (score 0-3)
final result = evaluator.evaluate(formula, {
'HeartRate': 0,
'Breathing': 0,
'MuscleTone': 1,
'Reflexes': 0,
'SkinColor': 1
});
expect(result, 'Score: 2 - Emergency care needed');
});
});
});
}

View file

@ -1,16 +1,21 @@
import 'package:d4rt_formulas/corpus.dart';
import 'package:d4rt_formulas/defaults/default_corpus.dart';
import 'package:d4rt_formulas/formula_evaluator.dart';
import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:d4rt_formulas/formula_models.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
Future<Corpus> createTestCorpus() async {
return createDefaultCorpus();
}
Future<Corpus> testCorpus = createTestCorpus();
test("Parses unit", () {
final setLiteral = {"name": "kilometer", "symbol": "km", "baseUnit": "meter", "factor": 1000};
final unit = UnitSpec.fromSet(setLiteral);
@ -23,37 +28,37 @@ void main() {
});
test("From km to in", () async {
final corpus = await createTestCorpus();
final corpus = await testCorpus;
final inches = corpus.convert(1, "kilometer", "inch");
expect( inches, closeTo(39370.078,0.001) );
});
test("From furlong to base", () async {
final corpus = await createTestCorpus();
final corpus = await testCorpus;
final m = corpus.convert(1, "furlong", "meter");
expect(m,closeTo(201.168,0.001));
});
test("From base to furlong", () async {
final corpus = await createTestCorpus();
final corpus = await testCorpus;
final m = corpus.convert(201.168, "meter", "furlong");
expect(m,closeTo(1,0.001));
});
test("From C to F", () async {
final corpus = await createTestCorpus();
final corpus = await testCorpus;
final m = corpus.convert(37, "Celsius", "Fahrenheit");
expect(m,closeTo(98.6,0.001));
});
test("From K to F", () async {
final corpus = await createTestCorpus();
final corpus = await testCorpus;
final m = corpus.convert(37, "Kelvin", "Fahrenheit");
expect(m,closeTo(-393.07,0.001));
});
test("From C to K", () async {
final corpus = await createTestCorpus();
final corpus = await testCorpus;
final m = corpus.convert(100, "Celsius", "Kelvin");
expect(m,closeTo(373.15,0.001));
});
@ -108,5 +113,22 @@ void main() {
expect(result, 98.0); // F = m * a = 10 * 9.8 = 98 N
});
group('APGAR Score', () {
test('evaluates APGAR score formula - Normal case', () async {
final corpus = await testCorpus;
final formula = corpus.getFormula("Apgar Score")!;
final evaluator = FormulaEvaluator();
final result = evaluator.evaluate(formula, {
'HeartRate': 2,
'Breathing': 2,
'MuscleTone': 2,
'Reflexes': 2,
'SkinColor': 2
});
expect(result, 'Score: 10 - Normal');
});
});
}