FormulaElement as abstract class

This commit is contained in:
Your Name 2026-02-11 09:07:13 +01:00
parent f70d744ef7
commit 6133e58226
5 changed files with 19 additions and 13 deletions

View file

@ -17,7 +17,8 @@
- Drift files have a lot of duplicate code. "web" version is the same as native version, only _openConnection is diferrent. Refactor to not duplicate code.
- [X] Create Formula.toStringLiteral. It is the reverse of Formula.fromSet( Formula.fromArrayStringLiteral(string)[0] )
- [X] Create UnitSpec.toStringLiteral, like Formula.toStringLiteral
- [ ] Make Formula and UnitSpec subclasses of FormulaElement. Change return type of functions that return Object to FormulaElement if necessary.
- [X] Make Formula and UnitSpec subclasses of FormulaElement. Change return type of functions that return Object to FormulaElement if necessary.
- [ ] Define toStringLiteral in FormulaElement.
- Database file location:
- [ ] In linux, the sqlite database file will be located following rules at https://specifications.freedesktop.org/basedir/latest/
- [ ] In Windows, the sqlite database file will be in %appdata%/Roaming

View file

@ -1,7 +1,7 @@
#!/bin/bash
set -e
set -x
#set -x
BUILDCACHE=./.build-container-cache
DOCKERFILE=./docker/Dockerfile

View file

@ -1,6 +1,7 @@
import 'package:d4rt/d4rt.dart';
import 'package:collection/collection.dart';
import 'package:d4rt_formulas/d4rt_formulas.dart';
import 'formula_models.dart';
class Multimap<K, V> extends DelegatingMap<K, List<V>> {
final Map<K, List<V>> _map;
@ -182,7 +183,7 @@ class Corpus{
/// Loads formula elements, making sure to load units first, then formulas
/// to avoid dependency issues.
void loadFormulaElements(List<Object> elements) {
void loadFormulaElements(List<FormulaElement> elements) {
List<UnitSpec> units = [];
List<Formula> formulas = [];

View file

@ -39,7 +39,8 @@ Future<Corpus> createDefaultCorpus() async{
print( "Loading units from $unitRes");
final literal = await loadResourceAsString(unitRes);
final units = UnitSpec.fromArrayStringLiteral(literal);
corpus.loadFormulaElements(units);
final formulaElements = units.cast<FormulaElement>();
corpus.loadFormulaElements(formulaElements);
}
}
@ -49,7 +50,8 @@ Future<Corpus> createDefaultCorpus() async{
for (final formRes in formulaResources) {
final literal = await loadResourceAsString(formRes);
final formulas = Formula.fromArrayStringLiteral(literal);
corpus.loadFormulaElements(formulas);
final formulaElements = formulas.cast<FormulaElement>();
corpus.loadFormulaElements(formulaElements);
}
}

View file

@ -38,10 +38,10 @@ List<Object?> parseD4rtLiteral(String arrayStringLiteral) {
/// Parses corpus elements from an array string literal.
/// Determines if each element is a formula or a unit and converts accordingly.
List<Object> parseCorpusElements(String arrayStringLiteral) {
List<FormulaElement> parseCorpusElements(String arrayStringLiteral) {
final List<Object?> elements = parseD4rtLiteral(arrayStringLiteral);
final List<Object> result = [];
final List<FormulaElement> result = [];
for (final element in elements) {
if (element is Map<Object?, Object?>) {
// Check if it's a formula by looking for required formula properties
@ -66,8 +66,10 @@ List<Object> parseCorpusElements(String arrayStringLiteral) {
typedef Number = double;
/// Abstract base class for formula elements
abstract class FormulaElement {}
class UnitSpec {
class UnitSpec extends FormulaElement {
final String name;
final String baseUnit;
final String symbol;
@ -217,7 +219,7 @@ class VariableSpec {
}
}
class Formula {
class Formula extends FormulaElement {
final String name;
final String? description;
final List<VariableSpec> input;