From b2ffea801a216f227412a79c95b945219d9cc88a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Gonz=C3=A1lez?= Date: Sun, 25 Jan 2026 19:03:57 +0100 Subject: [PATCH] formula works, mergin in main --- assets/formulas/formulas.d4rt | 15 +++-- lib/corpus.dart | 4 ++ lib/formula_evaluator.dart | 13 +++- pubspec.yaml | 2 +- test/formula_evaluator_test.dart | 107 ------------------------------- test/formula_models_test.dart | 36 +++++++++-- 6 files changed, 55 insertions(+), 122 deletions(-) diff --git a/assets/formulas/formulas.d4rt b/assets/formulas/formulas.d4rt index b14639c..a6dc9db 100644 --- a/assets/formulas/formulas.d4rt +++ b/assets/formulas/formulas.d4rt @@ -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"] diff --git a/lib/corpus.dart b/lib/corpus.dart index f6b37e9..7ad4f50 100644 --- a/lib/corpus.dart +++ b/lib/corpus.dart @@ -58,6 +58,10 @@ class Corpus{ return _allFormulas.values.toList(growable:false); } + Formula? getFormula(String name) { + return _allFormulas.get(name); + } + final Multimap _baseToUnits = Multimap.create(); final Map _allUnits = {}; diff --git a/lib/formula_evaluator.dart b/lib/formula_evaluator.dart index 1f6d961..acc978d 100644 --- a/lib/formula_evaluator.dart +++ b/lib/formula_evaluator.dart @@ -100,8 +100,17 @@ class FormulaEvaluator { dynamic evaluate(Formula formula, Map inputValues) { _validateInputValues(formula, inputValues); final completeSource = _buildCompleteSource(formula, inputValues); - final result = _interpreter.execute(source: completeSource); - return result; + 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 inputValues) { diff --git a/pubspec.yaml b/pubspec.yaml index c143b38..d70efdb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/formula_evaluator_test.dart b/test/formula_evaluator_test.dart index 7c553ce..0b4c04d 100644 --- a/test/formula_evaluator_test.dart +++ b/test/formula_evaluator_test.dart @@ -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'); - }); - }); }); } diff --git a/test/formula_models_test.dart b/test/formula_models_test.dart index 3979a2b..1456b3a 100644 --- a/test/formula_models_test.dart +++ b/test/formula_models_test.dart @@ -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 createTestCorpus() async { return createDefaultCorpus(); } + Future 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'); + }); + }); }