示例#1
0
def test_mutiplication_with_variable():
    v = Variable(3)
    o = Variable(2)

    assert v * o == 6
    assert v / o == 1.5
    assert v // o == 1
示例#2
0
def test_power():
    v = Variable(3)
    o = Variable(2)

    assert v**2 == 9
    assert 2**v == 8
    assert v**o == 9
示例#3
0
 def __init__(self):
     self.solver = Solver()
     self.a = Variable(1, 30)
     self.b = Variable(2, 10)
     self.c = Variable(3, 10)
     self.c_eq = EquationConstraint(
         lambda a, b, c: a + b + c, a=self.a, b=self.b, c=self.c
     )
     self.solver.add_constraint(self.c_eq)
示例#4
0
def test_modulo():
    v = Variable(3)
    o = Variable(2)

    assert v % 2 == 1
    assert 4 % v == 1
    assert v % o == 1
    assert divmod(v, 2) == (1, 1)
    assert divmod(4, v) == (1, 1)
    assert divmod(v, o) == (1, 1)
示例#5
0
def test_set_xy():
    pos = Position((1, 2))
    x = Variable()
    y = Variable()
    assert x is not pos.x
    assert y is not pos.y

    pos.set_x(x)
    pos.set_y(y)
    assert x is pos.x
    assert y is pos.y
示例#6
0
    def test_strength_change(self):
        """Test strength change"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        b.strength = 9
        self.assertEqual(c_eq._weakest, [b])
示例#7
0
    def test_strength_change(self):
        """Test strength change"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        b.strength = 9
        self.assertEqual(c_eq._weakest, [b])
示例#8
0
def test_line_constraint(item_pos):
    """Test line creation constraint.

    """
    line = (Variable(3), Variable(4)), (Variable(5), Variable(6))
    item_pos.item.constraint(line=(item_pos.pos1, line))
    assert 1 == len(item_pos.item._constraints)

    c = item_pos.item._constraints[0]
    assert isinstance(c, LineConstraint)
    assert (1, 2) == c._point
    assert ((3, 4), (5, 6)) == c._line
示例#9
0
def test_juggle_error_is_raised_when_constraints_can_not_be_resolved():
    solver = Solver()
    a = Variable()
    b = Variable()
    c = Variable(40, strength=REQUIRED)
    d = Variable(30, strength=REQUIRED)

    solver.add_constraint(EqualsConstraint(a, b))
    solver.add_constraint(EqualsConstraint(a, c))
    solver.add_constraint(EqualsConstraint(b, d))

    with pytest.raises(JuggleError):
        solver.solve()
