Example #1
0
def test___isub__():
    """Test -= operator for AttributeSystem."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)

    astr_empty = AttributeStructure()
    astr_a = AttributeStructure(a)
    astr_a_b = AttributeStructure(a, b)
    astr_a_R1 = AttributeStructure(a, R1)
    astr_a_b_R1 = AttributeStructure(a, b, R1)
    astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2)

    objs = ['a', 'b', 'c']

    asys = AttributeSystem(astr_empty, [])
    asys_a = AttributeSystem(astr_a, [])
    asys_a_b = AttributeSystem(astr_a_b, [])
    asys_a_R1 = AttributeSystem(astr_a_R1, [])
    asys_a_b_R1 = AttributeSystem(astr_a_b_R1, [])
    asys_a_b_R1_R2 = AttributeSystem(astr_a_b_R1_R2, [])

    asys_a_b_R1_R2 -= R2
    assert asys_a_b_R1_R2 == asys_a_b_R1
    assert asys_a_b_R1_R2 is not asys_a_b_R1
    asys_a_b_R1_R2 -= R1
    assert asys_a_b_R1_R2 == asys_a_b
    assert asys_a_b_R1_R2 is not asys_a_b
    asys_a_b_R1_R2 -= b
    assert asys_a_b_R1_R2 == asys_a
    assert asys_a_b_R1_R2 is not asys_a
    asys_a_b_R1_R2 -= a
    assert asys_a_b_R1_R2 == asys
    assert asys_a_b_R1_R2 is not asys
Example #2
0
def test___getitem__():
    """Test indexing of AttributeSystem."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)

    astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2)
    objs = ['a', 'b', 'c']

    asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs)

    ia = asys_a_b_R1_R2_o[a]
    assert ia == a
    assert ia is asys_a_b_R1_R2_o._attribute_structure["a"]
    iR1 = asys_a_b_R1_R2_o[R1]
    assert iR1 == R1
    assert iR1 is asys_a_b_R1_R2_o._attribute_structure["R1"]
    # Test object retrieval with string. Note, strings may not index
    # AttributeStructure members within this AttributeSystem, only objects.
    # To index objects in AttributeStructure in AttributeSystem, use index
    # with asys.astr[index]
    iobj = asys_a_b_R1_R2_o['a']
    assert iobj == objs[0]
    assert iobj is objs[0]

    # Test retrieval by string for AttributeStructure members
    # AttributeStructure is cloned so the retrieved objects is not the same
    # as the one's used to contruct original instance
    ias = asys_a_b_R1_R2_o._attribute_structure['a']
    assert ias == a
    assert ias is not a
    iR1s = asys_a_b_R1_R2_o._attribute_structure['R1']
    assert iR1s == R1
    assert iR1s is not R1
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
Example #4
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___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
Example #6
0
def test___add__():
    """Test + operator."""
    from vivid.classes.relation import Relation
    from vivid.classes.attribute_structure import AttributeStructure

    a1 = Attribute("a1", [])
    a2 = Attribute("a2", [])
    r1 = Relation("R1(a) <=> ", ["a1"], 1)
    astr = AttributeStructure()
    astr_a1 = AttributeStructure(a1)
    astr_a1_a2 = AttributeStructure(a1, a2)
    astr_a1_r1 = AttributeStructure(a1, r1)
    astr_a1_a2_r1 = AttributeStructure(a1, a2, r1)

    # add AttributeStructure and Attribute
    assert astr_a1 == astr + a1
    assert astr_a1 == a1 + astr
    # add Attribute and Attribute
    assert astr_a1_a2 == a1 + a2
    assert astr_a1_a2 == a2 + a1
    # add Attribute and Relation
    assert astr_a1_r1 == a1 + r1
    assert astr_a1_r1 == r1 + a1
    assert astr_a1_a2_r1 == a1 + a2 + r1
    assert astr_a1_a2_r1 == a2 + a1 + r1
    assert astr_a1_a2_r1 == r1 + a1 + a2
    # Test for when + fails, i.e. when Relation is added before attributes with
    # its D(R) labels.
    with pytest.raises(ValueError) as excinfo:
        r1 + a2 + a1
