FormulaElement as abstract class
This commit is contained in:
parent
f70d744ef7
commit
6133e58226
5 changed files with 19 additions and 13 deletions
3
TODO.md
3
TODO.md
|
|
@ -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
|
||||
|
|
|
|||
2
flutterw
2
flutterw
|
|
@ -1,7 +1,7 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
set -x
|
||||
#set -x
|
||||
|
||||
BUILDCACHE=./.build-container-cache
|
||||
DOCKERFILE=./docker/Dockerfile
|
||||
|
|
|
|||
|
|
@ -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 = [];
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,21 +38,21 @@ 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
|
||||
// Formulas typically have 'd4rtCode' and 'input'/'output' properties
|
||||
if (element.containsKey('d4rtCode')) {
|
||||
result.add(Formula.fromSet(element));
|
||||
}
|
||||
}
|
||||
// Units typically have 'name', 'symbol', and 'baseUnit' properties
|
||||
else if (element.containsKey('name') && element.containsKey('symbol')) {
|
||||
result.add(UnitSpec.fromSet(element));
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw ArgumentError('Unknown element type: $element');
|
||||
}
|
||||
|
|
@ -60,14 +60,16 @@ List<Object> parseCorpusElements(String arrayStringLiteral) {
|
|||
throw ArgumentError('Element must be a Map: $element');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue