Exemplo n.º 1
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()
Exemplo n.º 2
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()
Exemplo n.º 3
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
Exemplo n.º 4
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]
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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)
Exemplo n.º 8
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']
Exemplo n.º 9
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))"
Exemplo n.º 10
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
Exemplo n.º 11
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
Exemplo n.º 12
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
Exemplo n.º 13
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
Exemplo n.º 14
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)"
Exemplo n.º 15
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
Exemplo n.º 16
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))
Exemplo n.º 17
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
Exemplo n.º 18
0
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
Exemplo n.º 19
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))"
Exemplo n.º 20
0
def test___deepcopy__():
    """Test copy.deepcopy for Context 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, 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)

    from copy import deepcopy
    C_copy = deepcopy(C)
Exemplo 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
Exemplo n.º 22
0
def test_get_basis():
    """Test get_basis function for Formula."""
    point = Attribute('point', [Point('x', 'x', 'x', 'x')])
    fake = Attribute('fake', [])
    r_is_on = Relation('R1(h1, h2, h3) <=> is_on(h1, h2, h3)',
                       ['point', 'point', 'point'], 1)
    r_not_same_point = Relation('R2(h1, h2) <=> not_same_point(h1, h2)',
                                ['point', 'point'], 2)
    r_clocks_unequal = Relation('R3(h1, h2) <=> clocks_unequal(h1, h2)',
                                ['point', 'point'], 3)
    r_can_observe = Relation(
        'R4(p, sp_loc, wls, wle) <=> can_observe(p, sp_loc, wls, wle)',
        ['point', 'point', 'point', 'point'], 4)
    r_fake = Relation(
        'R5(p, wl1s, wl1e, wl2s, wl2e) <=> meets(p, wl1s, wl1e, wl2s, wl2e)',
        ['fake', 'point', 'fake', 'fake', 'point'], 5)

    attribute_structure = AttributeStructure(point, fake, r_is_on,
                                             r_not_same_point,
                                             r_clocks_unequal, r_can_observe,
                                             r_fake)

    rs_is_on = RelationSymbol('IS_ON', 3)
    rs_not_same_point = RelationSymbol('NOT_SAME_POINT', 2)
    rs_clocks_unequal = RelationSymbol('CLOCKS_UNEQUAL', 2)
    rs_can_observe = RelationSymbol('CAN_OBSERVE', 4)
    rs_fake = RelationSymbol('FAKE', 5)

    vocabulary = Vocabulary(['P1', 'P2', 'P3', 'P4', 'P5'], [
        rs_is_on, rs_not_same_point, rs_clocks_unequal, rs_can_observe, rs_fake
    ], [])

    profiles = [[rs_is_on, ('point', 1), ('point', 2), ('point', 3)],
                [rs_not_same_point, ('point', 1), ('point', 2)],
                [rs_clocks_unequal, ('point', 1), ('point', 2)],
                [
                    rs_can_observe, ('point', 1), ('point', 2), ('point', 3),
                    ('point', 4)
                ],
                [
                    rs_fake, ('fake', 1), ('point', 2), ('fake', 3),
                    ('fake', 4), ('point', 5)
                ]]

    mapping = {
        rs_is_on: 1,
        rs_not_same_point: 2,
        rs_clocks_unequal: 3,
        rs_can_observe: 4,
        rs_fake: 5
    }

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

    objects = ['p1', 'p2', 'p3', 'p4', 'p5']
    attribute_system = AttributeSystem(attribute_structure, objects)
    p = ConstantAssignment(vocabulary, attribute_system, {
        'P1': 'p1',
        'P2': 'p2',
        'P3': 'p3',
        'P4': 'p4',
        'P5': 'p5'
    })

    named_state = NamedState(
        attribute_system, p, {
            ('point', 'p1'): [Point(1.5, 1.5, 1.5, 1.5)],
            ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
            ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
            ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)],
            ('point', 'p5'): [Point(2.0, 2.0, 2.0, 2.0)]
        })

    f1 = Formula(vocabulary, 'IS_ON', 'P1', 'P3', 'P4')
    f2 = Formula(vocabulary, 'NOT_SAME_POINT', 'P1', 'P2')
    f3 = Formula(vocabulary, 'CLOCKS_UNEQUAL', 'P1', 'P2')
    f4 = Formula(vocabulary, 'CAN_OBSERVE', 'P1', 'P2', 'P3', 'P4')
    f5 = Formula(vocabulary, 'FAKE', 'P1', 'P2', 'P3', 'P4', 'P5')

    VA = VariableAssignment(vocabulary, attribute_system, {}, dummy=True)

    f1_basis = set([('point', 'p1'), ('point', 'p3'), ('point', 'p4')])
    f2_basis = set([('point', 'p1'), ('point', 'p2')])
    f1_f2_f3_basis = set([('point', 'p1'), ('point', 'p2'), ('point', 'p3'),
                          ('point', 'p4')])
    f5_basis = set([('fake', 'p1'), ('fake', 'p4'), ('point', 'p2'),
                    ('point', 'p5'), ('fake', 'p3')])
    f4_f5_basis = set([('fake', 'p1'), ('fake', 'p4'), ('point', 'p2'),
                       ('point', 'p5'), ('fake', 'p3'), ('point', 'p1'),
                       ('point', 'p3'), ('point', 'p4')])

    assert f1_basis == set(
        Formula.get_basis(named_state._p, VA, attribute_interpretation, f1))
    assert f2_basis == set(
        Formula.get_basis(named_state._p, VA, attribute_interpretation, f2))
    assert f1_f2_f3_basis == set(
        Formula.get_basis(named_state._p, VA, attribute_interpretation, f1, f2,
                          f3))
    assert f5_basis == set(
        Formula.get_basis(named_state._p, VA, attribute_interpretation, f5))
    assert f4_f5_basis == set(
        Formula.get_basis(named_state._p, VA, attribute_interpretation, f4,
                          f5))
Exemplo n.º 23
0
 def test_ValueError(vocabulary, name, *terms):
     """Test ValueError catching in Formula constructor."""
     with pytest.raises(ValueError) as excinfo:
         Formula(vocabulary, name, *terms)
Exemplo n.º 24
0
def test_assign_truth_value():
    """Test assign_truth_value() function of Formula object."""
    def test_TypeError(formula, attribute_interpretation, named_state, X):
        """Test TypeError catching in assign_truth_value()."""
        with pytest.raises(TypeError) as excinfo:
            formula.assign_truth_value(attribute_interpretation, named_state,
                                       X)

    def test_ValueError(formula, attribute_interpretation, named_state, X):
        """Test ValueError catching in assign_truth_value()."""
        with pytest.raises(ValueError) as excinfo:
            formula.assign_truth_value(attribute_interpretation, named_state,
                                       X)

    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)

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

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

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

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

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

    objects = ['s1', 's2']
    attribute_system = AttributeSystem(attribute_structure, objects)
    p = ConstantAssignment(vocabulary, attribute_system, {
        'C1': 's1',
        'C2': 's2'
    })
    named_state = NamedState(
        attribute_system, p, {
            ('hour', 's1'): [9, 13],
            ('minute', 's1'): [12],
            ('hour', 's2'): [8],
            ('minute', 's2'): [27]
        })

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

    VA = VariableAssignment(vocabulary, attribute_system, {}, dummy=True)

    bad_vocabulary = Vocabulary(['C1', 'C2', 'C3'],
                                [pm_rs, am_rs, ahead_rs, behind_rs],
                                ['V1', 'V2'])
    bad_p = ConstantAssignment(bad_vocabulary, attribute_system, {
        'C1': 's1',
        'C2': 's2'
    })
    bad_f = Formula(bad_vocabulary, 'Ahead', 'C1', 'C2')
    bad_t_f = Formula(bad_vocabulary, 'Ahead', 'C1')
    bad_v_attribute_interpretation = AttributeInterpretation(
        bad_vocabulary, attribute_structure, mapping, profiles)
    bad_p_attribute_interpretation = AttributeInterpretation(
        vocabulary, attribute_structure, mapping, bad_profiles)
    bad_named_state = NamedState(attribute_system, bad_p, {})
    bad_VA = VariableAssignment(bad_vocabulary,
                                attribute_system, {},
                                dummy=True)

    # Test invalid types
    test_TypeError(f, None, named_state, VA)
    test_TypeError(f, object, named_state, VA)
    test_TypeError(f, attribute_interpretation, None, VA)
    test_TypeError(f, attribute_interpretation, object, VA)
    test_TypeError(f, attribute_interpretation, named_state, None)
    test_TypeError(f, attribute_interpretation, named_state, object)
    # Test mismatched vocabularies
    test_ValueError(bad_f, attribute_interpretation, named_state, VA)
    test_ValueError(f, bad_v_attribute_interpretation, named_state, VA)
    test_ValueError(bad_f, attribute_interpretation, bad_named_state, VA)
    test_ValueError(f, attribute_interpretation, named_state, bad_VA)
    # Test profile length, DR length mismatch
    test_ValueError(f, bad_p_attribute_interpretation, named_state, VA)
    # Test profile j_x against length of terms
    test_ValueError(bad_t_f, attribute_interpretation, named_state, VA)
    assert f.assign_truth_value(attribute_interpretation, named_state, VA)

    from vivid.classes.point import Point
    point = Attribute('point', [Point('x', 'x', 'x', 'x')])
    r_is_on = Relation('R1(h1, h2, h3) <=> is_on(h1, h2, h3)',
                       ['point', 'point', 'point'], 1)
    r_not_same_point = Relation('R2(h1, h2) <=> not_same_point(h1, h2)',
                                ['point', 'point'], 2)
    r_clocks_unequal = Relation('R3(h1, h2) <=> clocks_unequal(h1, h2)',
                                ['point', 'point'], 3)
    r_can_observe = Relation(
        'R4(p, sp_loc, wls, wle) <=> can_observe(p, sp_loc, wls, wle)',
        ['point', 'point', 'point', 'point'], 4)
    r_meets = Relation(
        'R5(p, wl1s, wl1e, wl2s, wl2e) <=> meets(p, wl1s, wl1e, wl2s, wl2e)',
        ['point', 'point', 'point', 'point', 'point'], 5)

    attribute_structure = AttributeStructure(point, r_is_on, r_not_same_point,
                                             r_clocks_unequal, r_can_observe,
                                             r_meets)

    rs_is_on = RelationSymbol('IS_ON', 3)
    rs_not_same_point = RelationSymbol('NOT_SAME_POINT', 2)
    rs_clocks_unequal = RelationSymbol('CLOCKS_UNEQUAL', 2)
    rs_can_observe = RelationSymbol('CAN_OBSERVE', 4)
    rs_meets = RelationSymbol('MEETS', 5)

    vocabulary = Vocabulary(['P1', 'P2', 'P3', 'P4', 'P5'], [
        rs_is_on, rs_not_same_point, rs_clocks_unequal, rs_can_observe,
        rs_meets
    ], [])

    profiles = [[rs_is_on, ('point', 1), ('point', 2), ('point', 3)],
                [rs_not_same_point, ('point', 1), ('point', 2)],
                [rs_clocks_unequal, ('point', 1), ('point', 2)],
                [
                    rs_can_observe, ('point', 1), ('point', 2), ('point', 3),
                    ('point', 4)
                ],
                [
                    rs_meets, ('point', 1), ('point', 2), ('point', 3),
                    ('point', 4), ('point', 5)
                ]]

    mapping = {
        rs_is_on: 1,
        rs_not_same_point: 2,
        rs_clocks_unequal: 3,
        rs_can_observe: 4,
        rs_meets: 5
    }

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

    objects = ['p1', 'p2', 'p3', 'p4', 'p5']
    attribute_system = AttributeSystem(attribute_structure, objects)
    p = ConstantAssignment(vocabulary, attribute_system, {
        'P1': 'p1',
        'P2': 'p2',
        'P3': 'p3',
        'P4': 'p4',
        'P5': 'p5'
    })

    named_state = NamedState(
        attribute_system, p, {
            ('point', 'p1'): [Point(1.5, 1.5, 1.5, 1.5)],
            ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
            ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
            ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)],
            ('point', 'p5'): [Point(2.0, 2.0, 2.0, 2.0)]
        })

    f1 = Formula(vocabulary, 'IS_ON', 'P1', 'P3', 'P4')
    f2 = Formula(vocabulary, 'NOT_SAME_POINT', 'P1', 'P2')
    f3 = Formula(vocabulary, 'CLOCKS_UNEQUAL', 'P1', 'P2')
    f4 = Formula(vocabulary, 'CAN_OBSERVE', 'P1', 'P2', 'P3', 'P4')
    f5 = Formula(vocabulary, 'MEETS', 'P1', 'P2', 'P3', 'P4', 'P5')

    VA = VariableAssignment(vocabulary, attribute_system, {}, dummy=True)

    assumption_base = AssumptionBase(f1, f2, f3, f4)

    for f in assumption_base:
        assert f.assign_truth_value(attribute_interpretation, named_state, VA)

    named_state.set_ascription(('point', 'p4'), [Point(1.0, 1.0, 1.0, 1.0)])
    assert f5.assign_truth_value(attribute_interpretation, named_state, VA)
Exemplo n.º 25
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
Exemplo n.º 26
0
def test_assign_truth_value():
    """Test assign_truth_value() function of Formula object."""
    def test_TypeError(formula, attribute_interpretation, named_state, X):
        """Test TypeError catching in assign_truth_value()."""
        with pytest.raises(TypeError) as excinfo:
            formula.assign_truth_value(
                attribute_interpretation, named_state, X)

    def test_ValueError(formula, attribute_interpretation, named_state, X):
        """Test ValueError catching in assign_truth_value()."""
        with pytest.raises(ValueError) as excinfo:
            formula.assign_truth_value(
                attribute_interpretation, named_state, X)

    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)

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

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

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

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

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

    objects = ['s1', 's2']
    attribute_system = AttributeSystem(attribute_structure, objects)
    p = ConstantAssignment(
        vocabulary, attribute_system, {'C1': 's1', 'C2': 's2'})
    named_state = NamedState(attribute_system, p, {
                             ('hour', 's1'): [9, 13],
                             ('minute', 's1'): [12],
                             ('hour', 's2'): [8],
                             ('minute', 's2'): [27]})

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

    VA = VariableAssignment(vocabulary, attribute_system, {}, dummy=True)

    bad_vocabulary = Vocabulary(
        ['C1', 'C2', 'C3'], [pm_rs, am_rs, ahead_rs, behind_rs], ['V1', 'V2'])
    bad_p = ConstantAssignment(
        bad_vocabulary, attribute_system, {'C1': 's1', 'C2': 's2'})
    bad_f = Formula(bad_vocabulary, 'Ahead', 'C1', 'C2')
    bad_t_f = Formula(bad_vocabulary, 'Ahead', 'C1')
    bad_v_attribute_interpretation = AttributeInterpretation(
        bad_vocabulary, attribute_structure, mapping, profiles)
    bad_p_attribute_interpretation = AttributeInterpretation(
        vocabulary, attribute_structure, mapping, bad_profiles)
    bad_named_state = NamedState(attribute_system, bad_p, {})
    bad_VA = VariableAssignment(
        bad_vocabulary, attribute_system, {}, dummy=True)

    # Test invalid types
    test_TypeError(f, None, named_state, VA)
    test_TypeError(f, object, named_state, VA)
    test_TypeError(f, attribute_interpretation, None, VA)
    test_TypeError(f, attribute_interpretation, object, VA)
    test_TypeError(f, attribute_interpretation, named_state, None)
    test_TypeError(f, attribute_interpretation, named_state, object)
    # Test mismatched vocabularies
    test_ValueError(bad_f, attribute_interpretation, named_state, VA)
    test_ValueError(f, bad_v_attribute_interpretation, named_state, VA)
    test_ValueError(bad_f, attribute_interpretation, bad_named_state, VA)
    test_ValueError(f, attribute_interpretation, named_state, bad_VA)
    # Test profile length, DR length mismatch
    test_ValueError(f, bad_p_attribute_interpretation, named_state, VA)
    # Test profile j_x against length of terms
    test_ValueError(bad_t_f, attribute_interpretation, named_state, VA)
    assert f.assign_truth_value(attribute_interpretation, named_state, VA)

    from vivid.classes.point import Point
    point = Attribute('point', [Point('x', 'x', 'x', 'x')])
    r_is_on = Relation('R1(h1, h2, h3) <=> is_on(h1, h2, h3)',
                       ['point', 'point', 'point'], 1)
    r_not_same_point = Relation('R2(h1, h2) <=> not_same_point(h1, h2)',
                                ['point', 'point'], 2)
    r_clocks_unequal = Relation('R3(h1, h2) <=> clocks_unequal(h1, h2)',
                                ['point', 'point'], 3)
    r_can_observe = Relation(
        'R4(p, sp_loc, wls, wle) <=> can_observe(p, sp_loc, wls, wle)',
        ['point', 'point', 'point', 'point'], 4)
    r_meets = Relation(
        'R5(p, wl1s, wl1e, wl2s, wl2e) <=> meets(p, wl1s, wl1e, wl2s, wl2e)',
        ['point', 'point', 'point', 'point', 'point'], 5)

    attribute_structure = AttributeStructure(
        point, r_is_on, r_not_same_point, r_clocks_unequal, r_can_observe,
        r_meets)

    rs_is_on = RelationSymbol('IS_ON', 3)
    rs_not_same_point = RelationSymbol('NOT_SAME_POINT', 2)
    rs_clocks_unequal = RelationSymbol('CLOCKS_UNEQUAL', 2)
    rs_can_observe = RelationSymbol('CAN_OBSERVE', 4)
    rs_meets = RelationSymbol('MEETS', 5)

    vocabulary = Vocabulary(['P1', 'P2', 'P3', 'P4', 'P5'],
                            [rs_is_on, rs_not_same_point,
                             rs_clocks_unequal, rs_can_observe, rs_meets],
                            [])

    profiles = [
        [rs_is_on, ('point', 1), ('point', 2), ('point', 3)],
        [rs_not_same_point, ('point', 1), ('point', 2)],
        [rs_clocks_unequal, ('point', 1), ('point', 2)],
        [rs_can_observe,
         ('point', 1), ('point', 2), ('point', 3), ('point', 4)],
        [rs_meets,
         ('point', 1), ('point', 2), ('point', 3), ('point', 4), ('point', 5)]]

    mapping = {rs_is_on: 1, rs_not_same_point: 2, rs_clocks_unequal: 3,
               rs_can_observe: 4, rs_meets: 5}

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

    objects = ['p1', 'p2', 'p3', 'p4', 'p5']
    attribute_system = AttributeSystem(attribute_structure, objects)
    p = ConstantAssignment(vocabulary, attribute_system,
                           {'P1': 'p1', 'P2': 'p2', 'P3': 'p3', 'P4': 'p4',
                            'P5': 'p5'})

    named_state = NamedState(attribute_system, p, {
                             ('point', 'p1'): [Point(1.5, 1.5, 1.5, 1.5)],
                             ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
                             ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
                             ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)],
                             ('point', 'p5'): [Point(2.0, 2.0, 2.0, 2.0)]})

    f1 = Formula(vocabulary, 'IS_ON', 'P1', 'P3', 'P4')
    f2 = Formula(vocabulary, 'NOT_SAME_POINT', 'P1', 'P2')
    f3 = Formula(vocabulary, 'CLOCKS_UNEQUAL', 'P1', 'P2')
    f4 = Formula(vocabulary, 'CAN_OBSERVE', 'P1', 'P2', 'P3', 'P4')
    f5 = Formula(vocabulary, 'MEETS', 'P1', 'P2', 'P3', 'P4', 'P5')

    VA = VariableAssignment(vocabulary, attribute_system, {}, dummy=True)

    assumption_base = AssumptionBase(f1, f2, f3, f4)

    for f in assumption_base:
        assert f.assign_truth_value(attribute_interpretation, named_state, VA)

    named_state.set_ascription(('point', 'p4'), [Point(1.0, 1.0, 1.0, 1.0)])
    assert f5.assign_truth_value(attribute_interpretation, named_state, VA)
Exemplo n.º 27
0
def test_get_basis():
    """Test get_basis function for Formula."""
    point = Attribute('point', [Point('x', 'x', 'x', 'x')])
    fake = Attribute('fake', [])
    r_is_on = Relation('R1(h1, h2, h3) <=> is_on(h1, h2, h3)',
                       ['point', 'point', 'point'], 1)
    r_not_same_point = Relation('R2(h1, h2) <=> not_same_point(h1, h2)',
                                ['point', 'point'], 2)
    r_clocks_unequal = Relation('R3(h1, h2) <=> clocks_unequal(h1, h2)',
                                ['point', 'point'], 3)
    r_can_observe = Relation(
        'R4(p, sp_loc, wls, wle) <=> can_observe(p, sp_loc, wls, wle)',
        ['point', 'point', 'point', 'point'], 4)
    r_fake = Relation(
        'R5(p, wl1s, wl1e, wl2s, wl2e) <=> meets(p, wl1s, wl1e, wl2s, wl2e)',
        ['fake', 'point', 'fake', 'fake', 'point'], 5)

    attribute_structure = AttributeStructure(
        point, fake, r_is_on, r_not_same_point, r_clocks_unequal,
        r_can_observe, r_fake)

    rs_is_on = RelationSymbol('IS_ON', 3)
    rs_not_same_point = RelationSymbol('NOT_SAME_POINT', 2)
    rs_clocks_unequal = RelationSymbol('CLOCKS_UNEQUAL', 2)
    rs_can_observe = RelationSymbol('CAN_OBSERVE', 4)
    rs_fake = RelationSymbol('FAKE', 5)

    vocabulary = Vocabulary(['P1', 'P2', 'P3', 'P4', 'P5'],
                            [rs_is_on, rs_not_same_point,
                             rs_clocks_unequal, rs_can_observe, rs_fake],
                            [])

    profiles = [
        [rs_is_on, ('point', 1), ('point', 2), ('point', 3)],
        [rs_not_same_point, ('point', 1), ('point', 2)],
        [rs_clocks_unequal, ('point', 1), ('point', 2)],
        [rs_can_observe,
         ('point', 1), ('point', 2), ('point', 3), ('point', 4)],
        [rs_fake,
         ('fake', 1), ('point', 2), ('fake', 3), ('fake', 4), ('point', 5)]]

    mapping = {rs_is_on: 1, rs_not_same_point: 2, rs_clocks_unequal: 3,
               rs_can_observe: 4, rs_fake: 5}

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

    objects = ['p1', 'p2', 'p3', 'p4', 'p5']
    attribute_system = AttributeSystem(attribute_structure, objects)
    p = ConstantAssignment(vocabulary, attribute_system,
                           {'P1': 'p1', 'P2': 'p2', 'P3': 'p3', 'P4': 'p4',
                            'P5': 'p5'})

    named_state = NamedState(attribute_system, p, {
                             ('point', 'p1'): [Point(1.5, 1.5, 1.5, 1.5)],
                             ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
                             ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
                             ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)],
                             ('point', 'p5'): [Point(2.0, 2.0, 2.0, 2.0)]})

    f1 = Formula(vocabulary, 'IS_ON', 'P1', 'P3', 'P4')
    f2 = Formula(vocabulary, 'NOT_SAME_POINT', 'P1', 'P2')
    f3 = Formula(vocabulary, 'CLOCKS_UNEQUAL', 'P1', 'P2')
    f4 = Formula(vocabulary, 'CAN_OBSERVE', 'P1', 'P2', 'P3', 'P4')
    f5 = Formula(vocabulary, 'FAKE', 'P1', 'P2', 'P3', 'P4', 'P5')

    VA = VariableAssignment(vocabulary, attribute_system, {}, dummy=True)

    f1_basis = set([('point', 'p1'), ('point', 'p3'), ('point', 'p4')])
    f2_basis = set([('point', 'p1'), ('point', 'p2')])
    f1_f2_f3_basis = set(
        [('point', 'p1'), ('point', 'p2'), ('point', 'p3'), ('point', 'p4')])
    f5_basis = set(
        [('fake', 'p1'), ('fake', 'p4'), ('point', 'p2'), ('point', 'p5'),
         ('fake', 'p3')])
    f4_f5_basis = set(
        [('fake', 'p1'), ('fake', 'p4'), ('point', 'p2'), ('point', 'p5'),
         ('fake', 'p3'), ('point', 'p1'), ('point', 'p3'), ('point', 'p4')])

    assert f1_basis == set(Formula.get_basis(
        named_state._p, VA, attribute_interpretation, f1))
    assert f2_basis == set(Formula.get_basis(
        named_state._p, VA, attribute_interpretation, f2))
    assert f1_f2_f3_basis == set(Formula.get_basis(
        named_state._p, VA, attribute_interpretation, f1, f2, f3))
    assert f5_basis == set(Formula.get_basis(
        named_state._p, VA, attribute_interpretation, f5))
    assert f4_f5_basis == set(Formula.get_basis(
        named_state._p, VA, attribute_interpretation, f4, f5))
Exemplo n.º 28
0
def test___init__():
    """Test Context construction."""
    def test_TypeError(assumption_base, named_state):
        """Test TypeError raising in Context construction."""
        with pytest.raises(TypeError) as excinfo:
            Context(assumption_base, named_state)

    def test_ValueError(assumption_base, named_state):
        """Test ValueError raising in Context construction."""
        with pytest.raises(ValueError) as excinfo:
            Context(assumption_base, named_state)

    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'])
    bad_vocabulary = Vocabulary(['C1', 'C2'],
                                [ahead_rs, behind_rs, pm_rs, am_rs], ['V1'])

    f = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f_bad = Formula(bad_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'
    })
    bad_p = ConstantAssignment(bad_vocabulary, attribute_system, {
        'C1': 's1',
        'C2': 's2'
    })

    assumption_base = AssumptionBase(f, f2)
    named_state = NamedState(attribute_system, p)
    bad_assumption_base = AssumptionBase(f_bad)
    bad_named_state = NamedState(attribute_system, bad_p)

    test_TypeError(assumption_base, None)
    test_TypeError(assumption_base, object)
    test_TypeError(None, named_state)
    test_TypeError(object, named_state)
    test_ValueError(bad_assumption_base, named_state)
    test_ValueError(assumption_base, bad_named_state)

    C = Context(assumption_base, named_state)
    assert C._assumption_base == assumption_base
    # assert C._assumption_base is not assumption_base
    assert C._named_state == named_state
Exemplo n.º 29
0
    def standard_test():
        """Do test with standard parser."""
        hour = Attribute('hour', [Interval(0, 23)])
        minute = 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(hour, minute, r_ahead,
                                                 r_behind, r_pm, r_am)

        rs_ahead = RelationSymbol('Ahead', 4)
        rs_behind = RelationSymbol('Behind', 4)
        rs_pm = RelationSymbol('PM', 1)
        rs_am = RelationSymbol('AM', 1)
        vocabulary = Vocabulary(['C1', 'C2'],
                                [rs_ahead, rs_behind, rs_pm, rs_am],
                                ['V1', 'V2'])

        objects = ['s1', 's2']
        attribute_system = AttributeSystem(attribute_structure, objects)
        p = ConstantAssignment(vocabulary, attribute_system, {
            'C1': 's1',
            'C2': 's2'
        })

        profiles = [[rs_pm, ('hour', 1)], [rs_am, ('hour', 1)],
                    [
                        rs_behind, ('hour', 1), ('minute', 1), ('hour', 2),
                        ('minute', 2)
                    ],
                    [
                        rs_ahead, ('hour', 1), ('minute', 1), ('hour', 2),
                        ('minute', 2)
                    ]]

        attribute_interpretation = AttributeInterpretation(
            vocabulary, attribute_structure, {
                rs_pm: 1,
                rs_am: 2,
                rs_ahead: 3,
                rs_behind: 4
            }, profiles)

        f1 = Formula(vocabulary, 'Ahead', 'C1', 'C2')
        f2 = Formula(vocabulary, 'Behind', 'C2', 'C1')
        f3 = Formula(vocabulary, 'PM', 'C1')
        f4 = Formula(vocabulary, 'AM', 'C2')
        assumption_base = AssumptionBase(f1, f3, f4)
        named_state = NamedState(
            attribute_system, p, {
                ("hour", "s1"): [Interval(15, 17)],
                ("hour", "s2"): [11],
                ("minute", "s1"): [23],
                ("minute", "s2"): [23]
            })

        context = Context(assumption_base, named_state)

        assert context.entails_formula(f1, attribute_interpretation)
        assert context.entails_formula(f2, attribute_interpretation)
        assert context.entails_formula(f3, attribute_interpretation)
        assert context.entails_formula(f4, attribute_interpretation)

        vocabulary = Vocabulary(['C1'], [rs_ahead, rs_behind, rs_pm, rs_am],
                                ['V1', 'V2'])

        objects = ['s1', 's2']
        attribute_system = AttributeSystem(attribute_structure, objects)
        p = ConstantAssignment(vocabulary, attribute_system, {'C1': 's1'})

        profiles = [[rs_pm, ('hour', 1)], [rs_am, ('hour', 1)],
                    [
                        rs_behind, ('hour', 1), ('minute', 1), ('hour', 2),
                        ('minute', 2)
                    ],
                    [
                        rs_ahead, ('hour', 1), ('minute', 1), ('hour', 2),
                        ('minute', 2)
                    ]]

        attribute_interpretation = AttributeInterpretation(
            vocabulary, attribute_structure, {
                rs_pm: 1,
                rs_am: 2,
                rs_ahead: 3,
                rs_behind: 4
            }, profiles)

        f1 = Formula(vocabulary, 'Ahead', 'C1', 'V1')
        f2 = Formula(vocabulary, 'PM', 'C1')
        f3 = Formula(vocabulary, 'AM', 'V1')
        f4 = Formula(vocabulary, 'AM', 'V2')
        f5 = Formula(vocabulary, 'Behind', 'V1', 'C1')
        assumption_base = AssumptionBase(f1)
        named_state = NamedState(
            attribute_system, p, {
                ("hour", "s1"): [Interval(15, 17)],
                ("hour", "s2"): [11],
                ("minute", "s1"): [23],
                ("minute", "s2"): [23]
            })

        context = Context(assumption_base, named_state)
        assert context.entails_formula(f1, attribute_interpretation)
        assert context.entails_formula(f2, attribute_interpretation)
        assert context.entails_formula(f3, attribute_interpretation)
        # assert not here as it does not hold for every variable assignment;
        # e.g. the following configuration is valid but its truth value is unknown:
        # profile: [('hour', 'V1')]
        #           CA{'C1': 's1'}
        #           VA{'V2': 's2'}, which means all terms are not defined
        assert not context.entails_formula(f4, attribute_interpretation)
        assert context.entails_formula(f5, attribute_interpretation)
Exemplo n.º 30
0
def test___eq__():
    """Test == operator for Context."""
    def test_TypeError(self, other):
        """Test TypeError raising in == operator of Context."""
        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, pm_rs, am_rs],
                            ['V1', 'V2'])

    f = Formula(vocabulary, 'Ahead', 'C1', 'V1')
    f2 = Formula(vocabulary, 'Behind', 'C1', 'V1')
    f_copy = Formula(vocabulary, 'Ahead', 'C1', 'V1')

    a = Attribute('hour', [Interval(0, 23)])
    a2 = Attribute('minute', [Interval(0, 59)])
    r_ahead = Relation(
        'R3(h1,m1,hhh2,mm2) <=> h1 > hhh2 or (h1 = hhh2 and m1 > mm2)',
        ['hour', 'minute', 'hour', 'minute'], 3)
    attribute_structure = AttributeStructure(a, a2, r_ahead)
    objects = ['s1', 's2']
    attribute_system = AttributeSystem(attribute_structure, objects)
    attribute_system_copy = AttributeSystem(attribute_structure, objects)
    p = ConstantAssignment(vocabulary, attribute_system, {
        'C1': 's1',
        'C2': 's2'
    })
    p2 = ConstantAssignment(vocabulary, attribute_system, {
        'C1': 's2',
        'C2': 's1'
    })
    p_copy = ConstantAssignment(vocabulary, attribute_system, {
        'C1': 's1',
        'C2': 's2'
    })

    assumption_base = AssumptionBase(f)
    assumption_base_copy = AssumptionBase(f_copy)
    assumption_base2 = AssumptionBase(f, f2)
    named_state = NamedState(attribute_system, p)
    named_state_copy = NamedState(attribute_system_copy, p_copy)
    named_state2 = NamedState(attribute_system, p2)

    C = Context(assumption_base, named_state)
    C2 = Context(assumption_base, named_state)
    C3 = Context(assumption_base_copy, named_state_copy)
    C4 = Context(assumption_base2, named_state)
    C5 = Context(assumption_base, named_state2)

    test_TypeError(C, None)
    test_TypeError(C, object)
    test_TypeError(C, named_state)
    test_TypeError(C, assumption_base)
    assert C == C
    assert C is C
    assert C == C2
    assert C is not C2
    assert C == C3
    assert C is not C3
    assert not C == C4
    assert not C == C5
Exemplo n.º 31
0
    def point_test():
        """Do test with Point object and its parser."""
        from vivid.classes.point import Point
        point = Attribute('point', [Point('x', 'x', 'x', 'x')])
        r_is_on = Relation('R1(h1, h2, h3) <=> is_on(h1, h2, h3)',
                           ['point', 'point', 'point'], 1)
        r_not_same_point = Relation('R2(h1, h2) <=> not_same_point(h1, h2)',
                                    ['point', 'point'], 2)
        r_clocks_unequal = Relation('R3(h1, h2) <=> clocks_unequal(h1, h2)',
                                    ['point', 'point'], 3)
        r_can_observe = Relation(
            'R4(p, sp_loc, wls, wle) <=> can_observe(p, sp_loc, wls, wle)',
            ['point', 'point', 'point', 'point'], 4)

        attribute_structure = AttributeStructure(point, r_is_on,
                                                 r_not_same_point,
                                                 r_clocks_unequal,
                                                 r_can_observe)

        rs_is_on = RelationSymbol('IS_ON', 3)
        rs_not_same_point = RelationSymbol('NOT_SAME_POINT', 2)
        rs_clocks_unequal = RelationSymbol('CLOCKS_UNEQUAL', 2)
        rs_can_observe = RelationSymbol('CAN_OBSERVE', 4)

        vocabulary = Vocabulary(
            ['P1', 'P2', 'P3', 'P4'],
            [rs_is_on, rs_not_same_point, rs_clocks_unequal, rs_can_observe],
            [])

        profiles = [[rs_is_on, ('point', 1), ('point', 2), ('point', 3)],
                    [rs_not_same_point, ('point', 1), ('point', 2)],
                    [rs_clocks_unequal, ('point', 1), ('point', 2)],
                    [
                        rs_can_observe, ('point', 1), ('point', 2),
                        ('point', 3), ('point', 4)
                    ]]

        mapping = {
            rs_is_on: 1,
            rs_not_same_point: 2,
            rs_clocks_unequal: 3,
            rs_can_observe: 4
        }

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

        objects = ['p1', 'p2', 'p3', 'p4']
        attribute_system = AttributeSystem(attribute_structure, objects)
        p = ConstantAssignment(vocabulary, attribute_system, {
            'P1': 'p1',
            'P2': 'p2',
            'P3': 'p3',
            'P4': 'p4'
        })

        named_state = NamedState(
            attribute_system, p, {
                ('point', 'p1'): [Point(1.5, 1.5, 1.5, 1.5)],
                ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
                ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
                ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)]
            })

        f1 = Formula(vocabulary, 'IS_ON', 'P1', 'P3', 'P4')
        f2 = Formula(vocabulary, 'NOT_SAME_POINT', 'P1', 'P2')
        f3 = Formula(vocabulary, 'CLOCKS_UNEQUAL', 'P1', 'P2')
        f4 = Formula(vocabulary, 'CAN_OBSERVE', 'P1', 'P2', 'P3', 'P4')

        VA = VariableAssignment(vocabulary, attribute_system, {}, dummy=True)

        assumption_base = AssumptionBase(f2, f3, f4)
        context = Context(assumption_base, named_state)

        # A context always entails its own named state
        assert context.entails_named_state(named_state,
                                           attribute_interpretation)

        named_state = NamedState(
            attribute_system, p, {
                ('point', 'p1'):
                [Point(1.5, 1.5, 1.5, 1.5),
                 Point(2.5, 2.5, 2.5, 2.5)],
                ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
                ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
                ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)]
            })

        # A context entails named states that when the worlds of the context
        # extend the named state
        assert context.entails_named_state(named_state,
                                           attribute_interpretation)

        named_state = NamedState(
            attribute_system, p, {
                ('point', 'p1'):
                [Point(1.5, 1.5, 1.5, 1.5),
                 Point(2.5, 2.5, 2.5, 2.5)],
                ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
                ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
                ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)]
            })
        context = Context(assumption_base, named_state)

        named_state = NamedState(
            attribute_system, p, {
                ('point', 'p1'): [Point(4.25, 1.3, 5.4, 7.5)],
                ('point', 'p2'): [Point(2.0, 2.4, 2.0, 2.0)],
                ('point', 'p3'): [Point(1.0, 1.0, 5.0, 1.0)],
                ('point', 'p4'): [Point(3.0, 3.0, 3.0, 8.0)]
            })
        # A context does not entail a named state when the worlds of the
        # context do not extend the named state
        assert not context.entails_named_state(named_state,
                                               attribute_interpretation)

        assumption_base = AssumptionBase(
            Formula(vocabulary, 'NOT_SAME_POINT', 'P1', 'P1'))
        context = Context(assumption_base, named_state)

        named_state = NamedState(
            attribute_system, p, {
                ('point', 'p1'): [Point(8.5, 1.4, 2.1, 3.6)],
                ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
                ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
                ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)]
            })
        # A context trivially entails a named state when no world of the
        # context satisfies the context
        assert context.entails_named_state(named_state,
                                           attribute_interpretation)
Exemplo n.º 32
0
    def standard_test():
        """Do test with standard parser."""
        hour = Attribute('hour', [Interval(0, 23)])
        minute = 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(hour, minute, r_ahead,
                                                 r_behind, r_pm, r_am)

        rs_ahead = RelationSymbol('Ahead', 4)
        rs_behind = RelationSymbol('Behind', 4)
        rs_pm = RelationSymbol('PM', 1)
        rs_am = RelationSymbol('AM', 1)
        vocabulary = Vocabulary(['C1', 'C2'],
                                [rs_ahead, rs_behind, rs_pm, rs_am],
                                ['V1', 'V2'])

        objects = ['s1', 's2']
        attribute_system = AttributeSystem(attribute_structure, objects)
        p = ConstantAssignment(vocabulary, attribute_system, {
            'C1': 's1',
            'C2': 's2'
        })

        profiles = [[rs_pm, ('hour', 1)], [rs_am, ('hour', 1)],
                    [
                        rs_behind, ('hour', 1), ('minute', 1), ('hour', 2),
                        ('minute', 2)
                    ],
                    [
                        rs_ahead, ('hour', 1), ('minute', 1), ('hour', 2),
                        ('minute', 2)
                    ]]

        attribute_interpretation = AttributeInterpretation(
            vocabulary, attribute_structure, {
                rs_pm: 1,
                rs_am: 2,
                rs_ahead: 3,
                rs_behind: 4
            }, profiles)

        f1 = Formula(vocabulary, 'Ahead', 'C1', 'C2')
        f2 = Formula(vocabulary, 'Behind', 'C2', 'C1')
        f3 = Formula(vocabulary, 'PM', 'C1')
        f4 = Formula(vocabulary, 'AM', 'C2')
        assumption_base = AssumptionBase(f1, f3, f4)
        named_state = NamedState(
            attribute_system, p, {
                ("hour", "s1"): [Interval(15, 17)],
                ("hour", "s2"): [Interval(6, 9)],
                ("minute", "s1"): [23],
                ("minute", "s2"): [23]
            })
        context = Context(assumption_base, named_state)

        # A context always entails its own named state
        assert context.entails_named_state(named_state,
                                           attribute_interpretation)

        named_state = NamedState(
            attribute_system, p, {
                ("hour", "s1"): [Interval(15, 17)],
                ("hour", "s2"): [Interval(4, 11)],
                ("minute", "s1"): [23],
                ("minute", "s2"): [23]
            })
        # A context entails named states that when the worlds of the context
        # extend the named state
        assert context.entails_named_state(named_state,
                                           attribute_interpretation)

        f1 = Formula(vocabulary, 'PM', 'C1')
        assumption_base = AssumptionBase(f1)
        named_state = NamedState(
            attribute_system, p, {
                ("hour", "s1"): [Interval(15, 17)],
                ("hour", "s2"): [Interval(6, 9)],
                ("minute", "s1"): [23],
                ("minute", "s2"): [23]
            })
        context = Context(assumption_base, named_state)

        named_state = NamedState(
            attribute_system, p, {
                ("hour", "s1"): [23],
                ("hour", "s2"): [13],
                ("minute", "s1"): [23],
                ("minute", "s2"): [23]
            })
        # A context does not entail a named state when the worlds of the
        # context do not extend the named state
        assert not context.entails_named_state(named_state,
                                               attribute_interpretation)

        f1 = Formula(vocabulary, 'PM', 'C1')
        assumption_base = AssumptionBase(f1)
        named_state = NamedState(
            attribute_system, p, {
                ("hour", "s1"): [Interval(6, 9)],
                ("hour", "s2"): [Interval(15, 17)],
                ("minute", "s1"): [23],
                ("minute", "s2"): [23]
            })
        context = Context(assumption_base, named_state)

        named_state = NamedState(
            attribute_system, p, {
                ("hour", "s1"): [23],
                ("hour", "s2"): [13],
                ("minute", "s1"): [23],
                ("minute", "s2"): [23]
            })

        # A context trivially entails a named state when no world of the
        # context satisfies the context
        assert context.entails_named_state(named_state,
                                           attribute_interpretation)
Exemplo n.º 33
0
    def point_test():
        """Do test with Point object and its parser."""
        from vivid.classes.point import Point
        point = Attribute('point', [Point('x', 'x', 'x', 'x')])
        r_is_on = Relation('R1(h1, h2, h3) <=> is_on(h1, h2, h3)',
                           ['point', 'point', 'point'], 1)
        r_not_same_point = Relation('R2(h1, h2) <=> not_same_point(h1, h2)',
                                    ['point', 'point'], 2)
        r_clocks_unequal = Relation('R3(h1, h2) <=> clocks_unequal(h1, h2)',
                                    ['point', 'point'], 3)
        r_can_observe = Relation(
            'R4(p, sp_loc, wls, wle) <=> can_observe(p, sp_loc, wls, wle)',
            ['point', 'point', 'point', 'point'], 4)

        attribute_structure = AttributeStructure(point, r_is_on,
                                                 r_not_same_point,
                                                 r_clocks_unequal,
                                                 r_can_observe)

        rs_is_on = RelationSymbol('IS_ON', 3)
        rs_not_same_point = RelationSymbol('NOT_SAME_POINT', 2)
        rs_clocks_unequal = RelationSymbol('CLOCKS_UNEQUAL', 2)
        rs_can_observe = RelationSymbol('CAN_OBSERVE', 4)

        vocabulary = Vocabulary(
            ['P1', 'P2', 'P3', 'P4', 'P5'],
            [rs_is_on, rs_not_same_point, rs_clocks_unequal, rs_can_observe],
            [])

        profiles = [[rs_is_on, ('point', 1), ('point', 2), ('point', 3)],
                    [rs_not_same_point, ('point', 1), ('point', 2)],
                    [rs_clocks_unequal, ('point', 1), ('point', 2)],
                    [
                        rs_can_observe, ('point', 1), ('point', 2),
                        ('point', 3), ('point', 4)
                    ]]

        mapping = {
            rs_is_on: 1,
            rs_not_same_point: 2,
            rs_clocks_unequal: 3,
            rs_can_observe: 4
        }

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

        objects = ['p1', 'p2', 'p3', 'p4', 'p5']
        attribute_system = AttributeSystem(attribute_structure, objects)
        p = ConstantAssignment(vocabulary, attribute_system, {
            'P1': 'p1',
            'P2': 'p2',
            'P3': 'p3',
            'P4': 'p4',
            'P5': 'p5'
        })

        world = NamedState(
            attribute_system, p, {
                ('point', 'p1'): [Point(1.5, 1.5, 1.5, 1.5)],
                ('point', 'p2'): [Point(2.0, 2.0, 2.0, 2.0)],
                ('point', 'p3'): [Point(1.0, 1.0, 1.0, 1.0)],
                ('point', 'p4'): [Point(3.0, 3.0, 3.0, 3.0)]
            })

        f1 = Formula(vocabulary, 'IS_ON', 'P1', 'P3', 'P4')
        f2 = Formula(vocabulary, 'NOT_SAME_POINT', 'P1', 'P2')
        f3 = Formula(vocabulary, 'CLOCKS_UNEQUAL', 'P1', 'P2')
        f4 = Formula(vocabulary, 'CAN_OBSERVE', 'P1', 'P2', 'P3', 'P4')

        VA = VariableAssignment(vocabulary, attribute_system, {}, dummy=True)

        assumption_base = AssumptionBase(f2, f3, f4)
        context = Context(assumption_base, world)

        # Whenever we can observe a Point, we're either at that point or on
        # same worldline so IS_ON should hold
        assert context.entails_formula(f1, attribute_interpretation)
        # We always entail our own formulae
        assert context.entails_formula(f2, attribute_interpretation)
        assert context.entails_formula(f3, attribute_interpretation)
        assert context.entails_formula(f4, attribute_interpretation)