Example #7
0
def test___deepcopy__():
    """Test deepcopy"""
    color = Attribute("color", ['R', 'G', 'B'])
    size = Attribute("size", ['S', 'M', 'L'])
    a = AttributeStructure(color, size)
    o = ['s1', 's2']
    asys = AttributeSystem(a, o)

    ascr = {
        ('color', 's1'): ['R'],
        ('color', 's2'): ['B', 'G'],
        ('size', 's1'): ['M'],
        ('size', 's2'): ['L', 'S']
    }

    s = State(asys, ascr)

    from copy import deepcopy
    s_copy = deepcopy(s)

    assert s == s_copy
    assert s is not s_copy
    assert s._attribute_system == s_copy._attribute_system
    assert s._attribute_system is not s_copy._attribute_system
    assert s._ascriptions == s_copy._ascriptions
    assert s._ascriptions is not s_copy._ascriptions
def test___getitem__():
    """Test indexing of AttributeStructure."""
    def test_TypeError(astr, index):
        """Test indexing for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            astr[index]

    def test_KeyError(astr, index):
        """Test indexing for ValueErrors with given params."""
        with pytest.raises(KeyError) as excinfo:
            astr[index]

    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(b) <=> ", ["b"], 2)

    astr = AttributeStructure(a, R1)
    assert astr[a] == a
    assert astr[R1] == R1
    assert astr["a"] == a
    assert astr["R1"] == R1
    assert astr[1] == R1
    test_TypeError(astr, None)
    test_TypeError(astr, 1.0)
    test_TypeError(astr, [])
    test_TypeError(astr, object)
    test_KeyError(astr, "")
    test_KeyError(astr, 3)
    test_KeyError(astr, b)
    test_KeyError(astr, R2)
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'
def test___le__():
    """Test total ordering."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)
    astr_empty = AttributeStructure()
    astr_a = AttributeStructure(a)
    astr_a_R1 = AttributeStructure(a, R1)
    astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2)

    # Test subsets and strict subsets
    assert not astr_empty < astr_empty
    assert astr_empty <= astr_empty
    assert astr_empty < astr_a_b_R1_R2
    assert astr_empty <= astr_a_b_R1_R2
    assert astr_a < astr_a_b_R1_R2
    assert astr_a <= astr_a_b_R1_R2
    assert astr_a_R1 < astr_a_b_R1_R2
    assert astr_a_R1 <= astr_a_b_R1_R2
    assert not astr_a_b_R1_R2 < astr_a_b_R1_R2
    assert astr_a_b_R1_R2 <= astr_a_b_R1_R2

    # Test supersets and strict supersets
    assert not astr_empty > astr_empty
    assert astr_empty >= astr_empty
    assert astr_a_b_R1_R2 > astr_empty
    assert astr_a_b_R1_R2 >= astr_empty
    assert astr_a_b_R1_R2 > astr_a
    assert astr_a_b_R1_R2 >= astr_a
    assert astr_a_b_R1_R2 > astr_a_R1
    assert astr_a_b_R1_R2 >= astr_a_R1
    assert not astr_a_b_R1_R2 > astr_a_b_R1_R2
    assert astr_a_b_R1_R2 >= astr_a_b_R1_R2
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___init__():
    """Test AttributeStructure construction."""
    def test_TypeError(*ops):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            AttributeStructure(*ops)

    def test_ValueError(*ops):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(ValueError) as excinfo:
            AttributeStructure(*ops)

    # Test bad optional param
    test_TypeError("a")
    test_TypeError(object)

    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a1"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)
    R3 = Relation("R3(a) <=> ", ["a"], 3)

    # Test no duplicate labels
    test_ValueError(a, a)
    # Test no duplicate subscripts
    test_ValueError(R1, R1)
    # Test D(R) subset of labels
    test_ValueError(a, R1)
    # Test out of order construction
    AttributeStructure(R2, R3, b, a)
Example #13
0
def test___eq__():
    """Test == operator for AttributeSystem."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)

    astr_empty = AttributeStructure()
    astr_a_b = AttributeStructure(a, b)
    astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2)

    objs_empty = []
    objs = ['a', 'b', 'c']

    asys = AttributeSystem(astr_empty, objs_empty)
    asys_o = AttributeSystem(astr_empty, objs)
    asys_o_copy = AttributeSystem(astr_empty, objs)

    asys_a_b = AttributeSystem(astr_a_b, objs_empty)
    asys_a_b_copy = AttributeSystem(astr_a_b, objs_empty)

    asys_a_b_R1_R2 = AttributeSystem(astr_a_b_R1_R2, objs_empty)
    asys_a_b_R1_R2_copy = AttributeSystem(astr_a_b_R1_R2, objs_empty)

    asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs)
    asys_a_b_R1_R2_o_copy = AttributeSystem(astr_a_b_R1_R2, objs)

    # test identity
    assert asys == asys
    # components tests
    assert asys_o == asys_o_copy
    assert asys_a_b == asys_a_b_copy
    assert asys_a_b_R1_R2 == asys_a_b_R1_R2_copy
    assert asys_a_b_R1_R2_o == asys_a_b_R1_R2_o_copy
def test_get_labels():
    """Test get_labels function."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    c = Attribute("c", [])

    astr = AttributeStructure(a, b, c)
    assert astr.get_labels() == ['a', 'b', 'c']
def test_get_cardinality():
    """Test get cardinality of this AttributeStructure."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    c = Attribute("c", [])
    d = Attribute("d", [])
    astr = AttributeStructure(a, b, c, d)
    assert astr.get_cardinality() == 4
Example #16
0
def test_set_ascription():
    """Test set_ascription function."""
    def test_TypeError(state, ascription, valueset):
        """Test set_ascription for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            state.set_ascription(ascription, valueset)

    def test_ValueError(state, ascription, valueset):
        """Test set_ascription for ValueErrors with given params."""
        with pytest.raises(ValueError) as excinfo:
            state.set_ascription(ascription, valueset)

    def test_KeyError(state, ascription, valueset):
        """Test set_ascription for KeyErrors with given params."""
        with pytest.raises(KeyError) as excinfo:
            state.set_ascription(ascription, valueset)

    color = Attribute("color", ['R', 'G', 'B'])
    size = Attribute("size", ['S', 'M', 'L'])

    a = AttributeStructure(color, size)
    o = ['s1', 's2']

    asys = AttributeSystem(a, o)
    s = State(asys)

    # test bad ao_pair types/values
    test_TypeError(s, [], ['R'])
    test_ValueError(s, (), ['R'])
    test_ValueError(s, (1, 2, 3), ['R'])
    test_ValueError(s, (1, 2), ['R'])
    test_ValueError(s, (1, ''), ['R'])
    test_ValueError(s, ('', 1), ['R'])
    # test bad types for ValueSet
    test_TypeError(s, ('color', 's1'), None)
    test_TypeError(s, ('color', 's1'), ())
    test_TypeError(s, ('color', 's1'), 'a')
    test_TypeError(s, ('color', 's1'), object)
    # test empty ValueSet catching
    test_ValueError(s, ('color', 's1'), [])
    test_ValueError(s, ('color', 's1'), set([]))
    test_ValueError(s, ('color', 's1'), ValueSet([]))
    # test bad ao-pair keys
    test_KeyError(s, ('color', 'bad object'), ['R'])
    test_KeyError(s, ('bad label', 's2'), ['R'])
    # test nonsubset valuesets
    test_ValueError(s, ('color', 's2'), ['a'])
    test_ValueError(s, ('color', 's2'), [1])

    s.set_ascription(('color', 's2'), ['R'])
    assert s[('color', 's2')] == ValueSet(['R'])
    # check reversion to superset is possible
    s.set_ascription(('color', 's2'), ['R', 'G'])
    assert s[('color', 's2')] == ValueSet(['R', 'G'])
    s.set_ascription(('size', 's1'), ['M', 'S'])
    assert s[('size', 's1')] == ValueSet(['S', 'M'])
Example #17
0
def test_get_worlds():
    """Test get_worlds function."""
    color = Attribute("color", ['R', 'G', 'B'])
    size = Attribute("size", ['S', 'M', 'L'])
    a = AttributeStructure(color, size)
    o = ['s1', 's2']
    asys = AttributeSystem(a, o)

    ascr = {
        ('color', 's1'): ['R'],
        ('color', 's2'): ['B', 'G'],
        ('size', 's1'): ['M'],
        ('size', 's2'): ['L', 'S']
    }

    ascr1 = {
        ('color', 's1'): ['R'],
        ('color', 's2'): ['G'],
        ('size', 's1'): ['M'],
        ('size', 's2'): ['L']
    }

    ascr2 = {
        ('color', 's1'): ['R'],
        ('color', 's2'): ['G'],
        ('size', 's1'): ['M'],
        ('size', 's2'): ['S']
    }

    ascr3 = {
        ('color', 's1'): ['R'],
        ('color', 's2'): ['B'],
        ('size', 's1'): ['M'],
        ('size', 's2'): ['L']
    }

    ascr4 = {
        ('color', 's1'): ['R'],
        ('color', 's2'): ['B'],
        ('size', 's1'): ['M'],
        ('size', 's2'): ['S']
    }

    s = State(asys, ascr)

    w1 = State(asys, ascr1)
    w2 = State(asys, ascr2)
    w3 = State(asys, ascr3)
    w4 = State(asys, ascr4)
    worlds = [w1, w2, w3, w4]

    for w in s.get_worlds():
        assert w in worlds

    assert len(s.get_worlds()) == len(worlds)
def test___repr__():
    """Test repr(AttributeStructure)."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)
    astr = AttributeStructure(a, b, R1, R2)
    astr2 = AttributeStructure(R2, R1, b, a)

    assert str(astr) == "(a: {}, b: {} ; R1,R2)"
    assert str(astr2) == "(a: {}, b: {} ; R1,R2)"
Example #19
0
def test___repr__():
    """Test repr()."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)

    astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2)
    objs = ['a', 'b', 'c']

    asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs)
    assert repr(asys_a_b_R1_R2_o) == "({a, b, c} ; (a: {}, b: {} ; R1,R2))"
    assert repr(AttributeSystem(AttributeStructure(), [])) == "({} ; ( ; ))"
def test___deepcopy__():
    """Test copy.deepcopy for AttributeStructure object."""
    import copy
    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)
    astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2)

    astr_copy = copy.deepcopy(astr_a_b_R1_R2)
    assert astr_copy == astr_a_b_R1_R2
    assert astr_copy._attributes is not astr_a_b_R1_R2._attributes
    assert astr_copy._relations is not astr_a_b_R1_R2._relations
    assert astr_copy is not astr_a_b_R1_R2
Example #21
0
def test__key():
    """Test Attribute hash key."""
    A = Attribute("", [])
    A2 = Attribute("label", [1, 'string', True])
    assert A._key() == ("", 'V()')
    print A2._key()
    assert A2._key() == ('label', 'V(1, string, True)')
def test___isub__():
    """Test -= operator for AttributeStructure."""
    def test_ValueError(astr, sub):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(ValueError) as excinfo:
            astr -= sub

    def test_KeyError(astr, sub):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(KeyError) as excinfo:
            astr -= sub

    def test_TypeError(astr, sub):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            astr -= sub

    a = Attribute("a", [])
    b = Attribute("b", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)
    astr_empty = AttributeStructure()
    astr_a = AttributeStructure(a)
    astr_a_R1 = AttributeStructure(a, R1)
    astr_a_b = AttributeStructure(a, b)
    astr_a_b_R1 = AttributeStructure(a, b, R1)
    astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2)

    # Test invalid subtraction error catching
    test_KeyError(astr_empty, a)
    test_KeyError(astr_empty, R1)
    test_TypeError(astr_empty, None)
    test_TypeError(astr_empty, "")
    test_TypeError(astr_empty, object)
    test_ValueError(astr_empty, astr_a)
    test_ValueError(astr_empty, astr_a_R1)

    astr_a_copy = AttributeStructure(a)
    test_ValueError(astr_a, astr_a_R1)
    assert astr_a_copy == astr_a

    # Test attribute removal
    astr_a_b_R1_R2 -= R2
    assert astr_a_b_R1 == astr_a_b_R1_R2
    astr_a_b_R1_R2 -= R1
    assert astr_a_b == astr_a_b_R1_R2
    astr_a_b_R1_R2 -= b
    assert astr_a == astr_a_b_R1_R2
    astr_a_b_R1_R2 -= a
    assert astr_empty == astr_a_b_R1_R2
Example #23
0
def test_is_alternate_extension():
    """Test is_alternate_extension function."""
    from copy import deepcopy
    color, size = Attribute("color", ['R', 'G', 'B']), Attribute(
        "size", ['S', 'M', 'L'])

    a = AttributeStructure(color, size)
    o = ['s1', 's2']

    asys = AttributeSystem(a, o)
    s = State(asys)

    s.set_ascription(('color', 's1'), ['R', 'B'])
    s.set_ascription(('size', 's2'), ['M', 'L'])

    s1 = deepcopy(s)
    s1.set_ascription(('color', 's1'), ['B'])
    s1.set_ascription(('size', 's1'), ['S', 'M'])
    s1.set_ascription(('color', 's2'), ['B', 'G'])
    s2 = deepcopy(s)
    s2.set_ascription(('size', 's1'), ['L'])
    s2.set_ascription(('size', 's2'), ['L'])
    s3 = deepcopy(s)
    s3.set_ascription(('color', 's1'), ['R'])

    aes = s.get_alternate_extensions(s1, s2, s3)
    ae_s5, ae_s6, ae_s4 = aes

    for ae in aes:
        print s.is_alternate_extension(ae, s1, s2, s3)

    color, size = Attribute("color", ['R', 'G', 'B']), Attribute(
        "size", ['S', 'M', 'L'])

    a = AttributeStructure(color, size)
    o = ['s']

    asys = AttributeSystem(a, o)
    s = State(asys)

    s1 = deepcopy(s)
    s1.set_ascription(('color', 's'), ['B', 'G'])
    s1.set_ascription(('size', 's'), ['S'])

    aes = s.get_alternate_extensions(s1)
    ae_s2, ae_s3 = aes

    for ae in aes:
        print s.is_alternate_extension(ae, s1)
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___ne__():
    """!= operator for AttributeStructure."""
    a1 = Attribute("a1", [])
    a2 = Attribute("a2", [])
    R1 = Relation("R1(a) <=> ", ["a1"], 1)
    R2 = Relation("R2(a) <=> ", ["a1"], 2)

    astr1 = AttributeStructure(a1, a2, R1, R2)
    astr2 = AttributeStructure(a1, R2)
    astr3 = AttributeStructure()

    # strict subset-superset tests
    assert astr1 != astr2
    assert astr2 != astr1
    # empty comparison test
    assert astr1 != astr3
    assert astr3 != astr1
Example #26
0
def test_is_automorphic():
    """Test if system is automorphic."""
    a = Attribute("a", [])
    b = Attribute("b", [])
    c = Attribute("c", ['a'])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["b"], 2)

    astr_c = AttributeStructure(c)
    astr_a_b_R1_R2 = AttributeStructure(a, b, R1, R2)
    objs = ['a', 'b', 'c']

    asys_a_b_R1_R2_o = AttributeSystem(astr_a_b_R1_R2, objs)
    asys_auto = AttributeSystem(astr_c, objs)

    assert asys_auto.is_automorphic()
    assert not asys_a_b_R1_R2_o.is_automorphic()
Example #27
0
def test___deepcopy__():
    """Test copy.deepcopy functionality of Attribute object."""
    from copy import deepcopy
    a = Attribute("label", ["v1", "v2"])
    a_copy = deepcopy(a)

    assert a == a_copy
    assert a is not a_copy
Example #28
0
def test_join():
    """Test join function for States."""
    def test_ValueError(s1, s2):
        """Test constructor for ValueErrors with given params."""
        with pytest.raises(ValueError) as excinfo:
            State.join(s1, s2)

    color = Attribute("color", ['R', 'G', 'B'])
    size = Attribute("size", ['S', 'M', 'L'])
    a = AttributeStructure(color, size)
    o = ['s1', 's2']
    asys = AttributeSystem(a, o)

    ascr1 = {
        ('color', 's1'): ['R'],
        ('color', 's2'): ['B'],
        ('size', 's1'): ['M'],
        ('size', 's2'): ['L', 'S']
    }
    ascr2 = {
        ('color', 's1'): ['G'],
        ('color', 's2'): ['G'],
        ('size', 's1'): ['L'],
        ('size', 's2'): ['M', 'S']
    }
    ascr3 = {
        ('color', 's1'): ['R', 'G'],
        ('color', 's2'): ['B', 'G'],
        ('size', 's1'): ['L', 'M'],
        ('size', 's2'): ['M', 'S', 'L']
    }

    s1 = State(asys, ascr1)
    s2 = State(asys, ascr2)
    s3 = State(asys, ascr3)

    assert s3 == State.join(s1, s2)

    length = Attribute("length", [1, 3, 5])
    shape = Attribute("shape", ['circle', 'triangle', 'rectangle'])
    a = AttributeStructure(length, shape)
    o = ['s1', 's2']
    bad_asys = AttributeSystem(a, o)
    bad_state = State(bad_asys)

    test_ValueError(s1, bad_state)
def test_get_subscripts():
    """Test retrieval of subscripts from Relation's in AttributeStructure."""
    a = Attribute("a", [])
    R1 = Relation("R1(a) <=> ", ["a"], 1)
    R2 = Relation("R2(a) <=> ", ["a"], 2)
    R3 = Relation("R3(a) <=> ", ["a"], 3)
    R4 = Relation("R4(a) <=> ", ["a"], 4)
    astr = AttributeStructure(a, R1, R2, R3, R4)
    assert astr.get_subscripts() == [1, 2, 3, 4]
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'}"
Example #31
0
def test___hash__():
    """test hash function for Attribute."""
    A = Attribute("label", [1, "string", True])
    B = Attribute("", [])
    assert type(hash(B._key())) == int
    assert type(hash(A._key())) == int
Example #32
0
def test___repr__():
    """ Test repr()."""
    from vivid.classes.interval import Interval
    a = Attribute("label", ["1", Interval(40, 50), True, 4.0, 6L])
    print a.__repr__()
    assert a.__repr__() == "\"label: {4.0,6,1,True,I(40, 50)}\""