summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/act4e_solutions/sets_representation.py34
1 files changed, 27 insertions, 7 deletions
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()]