Exemple #1
0
def test___getitem__():
    """Test indexing for ValueSet object."""
    def test_TypeError(valueset, index):
        "test TypeError raising for indexing."
        with pytest.raises(TypeError):
            valueset._values[index]

    def test_IndexError(valueset, index):
        "test IndexError raising for indexing."
        with pytest.raises(IndexError):
            valueset._values[index]

    v = ValueSet([1, 2, 'a', 'b', False, True,
                  Interval(100, 1000),
                  Point(1.0)])
    test_TypeError(v, '')
    test_IndexError(v, 8)
    assert v[0] == 1
    assert v[0] is v._values[0]
    assert v[1] == 2
    assert v[1] is v._values[1]
    assert v[2] == 'a'
    assert v[2] is v._values[2]
    assert v[3] == 'b'
    assert v[3] is v._values[3]
    assert v[4] is False
    assert v[4] is v._values[4]
    assert v[5] is True
    assert v[5] is v._values[5]
    assert v[6] == Interval(100, 1000)
    assert v[6] is v._values[6]
    assert v[7] == Point(1.0)
    assert v[7] is v._values[7]
def test_unstringify():
    """Test unstringify function for LineSegment."""
    p1, p2 = Point(1.0), Point(2.0)
    p3, p4 = Point(1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0)
    a = LineSegment(p1, p2)
    b = LineSegment(p3, p4)
    assert a == LineSegment.unstringify("L(P(1.0),P(2.0))")
    assert b == LineSegment.unstringify("L(P(1.0,1.0,1.0),P(2.0,2.0,2.0))")
def test___repr__():
    """Test repr(LineSegment)."""
    p1, p2 = Point(1.0), Point(2.0)
    p3, p4 = Point(1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0)
    a = LineSegment(p1, p2)
    b = LineSegment(p3, p4)
    assert repr(a) == "L(P(1.0),P(2.0))"
    assert repr(b) == "L(P(1.0,1.0,1.0),P(2.0,2.0,2.0))"
def test___deepcopy__():
    """Test copy.deepcopy."""
    from copy import deepcopy
    a = LineSegment(Point(1.0), Point(2.0))
    b = deepcopy(a)

    assert a == b
    assert a is not b
    assert a[0] is not b[0]
    assert a[1] is not b[1]
def test___init__():
    """Test LineSegment constructor."""
    def test_ValueError(start_point, end_point):
        """Test constructor for ValueErrors with given params."""
        with pytest.raises(ValueError) as excinfo:
            LineSegment(start_point, end_point)

    test_ValueError(None, None)
    test_ValueError(None, object)
    test_ValueError(object, object)
    test_ValueError(Point(1.0), None)
    test_ValueError(Point(1.0), Point('x'))
    test_ValueError(Point(1.0), Point(1.0, 1.0))
Exemple #6
0
def test___eq__():
    """Test == operator."""
    VS1, VS2 = ValueSet([]), ValueSet([])
    assert VS1 == VS2
    VS1, VS2 = ValueSet([1]), ValueSet([1])
    assert VS1 == VS2
    # Test out of order
    VS1, VS2 = ValueSet([1, 3, 5]), ValueSet([5, 3, 1])
    assert VS1 == VS2
    VS1 = ValueSet([1, 'b', 3, 'c', 'a', 5])
    VS2 = ValueSet([5, 3, 1, 'a', 'b', 'c'])
    assert VS1 == VS2
    VS1 = ValueSet([1, 'b', 3, 'c', 'a', 5, Interval(1, 10), Point(1.0)])
    VS2 = ValueSet([Interval(1, 10), 5, 3, 1, Point(1.0), 'a', 'b', 'c'])
    assert VS1 == VS2
    VS1 = ValueSet([Point(1.0, 1.0)])
    VS2 = ValueSet([Point(1.0)])
    assert not VS1 == VS2
    # test type mismatch for Interval
    VS1, VS2 = ValueSet([Interval(1, 10)]), ValueSet([Interval(1.0, 10.0)])
    assert VS1 != VS2
    VS1, VS2 = ValueSet([LineSegment(Point(1.0), Point(2.0))]), ValueSet([LineSegment(Point(1.0), Point(2.0))])
    assert VS1 == VS2
    VS1, VS2 = ValueSet([LineSegment(Point(1.0), Point(2.0))]), ValueSet([LineSegment(Point(-1.0), Point(-2.0))])
    assert VS1 != VS2
