def test_dnf_form() -> None: assert venn_from_str('1 & 2', int).to_dnf_set().is_equivalent_to( venn_from_str('1 & 2', int)) assert venn_from_str('1 | 2', int).to_dnf_set().is_equivalent_to( venn_from_str('1 | 2', int)) assert UniversalSet().to_dnf_set().is_equivalent_to(UniversalSet()) assert EmptySet().to_dnf_set().is_equivalent_to(EmptySet())
def test_superset_subset_for_unary_sets() -> None: # UniversalSet == UniversalSet assert UniversalSet().is_superset_of(UniversalSet()) assert UniversalSet().is_subset_of(UniversalSet()) # UniversalSet contains all other sets assert UniversalSet().is_superset_of(EmptySet()) assert UniversalSet().is_superset_of(ValueSet(1)) # EmptySet is contained in all sets assert EmptySet().is_subset_of(EmptySet()) assert EmptySet().is_superset_of(EmptySet()) assert EmptySet().is_subset_of(ValueSet(2)) # PropertySets <-> PropertySets assert ValueSet(2).is_superset_of(ValueSet(2)) assert ValueSet(2).is_subset_of(ValueSet(2)) assert not ValueSet(2).is_subset_of(ValueSet(3)) assert not ValueSet(2).is_superset_of(ValueSet(3)) # PropertySet <-> UniversalSet assert ValueSet(1).is_subset_of(UniversalSet()) assert not UniversalSet().is_subset_of(ValueSet(1)) assert UniversalSet().is_superset_of(ValueSet(1)) assert not ValueSet(1).is_superset_of(UniversalSet()) # PropertySet <-> EmptySet assert ValueSet(2).is_superset_of(EmptySet()) assert not EmptySet().is_superset_of(ValueSet(2)) assert EmptySet().is_subset_of(ValueSet(1)) assert not ValueSet(1).is_subset_of(EmptySet())
def test_nary_sets_constructor() -> None: assert Union() == EmptySet() assert Intersection() == UniversalSet() assert DisjunctiveUnion() == EmptySet() assert Union(venn_from_str('1', int)).is_equivalent_to(venn_from_str('1', int)) assert Intersection(venn_from_str('1', int)).is_equivalent_to(venn_from_str('1', int)) assert DisjunctiveUnion(venn_from_str('1', int)).is_equivalent_to(venn_from_str('1', int))
def test_is_equivalent_to() -> None: assert UniversalSet().is_equivalent_to(UniversalSet()) assert EmptySet().is_equivalent_to(EmptySet()) assert not UniversalSet().is_equivalent_to(ValueSet(1)) assert not ValueSet(1).is_equivalent_to(UniversalSet()) assert UniversalSet().is_equivalent_to(Union(ValueSet(1), Complement(ValueSet(1))))
def test_nested_list_form() -> None: assert venn_from_str('1', int).to_dnf_nested_list() == [[venn_from_str('1', int)]] x = venn_from_str('1 & ( 2 | 3 )', int) assert [ValueSet(1), ValueSet(2)] in x.to_dnf_nested_list() assert [ValueSet(1), ValueSet(3)] in x.to_dnf_nested_list() assert venn_from_str('1 & 2', int).to_dnf_nested_list() == [[ValueSet(1), ValueSet(2)]] assert venn_from_str('1 | 2', int).to_dnf_nested_list() == [[ValueSet(1)], [ValueSet(2)]] assert UniversalSet().to_dnf_nested_list() == [[UniversalSet()]] assert EmptySet().to_dnf_nested_list() == [[EmptySet()]]
def test_list_form() -> None: assert venn_from_str('1', int).to_dnf_list() == [venn_from_str('1', int)] assert venn_from_str('1 & 2', int).to_dnf_list() == [venn_from_str('1 & 2', int)] assert set(venn_from_str('1 | 2', int).to_dnf_list()) == {ValueSet(1), ValueSet(2)} x = venn_from_str('1 & ( 2 | 3 )', int) assert any(elem.is_equivalent_to(venn_from_str('1 & 2', int)) for elem in x.to_dnf_list()) assert any(elem.is_equivalent_to(venn_from_str('1 & 3', int)) for elem in x.to_dnf_list()) assert UniversalSet().to_dnf_list() == [UniversalSet()] assert EmptySet().to_dnf_list() == [EmptySet()]
def test_union_properties(sets: List[Set]) -> None: for x in sets: assert UniversalSet().is_equivalent_to(Union(x, Complement(x))) assert UniversalSet().is_equivalent_to(Union(Complement(x), x)) assert Union(x, Complement(x)).is_equivalent_to(UniversalSet()) assert Union(Complement(x), x).is_equivalent_to(UniversalSet()) assert x.is_equivalent_to(Union(EmptySet(), x)) assert x.is_equivalent_to(Union(x, EmptySet())) assert UniversalSet().is_equivalent_to(Union(UniversalSet(), x)) assert UniversalSet().is_equivalent_to(Union(x, UniversalSet())) assert x.is_equivalent_to(Union(x, x))
def synthesis_factor(state_target: StateTarget) -> VennSet[Target]: fac = EmptySet() # type: VennSet[Target] for rxn in (x for x in reaction_targets if x.synthesises(state_target)): fac = Union(fac, ValueSet(rxn)) for prod_rxn in (x for x in reaction_targets if x.produces(state_target)): sources = [] for source in prod_rxn.consumed_targets: sources.append( [source] + [x for x in reaction_targets if x.synthesises(source)]) for source_combi in product(*sources): # At least one source should be synthesised. if all(isinstance(x, StateTarget) for x in source_combi): continue assert any( isinstance(x, ReactionTarget) and x.synthesised_targets for x in source_combi) fac = Union( fac, Intersection(ValueSet(prod_rxn), *(ValueSet(x) for x in source_combi))) return fac
def kappa(state_target: StateTarget, level: int) -> VennSet[Target]: res = EmptySet() # type: VennSet[Target] for r in (x for x in reaction_targets if x.consumes(state_target)): rxn_term = ValueSet(r) # type: VennSet[Target] for s in (x for x in state_targets if r.consumes(x)): rxn_term = Intersection(rxn_term, ValueSet(s), degradation_factor(s)) res = Union(res, rxn_term) return res
def sets() -> List[Set]: return [ EmptySet(), ValueSet(1), UniversalSet(), Union(ValueSet(1), ValueSet(2)), Intersection(ValueSet(1), ValueSet(2)), Intersection(ValueSet(1), Complement(ValueSet(2))), Union(Intersection(ValueSet(1), ValueSet(2)), ValueSet(3)), Union(Intersection(ValueSet(1), ValueSet(2)), Intersection(ValueSet(3), ValueSet(4))), Union(Complement(Union(ValueSet(1), Complement(ValueSet(2)))), Intersection(ValueSet(3), ValueSet(4))) ]
def pi(state_target: StateTarget, level: int) -> VennSet[Target]: res = EmptySet() # type: VennSet[Target] for r in (x for x in reaction_targets if x.produces(state_target)): rxn_term = ValueSet(r) # type: VennSet[Target] for s in (x for x in state_targets if r.consumes(x)): if r.degraded_targets: state_term = ValueSet(s) # type: VennSet[Target] else: state_term = Intersection(ValueSet(s), degradation_factor(s)) for l in range(level): state_term = Union(state_term, sigma(s, level - 1)) rxn_term = Intersection(rxn_term, state_term) res = Union(res, rxn_term) return res