Ejemplo n.º 1
0
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]
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
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
Ejemplo n.º 6
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']
Ejemplo n.º 7
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
Ejemplo n.º 8
0
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
Ejemplo n.º 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)]]"
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
def test___ne__():
    """Test != operator for RelationSymbol."""
    rs1 = RelationSymbol('name1', 1)
    rs2 = RelationSymbol('name1', 1)
    rs3 = RelationSymbol('name1', 2)
    rs4 = RelationSymbol('name2', 1)

    assert not rs1 != rs1
    assert not rs1 != rs2
    assert rs1 != rs3
    assert rs1 != rs4
Ejemplo n.º 13
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)"
Ejemplo n.º 14
0
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'
Ejemplo n.º 15
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()
Ejemplo n.º 16
0
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))"
Ejemplo n.º 17
0
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'})
Ejemplo n.º 18
0
def test___deepcopy__():
    """Test copy.deepcopy for RelationSymbol object."""
    from copy import deepcopy
    r = RelationSymbol('name', 1)
    r_copy = deepcopy(r)

    assert r == r_copy
    assert r is not r_copy
Ejemplo n.º 19
0
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
Ejemplo n.º 20
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)
Ejemplo n.º 21
0
def test___add__():
    """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_empty = AssumptionBase(vocabulary)
    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)

    assert a_1 == a_empty + f1
    assert a_1 == f1 + a_empty
    assert a_1 == a_empty + a_1
    assert a_1 == a_1 + a_empty
    assert a_1_2 == a_1 + f2
    assert a_1_2 == f2 + a_1
    assert a_1_2 is not a_1
    assert a_1_2 is not f2
    ref_a = a_1 + f2
    f2._name = "test"
    assert ref_a[-1]._name != f2._name
    assert a_1_2 == a_1 + a_2
    assert a_1_2_3 == a_1 + a_2 + a_3
    assert a_1_2_3 == a_1 + f2 + f3
    assert a_1_2_3_4 == a_1 + f2 + f3 + f4

    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 is a_empty._vocabulary
Ejemplo n.º 22
0
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
Ejemplo n.º 23
0
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__() == "([], [], [])"
Ejemplo n.º 24
0
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
Ejemplo n.º 25
0
def test___deepcopy__():
    """Test Test copy.deepcopy for Formula object."""
    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')

    from copy import deepcopy
    f_copy = deepcopy(f)

    assert f == f_copy
    assert f is not f_copy
    assert f._vocabulary is f_copy._vocabulary
    assert f._terms is not f_copy._terms

    f._name = "F"
    assert f._name != f_copy._name
Ejemplo n.º 26
0
def test___repr__():
    """Test str(Context)."""
    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, pm_rs, am_rs],
                            ['V1', 'V2'])

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

    a = Attribute('hour', [Interval(0, 23)])
    a2 = Attribute('minute', [Interval(0, 59)])
    r_pm = Relation('R1(h1) <=> h1 > 11', ['hour'], 1)
    r_am = Relation('R2(h1) <=> h1 <= 11', ['hour'], 2)
    r_ahead = Relation(
        'R3(h1,m1,hhh2,mm2) <=> h1 > hhh2 or (h1 = hhh2 and m1 > mm2)',
        ['hour', 'minute', 'hour', 'minute'], 3)
    r_behind = Relation(
        'R4(h1,m1,h2,m2) <=> h1 <= h2 or (h1 = h2 and m1 < m2)',
        ['hour', 'minute', 'hour', 'minute'], 4)
    attribute_structure = AttributeStructure(a, a2, r_ahead, r_behind, r_pm,
                                             r_am)
    objects = ['s1', 's2']
    attribute_system = AttributeSystem(attribute_structure, objects)
    p = ConstantAssignment(vocabulary, attribute_system, {
        'C1': 's1',
        'C2': 's2'
    })

    assumption_base = AssumptionBase(f)
    named_state = NamedState(attribute_system, p)

    C = Context(assumption_base, named_state)

    assert repr(C) == "hour(s1): {V(I(0, 23))}\n" + \
                      "hour(s2): {V(I(0, 23))}\n" + \
                      "minute(s1): {V(I(0, 59))}\n" + \
                      "minute(s2): {V(I(0, 59))}\n" + \
                      "CA{'C2': 's2', 'C1': 's1'}\n" + \
                      "AB(Ahead(C1, V1))"
Ejemplo n.º 27
0
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()
Ejemplo n.º 28
0
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
Ejemplo n.º 29
0
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))
Ejemplo n.º 30
0
def test___deepcopy__():
    """Test copy.deepcopy for AttributeInterpretation object."""
    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)

    from copy import deepcopy
    ai_copy = deepcopy(ai)
    assert ai == ai_copy
    assert ai is not ai_copy
    assert ai._vocabulary is ai_copy._vocabulary
    assert ai._attribute_structure is not ai_copy._attribute_structure
    assert ai._mapping is not ai_copy._mapping
    assert ai._profiles is not ai_copy._profiles
    assert ai._table is not ai_copy._table
    assert ai._relation_symbols is not ai_copy._relation_symbols
Ejemplo n.º 31
0
def test__key():
    """Test key for RelationSymbol hashing."""
    r = RelationSymbol('name', 1)
    assert r._key() == ('name', 1)
Ejemplo n.º 32
0
def test___repr__():
    """Test repr(RelationSymbol."""
    rs1 = RelationSymbol('name', 1)
    rs2 = RelationSymbol('', 1)
    assert rs1.__repr__() == 'name'
    assert rs2.__repr__() == ''