Esempio n. 1
0
 def test_exec_pplan_create_with_one_marked_cn(self):
   v = Variable("v", 1, self.cs)
   cn = Constraint(lambda x: x == 5, Strength.STRONG, v, Method([], v, lambda: 5))
   self.cs.add_constraint(cn)
   cn.mark = self.cs.mark
   self.cs.exec_roots = [cn]
   self.assertEqual([], self.cs.exec_pplan_create())
Esempio n. 2
0
  def test_pplan_add_for_one_constraint(self):
    v = Variable("v", 1, self.constraint_system)
    constraint = Constraint(lambda x: x == 5, Strength.STRONG, v, Method([], v, lambda: 5))
    self.constraint_system.add_constraint(constraint)

    self.assertTrue(constraint, v.determined_by)
    self.assertEqual(
      constraint.add_to_pplan([], self.constraint_system.marker.new_mark()),
      [constraint],
      "should contain only the constraint")
Esempio n. 3
0
class ConstraintTests(TestCase):
    def setUp(self):
        self.constraint_system = ConstraintSystem()
        self.variables = self.constraint_system.create_variables(
            ["v1", "v2", "v3"], [4, 5, 3])
        self.v1, self.v2, self.v3 = self.variables
        method1_2 = Method(self.v1, self.v2, lambda x: x // 2)
        method1_3 = Method(self.v1, self.v3, lambda x: x // 3)

        self.constraint = Constraint(lambda v1, v2, v3: True, Strength.STRONG,
                                     self.variables, [method1_3, method1_2])

        self.constraint_system.add_constraint(self.constraint)

    def tearDown(self):
        pass

    def test_adding_enforced_to_pplan(self):
        self.assertIsNone(self.constraint.mark)

        mark = self.constraint_system.marker.new_mark()
        pplan = self.constraint.add_to_pplan([], mark)

        self.assertEqual([self.constraint], pplan)
        self.assertEqual(mark, self.constraint.mark)

    def test_adding_unenforced_to_pplan(self):
        self.constraint.selected_method = None
        self.assertIsNone(self.constraint.mark)

        pplan = self.constraint.add_to_pplan(
            [], self.constraint_system.marker.new_mark())

        self.assertEqual([], pplan)
        self.assertIsNone(self.constraint.mark)

    def test_adding_with_the_same_mark(self):

        mark = self.constraint_system.marker.new_mark()
        self.constraint.mark = mark
        pplan = self.constraint.add_to_pplan([], mark)

        self.assertEqual([], pplan)
        self.assertEqual(mark, self.constraint.mark)

    def test_adding_with_other_mark(self):

        mark1 = self.constraint_system.marker.new_mark()
        mark2 = self.constraint_system.marker.new_mark()
        self.constraint.mark = mark1
        pplan = self.constraint.add_to_pplan([], mark2)

        self.assertEqual([self.constraint], pplan)
        self.assertEqual(mark2, self.constraint.mark)
Esempio n. 4
0
    def test_pplan_add_for_one_constraint(self):
        v = Variable("v", 1, self.constraint_system)
        constraint = Constraint(lambda x: x == 5, Strength.STRONG, v,
                                Method([], v, lambda: 5))
        self.constraint_system.add_constraint(constraint)

        self.assertTrue(constraint, v.determined_by)
        self.assertEqual(
            constraint.add_to_pplan([],
                                    self.constraint_system.marker.new_mark()),
            [constraint], "should contain only the constraint")
Esempio n. 5
0
    def setUp(self):
        self.constraint_system = ConstraintSystem()
        self.variables = self.constraint_system.create_variables(
            ["v1", "v2", "v3"], [4, 5, 3])
        self.v1, self.v2, self.v3 = self.variables
        method1_2 = Method(self.v1, self.v2, lambda x: x // 2)
        method1_3 = Method(self.v1, self.v3, lambda x: x // 3)

        self.constraint = Constraint(lambda v1, v2, v3: True, Strength.STRONG,
                                     self.variables, [method1_3, method1_2])

        self.constraint_system.add_constraint(self.constraint)
Esempio n. 6
0
class ConstraintTests(TestCase):

  def setUp(self):
    self.constraint_system = ConstraintSystem()
    self.variables = self.constraint_system.create_variables(["v1", "v2", "v3"], [4, 5, 3])
    self.v1, self.v2, self.v3 = self.variables
    method1_2 = Method(self.v1, self.v2, lambda x: x // 2)
    method1_3 = Method(self.v1, self.v3, lambda x: x // 3)

    self.constraint = Constraint(lambda v1, v2, v3: True, Strength.STRONG, self.variables, [method1_3, method1_2])

    self.constraint_system.add_constraint(self.constraint)

  def tearDown(self):
    pass

  def test_adding_enforced_to_pplan(self):
    self.assertIsNone(self.constraint.mark)

    mark = self.constraint_system.marker.new_mark()
    pplan = self.constraint.add_to_pplan([], mark)

    self.assertEqual([self.constraint], pplan)
    self.assertEqual(mark, self.constraint.mark)

  def test_adding_unenforced_to_pplan(self):
    self.constraint.selected_method = None
    self.assertIsNone(self.constraint.mark)

    pplan = self.constraint.add_to_pplan([], self.constraint_system.marker.new_mark())

    self.assertEqual([], pplan)
    self.assertIsNone(self.constraint.mark)

  def test_adding_with_the_same_mark(self):

    mark = self.constraint_system.marker.new_mark()
    self.constraint.mark = mark
    pplan = self.constraint.add_to_pplan([], mark)

    self.assertEqual([], pplan)
    self.assertEqual(mark, self.constraint.mark)

  def test_adding_with_other_mark(self):

    mark1 = self.constraint_system.marker.new_mark()
    mark2 = self.constraint_system.marker.new_mark()
    self.constraint.mark = mark1
    pplan = self.constraint.add_to_pplan([], mark2)

    self.assertEqual([self.constraint], pplan)
    self.assertEqual(mark2, self.constraint.mark)
Esempio n. 7
0
    def create_a_equals_b_plus_2_constraint(self):
        mA = Method([self.a], [self.b], lambda a: a - 2)

        mB = Method([self.b], [self.a], lambda b: b + 2)

        return Constraint(lambda a, b: a == b + 2, Strength.STRONG,
                          [self.a, self.b], [mA, mB])
Esempio n. 8
0
    def setUp(self):
        self.constraint_system = ConstraintSystem()
        self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system)
        self.point2 = Variable("Point 2", Point(10, 30),
                               self.constraint_system)
        self.midpoint = Variable("midpoint", Point(0, 0),
                                 self.constraint_system)

        mMp = Method([self.point1, self.point2], [self.midpoint],
                     lambda p1, p2: Point((p1.X + p2.X) / 2,
                                          (p1.Y + p2.Y) / 2))

        mP1 = Method([self.midpoint, self.point2], [self.point1],
                     lambda mp, p2: Point((2 * mp.X - p2.X),
                                          (2 * mp.Y - p2.Y)))

        mP2 = Method([self.midpoint, self.point1], [self.point2],
                     lambda mp, p1: Point((2 * mp.X - p1.X),
                                          (2 * mp.Y - p1.Y)))

        constraint = Constraint(
            lambda point1, point2, midpoint: midpoint.is_midpoint(
                point1, point2), Strength.STRONG,
            [self.point1, self.point2, self.midpoint], [mMp, mP1, mP2])

        self.constraint_system.add_constraint(constraint)
Esempio n. 9
0
    def create_a_equals_c_minus_1_constraint(self):
        mA = Method([self.a], [self.c], lambda a: c - 1)

        mC = Method([self.c], [self.a], lambda c: a + 1)

        return Constraint(lambda a, c: a == c - 1, Strength.STRONG,
                          [self.a, self.c], [mA, mC])
Esempio n. 10
0
    def create_a_plus_b_equals_c_constraint(self):
        mC = Method([self.a, self.b], [self.c], lambda a, b: a + b)

        mB = Method([self.a, self.c], [self.b], lambda a, c: c - a)

        mA = Method([self.b, self.c], [self.a], lambda b, c: c - b)

        return Constraint(lambda a, b, c: a + b == c, Strength.STRONG,
                          [self.a, self.b, self.c], [mA, mB, mC])
Esempio n. 11
0
  def setUp(self):
    self.constraint_system = ConstraintSystem()
    self.point1 = Variable("Point 1", Point(4, 10), self.constraint_system)
    self.point2 = Variable("Point 2", Point(10, 30), self.constraint_system)
    self.point3 = Variable("Point 3", Point(50, 20), self.constraint_system)
    self.point4 = Variable("Point 4", Point(100, 30), self.constraint_system)
    self.midpoint = Variable("midpoint", Point(0, 0), self.constraint_system)

    mpmp3p4 = Method([self.midpoint, self.point3, self.point4], [self.point1, self.point2],
      lambda pm, p3, p4: (
        Point(
          pm.X - (p4.X - p3.X),
          pm.Y - (p4.Y - p3.Y)
        ),
        Point(
          pm.X + (p4.X - p3.X),
          pm.Y + (p4.Y - p3.Y)
        )
      )
    )
    mp1pmp3 = Method([self.point1, self.midpoint, self.point3], [self.point2, self.point4],
      lambda p1, pm, p3: (
        Point(
          2 * pm.X - p1.X,
          2 * pm.Y - p1.Y
        ),
        Point(
          p3.X + (pm.X - p1.X),
          p3.Y + (pm.Y - p1.Y)
        )
      )
    )
    mpmp2p4 = Method([self.midpoint, self.point2, self.point4], [self.point1, self.point3],
      lambda pm, p2, p4: (
        Point(
          2 * pm.X - p2.X,
          2 * pm.Y - p2.Y
        ),
        Point(
          p4.X + (pm.X - p2.X),
          p4.Y + (pm.Y - p2.Y)
        )
      )
    )

    constraint = Constraint(
      lambda p1, p2, p3, p4, pm: pm.is_midpoint(p1, p2) and
                           p1.distance(pm) == p3.distance(p4),
      Strength.STRONG,
      [self.point1, self.point2, self.point3, self.point4, self.midpoint],
      [mpmp3p4, mp1pmp3, mpmp2p4])

    self.constraint_system.add_constraint(constraint)

    self.point3.stay()
    self.point4.stay()
Esempio n. 12
0
  def setUp(self):
    self.cs = ConstraintSystem()
    self.vars = self.cs.create_variables(["v1", "v2", "v3"], [4, 5, 3])
    self.v1, self.v2, self.v3 = self.vars
    m1_2 = Method(self.v1, self.v2, lambda x: x // 2)
    m1_3 = Method(self.v1, self.v3, lambda x: x // 3)

    self.cn = Constraint(lambda v1, v2, v3: True, Strength.STRONG, self.vars, [m1_3, m1_2])

    self.cs.add_constraint(self.cn)
Esempio n. 13
0
    def equality_constraint(variable1, variable2, strength, name=""):
        """Creates an equality constraint, i.e. two variables are always equal."""
        m1 = Method([variable1], [variable2], lambda v1: v1)

        m2 = Method([variable2], [variable1], lambda v2: v2)

        constraint = Constraint(lambda v1, v2: v1 == v2, strength,
                                [variable1, variable2], [m1, m2], name)

        return constraint
Esempio n. 14
0
  def setUp(self):
    self.constraint_system = ConstraintSystem()
    self.variables = self.constraint_system.create_variables(["v1", "v2", "v3"], [4, 5, 3])
    self.v1, self.v2, self.v3 = self.variables
    method1_2 = Method(self.v1, self.v2, lambda x: x // 2)
    method1_3 = Method(self.v1, self.v3, lambda x: x // 3)

    self.constraint = Constraint(lambda v1, v2, v3: True, Strength.STRONG, self.variables, [method1_3, method1_2])

    self.constraint_system.add_constraint(self.constraint)
Esempio n. 15
0
    def test_should_not_detect_a_cycle(self):
        a, b, c = self.constraint_system.create_variables(["a", "b", "c"],
                                                          [1, 2, 3])

        method1 = Method([a, c], [b], lambda a, c: a + c)
        method2 = Method([b, c], [a], lambda b, c: b - c)

        constraint = Constraint(lambda a, b, c: a + c == b, Strength.STRONG,
                                [a, b, c], [method1, method2], "constraint1")

        self.constraint_system.add_constraint(constraint)
        a.stay()
        b.set_value(4)
        c.set_value(8)
Esempio n. 16
0
    def scale_constraint(destination,
                         source,
                         scale,
                         offset,
                         strength,
                         name=""):
        """Creates a linear equation constraint, i.e. destination = scale * source + offset."""
        m1 = Method([source, scale, offset], [destination],
                    lambda source, scale, offset: scale * source + offset)

        m2 = Method([destination, scale, offset], [source], lambda destination,
                    scale, offset: float(destination - offset) / scale)

        constraint = Constraint(
            lambda destination, source, scale, offset: destination == scale *
            source + offset, strength, [destination, source, scale, offset],
            [m1, m2], name)

        return constraint
Esempio n. 17
0
    def test_pplan_add_for_two_constraint(self):
        v = Variable("v", 1, self.constraint_system)
        constraint1 = Constraint(lambda x: x == 5, Strength.WEAK, v,
                                 Method([], v, lambda: 5))
        constraint2 = Constraint(lambda x: x == 6, Strength.STRONG, v,
                                 Method([], v, lambda: 6))
        self.constraint_system.add_constraint(constraint1)
        self.constraint_system.add_constraint(constraint2)

        self.assertFalse(constraint1.is_enforced())
        self.assertTrue(constraint2.is_enforced())
        self.assertTrue(constraint2, v.determined_by)

        self.assertEqual(
            constraint2.add_to_pplan([],
                                     self.constraint_system.marker.new_mark()),
            [constraint2], "does not add other constraint of \
      a variable if it is not enforced")

        self.assertEqual(
            constraint1.add_to_pplan([],
                                     self.constraint_system.marker.new_mark()),
            [], "does not add unenforced constraints")
Esempio n. 18
0
  def test_pplan_add_for_two_constraint(self):
    v = Variable("v", 1, self.constraint_system)
    constraint1 = Constraint(lambda x: x == 5, Strength.WEAK, v, Method([], v, lambda: 5))
    constraint2 = Constraint(lambda x: x == 6, Strength.STRONG, v, Method([], v, lambda: 6))
    self.constraint_system.add_constraint(constraint1)
    self.constraint_system.add_constraint(constraint2)

    self.assertFalse(constraint1.is_enforced())
    self.assertTrue(constraint2.is_enforced())
    self.assertTrue(constraint2, v.determined_by)

    self.assertEqual(
      constraint2.add_to_pplan([], self.constraint_system.marker.new_mark()),
      [constraint2],
      "does not add other constraint of \
      a variable if it is not enforced")

    self.assertEqual(
      constraint1.add_to_pplan([], self.constraint_system.marker.new_mark()),
      [],
      "does not add unenforced constraints")
Esempio n. 19
0
class ConstraintTests(TestCase):

  def setUp(self):
    self.cs = ConstraintSystem()
    self.vars = self.cs.create_variables(["v1", "v2", "v3"], [4, 5, 3])
    self.v1, self.v2, self.v3 = self.vars
    m1_2 = Method(self.v1, self.v2, lambda x: x // 2)
    m1_3 = Method(self.v1, self.v3, lambda x: x // 3)

    self.cn = Constraint(lambda v1, v2, v3: True, Strength.STRONG, self.vars, [m1_3, m1_2])

    self.cs.add_constraint(self.cn)

  def tearDown(self):
    pass


  def test_adding_enforced_to_pplan(self):
    self.assertIsNone(self.cn.mark)

    mark = self.cs.marker.new_mark()
    pplan = self.cn.add_to_pplan([], mark)

    self.assertEqual([self.cn], pplan)
    self.assertEqual(mark, self.cn.mark)

  def test_adding_unenforced_to_pplan(self):
    self.cn.selected_method = None
    self.assertIsNone(self.cn.mark)

    pplan = self.cn.add_to_pplan([], self.cs.marker.new_mark())

    self.assertEqual([], pplan)
    self.assertIsNone(self.cn.mark)

  def test_adding_with_the_same_mark(self):

    mark = self.cs.marker.new_mark()
    self.cn.mark = mark
    pplan = self.cn.add_to_pplan([], mark)

    self.assertEqual([], pplan)
    self.assertEqual(mark, self.cn.mark)

  def test_adding_with_other_mark(self):

    mark1 = self.cs.marker.new_mark()
    mark2 = self.cs.marker.new_mark()
    self.cn.mark = mark1
    pplan = self.cn.add_to_pplan([], mark2)

    self.assertEqual([self.cn], pplan)
    self.assertEqual(mark2, self.cn.mark)


  def test_adding_iterative_should_be_in_the_right_order(self):
    cs = ConstraintSystem()

    all_vars = cs.create_variables(["a", "b", "c"], [1, 2, 3])
    a, b, c = all_vars

    m1 = Method([a, c], [b], lambda a,c: a + c)
    m2 = Method([b, c], [a], lambda b,c: b - c)

    cn = Constraint(lambda a,b,c: a + c == b, Strength.STRONG, [a, b, c], [m1, m2], "cn1")

    self.cs.add_constraint(cn)

    a.stay()
    a_stay_cn = a.stay_constraint

    c.set_value(8)
    c_set_cn = cs.forced_constraint

    cs.exec_roots = [c_set_cn, a_stay_cn]
    self.assertEqual([cn, c_set_cn, a_stay_cn], cs.exec_pplan_create())
Esempio n. 20
0
cs = ConstraintSystem()

v1 = Variable("v1", 16, cs)
v2 = Variable("v2", 16, cs)
v3 = Variable("v3", 16, cs)
v4 = Variable("v4", 10, cs)

v4.stay()

# v1 + v2 = v3, v1 - v2 = v4
m12_34 = Method([v1, v2], [v3, v4], lambda v1, v2: (v1 + v2, v1 - v2))

m34_12 = Method([v3, v4], [v1, v2], lambda v3, v4: ((v3 + v4) / 2,
                                                    (v3 - v4) / 2))

cn1 = Constraint(lambda v1, v2, v3, v4: v1 + v2 == v3 and v1 - v2 == v4,
                 Strength.STRONG, [v1, v2, v3, v4], [m12_34, m34_12])

cs.add_constraint(cn1)


def print_values():
    print("v1 = " + str(v1.get_value()))
    print("v2 = " + str(v2.get_value()))
    print("v3 = " + str(v3.get_value()))
    print("v4 = " + str(v4.get_value()))


print("Constraint: v1 + v2 = v3, v1 - v2 = v4.")
print_values()

print("Set v1 to 5.")
Esempio n. 21
0
 def setUp(self):
     self.cs = ConstraintSystem()
     self.v1 = Variable("v1", 15, self.cs)
     self.c1 = Constraint(lambda x: True, Strength.REQUIRED, [self.v1], [])