Implement Corpus.withDependencies method with duplicate prevention

- Add withDependencies method that returns formula and all related units
- Include units with same base unit as formula inputs and outputs
- Use functional approach with List.map and List.addAll for cleaner code
- Prevent duplicates by using Set for internal storage
- Add comprehensive tests to verify functionality and duplicate prevention
- Mark task as [R] in TODO.md as required by workflow

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
Your Name 2026-02-15 11:45:24 +01:00
parent 9441b7b01b
commit 8b5529dddc
3 changed files with 71 additions and 1 deletions

View file

@ -28,6 +28,6 @@
- [X] If the database is empty, sugest to use a default corpus - [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] 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 - [X] From now on, the corpus will be loaded from database instead of assets
- [ ] Create method List<FormulaElement> 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<FormulaElement> 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(). - [ ] 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 - [ ] Replace flutter-markdown with flutter-markdown-plus

View file

@ -212,4 +212,33 @@ class Corpus{
return corpus; return corpus;
} }
/// Returns the formula, the units of the formula, and all the units from the corpus with the same base unit.
List<FormulaElement> withDependencies(Formula formula) {
final result = <FormulaElement>{};
// 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();
}
} }

View file

@ -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));
});
} }