コード例 #1
0
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
コード例 #2
0
 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))
コード例 #3
0
    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)
コード例 #4
0
 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)
コード例 #5
0
    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))
コード例 #6
0
 def test_eq_hash_1(self):
     self.assertIsEqual(Clause(1), self.clause1)
コード例 #7
0
 def test_type_error_6(self):
     self.assertTypeError(lambda: Clause(None))
コード例 #8
0
 def test_eq_2(self):
     self.assertIsEqual(ConjunctiveNormalForm(Clause(1)), self.cnf2)
コード例 #9
0
 def test_contains_3(self):
     self.assertMembership(
         for_container=self.cnf3,
         elements=[Clause(1), Clause(1, 2)],
         not_elements=[Clause(-4, -2)])
コード例 #10
0
 def test_eq_hash_6(self):
     self.assertIsEqual(Clause(-1, -2), self.clause6)
コード例 #11
0
 def test_eq_hash_4(self):
     self.assertIsEqual(Clause(-1, 2), self.clause4)
コード例 #12
0
 def test_type_error_1(self):
     self.assertTypeError(lambda: Clause('foo'))
コード例 #13
0
 def test_value_error_4(self):
     self.assertValueError(lambda: Clause(0, -1, 2))
コード例 #14
0
 def test_value_error_2(self):
     self.assertValueError(lambda: Clause(0, 1))
コード例 #15
0
 def test_empty_clause(self):
     self.assertTypeError(lambda: Clause())
コード例 #16
0
 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))
コード例 #17
0
 def test_eq_4(self):
     self.assertIsEqual(ConjunctiveNormalForm(Clause(-1, -2), Clause(-1, -2)), self.cnf4)
コード例 #18
0
 def test_eq_3(self):
     self.assertIsEqual(ConjunctiveNormalForm(Clause(1), Clause(1, 2)), self.cnf3)
コード例 #19
0
 def test_eq_hash_2(self):
     self.assertIsEqual(Clause(-1), self.clause2)
コード例 #20
0
 def test_eq_hash_3(self):
     self.assertIsEqual(Clause(1, 2), self.clause3)
コード例 #21
0
 def test_type_error_2(self):
     self.assertTypeError(lambda: Clause(True))
コード例 #22
0
 def test_eq_hash_5(self):
     self.assertIsEqual(Clause(1, -2), self.clause5)
コード例 #23
0
 def test_contains_2(self):
     self.assertMembership(
         for_container=self.cnf2,
         elements=[Clause(1)],
         not_elements=[Clause(1, 2)])
コード例 #24
0
 def __init__(self, clause: Clause, props: Dict[int, Property]):
     self._clause = clause.deepcopy()
     self._props = copy_property_map(props)
コード例 #25
0
 def test_type_error_5(self):
     self.assertTypeError(lambda: Clause(1.4))
コード例 #26
0
 def test_type_error_3(self):
     self.assertTypeError(lambda: Clause({}))
コード例 #27
0
 def test_contains_4(self):
     self.assertMembership(
         for_container=self.cnf4,
         elements=[Clause(-1, -2)],
         not_elements=[Clause(1)])
コード例 #28
0
 def test_type_error_4(self):
     self.assertTypeError(lambda: Clause([]))
コード例 #29
0
 def test_contains_1(self):
     self.assertMembership(
         for_container=self.cnf1,
         elements=[Clause(1, 2), Clause(3, 4)],
         not_elements=[Clause(1)])