def test___deepcopy__():
    """Test copy.deepcopy for AssumptionBase object."""
    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])
    vocabulary2 = Vocabulary(
        ['C1', 'C2', 'C3'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a = AssumptionBase(f1, f2, f3, f4)

    from copy import deepcopy
    a_copy = deepcopy(a)

    assert a == a_copy
    assert a is not a_copy
    assert a._vocabulary is a_copy._vocabulary
    assert a._formulae[0] is not a_copy._formulae[0]
    assert a._formulae[1] is not a_copy._formulae[1]
    assert a._formulae[2] is not a_copy._formulae[2]
    assert a._formulae[3] is not a_copy._formulae[3]
Exemple #2
0
def test___ne__():
    """Test != operator for Formula object."""
    def test_TypeError(f1, f2):
        """Test TypeError catching in != operator of Formula."""
        with pytest.raises(TypeError) as excinfo:
            f1 != f2

    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, pm_rs],
                            ['V1', 'V2'])
    vocabulary2 = Vocabulary(['C1', 'C2', 'C3'], [ahead_rs, behind_rs, pm_rs],
                             ['V1', 'V2'])

    f = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f1 = Formula(vocabulary, 'Ahead', 'C1')
    f2 = Formula(vocabulary, 'Ahead', 'V1', 'C1')
    f3 = Formula(vocabulary, 'Ahead', 'V1', 'C1', 'V1', 'C1')
    f4 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f5 = Formula(vocabulary2, 'Ahead', 'C1', 'V1')

    test_TypeError(f, None)
    test_TypeError(f, object)

    assert not f != f
    assert f != f1
    assert not f != f2
    assert not f != f3
    assert f != f4
    assert f != f5
def test___le__():
    """Test overloaded <= subset operator for ConstantAssignment."""
    vocabulary1 = Vocabulary(['C1', 'C2', 'C3'], [RelationSymbol('R', 1)],
                             ['V'])
    vocabulary2 = Vocabulary(['C\''], [RelationSymbol('R\'', 1)], ['V\''])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr1 = AttributeStructure(a, b)
    astr2 = AttributeStructure(b)
    objs1 = ['a', 'b', 'c']
    objs2 = ['a']
    attribute_system1 = AttributeSystem(astr1, objs1)
    attribute_system2 = AttributeSystem(astr2, objs2)

    mapping1 = {'C1': 'a'}
    mapping2 = {'C1': 'a', 'C2': 'b'}
    mapping3 = {'C1': 'a', 'C2': 'b', 'C3': 'c'}
    mapping4 = {'C\'': 'a'}
    mapping5 = {'C1': 'b'}

    CA1 = ConstantAssignment(vocabulary1, attribute_system1, mapping1)
    CA2 = ConstantAssignment(vocabulary1, attribute_system1, mapping2)
    CA3 = ConstantAssignment(vocabulary1, attribute_system1, mapping3)
    CA4 = ConstantAssignment(vocabulary2, attribute_system2, mapping4)
    CA5 = ConstantAssignment(vocabulary1, attribute_system1, mapping5)

    assert CA1 <= CA1
    assert CA1 <= CA2
    assert CA1 <= CA2 <= CA3
    assert not CA1 <= CA4
    assert not CA4 <= CA1
    assert not CA5 <= CA2
def test___ne__():
    """Test != operator for ConstantAssignment object."""
    vocabulary1 = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V'])
    vocabulary2 = Vocabulary(['C\''], [RelationSymbol('R\'', 1)], ['V\''])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr1 = AttributeStructure(a, b)
    astr2 = AttributeStructure(b)
    objs1 = ['a', 'b', 'c']
    objs2 = ['a']
    attribute_system1 = AttributeSystem(astr1, objs1)
    attribute_system2 = AttributeSystem(astr2, objs2)

    mapping1 = {'C': 'a'}
    mapping2 = {'C': 'b'}

    A1 = ConstantAssignment(vocabulary1, attribute_system1, mapping1)
    A2 = ConstantAssignment(vocabulary1, attribute_system1, mapping1)
    A3 = ConstantAssignment(vocabulary2, attribute_system1, {})
    A4 = ConstantAssignment(vocabulary1, attribute_system2, {})
    A5 = ConstantAssignment(vocabulary1, attribute_system1, mapping2)

    assert not A1 != A1
    assert not A1 != A2
    assert A1 != A3
    assert A1 != A4
    assert A1 != A5
