From d208bf8102bea5212027efc71e51b4dbddfda0f9 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 10 Oct 2023 11:26:46 +0200 Subject: Implement loading of set products in SolFiniteSetRepresentation --- src/act4e_solutions/sets_representation.py | 34 ++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/act4e_solutions/sets_representation.py b/src/act4e_solutions/sets_representation.py index 64d958a..32931da 100644 --- a/src/act4e_solutions/sets_representation.py +++ b/src/act4e_solutions/sets_representation.py @@ -1,6 +1,7 @@ from typing import Any, Collection, Iterator, List, TypeVar, cast import act4e_interfaces as I +from .sets_product import MyFiniteSetProduct E = TypeVar("E") @@ -18,7 +19,7 @@ class MyFiniteSet(I.FiniteSet[E]): def elements(self) -> Iterator[E]: return iter(self._elements) - def save(self, h: I.IOHelper, x: E) -> I.ConcreteRepr: + def save(self, h: I.IOHelper, x: E) -> List[E]: return cast(I.ConcreteRepr, x) def load(self, h: I.IOHelper, o: I.ConcreteRepr): @@ -30,14 +31,33 @@ class SolFiniteSetRepresentation(I.FiniteSetRepresentation): if not isinstance(data, dict): raise I.InvalidFormat() - if not "elements" in data: - raise I.InvalidFormat() + if "elements" in data: + if not isinstance(data["elements"], list): + raise I.InvalidFormat() - if not isinstance(data["elements"], list): - raise I.InvalidFormat() + elements = data["elements"] + return MyFiniteSet(elements) + + elif "product" in data: + if not isinstance(data["product"], list): + raise I.InvalidFormat() + + components = [] + for comp in data["product"]: + if not isinstance(comp, dict): + raise I.InvalidFormat() - elements = data["elements"] - return MyFiniteSet(elements) + if not "elements" in comp: + raise I.InvalidFormat() + + if not isinstance(comp["elements"], list): + raise I.InvalidFormat() + + components.append(MyFiniteSet(comp["elements"])) + return MyFiniteSetProduct(components) + + else: + raise I.InvalidFormat() def save(self, h: I.IOHelper, f: I.FiniteSet[Any]) -> I.FiniteSet_desc: all_elements = [f.save(h, e) for e in f.elements()] -- cgit v1.2.1