def _merge(left: Dict[int, Property], right: Dict[int, Property], clauses: List[Clause]): """ Merges this Property with the other property map without changing this one. """ remap = {} new_properties = copy_property_map(left) next_key = max(new_properties.keys()) for (other_key, other_value) in right.items(): if other_value in new_properties.values(): for (self_key, self_value) in new_properties.items(): if self_value == other_value: remap[other_key] = self_key break else: next_key += 1 new_properties[next_key] = other_value remap[other_key] = next_key # construct the new other clauses def new_literal(i): a = abs(i) if a not in remap: return i v = remap[a] return -v if i < 0 else v other_clauses = [Clause(*{new_literal(i) for i in clause}) for clause in clauses] return new_properties, other_clauses
def test_invalid_train_description_5(self): self.assertValueError(lambda: TrainDescription( train_name='foo', train_version='1.0', properties={ 1: url_by_name('FOO'), 2: token_by_name('BAR'), 3: enum_by_name('BAZ', choices=['VALUE1', 'VALUE2']) }, formulas=[ ConjunctiveNormalForm( Clause(1, -3), Clause(4), ) ], model_summary='model summary', algorithm_requirement=None))
def __or__(self, other): if not isinstance(other, DisjunctionBuilder): raise ValueError( 'Cannot \'or\' CnfBuilder and class {}. Must and to CnfBuilder.'.format(other.__class__)) new_properties, other_clauses = _merge(self.props, other.props, [other.clause]) new_clause = Clause(*{i for i in self.clause}.union(other_clauses[0])) return DisjunctionComposite(new_clause, new_properties)
def setUp(self): self.clause1 = Clause(1) self.clause2 = Clause(-1) self.clause3 = Clause(1, 2) self.clause4 = Clause(-1, 2) self.clause5 = Clause(1, -2) self.clause6 = Clause(-1, -2)
def setUp(self): self.maxDiff = None self.td1 = TrainDescription( train_name='test_train', train_version='1.0', properties={ 1: url_by_name('FOO'), 2: token_by_name('BAR'), 3: enum_by_name('BAZ', choices=['VALUE1', 'VALUE2']) }, formulas=[ConjunctiveNormalForm(Clause(1))], model_summary='model summary', algorithm_requirement=FormulaAlgorithmRequirement(1)) self.td2 = TrainDescription( train_name='test_train', train_version='1.0', properties={ 1: url_by_name('FOO'), 2: token_by_name('BAR'), 3: enum_by_name('BAZ', choices=['VALUE1', 'VALUE2']) }, formulas=[ConjunctiveNormalForm(Clause(1))], model_summary='model summary', algorithm_requirement=None) self.td3 = TrainDescription( train_name='test_train', train_version='1.0', properties={ 1: url_by_name('FOO'), 2: token_by_name('BAR'), 3: enum_by_name('BAZ', choices=['VALUE1', 'VALUE2']) }, formulas=[ ConjunctiveNormalForm( Clause(1), Clause(-2, 1, 3), Clause(3, 1, -2, -3) ), ConjunctiveNormalForm( Clause(3, 1), Clause(2, 1, 3, -3) ), ConjunctiveNormalForm( Clause(-1) ) ], model_summary='model summary', algorithm_requirement=FormulaAlgorithmRequirement(3))
def test_eq_hash_1(self): self.assertIsEqual(Clause(1), self.clause1)
def test_type_error_6(self): self.assertTypeError(lambda: Clause(None))
def test_eq_2(self): self.assertIsEqual(ConjunctiveNormalForm(Clause(1)), self.cnf2)
def test_contains_3(self): self.assertMembership( for_container=self.cnf3, elements=[Clause(1), Clause(1, 2)], not_elements=[Clause(-4, -2)])
def test_eq_hash_6(self): self.assertIsEqual(Clause(-1, -2), self.clause6)
def test_eq_hash_4(self): self.assertIsEqual(Clause(-1, 2), self.clause4)
def test_type_error_1(self): self.assertTypeError(lambda: Clause('foo'))
def test_value_error_4(self): self.assertValueError(lambda: Clause(0, -1, 2))
def test_value_error_2(self): self.assertValueError(lambda: Clause(0, 1))
def test_empty_clause(self): self.assertTypeError(lambda: Clause())
def setUp(self): self.cnf1 = ConjunctiveNormalForm(Clause(3, 4), Clause(1, 2)) self.cnf2 = ConjunctiveNormalForm(Clause(1)) self.cnf3 = ConjunctiveNormalForm(Clause(1), Clause(1, 2)) self.cnf4 = ConjunctiveNormalForm(Clause(-1, -2), Clause(-1, -2))
def test_eq_4(self): self.assertIsEqual(ConjunctiveNormalForm(Clause(-1, -2), Clause(-1, -2)), self.cnf4)
def test_eq_3(self): self.assertIsEqual(ConjunctiveNormalForm(Clause(1), Clause(1, 2)), self.cnf3)
def test_eq_hash_2(self): self.assertIsEqual(Clause(-1), self.clause2)
def test_eq_hash_3(self): self.assertIsEqual(Clause(1, 2), self.clause3)
def test_type_error_2(self): self.assertTypeError(lambda: Clause(True))
def test_eq_hash_5(self): self.assertIsEqual(Clause(1, -2), self.clause5)
def test_contains_2(self): self.assertMembership( for_container=self.cnf2, elements=[Clause(1)], not_elements=[Clause(1, 2)])
def __init__(self, clause: Clause, props: Dict[int, Property]): self._clause = clause.deepcopy() self._props = copy_property_map(props)
def test_type_error_5(self): self.assertTypeError(lambda: Clause(1.4))
def test_type_error_3(self): self.assertTypeError(lambda: Clause({}))
def test_contains_4(self): self.assertMembership( for_container=self.cnf4, elements=[Clause(-1, -2)], not_elements=[Clause(1)])
def test_type_error_4(self): self.assertTypeError(lambda: Clause([]))
def test_contains_1(self): self.assertMembership( for_container=self.cnf1, elements=[Clause(1, 2), Clause(3, 4)], not_elements=[Clause(1)])