def test___ne__():
    """Test != operator for AssumptionBase objects."""
    def test_TypeError(self, other):
        """Test TypeError catching in != operator for AssumptionBase."""
        with pytest.raises(TypeError) as excinfo:
            self != other

    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])
    vocabulary2 = Vocabulary(
        ['C1', 'C2', 'C3'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a1 = AssumptionBase(f1, f2, f3, f4)
    a2 = AssumptionBase(f2, f4, f1, f3)
    a3 = AssumptionBase(f2, f1, f3)
    a_empty_1 = AssumptionBase(vocabulary)
    a_empty_2 = AssumptionBase(vocabulary2)

    test_TypeError(a1, None)
    test_TypeError(a1, f1)
    assert not a1 != a1
    assert a1 is a1
    assert not a1 != a2
    assert a1 is not a2
    assert a1 != a3
    assert a_empty_1 != a_empty_2
def test___hash__():
    """Test hash(Vocabulary)."""
    C, R, V = ['C1', 'C2'], [RelationSymbol('R', 1)], ['V']
    vocabulary = Vocabulary(C, R, V)
    C, R, V = ['C2', 'C1'], [RelationSymbol('R', 1)], ['V']
    vocabulary2 = Vocabulary(C, R, V)

    assert hash(vocabulary) == hash(vocabulary2)
def test___repr__():
    """Test repr(Vocabulary)."""
    C = ["C"]
    R = [RelationSymbol("R", 1)]
    V = ["V"]
    vocab = Vocabulary(C, R, V)
    assert vocab.__repr__() == "([C], [R], [V])"

    vocab_empty = Vocabulary([], [], [])
    assert vocab_empty.__repr__() == "([], [], [])"
Exemple #8
0
def test__key():
    """Test key for hash function."""
    C, R, V = ['C'], [RelationSymbol('R', 1)], ['V']
    vocabulary = Vocabulary(C, R, V)

    formula = Formula(vocabulary, 'R', 'C', 'V')
    assert (hash(vocabulary), 'R', ('C', 'V')) == formula._key()
Exemple #9
0
def test___repr__():
    """Test repr(AttributeInterpretation)."""
    a = Attribute('hour', ['0,...,23'])
    a2 = Attribute('minute', ['0,...,59'])
    r_ahead = Relation('R1(h1,m1,h2,m2) <=> h1 > h2 or (h1 = h2 and m1 > m2)',
                       ['hour', 'minute', 'hour', 'minute'], 1)
    r_behind = Relation('R2(h1,m1,h2,m2) <=> h1 < h2 or (h1 = h2 and m1 < m2)',
                        ['hour', 'minute', 'hour', 'minute'], 2)
    r_pm = Relation('R3(h1) <=> h1 > 12', ['hour'], 3)
    r_am = Relation('R4(h1) <=> h1 < 12', ['hour'], 4)
    attribute_structure = AttributeStructure(
        a, a2, r_ahead, r_behind, r_pm, r_am)

    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, pm_rs], ['V1', 'V2'])

    profiles = [
        [ahead_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]
    ]

    mapping = {ahead_rs: 1, behind_rs: 2, pm_rs: 3}

    ai = AttributeInterpretation(
        vocabulary, attribute_structure, mapping, profiles)
    assert repr(ai) == "[Ahead, 4, 'R1', [('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)]]\n" + \
                       "[Behind, 4, 'R2', [('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)]]\n" + \
                       "[PM, 1, 'R3', [('hour', 1)]]"
def test___init__():
    """Test VariableAssignment constructor."""
    def test_TypeError(vocabulary, attribute_system, mapping):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            VariableAssignment(vocabulary, attribute_system, mapping)

    def test_ValueError(vocabulary, attribute_system, mapping):
        """Test constructor for ValueErrors with given params."""
        with pytest.raises(ValueError) as excinfo:
            VariableAssignment(vocabulary, attribute_system, mapping)

    vocabulary = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V'])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr = AttributeStructure(a, b)
    objs = ['a', 'b', 'c']
    attribute_system = AttributeSystem(astr, objs)

    test_TypeError(vocabulary, attribute_system, {'V': 1})
    test_TypeError(vocabulary, attribute_system, {1: 'a'})
    test_ValueError(vocabulary, attribute_system, {'V': 'bad'})
    test_ValueError(vocabulary, attribute_system, {'bad': 'a'})

    VA = VariableAssignment(vocabulary, attribute_system, {'V': 'a'})
