From 1b570e45a4e99a4e7f9ad9d01b4fa93e38fbff38 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Sun, 18 Dec 2022 13:45:35 +0100 Subject: schema ordering --- bsfs/schema/schema.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'bsfs') diff --git a/bsfs/schema/schema.py b/bsfs/schema/schema.py index 0e053c0..b6f37a7 100644 --- a/bsfs/schema/schema.py +++ b/bsfs/schema/schema.py @@ -103,6 +103,41 @@ class Schema(): SchemaDiff = namedtuple('SchemaDiff', ['nodes', 'literals', 'predicates']) + def _issubset(self, other: 'Schema') -> bool: + # inconsistent schema can't be ordered. + if not self.consistent_with(other): + return False + # since schemas are consistent, it's sufficient to compare their URIs. + # self's sets are fully contained in other's sets + # pylint: disable=protected-access + return set(self._predicates) <= set(other._predicates) \ + and set(self._nodes) <= set(other._nodes) \ + and set(self._literals) <= set(other._literals) + + def __lt__(self, other: typing.Any) -> bool: + """Return True if *other* is a true subset of *self*.""" + if not isinstance(other, Schema): # other is not a Schema + return NotImplemented + return self != other and self._issubset(other) + + def __le__(self, other: typing.Any) -> bool: + """Return True if *other* is a subset of *self*.""" + if not isinstance(other, Schema): # other is not a Schema + return NotImplemented + return self == other or self._issubset(other) + + def __gt__(self, other: typing.Any) -> bool: + """Return True if *other* is a true superset of *self*.""" + if not isinstance(other, Schema): # other is not a Schema + return NotImplemented + return self != other and other._issubset(self) + + def __ge__(self, other: typing.Any) -> bool: + """Return True if *other* is a superset of *self*.""" + if not isinstance(other, Schema): # other is not a Schema + return NotImplemented + return self == other or other._issubset(self) + def diff(self, other: 'Schema') -> SchemaDiff: """Return node, literals, and predicates that are in *self* but not in *other*.""" return self.SchemaDiff( -- cgit v1.2.3