Exemple #7
0
def test_is_on():
    """Test is_on function used in LRB paper."""
    point = Point(1.0)
    start_point = Point(0.0)
    end_point = Point(3.0)
    assert point.is_on(start_point, end_point)
    assert point.is_on(end_point, start_point)

    point = Point(1.0, 1.0)
    start_point = Point(0.0, 0.0)
    end_point = Point(3.0, 3.0)
    assert point.is_on(start_point, end_point)
    assert point.is_on(end_point, start_point)

    point = Point(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
    start_point = Point(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
    end_point = Point(3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0)
    assert point.is_on(start_point, end_point)
    assert point.is_on(end_point, start_point)

    point = Point(1.000000000000001)
    start_point = Point(0.000000000000001)
    end_point = Point(3.000000000000001)
    assert point.is_on(start_point, end_point)
    assert point.is_on(end_point, start_point)
Exemple #8
0
def test___deepcopy__():
    """Test copy.deepcopy for Point object."""
    from copy import deepcopy

    p1 = Point(1.0)
    p2 = Point(1.0, 2.0)

    p1_copy = deepcopy(p1)
    p2_copy = deepcopy(p2)

    assert p1 == p1_copy
    assert p1 is not p1_copy
    assert p2 == p2_copy
    assert p2 is not p2_copy
def test_meets():
    """Test meets wrapper for Point function."""
    a = LineSegment(Point(0.0, 0.0), Point(5.0, 5.0))
    b = LineSegment(Point(0.0, 0.0), Point(5.0, 5.0))
    p1 = Point(0.0, 0.0)
    p2 = Point(1.0, 1.0)
    p3 = Point(5.0, 5.0)
    assert LineSegment.meets(p1, a, b)
    assert LineSegment.meets(p2, a, b)
    assert LineSegment.meets(p3, a, b)
    c = LineSegment(Point(5.0, 0.0), Point(0.0, 5.0))
    p4 = Point(2.5, 2.5)
    assert not LineSegment.meets(p1, a, c)
    assert not LineSegment.meets(p2, a, c)
    assert not LineSegment.meets(p3, a, c)
    assert LineSegment.meets(p4, a, c)
Exemple #10
0
def test_can_observe():
    """Test can_observe function used in LRB paper."""
    point = Point(1.0, 1.0, 1.0, 1.0)
    spacetime_loc = Point(0.0, 0.0, 0.0, 0.0)
    worldline_start = Point(-10.0, -10.0, -10.0, -10.0)
    worldline_end = Point(10.0, 10.0, 10.0, 10.0)
    assert point.can_observe(spacetime_loc, worldline_start, worldline_end)
    spacetime_loc = Point(1.0, 1.0, 1.0, 1.0)
    assert point.can_observe(spacetime_loc, worldline_start, worldline_end)
Exemple #11
0
def test___repr__():
    """Test repr() for ValueSet object."""
    v1 = ValueSet([1, 3, 5, 'a', 'b', 'c', False, True,
                  Interval(100, 105), Interval(2.0, 10.0),
                  Point(1.0)])
    v2 = ValueSet([Interval(100, 105),
                  5, 3, 1,
                  Point(1.0),
                  'a',
                   Interval(2.0, 10.0),
                   'b', True, 'c', False])
    v3 = ValueSet([])
    assert v1.__repr__() == "V(1, 3, 5, a, b, c, False, True, I(100, 105), I(2.0, 10.0), P(1.0))"
    # test out of order
    assert v2.__repr__() == "V(1, 3, 5, a, b, c, False, True, I(100, 105), I(2.0, 10.0), P(1.0))"
    # test empty
    assert v3.__repr__() == "V()"
Exemple #12
0
def test___iter__():
    """Test iterator for ValueSet object."""
    v = ValueSet([Interval(100, 105),
                  5, 3, 1, 'a',
                  Interval(2.0, 10.0),
                  'b', True, 'c', False,
                  Point(1.0)])
    assert [i for i in v.__iter__()] == v._values
Exemple #13
0
def test___len__():
    """Test len() function for ValueSet object."""
    v = ValueSet([Interval(100, 105), 5, 3, 1, 'a',
                  Interval(2.0, 10.0),
                  'b', True, 'c', False,
                  Point(1.0)])
    v2 = ValueSet([])
    assert len(v) == 11
    assert len(v2) == 0
Exemple #14
0
def test___nonzero__():
    """Test boolean behavior for ValueSet."""
    v = ValueSet([Interval(100, 105),
                  5, 3, 1, 'a',
                  Interval(2.0, 10.0),
                  'b', True, 'c', False,
                  Point(1.0)])
    assert v
    v2 = ValueSet([])
    assert not v2
Exemple #15
0
def test_meets():
    """Test meets function used in LRB paper."""
    spacetime_loc = Point(0.0, 0.0, 0.0, 0.0)
    worldline_1_start = Point(-10.0, -10.0, -10.0, -10.0)
    worldline_1_end = Point(10.0, 10.0, 10.0, 10.0)
    worldline_2_start = Point(10.0, 10.0, 10.0, 10.0)
    worldline_2_end = Point(-10.0, -10.0, -10.0, -10.0)
    assert spacetime_loc.meets(
        worldline_1_start, worldline_1_end, worldline_2_start, worldline_2_end)
Exemple #16
0
def test_unstringify():
    """Test Point object reconstruction."""
    def test_ValueError(point_string):
        """Test constructor for ValueErrors with given params."""
        with pytest.raises(ValueError) as excinfo:
            Point.unstringify(point_string)

    test_ValueError('asdf')
    test_ValueError('P()')
    test_ValueError('P(xx)')
    test_ValueError('P(,2)')

    p1 = Point('x')
    p2 = Point('x', 'x')
    p3 = Point(1.0)
    p4 = Point(1.0, 1.0)
    assert Point.unstringify(str(p1)) == Point('x')
    assert Point.unstringify(str(p2)) == Point('x', 'x')
    assert Point.unstringify(str(p3)) == Point(1.0)
    assert Point.unstringify(str(p4)) == Point(1.0, 1.0)
Exemple #17
0
def test___deepcopy__():
    """Test copy.deepcopy for ValueSet object."""
    import copy
    v = ValueSet([1, 3, 5, 'a', 'b', 'c', False, True,
                  Interval(100, 105), Interval(2.0, 10.0),
                  Point(1.0)])
    v_copy = copy.deepcopy(v)
    assert v == v_copy
    assert v is not v_copy
    assert v[8] is not v_copy[8]
    assert v[9] is not v_copy[9]
    assert v[10] is not v_copy[10]
Exemple #18
0
def test___eq__():
    """Test == operator for Point object."""
    p1 = Point(1.0)
    p2 = Point(1.0)
    p3 = Point(1.0, 2.0, 3.0)
    p4 = Point(1.0, 2.0, 3.0)
    p5 = Point('x')
    p6 = Point('x')
    p7 = Point('x', 'x', 'x')
    p8 = Point('x', 'x', 'x')

    assert p1 == p2
    assert p3 == p4
    assert p5 == p6
    assert p7 == p8
Exemple #19
0
def test___contains__():
    """Test in operator for ValueSet object."""
    v = ValueSet([1, 2, 'a', 'b', False, True,
                  Interval(100, 1000),
                  Point(1.0), LineSegment(Point(0.0), Point(1.0))])
    assert 1 in v
    assert 2 in v
    assert 'a' in v
    assert 'b' in v
    assert False in v
    assert True in v
    assert LineSegment(Point(0.0), Point(1.0)) in v
    assert not LineSegment(Point(10.0), Point(1.0)) in v
    assert Interval(100, 1000) in v
    assert Point(1.0) in v
    assert not Interval(400, 500) in v
    assert not Interval(100.0, 1000.0) in v
    assert not Interval(100L, 1000L) in v
    assert not Point('x') in v
def test___getitem__():
    """Test indexing."""
    def test_TypeError(index, segment):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            segment[index]

    def test_IndexError(index, segment):
        """Test constructor for IndexErrors with given params."""
        with pytest.raises(IndexError) as excinfo:
            segment[index]

    p1, p2 = Point(1.0), Point(2.0)
    a = LineSegment(p1, p2)
    assert a[0] == p1
    assert a[1] == p2

    test_TypeError(a, a)
    test_TypeError('', a)
    test_TypeError(None, a)
    test_TypeError(object, a)
    test_IndexError(-1, a)
    test_IndexError(2, a)
Exemple #21
0
def test_not_same_point():
    """Test not_same_point function used in LRB paper."""
    point_1 = Point(1.0)
    point_2 = Point(0.0)
    assert point_1.not_same_point(point_2)

    point_1 = Point(1.0, 1.0)
    point_2 = Point(0.0, 0.0)
    assert point_1.not_same_point(point_2)

    point_1 = Point(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
    point_2 = Point(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
    assert point_1.not_same_point(point_2)

    point_1 = Point(1.000000000000001)
    point_2 = Point(0.000000000000001)
    assert point_1.not_same_point(point_2)
Exemple #22
0
def test_clocks_unequal():
    """Test clocks_unequal function used in LRB paper."""

    point_1 = Point(1.0, 1.0)
    point_2 = Point(0.0, 0.0)
    assert point_1.clocks_unequal(point_2)

    point_1 = Point(1.0, 1.0, 1.0, 1.0)
    point_2 = Point(0.0, 0.0, 0.0, 0.0)
    assert point_1.clocks_unequal(point_2)

    point_1 = Point(0.0, 1.000000000000001)
    point_2 = Point(0.0, 0.000000000000001)
    assert point_1.clocks_unequal(point_2)
Exemple #23
0
def test___setitem__():
    """Test ValueSet[key] = value assignment for ValueSet object."""
    def test_TypeError(valueset, key, value):
        """Test TypeError raising in index assignment."""
        with pytest.raises(TypeError) as excinfo:
            valueset[key] = value

    def test_ValueError(valueset, key, value):
        """Test ValueError raising in index assignment."""
        with pytest.raises(ValueError) as excinfo:
            valueset[key] = value

    def test_AttributeError(valueset, key, value):
        """Test AttributeError raising in index assignment."""
        with pytest.raises(AttributeError) as excinfo:
            valueset[key] = value

    def test_IndexError(valueset, key, value):
        """Test IndexError raising in index assignment."""
        with pytest.raises(IndexError) as excinfo:
            valueset[key] = value

    v = ValueSet([1, 3, 5, 'a', 'b', 'c', False, True,
                  Interval(100, 105),
                  Interval(2.0, 10.0),
                  Point(1.0)])

    v[0] = 1000
    assert v[0] == 1000
    # test invalid key types
    test_TypeError(v, '', 1)
    test_TypeError(v, object, 1)
    test_TypeError(v, None, 1)
    test_TypeError(v, 1.0, 1)
    test_TypeError(v, 1L, 1)
    # test duplicate value catching
    test_ValueError(v, 1, 1)
    test_IndexError(v, 11, -37)
    test_TypeError(v, 1, object)
Exemple #24
0
def test___init__():
    """Test Point constructor."""
    def test_TypeError(*dimension_values):
        """Test constructor for TypeErrors with given params."""
        with pytest.raises(TypeError) as excinfo:
            Point(*dimension_values)

    def test_ValueError(*dimension_values):
        """Test constructor for ValueErrors with given params."""
        with pytest.raises(ValueError) as excinfo:
            Point(*dimension_values)

    test_ValueError()
    test_TypeError(1)
    test_TypeError('')
    test_TypeError(None)
    test_TypeError(object)

    coords = (1.0, 2.0, 3.0)
    p = Point(*coords)

    assert p._coordinate is not coords
Exemple #25
0
def test___init__():
    """Test ValueSet Constructor"""
    def test_TypeError(valueset):
        """Test an individual ValueSet construction."""
        with pytest.raises(TypeError) as excinfo:
            ValueSet(valueset)

    # test value_set errors
    test_TypeError(1)
    test_TypeError(1.0)
    test_TypeError("")
    test_TypeError(object)
    test_TypeError(Interval(0, 10))
    test_TypeError(Point(0.0))

    VS1 = ValueSet([Interval(20, 100)])
    VS = ValueSet([LineSegment(Point(1.0), Point(2.0)),
                   LineSegment(Point(1.0), Point(2.0))])

    assert VS == ValueSet([LineSegment(Point(1.0), Point(2.0))])
def test___ne__():
    """Test != operator."""
    assert not LineSegment(Point(1.0), Point(1.0)) != LineSegment(Point(1.0), Point(1.0))
    assert LineSegment(Point(1.0), Point(1.0)) != LineSegment(Point('x'), Point('x'))
    assert LineSegment(Point(1.0), Point(1.0)) != LineSegment(Point(1.0, 1.0), Point(1.0, 1.0))
def test_total_ordering():
    """Test ."""
    a = LineSegment(Point(1.0), Point(2.0))
    b = LineSegment(Point(0.0), Point(2.0))
    c = LineSegment(Point(1.0), Point(3.0))
    d = LineSegment(Point(0.0), Point(3.0))
    e = LineSegment(Point('x'), Point('x'))

    assert a <= a
    assert not a < a
    assert a >= a
    assert not a > a
    assert a <= b
    assert a < b
    assert a <= c
    assert a < c
    assert a <= d
    assert a < d
    assert a <= e
    assert a < e
    assert b >= a
    assert b > a
    assert c >= a
    assert c > a
    assert d >= a
    assert d > a
    assert e >= a
    assert e > a
    assert a < b < d < e

    a = LineSegment(Point(1.0, 1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0, 2.0))
    b = LineSegment(Point(0.0, 0.0, 0.0, 0.0), Point(2.0, 2.0, 2.0, 2.0))
    c = LineSegment(Point(1.0, 1.0, 1.0, 1.0), Point(3.0, 3.0, 3.0, 3.0))
    d = LineSegment(Point(0.0, 0.0, 0.0, 0.0), Point(3.0, 3.0, 3.0, 3.0))
    e = LineSegment(Point('x', 'x', 'x', 'x'), Point('x', 'x', 'x', 'x'))

    assert a <= a
    assert not a < a
    assert a >= a
    assert not a > a
    assert a <= b
    assert a < b
    assert a <= c
    assert a < c
    assert a <= d
    assert a < d
    assert a <= e
    assert a < e
    assert b >= a
    assert b > a
    assert c >= a
    assert c > a
    assert d >= a
    assert d > a
    assert e >= a
    assert e > a
    assert a < b < d < e

    a = LineSegment(Point(1.0, 1.0), Point(2.0, 2.0))
    b = LineSegment(Point(0.0, 1.0), Point(2.0, 2.0))

    assert not a < b
    assert not a > b
    assert not a <= b
    assert not a >= b
Exemple #28
0
def test__key():
    """Test key of Point used for hashing."""
    coords = (1.0, 2.0, 3.0)
    p = Point(*coords)
    assert p._key() == coords
Exemple #29
0
 def test_ValueError(point_string):
     """Test constructor for ValueErrors with given params."""
     with pytest.raises(ValueError) as excinfo:
         Point.unstringify(point_string)
def test__key():
    """Test key used in hashing."""
    a = LineSegment(Point(1.0), Point(2.0))
    assert a._key() == (Point(1.0), Point(2.0))
def test___eq__():
    """Test == operator."""
    assert LineSegment(Point(1.0), Point(1.0)) == LineSegment(Point(1.0), Point(1.0))
    assert not LineSegment(Point(1.0), Point(1.0)) == LineSegment(Point('x'), Point('x'))
    assert not LineSegment(Point(1.0), Point(1.0)) == LineSegment(Point(1.0, 1.0), Point(1.0, 1.0))
Exemple #32
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)
def test___hash__():
    """Test hash(LineSegment)."""
    p1, p2 = Point(1.0), Point(2.0)
    a = LineSegment(p1, p2)
    assert hash(a) == 2847735836036288514
Exemple #34
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)
Exemple #35
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))
def test___contains__():
    """Test in operator."""
    def test_AttributeError(member, segment):
        """Test constructor for AttributeErrors with given params."""
        with pytest.raises(AttributeError) as excinfo:
            member in segment

    a = LineSegment(Point(1.0), Point(2.0))
    b = LineSegment(Point(0.0), Point(2.0))
    c = LineSegment(Point(1.0), Point(3.0))
    d = LineSegment(Point(0.0), Point(3.0))
    e = LineSegment(Point('x'), Point('x'))
    p = Point(1.5)

    test_AttributeError('', a)
    test_AttributeError(None, a)
    test_AttributeError(object, a)

    assert a in a
    assert a in b
    assert b not in c
    assert a in c
    assert a in d
    assert a in e
    assert p in a
    assert p in b
    assert p in c
    assert p in d
    assert p in e

    a = LineSegment(Point(1.0, 1.0, 1.0), Point(2.0, 2.0, 2.0))
    b = LineSegment(Point(0.0, 0.0, 0.0), Point(2.0, 2.0, 2.0))
    c = LineSegment(Point(1.0, 1.0, 1.0), Point(3.0, 3.0, 3.0))
    d = LineSegment(Point(0.0, 0.0, 0.0), Point(3.0, 3.0, 3.0))
    e = LineSegment(Point('x', 'x', 'x'), Point('x', 'x', 'x'))
    p = Point(1.5, 1.5, 1.5)

    assert a in a
    assert a in b
    assert b not in c
    assert a in c
    assert a in d
    assert a in e
    assert p in a
    assert p in b
    assert p in c
    assert p in d
    assert p in e