def test___getitem__():
    """Test indexing for VariableAssignment object."""
    def test_TypeError(variable_assignment, key):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            variable_assignment[key]

    def test_KeyError(variable_assignment, key):
        """Test constructor for KeyErrors with given params."""
        with pytest.raises(KeyError) as excinfo:
            variable_assignment[key]

    vocabulary = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V'])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr = AttributeStructure(a, b)
    objs = ['a', 'b', 'c']
    attribute_system = AttributeSystem(astr, objs)

    mapping = {'V': 'a'}

    VA = VariableAssignment(vocabulary, attribute_system, mapping)

    test_TypeError(VA, 1)
    test_TypeError(VA, None)
    test_TypeError(VA, object)
    test_KeyError(VA, '')

    assert VA['V'] == 'a'
Exemple #12
0
def test___init__():
    """Test Formula constructor."""
    def test_TypeError(vocabulary, name, *terms):
        """Test TypeError catching in Formula constructor."""
        with pytest.raises(TypeError) as excinfo:
            Formula(vocabulary, name, *terms)

    def test_ValueError(vocabulary, name, *terms):
        """Test ValueError catching in Formula constructor."""
        with pytest.raises(ValueError) as excinfo:
            Formula(vocabulary, name, *terms)

    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, pm_rs],
                            ['V1', 'V2'])

    test_TypeError(None, 'Ahead', 'C1', 'V1')
    test_TypeError(object, 'Ahead', 'C1', 'V1')
    test_TypeError(vocabulary, None, 'C1', 'V1')
    test_TypeError(vocabulary, object, 'C1', 'V1')
    test_ValueError(vocabulary, 'Ahead')
    test_ValueError(vocabulary, 'Ahead', 'nope')

    F = Formula(vocabulary, 'Ahead', 'C1', 'C1', 'C1')
    assert F._terms == ['C1', 'C1', 'C1']
Exemple #13
0
def test___add__():
    """Test + operator for Formula object."""
    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs],
                            ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a1 = AssumptionBase(f1, f2)

    a = f1 + f2
    assert a._vocabulary is f1._vocabulary is f2._vocabulary
    a = f2 + f1
    assert a._vocabulary is f1._vocabulary is f2._vocabulary
    assert hasattr(a, "_is_AssumptionBase")
    a = f3 + a1
    assert a._vocabulary is a1._vocabulary is f3._vocabulary
    assert hasattr(a, "_is_AssumptionBase")

    a = f1 + f2 + f3 + f4
    assert a._vocabulary is f1._vocabulary is f2._vocabulary is f3._vocabulary \
        is f4._vocabulary
    assert hasattr(a, "_is_AssumptionBase")
    assert len(a) == 4
