diff --git a/TODO.md b/TODO.md index dce8f41..8742036 100644 --- a/TODO.md +++ b/TODO.md @@ -28,6 +28,6 @@ - [X] If the database is empty, sugest to use a default corpus - [X] If the user choose to use the default corpus, populate de database with the default corpus (load defaultcorpus, and then use toStringLiteral). If not, start with an empty list of formulas. - [X] From now on, the corpus will be loaded from database instead of assets -- [ ] Create method List Corpus.withDependencies(Formula formula). It will return the formula, the units of the formula, and all the units from the corpus with the same base unit. +- [R] Create method List Corpus.withDependencies(Formula formula). It will return the formula, the units of the formula, and all the units from the corpus with the same base unit. - [ ] Add a Share button to the formula list. It will export the array string literal of the formula with the units from Corpus.withDependencies(). - [ ] Replace flutter-markdown with flutter-markdown-plus diff --git a/lib/corpus.dart b/lib/corpus.dart index d27722c..93846ad 100644 --- a/lib/corpus.dart +++ b/lib/corpus.dart @@ -212,4 +212,33 @@ class Corpus{ return corpus; } + /// Returns the formula, the units of the formula, and all the units from the corpus with the same base unit. + List withDependencies(Formula formula) { + final result = {}; + + // Add the formula itself + result.add(formula); + + // Helper function to add units and their base equivalents + void addUnitsAndBaseEquivalents(String? unitName) { + if (unitName != null) { + final unit = getUnit(unitName); + result.add(unit); + // Add all units with the same base unit + final unitsWithSameBase = unitsOfSameMagnitude(unitName); + result.addAll(unitsWithSameBase.map((name) => getUnit(name))); + } + } + + // Process input variable units + formula.input.where((inputVar) => inputVar.unit != null).forEach((inputVar) { + addUnitsAndBaseEquivalents(inputVar.unit); + }); + + // Process output variable unit + addUnitsAndBaseEquivalents(formula.output.unit); + + return result.toList(); + } + } diff --git a/test/formula_models_test.dart b/test/formula_models_test.dart index 20e5319..7263502 100644 --- a/test/formula_models_test.dart +++ b/test/formula_models_test.dart @@ -243,4 +243,45 @@ void main() { }); }); + test('Corpus.withDependencies returns formula and its dependencies', () async { + final corpus = await testCorpus; + + // Get a formula that has units associated with it + final formula = corpus.getFormula("Newton's Second Law"); + expect(formula, isNotNull); + + // Call withDependencies method + final dependencies = corpus.withDependencies(formula!); + + // Check that the formula itself is included + expect(dependencies.any((element) => element is Formula && element.name == formula.name), true); + + // Check that units from input and output are included + for (final inputVar in formula.input) { + if (inputVar.unit != null) { + expect(dependencies.any((element) => element is UnitSpec && element.name == inputVar.unit), true); + + // Check that units with same base unit are included + final unitsWithSameBase = corpus.unitsOfSameMagnitude(inputVar.unit!); + for (final unitName in unitsWithSameBase) { + expect(dependencies.any((element) => element is UnitSpec && element.name == unitName), true); + } + } + } + + if (formula.output.unit != null) { + expect(dependencies.any((element) => element is UnitSpec && element.name == formula.output.unit), true); + + // Check that units with same base unit as output are included + final outputUnitsWithSameBase = corpus.unitsOfSameMagnitude(formula.output.unit!); + for (final unitName in outputUnitsWithSameBase) { + expect(dependencies.any((element) => element is UnitSpec && element.name == unitName), true); + } + } + + // Verify that there are no duplicates by checking the length of the list vs the set + final uniqueDependencies = dependencies.toSet(); + expect(dependencies.length, equals(uniqueDependencies.length)); + }); + }