示例#10
0
    def test_weakest_list(self):
        """Test weakest list"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        # because of kwargs above we cannot test by the order of arguments
        self.assertTrue(b in c_eq._weakest)
        self.assertTrue(c in c_eq._weakest)
示例#11
0
    def test_weakest_list_order(self):
        """Test weakest list order"""
        solver = Solver()
        a = Variable(1, 30)
        b = Variable(2, 10)
        c = Variable(3, 10)

        c_eq = EquationConstraint(lambda a, b, c: a + b + c, a=a, b=b, c=c)
        solver.add_constraint(c_eq)

        weakest = [el for el in c_eq._weakest]
        a.value = 4

        self.assertEqual(c_eq._weakest, weakest) # does not change if non-weak variable changed

        b.value = 5
        self.assertEqual(c_eq.weakest(), c)

        b.value = 6
        self.assertEqual(c_eq.weakest(), c)

        # b changed above, now change a - all weakest variables changed
        # return the oldest changed variable then
        c.value = 6
        self.assertEqual(c_eq.weakest(), b)

        b.value = 6
        self.assertEqual(c_eq.weakest(), c)
示例#12
0
    def test_left_of_constraint(self):
        """
        Test "less than" constraint (horizontal) creation.
        """
        item = Item()
        p1 = Variable(1), Variable(2)
        p2 = Variable(3), Variable(4)
        item.constraint(left_of=(p1, p2))
        self.assertEquals(1, len(item._constraints))

        c = item._constraints[0]
        self.assertTrue(isinstance(c, LessThanConstraint))
        self.assertEquals(1, c.smaller)
        self.assertEquals(3, c.bigger)
示例#13
0
    def test_above_constraint(self):
        """
        Test "less than" constraint (vertical) creation.
        """
        item = Item()
        p1 = Variable(1), Variable(2)
        p2 = Variable(3), Variable(4)
        item.constraint(above=(p1, p2))
        self.assertEquals(1, len(item._constraints))

        c = item._constraints[0]
        self.assertTrue(isinstance(c, LessThanConstraint))
        self.assertEquals(2, c.smaller)
        self.assertEquals(4, c.bigger)
示例#14
0
def test_equality():
    v = Variable(3)
    w = Variable(3)
    o = Variable(2)

    assert v == 3
    assert 3 == v
    assert v == w
    assert not v == o

    assert v != 2
    assert 2 != v
    assert not 3 != v
    assert v != o
示例#15
0
    def test_horizontal_constraint(self):
        """
        Test horizontal constraint creation.
        """
        item = Item()
        p1 = Variable(1), Variable(2)
        p2 = Variable(3), Variable(4)
        item.constraint(horizontal=(p1, p2))
        self.assertEqual(1, len(item._constraints))

        c = item._constraints[0]
        self.assertTrue(isinstance(c, EqualsConstraint))
        # expect constraint on y-axis
        self.assertEqual(2, c.a)
        self.assertEqual(4, c.b)
示例#16
0
    def test_vertical_constraint(self):
        """
        Test vertical constraint creation.
        """
        item = Item()
        p1 = Variable(1), Variable(2)
        p2 = Variable(3), Variable(4)
        item.constraint(vertical=(p1, p2))
        self.assertEquals(1, len(item._constraints))

        c = item._constraints[0]
        self.assertTrue(isinstance(c, EqualsConstraint))
        # expect constraint on x-axis
        self.assertEquals(1, c.a)
        self.assertEquals(3, c.b)
示例#17
0
文件: state.py 项目: DLR-RM/RAFCON
    def __init__(self, state_m, size, custom_background_color,
                 background_color, hierarchy_level):
        super(StateView, self).__init__(size[0], size[1])
        assert isinstance(state_m, AbstractStateModel)
        # Reapply size, as Gaphas sets default minimum size to 1, which is too large for highly nested states
        self.min_width = self.min_height = 0
        self.width = size[0]
        self.height = size[1]
        self.custom_background_color = custom_background_color
        self.background_color = background_color
        self.background_changed = False

        self._c_min_w = self._constraints[0]
        self._c_min_h = self._constraints[1]

        self.is_root_state_of_library = state_m.state.is_root_state_of_library

        self._state_m = ref(state_m)
        self.hierarchy_level = hierarchy_level

        self._income = None
        self._outcomes = []
        self._inputs = []
        self._outputs = []
        self._scoped_variables = []
        self._scoped_variables_ports = []

        self.keep_rect_constraints = {}
        self.port_constraints = {}

        self._moving = False

        self._view = None

        self.__symbol_size_cache = {}
        self._image_cache = ImageCache()

        self._border_width = Variable(
            min(self.width, self.height) /
            constants.BORDER_WIDTH_STATE_SIZE_FACTOR)
        border_width_constraint = BorderWidthConstraint(
            self._handles[NW].pos, self._handles[SE].pos, self._border_width,
            constants.BORDER_WIDTH_STATE_SIZE_FACTOR)
        self._constraints.append(border_width_constraint)

        # Initialize NameView
        name_meta = state_m.get_meta_data_editor()['name']
        if not contains_geometric_info(name_meta['size']):
            name_width = self.width - 2 * self._border_width
            name_height = self.height * 0.4
            name_meta = state_m.set_meta_data_editor(
                'name.size', (name_width, name_height))['name']
        name_size = name_meta['size']

        self._name_view = NameView(state_m.state.name, name_size)

        if not contains_geometric_info(name_meta['rel_pos']):
            name_meta['rel_pos'] = (self.border_width, self.border_width)
        name_pos = name_meta['rel_pos']
        self.name_view.matrix.translate(*name_pos)
示例#18
0
def test_add_to_variable():
    v = Variable(3)

    assert v + 1 == 4
    assert v - 1 == 2
    assert 1 + v == 4
    assert 4 - v == 1
示例#19
0
def test_presentation_should_unlink_when_diagram_changes(diagram):
    presentation = diagram.create(Example)
    diagram.connections.add_constraint(presentation,
                                       BaseConstraint(Variable()))
    assert len(list(
        diagram.connections.get_connections(item=presentation))) == 1

    presentation.diagram = None

    assert not list(diagram.connections.get_connections(item=presentation))
示例#20
0
def test_variable_decorator_set_variable():
    class A:
        x = variable(varname="sx", strength=STRONG)

    a = A()
    v = Variable(4)
    a.x = v

    assert a.x == 4
    assert a.x is not v
示例#21
0
def test_mutiplication():
    v = Variable(3)

    assert v * 2 == 6
    assert v / 2 == 1.5
    assert v // 2 == 1

    assert 2 * v == 6
    assert 4.5 / v == 1.5
    assert 4 // v == 1
示例#22
0
    def test_min_size(self):
        """Test minimal size constraint"""
        solver = Solver()
        v1 = Variable(0)
        v2 = Variable(10)
        v3 = Variable(10)
        c1 = EqualsConstraint(a=v2, b=v3)
        c2 = LessThanConstraint(smaller=v1, bigger=v3, delta=10)
        solver.add_constraint(c1)
        solver.add_constraint(c2)

        # check everyting is ok on start
        solver.solve()
        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)

        # change v1 to 2, after solve it should be 0 again due to LT
        # constraint
        v1.value = 2
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)

        # change v3 to 20, after solve v2 will follow thanks to EQ
        # constraint
        v3.value = 20
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(20, v2)
        self.assertEquals(20, v3)

        # change v3 to 0, after solve it shoul be 10 due to LT.delta = 10,
        # v2 should also be 10 due to EQ constraint
        v3.value = 0
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)
示例#23
0
def test_min_size(solv):
    """Test minimal size constraint.

    """
    v1 = Variable(0)
    v2 = Variable(10)
    v3 = Variable(10)
    c1 = EqualsConstraint(a=v2, b=v3)
    c2 = LessThanConstraint(smaller=v1, bigger=v3, delta=10)
    solv.solver.add_constraint(c1)
    solv.solver.add_constraint(c2)

    # Check everything is ok on start
    solv.solver.solve()
    assert 0 == v1
    assert 10 == v2
    assert 10 == v3

    # Change v1 to 2, after solve it should be 0 again due to LT constraint
    v1.value = 2
    solv.solver.solve()

    assert 0 == v1
    assert 10 == v2
    assert 10 == v3

    # Change v3 to 20, after solve v2 will follow thanks to EQ constraint
    v3.value = 20
    solv.solver.solve()

    assert 0 == v1
    assert 20 == v2
    assert 20 == v3

    # Change v3 to 0, after solve it should be 10 due to LT.delta = 10, v2
    # should also be 10 due to EQ constraint
    v3.value = 0
    solv.solver.solve()

    assert 0 == v1
    assert 10 == v2
    assert 10 == v3
示例#24
0
def test_notify_for_nested_constraint():
    events = []
    solver = Solver()
    a = Variable()
    b = Variable()
    nested = EqualsConstraint(a, b)
    multi = MultiConstraint(nested)

    solver.add_constraint(multi)
    solver.solve()

    def handler(constraint):
        events.append(constraint)

    solver.add_handler(handler)

    a.value = 10

    solver.solve()

    assert multi in events
    assert nested not in events
示例#25
0
def test_comparison():
    v = Variable(3)

    assert v > 2
    assert v < 4
    assert v >= 2
    assert v >= 3
    assert v <= 4
    assert v <= 3

    assert not v > 3
    assert not v < 3
    assert not v <= 2
    assert not v >= 4
示例#26
0
def test_inverse_comparison():
    v = Variable(3)

    assert 4 > v
    assert 2 < v
    assert 4 >= v
    assert 3 >= v
    assert 2 <= v
    assert 3 <= v

    assert not 3 > v
    assert not 3 < v
    assert not 4 <= v
    assert not 2 >= v
示例#27
0
def test_pos_constraint():
    """Test position constraint"""
    x1, y1 = Variable(10), Variable(11)
    x2, y2 = Variable(12), Variable(13)
    pc = PositionConstraint(origin=(x1, y1), point=(x2, y2))
    pc.solve_for()

    # origin shall remain the same
    assert 10 == x1
    assert 11 == y1

    # point shall be moved to origin
    assert 10 == x2
    assert 11 == y2

    # change just x of origin
    x1.value = 15
    pc.solve_for()
    assert 15 == x2

    # change just y of origin
    y1.value = 14
    pc.solve_for()
    assert 14 == y2
    def test_pos_constraint(self):
        """Test position constraint"""
        x1, y1 = Variable(10), Variable(11)
        x2, y2 = Variable(12), Variable(13)
        pc = PositionConstraint(origin=(x1, y1), point=(x2, y2))
        pc.solve_for()

        # origin shall remain the same
        self.assertEquals(10, x1)
        self.assertEquals(11, y1)

        # point shall be moved to origin
        self.assertEquals(10, x2)
        self.assertEquals(11, y2)

        # change just x of origin
        x1.value = 15
        pc.solve_for()
        self.assertEquals(15, x2)

        # change just y of origin
        y1.value = 14
        pc.solve_for()
        self.assertEquals(14, y2)
示例#29
0
    def test_min_size(self):
        """Test minimal size constraint"""
        solver = Solver()
        v1 = Variable(0)
        v2 = Variable(10)
        v3 = Variable(10)
        c1 = EqualsConstraint(a=v2, b=v3)
        c2 = LessThanConstraint(smaller=v1, bigger=v3, delta=10)
        solver.add_constraint(c1)
        solver.add_constraint(c2)

        # check everyting is ok on start
        solver.solve()
        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)

        # change v1 to 2, after solve it should be 0 again due to LT
        # constraint
        v1.value = 2
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)

        # change v3 to 20, after solve v2 will follow thanks to EQ
        # constraint
        v3.value = 20
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(20, v2)
        self.assertEquals(20, v3)

        # change v3 to 0, after solve it shoul be 10 due to LT.delta = 10,
        # v2 should also be 10 due to EQ constraint
        v3.value = 0
        solver.solve()

        self.assertEquals(0, v1)
        self.assertEquals(10, v2)
        self.assertEquals(10, v3)
示例#30
0
def test_min_size(solv):
    """Test minimal size constraint.

    """
    v1 = Variable(0)
    v2 = Variable(10)
    v3 = Variable(10)
    c1 = EqualsConstraint(a=v2, b=v3)
    c2 = LessThanConstraint(smaller=v1, bigger=v3, delta=10)
    solv.solver.add_constraint(c1)
    solv.solver.add_constraint(c2)

    # Check everything is ok on start
    solv.solver.solve()
    assert 0 == v1
    assert 10 == v2
    assert 10 == v3

    # Change v1 to 2, after solve it should be 0 again due to LT constraint
    v1.value = 2
    solv.solver.solve()

    assert 0 == v1
    assert 10 == v2
    assert 10 == v3

    # Change v3 to 20, after solve v2 will follow thanks to EQ constraint
    v3.value = 20
    solv.solver.solve()

    assert 0 == v1
    assert 20 == v2
    assert 20 == v3

    # Change v3 to 0, after solve it should be 10 due to LT.delta = 10, v2
    # should also be 10 due to EQ constraint
    v3.value = 0
    solv.solver.solve()

    assert 0 == v1
    assert 10 == v2
    assert 10 == v3
示例#31
0
    def test_line_constraint(self):
        """
        Test line creation constraint.
        """
        item = Item()
        pos = Variable(1), Variable(2)
        line = (Variable(3), Variable(4)), (Variable(5), Variable(6))
        item.constraint(line=(pos, line))
        self.assertEquals(1, len(item._constraints))

        c = item._constraints[0]
        self.assertTrue(isinstance(c, LineConstraint))
        self.assertEquals((1, 2), c._point)
        self.assertEquals(((3, 4), (5, 6)), c._line)
示例#32
0
def test_delta_below_zero():
    """Test line align constraint with delta below zero."""
    line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
    point = (Variable(15), Variable(10))
    lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=-5)
    lc.solve_for()
    assert round(abs(10.84 - point[0].value), 2) == 0
    assert round(abs(7.23 - point[1].value), 2) == 0

    line[1][0].value = 40
    line[1][1].value = 30
    lc.solve_for()
    assert round(abs(16.0 - point[0].value), 2) == 0
    assert round(abs(12.00 - point[1].value), 2) == 0
示例#33
0
def test_delta():
    """Test line align constraint delta."""
    line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
    point = (Variable(15), Variable(10))
    lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=5)
    lc.solve_for()
    assert round(abs(19.16 - point[0].value), 2) == 0
    assert round(abs(12.77 - point[1].value), 2) == 0

    line[1][0].value = 40
    line[1][1].value = 30
    lc.solve_for()
    assert round(abs(24.00 - point[0].value), 2) == 0
    assert round(abs(18.00 - point[1].value), 2) == 0
示例#34
0
    def test_delta(self):
        """Test line align delta
        """
        line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
        point = (Variable(15), Variable(10))
        lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=5)
        lc.solve_for()
        self.assertAlmostEqual(19.16, point[0].value, 2)
        self.assertAlmostEqual(12.77, point[1].value, 2)

        line[1][0].value = 40
        line[1][1].value =  30
        lc.solve_for()
        self.assertAlmostEqual(24.00, point[0].value, 2)
        self.assertAlmostEqual(18.00, point[1].value, 2)
示例#35
0
    def test_delta_below_zero(self):
        """Test line align with delta below zero
        """
        line = (Variable(0), Variable(0)), (Variable(30), Variable(20))
        point = (Variable(15), Variable(10))
        lc = LineAlignConstraint(line=line, point=point, align=0.5, delta=-5)
        lc.solve_for()
        self.assertAlmostEqual(10.84, point[0].value, 2)
        self.assertAlmostEqual(7.23, point[1].value, 2)

        line[1][0].value = 40
        line[1][1].value =  30
        lc.solve_for()
        self.assertAlmostEqual(16.0, point[0].value, 2)
        self.assertAlmostEqual(12.00, point[1].value, 2)
示例#36
0
def test_pos_constraint():
    """Test position constraint."""
    x1, y1 = Variable(10), Variable(11)
    x2, y2 = Variable(12), Variable(13)
    pc = PositionConstraint(origin=(x1, y1), point=(x2, y2))
    pc.solve_for()

    # origin shall remain the same
    assert 10 == x1
    assert 11 == y1

    # point shall be moved to origin
    assert 10 == x2
    assert 11 == y2

    # change just x of origin
    x1.value = 15
    pc.solve_for()
    assert 15 == x2

    # change just y of origin
    y1.value = 14
    pc.solve_for()
    assert 14 == y2