def test___iadd__():
    """Test + operator for AssumptionBase object."""
    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    test_rs = RelationSymbol('test', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'],
        [ahead_rs, behind_rs, am_rs, pm_rs, test_rs],
        ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a_1 = AssumptionBase(f1)
    a_2 = AssumptionBase(f2)
    a_3 = AssumptionBase(f3)
    a_1_2 = AssumptionBase(f1, f2)
    a_1_2_3 = AssumptionBase(f1, f2, f3)
    a_1_2_3_4 = AssumptionBase(f1, f2, f3, f4)

    a_1 += a_2
    assert a_1 == a_1_2
    assert a_1 is not a_1_2
    a_1 += (f3 + f4)
    assert a_1 == a_1_2_3_4
    assert a_1 is not a_1_2_3_4

    assert f1._vocabulary is f2._vocabulary is f3._vocabulary is \
        f4._vocabulary is a_1._vocabulary is a_2._vocabulary is \
        a_3._vocabulary is a_1_2._vocabulary is a_1_2_3._vocabulary is \
        a_1_2_3_4._vocabulary
def test___ne__():
    """Test != operator for Vocabulary."""
    C = ['C']
    R = [RelationSymbol('R', 1)]
    V = ['V']
    vocab = Vocabulary(C, R, V)
    vocab_copy = Vocabulary(C, R, V)

    C = ['C\'']
    R = [RelationSymbol('R\'', 1)]
    V = ['V\'']
    vocab_prime = Vocabulary(C, R, V)

    assert not vocab != vocab
    assert not vocab != vocab_copy
    assert vocab is not vocab_copy
    assert vocab != vocab_prime
Exemple #16
0
def test___hash__():
    """Test hash(Vocabulary)."""
    C, R, V = ['C'], [RelationSymbol('R', 1)], ['V']
    vocabulary = Vocabulary(C, R, V)

    formula1 = Formula(vocabulary, 'R', 'C', 'V')
    formula2 = Formula(vocabulary, 'R', 'V', 'C')

    assert hash(formula1) == hash(formula2)
def test___len__():
    """Test len(AssumptionBase)."""
    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])
    vocabulary2 = Vocabulary(
        ['C1', 'C2', 'C3'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a1 = AssumptionBase(f1, f2, f3, f4)

    assert len(AssumptionBase(vocabulary)) == 0
    assert len(a1) == 4
def test___contains__():
    """Test in operator for Vocabulary."""
    C = ['C']
    R = [RelationSymbol('R', 1)]
    V = ['V']
    vocabulary = Vocabulary(C, R, V)

    assert 'C' in vocabulary
    assert RelationSymbol('R', 1) in vocabulary
    assert 'V' in vocabulary
def test___getitem__():
    """Test indexing of AssumptionBase object."""
    def test_KeyError(assumption_base, key):
        """Test KeyError catching in AssumptionBase constructor."""
        with pytest.raises(KeyError) as excinfo:
            assumption_base[key]

    def test_IndexError(assumption_base, key):
        """Test IndexError catching in AssumptionBase constructor."""
        with pytest.raises(IndexError) as excinfo:
            assumption_base[key]

    def test_TypeError(assumption_base, key):
        """Test TypeError catching in AssumptionBase constructor."""
        with pytest.raises(TypeError) as excinfo:
            assumption_base[key]

    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])
    vocabulary2 = Vocabulary(
        ['C1', 'C2', 'C3'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a = AssumptionBase(f1, f2, f3)

    test_TypeError(a, None)
    test_TypeError(a, object)
    test_KeyError(a, 'AM')
    test_KeyError(a, f4)
    test_IndexError(a, 4)
    test_IndexError(a, 1000)

    assert a[f1] is f1
    assert a['Behind'] is f2
    assert a[2] is f3
Exemple #20
0
def test___repr__():
    """Test repr(Formula)."""
    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    vocabulary = Vocabulary(['C1', 'C2'], [ahead_rs, behind_rs, pm_rs],
                            ['V1', 'V2'])

    f = Formula(vocabulary, 'Ahead', 'C1', 'V1')

    assert repr(f) == "Ahead(C1, V1)"
def test___contains__():
    """Test in and not in operators for AssumptionBase object."""
    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])
    vocabulary2 = Vocabulary(
        ['C1', 'C2', 'C3'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a1 = AssumptionBase(f1, f2, f3)

    assert f1 in a1
    assert f2 in a1
    assert f3 in a1
    assert f4 not in a1
def test___iter__():
    """Test iterator for AssumptionBase."""
    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])
    vocabulary2 = Vocabulary(
        ['C1', 'C2', 'C3'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a1 = AssumptionBase(f1, f2, f3, f4)

    assert set([f1, f2, f3, f4]) == set([f for f in a1])
    assert set([f1, f2, f3, f4]) == set([f for f in iter(a1)])
    assert set([f1, f2, f3, f4]) == set(a1)
    assert set([f1, f2, f3, f4]) == set(iter(a1))
def test_is_total():
    """Test is_total function for ConstantAssignment object."""
    vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V'])
    vocabulary_total = Vocabulary(['C1', 'C2', 'C3'], [RelationSymbol('R', 1)],
                                  ['V'])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr = AttributeStructure(a, b)
    objs = ['a', 'b', 'c']
    attribute_system = AttributeSystem(astr, objs)

    mapping = {'C': 'a'}
    total_mapping = {'C1': 'a', 'C2': 'b', 'C3': 'c'}

    CA = ConstantAssignment(vocabulary, attribute_system, mapping)
    CA_total = ConstantAssignment(vocabulary_total, attribute_system,
                                  total_mapping)

    assert not CA.is_total()
    assert CA_total.is_total()
def test___init__():
    """Test AssumptionBase constructor."""
    def test_TypeError(*formulae):
        """Test TypeError catching in AssumptionBase constructor."""
        with pytest.raises(TypeError) as excinfo:
            AssumptionBase(*formulae)

    def test_ValueError(*formulae):
        """Test ValueError catching in AssumptionBase constructor."""
        with pytest.raises(ValueError) as excinfo:
            AssumptionBase(*formulae)

    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])
    vocabulary2 = Vocabulary(
        ['C1', 'C2', 'C3'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary2, 'AM', 'C1')
    a1 = AssumptionBase(f1, f2, f3)
    a_empty = AssumptionBase(vocabulary)

    test_TypeError(f1, None)
    test_TypeError(f1, object)
    test_ValueError(f1, f1)
    test_ValueError(f1, f4)

    assert a1[0] is f1
    assert a1[1] is f2
    assert a1[2] is f3
    assert a1._vocabulary is f1._vocabulary is f2._vocabulary is \
        f3._vocabulary is a_empty._vocabulary
def test___eq__():
    """Test == operator for VariableAssignment object."""
    vocabulary1 = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V'])
    vocabulary2 = Vocabulary(['C\''], [RelationSymbol('R\'', 1)], ['V\''])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr1 = AttributeStructure(a, b)
    astr2 = AttributeStructure(b)
    objs1 = ['a', 'b', 'c']
    objs2 = ['a']
    attribute_system1 = AttributeSystem(astr1, objs1)
    attribute_system2 = AttributeSystem(astr2, objs2)

    mapping1 = {'V': 'a'}
    mapping2 = {'V': 'b'}

    A1 = VariableAssignment(vocabulary1, attribute_system1, mapping1)
    A2 = VariableAssignment(vocabulary1, attribute_system1, mapping1)
    A3 = VariableAssignment(vocabulary1, attribute_system1, mapping2)

    assert A1 == A1
    assert A1 == A2
    assert not A1 == A3
def test___repr__():
    """Test repr(VariableAssignment)."""
    vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V'])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr = AttributeStructure(a, b)
    objs = ['a', 'b', 'c']
    attribute_system = AttributeSystem(astr, objs)

    mapping = {'V': 'a'}

    VA = VariableAssignment(vocabulary, attribute_system, mapping)

    assert VA.__repr__() == "VA{'V': 'a'}"
def test___deepcopy__():
    """Test copy.deepcopy for Vocabulary."""
    from copy import deepcopy

    C = ['C']
    R = [RelationSymbol('R', 1)]
    V = ['V']
    vocab = Vocabulary(C, R, V)
    vocab_copy = deepcopy(vocab)

    assert vocab == vocab_copy
    assert vocab is not vocab_copy
    assert vocab._C is not vocab_copy._C
    assert vocab._R is not vocab_copy._R
    assert vocab._V is not vocab_copy._V
def test___init__():
    """Test Assignment constructor."""
    def test_TypeError(vocabulary, attribute_system):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            Assignment(vocabulary, attribute_system)

    vocabulary = Vocabulary(['C'], [RelationSymbol('R', 1)], ['V'])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr = AttributeStructure(a, b)
    objs = ['a', 'b', 'c']
    attribute_system = AttributeSystem(astr, objs)

    # test type errors
    test_TypeError(None, attribute_system)
    test_TypeError('', attribute_system)
    test_TypeError(object, attribute_system)
    test_TypeError(vocabulary, None)
    test_TypeError(vocabulary, '')
    test_TypeError(vocabulary, object)

    # test reference keeping and breaking
    A = Assignment(vocabulary, attribute_system)
    assert vocabulary is A._vocabulary
    assert attribute_system is not A._attribute_system

    vocabulary.add_constant('cx')
    vocabulary.add_variable('vx')
    assert 'cx' in A._vocabulary._C
    assert 'vx' in A._vocabulary._V
    A._vocabulary.add_constant('cx2')
    A._vocabulary.add_variable('vx2')
    assert 'cx2' in vocabulary._C
    assert 'vx2' in vocabulary._V
Exemple #29
0
def test_add_variable():
    """Test add_variable() function for Vocabulary object."""

    def test_TypeError(vocabulary, v):
        """Test TypeErrors in add_constant."""
        with pytest.raises(TypeError) as excinfo:
            vocabulary.add_variable(v)

    def test_ValueError(vocabulary, v):
        """Test ValueErrors in add constant."""
        with pytest.raises(ValueError) as excinfo:
            vocabulary.add_variable(v)

    C, R, V = ["C"], [RelationSymbol("R", 1)], ["V"]
    vocab = Vocabulary(C, R, V)

    test_TypeError(vocab, None)
    test_TypeError(vocab, object)
    test_ValueError(vocab, "C")
    test_ValueError(vocab, "V")
    vocab.add_variable("x")
    assert vocab._V == ["V", "x"]
    vocab.add_variable("a")
    assert vocab._V == ["a", "V", "x"]
def test___repr__():
    """Test repr(Vocabulary)."""
    C = ['C']
    R = [RelationSymbol('R', 1)]
    V = ['V']
    vocab = Vocabulary(C, R, V)
    assert vocab.__repr__() == "([C], [R], [V])"

    vocab_empty = Vocabulary([], [], [])
    assert vocab_empty.__repr__() == "([], [], [])"
def test___repr__():
    """Test repr(AssumptionBase)."""
    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    am_rs = RelationSymbol('AM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, am_rs, pm_rs], ['V1', 'V2'])

    f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f3 = Formula(vocabulary, 'PM', 'C1')
    f4 = Formula(vocabulary, 'AM', 'C1')
    a1 = AssumptionBase(f1, f2, f3, f4)

    assert repr(a1) == "AB(AM(C1), Ahead(C1, V1), Behind(C1, V1), PM(C1))"
def test___repr__():
    """Test repr(ConstantAssignment)."""
    vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V'])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr = AttributeStructure(a, b)
    objs = ['a', 'b', 'c']
    attribute_system = AttributeSystem(astr, objs)

    mapping = {'C': 'a'}
    mapping2 = {'C': 'a', 'C\'': 'b'}

    CA = ConstantAssignment(vocabulary, attribute_system, mapping)
    CA2 = ConstantAssignment(vocabulary, attribute_system, mapping2)

    assert CA.__repr__() == "CA{'C': 'a'}"
    assert CA2.__repr__() == "CA{'C': 'a', \"C\'\": 'b'}"
def test_get_domain():
    """Test get_domain function for ConstantAssignment object."""
    vocabulary = Vocabulary(['C', 'C\''], [RelationSymbol('R', 1)], ['V'])

    a = Attribute("a", [])
    b = Attribute("b", [])
    astr = AttributeStructure(a, b)
    objs = ['a', 'b', 'c']
    attribute_system = AttributeSystem(astr, objs)

    mapping = {'C': 'a'}
    mapping2 = {'C': 'a', 'C\'': 'b'}

    CA = ConstantAssignment(vocabulary, attribute_system, mapping)
    CA2 = ConstantAssignment(vocabulary, attribute_system, mapping2)

    assert CA.get_domain() == ['C']
    assert CA2.get_domain() == ['C', "C'"]
def test___init__():
    """Test AttributeInterpretation constructor."""
    def test_TypeError(vocabulary, attribute_structure, mapping, profiles):
        """Test TypeError catching in AttributeInterpretation constructor."""
        with pytest.raises(TypeError) as excinfo:
            AttributeInterpretation(
                vocabulary, attribute_structure, mapping, profiles)

    def test_ValueError(vocabulary, attribute_structure, mapping, profiles):
        """Test ValueError catching in AttributeInterpretation constructor."""
        with pytest.raises(ValueError) as excinfo:
            AttributeInterpretation(
                vocabulary, attribute_structure, mapping, profiles)

    a = Attribute('hour', ['0,...,23'])
    a2 = Attribute('minute', ['0,...,59'])
    r_ahead = Relation('R1(h1,m1,h2,m2) <=> h1 > h2 or (h1 = h2 and m1 > m2)',
                       ['hour', 'minute', 'hour', 'minute'], 1)
    r_behind = Relation('R2(h1,m1,h2,m2) <=> h1 < h2 or (h1 = h2 and m1 < m2)',
                        ['hour', 'minute', 'hour', 'minute'], 2)
    r_pm = Relation('R3(h1) <=> h1 > 12', ['hour'], 3)
    r_am = Relation('R4(h1) <=> h1 < 12', ['hour'], 4)
    attribute_structure = AttributeStructure(
        a, a2, r_ahead, r_behind, r_pm, r_am)

    ahead_rs = RelationSymbol('Ahead', 4)
    behind_rs = RelationSymbol('Behind', 4)
    pm_rs = RelationSymbol('PM', 1)
    vocabulary = Vocabulary(
        ['C1', 'C2'], [ahead_rs, behind_rs, pm_rs], ['V1', 'V2'])

    profiles = [
        [ahead_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]]
    bad_profiles = [
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]]
    mapping = {ahead_rs: 1, behind_rs: 2, pm_rs: 3}
    bad_source_mapping = {ahead_rs: 1, "behind_rs": 2, pm_rs: 3}
    bad_target_mapping = {ahead_rs: 1, behind_rs: 'R2', pm_rs: 3}
    bad_target_mapping2 = {ahead_rs: 1, behind_rs: 2.0, pm_rs: 3}
    dup_subscr_mapping = {ahead_rs: 2, behind_rs: 2, pm_rs: 3}

    ai = AttributeInterpretation(
        vocabulary, attribute_structure, mapping, profiles)

    test_TypeError(None, attribute_structure, mapping, profiles)
    test_TypeError(object, attribute_structure, mapping, profiles)
    test_TypeError(vocabulary, None, mapping, profiles)
    test_TypeError(vocabulary, object, mapping, profiles)
    test_TypeError(vocabulary, AttributeSystem(attribute_structure, ['o']),
                   mapping, profiles)
    test_TypeError(vocabulary, attribute_structure, None, profiles)
    test_TypeError(vocabulary, attribute_structure, object, profiles)
    test_TypeError(vocabulary, attribute_structure, mapping, None)
    test_TypeError(vocabulary, attribute_structure, mapping, object)

    test_ValueError(
        vocabulary, attribute_structure, bad_source_mapping, profiles)
    test_ValueError(
        vocabulary, attribute_structure, bad_target_mapping, profiles)
    test_ValueError(
        vocabulary, attribute_structure, bad_target_mapping2, profiles)

    test_ValueError(vocabulary, attribute_structure, mapping, bad_profiles)
    test_ValueError(
        vocabulary, attribute_structure, dup_subscr_mapping, profiles)

    bad_mapping = {RelationSymbol("not in Vocabulary or profiles", 2): 1,
                   behind_rs: 2, pm_rs: 3}
    test_ValueError(vocabulary, attribute_structure, bad_mapping, profiles)
    bad_vocabulary = Vocabulary(['C1', 'C2'],
                                [ahead_rs, behind_rs, pm_rs,
                                RelationSymbol("not in source/profiles", 2)],
                                ['V1', 'V2'])

    test_ValueError(bad_vocabulary, attribute_structure, mapping, profiles)
    bad_target_mapping = {ahead_rs: 1, behind_rs: 6, pm_rs: 3}
    test_ValueError(
        vocabulary, attribute_structure, bad_target_mapping, profiles)

    bad_profiles = [
        [ahead_rs, ('doesn\'t exist', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]]
    test_ValueError(vocabulary, attribute_structure, mapping, bad_profiles)
    bad_profiles = [
        [ahead_rs, ('hour', 10), ('minute', 1), ('hour', 2), ('minute', 2)],
        [behind_rs, ('hour', 1), ('minute', 1), ('hour', 2), ('minute', 2)],
        [pm_rs, ('hour', 1)]]
    test_ValueError(vocabulary, attribute_structure, mapping, bad_profiles)

    AI = AttributeInterpretation(
        vocabulary, attribute_structure, mapping, profiles)
    assert AI._attribute_structure == attribute_structure
    assert AI._attribute_structure is not attribute_structure
    assert AI._vocabulary == vocabulary
    assert AI._vocabulary is vocabulary

    AI._vocabulary.add_constant('cx')
    AI._vocabulary.add_variable('vx')
    assert 'cx' in vocabulary._C
    assert 'vx' in vocabulary._V
    vocabulary.add_constant('cx2')
    vocabulary.add_variable('vx2')
    assert 'cx2' in AI._vocabulary._C
    assert 'vx2' in AI._vocabulary._V
Exemple #35
0
def test_memory_binding():
    """Test that a vocabulary is shared across all objects that use it."""
    from vivid.classes.attribute import Attribute
    from vivid.classes.relation import Relation
    from vivid.classes.attribute_structure import AttributeStructure
    from vivid.classes.attribute_system import AttributeSystem
    from vivid.classes.constant_assignment import ConstantAssignment
    from vivid.classes.variable_assignment import VariableAssignment
    from vivid.classes.named_state import NamedState
    from vivid.classes.formula import Formula
    from vivid.classes.assumption_base import AssumptionBase
    from vivid.classes.attribute_interpretation import AttributeInterpretation
    from vivid.classes.context import Context

    color = Attribute("color", ["R", "G", "B"])
    size = Attribute("size", ["S", "M", "L"])
    r = Relation("R1(c) <=> c", ["color"], 1)
    attribute_structure = AttributeStructure(color, size, r)
    o = ["s1", "s2"]
    attribute_system = AttributeSystem(attribute_structure, o)

    dummy_rs = RelationSymbol("DUMMY", 3)
    vocabulary = Vocabulary(["dummy"], [dummy_rs], [])
    p = ConstantAssignment(vocabulary, attribute_system, {})
    p2 = ConstantAssignment(vocabulary, attribute_system, {})
    X = VariableAssignment(vocabulary, attribute_system, {})

    s = NamedState(attribute_system, p)
    s2 = NamedState(attribute_system, p2)

    f = Formula(vocabulary, "DUMMY", "dummy")
    assumption_base = AssumptionBase(f)

    context = Context(assumption_base, s)

    profiles = [[dummy_rs, ("color", 1)]]

    mapping = {dummy_rs: 1}

    attribute_interpretation = AttributeInterpretation(vocabulary, attribute_structure, mapping, profiles)

    vocabulary.add_constant("Vocabulary")
    p._vocabulary.add_constant("constant")
    X._vocabulary.add_variable("variable")
    s._p._vocabulary.add_constant("named_state")
    f._vocabulary.add_constant("formula")
    assumption_base._vocabulary.add_constant("assumption_base")
    context._named_state._p._vocabulary.add_constant("context")
    attribute_interpretation._vocabulary.add_constant("attribute_interpretation")

    assert (
        str(vocabulary)
        == str(p._vocabulary)
        == str(X._vocabulary)
        == str(s._p._vocabulary)
        == str(f._vocabulary)
        == str(assumption_base._vocabulary)
        == str(context._named_state._p._vocabulary)
        == str(attribute_interpretation._vocabulary)
    )

    assert vocabulary is p._vocabulary
    assert vocabulary is X._vocabulary
    assert vocabulary is s._p._vocabulary
    assert vocabulary is f._vocabulary
    assert vocabulary is assumption_base._vocabulary
    assert vocabulary is context._named_state._p._vocabulary
    assert vocabulary is attribute_interpretation._vocabulary

    from copy import deepcopy

    p_copy = deepcopy(p)
    X_copy = deepcopy(X)
    s_copy = deepcopy(s)
    f_copy = deepcopy(f)
    assumption_base_copy = deepcopy(assumption_base)
    context_copy = deepcopy(context)
    attribute_interpretation_copy = deepcopy(attribute_interpretation)

    assert vocabulary is p_copy._vocabulary
    assert vocabulary is X_copy._vocabulary
    assert vocabulary is s_copy._p._vocabulary
    assert vocabulary is f_copy._vocabulary
    assert vocabulary is assumption_base_copy._vocabulary
    assert vocabulary is context_copy._named_state._p._vocabulary
    assert vocabulary is attribute_interpretation_copy._vocabulary
Exemple #36
0
def test__key():
    """Test key for hash function."""
    C, R, V = ["C"], [RelationSymbol("R", 1)], ["V"]
    vocabulary = Vocabulary(C, R, V)
    assert (("C",), (RelationSymbol("R", 1),), ("V",)) == vocabulary._key()