formula works, mergin in main
This commit is contained in:
parent
fd5999d586
commit
b2ffea801a
6 changed files with 55 additions and 122 deletions
|
|
@ -135,11 +135,16 @@ Where:
|
||||||
"output": {"name": "Result", "unit": "scalar"},
|
"output": {"name": "Result", "unit": "scalar"},
|
||||||
"d4rtCode": """
|
"d4rtCode": """
|
||||||
var total = HeartRate + Breathing + MuscleTone + Reflexes + SkinColor;
|
var total = HeartRate + Breathing + MuscleTone + Reflexes + SkinColor;
|
||||||
var interpretation = switch (total) {
|
late var interpretation;
|
||||||
>= 7 => 'Normal',
|
if( total < 4 ) {
|
||||||
4-6 => 'Requires attention',
|
interpretation = 'Critical condition';
|
||||||
_ => 'Emergency care needed'
|
}
|
||||||
};
|
else if( total < 7 ){
|
||||||
|
interpretation = 'Needs assistance';
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
interpretation = 'Normal';
|
||||||
|
}
|
||||||
Result = 'Score: \$total - \$interpretation';
|
Result = 'Score: \$total - \$interpretation';
|
||||||
""",
|
""",
|
||||||
"tags": ["medical", "pediatrics", "assessment"]
|
"tags": ["medical", "pediatrics", "assessment"]
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,10 @@ class Corpus{
|
||||||
return _allFormulas.values.toList(growable:false);
|
return _allFormulas.values.toList(growable:false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Formula? getFormula(String name) {
|
||||||
|
return _allFormulas.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
final Multimap<String, String> _baseToUnits = Multimap.create();
|
final Multimap<String, String> _baseToUnits = Multimap.create();
|
||||||
final Map<String, UnitSpec> _allUnits = {};
|
final Map<String, UnitSpec> _allUnits = {};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -100,9 +100,18 @@ class FormulaEvaluator {
|
||||||
dynamic evaluate(Formula formula, Map<String, dynamic> inputValues) {
|
dynamic evaluate(Formula formula, Map<String, dynamic> inputValues) {
|
||||||
_validateInputValues(formula, inputValues);
|
_validateInputValues(formula, inputValues);
|
||||||
final completeSource = _buildCompleteSource(formula, inputValues);
|
final completeSource = _buildCompleteSource(formula, inputValues);
|
||||||
|
try {
|
||||||
final result = _interpreter.execute(source: completeSource);
|
final result = _interpreter.execute(source: completeSource);
|
||||||
return result;
|
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) {
|
void _validateInputValues(Formula formula, Map<String, dynamic> inputValues) {
|
||||||
final missingVars = <String>[];
|
final missingVars = <String>[];
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
||||||
version: 1.0.0+1
|
version: 1.0.0+1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.8.1
|
sdk: ^3.10.7
|
||||||
|
|
||||||
# Dependencies specify other packages that your package needs in order to work.
|
# Dependencies specify other packages that your package needs in order to work.
|
||||||
# To automatically upgrade your package dependencies to the latest versions
|
# To automatically upgrade your package dependencies to the latest versions
|
||||||
|
|
|
||||||
|
|
@ -208,112 +208,5 @@ void main() {
|
||||||
expect(result, closeTo(9.8596, 0.0001));
|
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');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,21 @@
|
||||||
import 'package:d4rt_formulas/corpus.dart';
|
import 'package:d4rt_formulas/corpus.dart';
|
||||||
import 'package:d4rt_formulas/defaults/default_corpus.dart';
|
import 'package:d4rt_formulas/defaults/default_corpus.dart';
|
||||||
import 'package:d4rt_formulas/formula_evaluator.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';
|
import 'package:d4rt_formulas/formula_models.dart';
|
||||||
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
Future<Corpus> createTestCorpus() async {
|
Future<Corpus> createTestCorpus() async {
|
||||||
return createDefaultCorpus();
|
return createDefaultCorpus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<Corpus> testCorpus = createTestCorpus();
|
||||||
|
|
||||||
|
|
||||||
test("Parses unit", () {
|
test("Parses unit", () {
|
||||||
final setLiteral = {"name": "kilometer", "symbol": "km", "baseUnit": "meter", "factor": 1000};
|
final setLiteral = {"name": "kilometer", "symbol": "km", "baseUnit": "meter", "factor": 1000};
|
||||||
final unit = UnitSpec.fromSet(setLiteral);
|
final unit = UnitSpec.fromSet(setLiteral);
|
||||||
|
|
@ -23,37 +28,37 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("From km to in", () async {
|
test("From km to in", () async {
|
||||||
final corpus = await createTestCorpus();
|
final corpus = await testCorpus;
|
||||||
final inches = corpus.convert(1, "kilometer", "inch");
|
final inches = corpus.convert(1, "kilometer", "inch");
|
||||||
expect( inches, closeTo(39370.078,0.001) );
|
expect( inches, closeTo(39370.078,0.001) );
|
||||||
});
|
});
|
||||||
|
|
||||||
test("From furlong to base", () async {
|
test("From furlong to base", () async {
|
||||||
final corpus = await createTestCorpus();
|
final corpus = await testCorpus;
|
||||||
final m = corpus.convert(1, "furlong", "meter");
|
final m = corpus.convert(1, "furlong", "meter");
|
||||||
expect(m,closeTo(201.168,0.001));
|
expect(m,closeTo(201.168,0.001));
|
||||||
});
|
});
|
||||||
|
|
||||||
test("From base to furlong", () async {
|
test("From base to furlong", () async {
|
||||||
final corpus = await createTestCorpus();
|
final corpus = await testCorpus;
|
||||||
final m = corpus.convert(201.168, "meter", "furlong");
|
final m = corpus.convert(201.168, "meter", "furlong");
|
||||||
expect(m,closeTo(1,0.001));
|
expect(m,closeTo(1,0.001));
|
||||||
});
|
});
|
||||||
|
|
||||||
test("From C to F", () async {
|
test("From C to F", () async {
|
||||||
final corpus = await createTestCorpus();
|
final corpus = await testCorpus;
|
||||||
final m = corpus.convert(37, "Celsius", "Fahrenheit");
|
final m = corpus.convert(37, "Celsius", "Fahrenheit");
|
||||||
expect(m,closeTo(98.6,0.001));
|
expect(m,closeTo(98.6,0.001));
|
||||||
});
|
});
|
||||||
|
|
||||||
test("From K to F", () async {
|
test("From K to F", () async {
|
||||||
final corpus = await createTestCorpus();
|
final corpus = await testCorpus;
|
||||||
final m = corpus.convert(37, "Kelvin", "Fahrenheit");
|
final m = corpus.convert(37, "Kelvin", "Fahrenheit");
|
||||||
expect(m,closeTo(-393.07,0.001));
|
expect(m,closeTo(-393.07,0.001));
|
||||||
});
|
});
|
||||||
|
|
||||||
test("From C to K", () async {
|
test("From C to K", () async {
|
||||||
final corpus = await createTestCorpus();
|
final corpus = await testCorpus;
|
||||||
final m = corpus.convert(100, "Celsius", "Kelvin");
|
final m = corpus.convert(100, "Celsius", "Kelvin");
|
||||||
expect(m,closeTo(373.15,0.001));
|
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
|
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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue