def test_add_constant():
    """Test add_constant() function for Vocabulary object."""
    def test_TypeError(vocabulary, c):
        """Test TypeErrors in add_constant."""
        with pytest.raises(TypeError) as excinfo:
            vocabulary.add_constant(c)

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

    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_constant('x')
    assert vocab._C == ['C', 'x']
    vocab.add_constant('a')
    assert vocab._C == ['a', 'C', 'x']
def test_add_constant():
    """Test add_constant() function for Vocabulary object."""

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

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

    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_constant("x")
    assert vocab._C == ["C", "x"]
    vocab.add_constant("a")
    assert vocab._C == ["a", "C", "x"]
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 #4
0
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
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
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