def test_uniform_rule(self):
        mean = random.random()
        min = mean - 1
        max = mean + 1

        rule = create_rule('uniform', mean=mean, min=min, max=max)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.mean, mean)
        self.assertEqual(rule.min, min),
        self.assertEqual(rule.max, max)

        for tag in rule.dofs:
            self.assertFalse(rule.dofs[tag])

        for tag in DOF_TAGS:
            dofs = dict()
            for t in DOF_TAGS:
                dofs[t] = t == tag
            rule.dofs = dofs
            pose = rule.get_pose()
            for t in DOF_TAGS:
                if t == tag:
                    self.assertGreaterEqual(getattr(pose, t), min)
                    self.assertLessEqual(getattr(pose, t), max)
                else:
                    if t in ['x', 'y', 'z']:
                        self.assertEqual(getattr(pose, t), 0)
    def test_random_rule(self):
        scaling_factor = random.random()
        offset = random.random()

        rule = create_rule('random',
                           scaling_factor=scaling_factor,
                           offset=offset)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.scaling_factor, scaling_factor)
        self.assertEqual(rule.offset, offset)

        for tag in rule.dofs:
            self.assertFalse(rule.dofs[tag])

        for tag in DOF_TAGS:
            dofs = dict()
            for t in DOF_TAGS:
                dofs[t] = t == tag
            rule.dofs = dofs
            pose = rule.get_pose()
            for t in DOF_TAGS:
                if t == tag:
                    self.assertNotEqual(getattr(pose, t), 0)
                else:
                    if t in ['x', 'y', 'z']:
                        self.assertEqual(getattr(pose, t), 0)
    def test_from_set_rule(self):
        values = [random.random() for _ in range(5)]
        rule = create_rule('from_set', values=values)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.values, values)

        for tag in rule.dofs:
            self.assertFalse(rule.dofs[tag])

        for tag in DOF_TAGS:
            dofs = dict()
            for t in DOF_TAGS:
                dofs[t] = t == tag
            rule.dofs = dofs
            pose = rule.get_pose()
            for t in DOF_TAGS:
                if t == tag:
                    found_value = False
                    for v in values:
                        if np.isclose(v, getattr(pose, t)):
                            found_value = True
                            break

                    self.assertTrue(found_value)
                else:
                    if t in ['x', 'y', 'z']:
                        self.assertEqual(getattr(pose, t), 0)
    def test_deprecated_rules_args(self):
        DOFS = dict(x=True, y=True, z=True)

        rule = create_rule(policy=dict(name='workspace', args='my_workspace'),
                           dofs=DOFS)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.name, 'workspace')
        self.assertEqual(rule._workspace_tag, 'my_workspace')

        rule = create_rule(policy=dict(name='uniform',
                                       args=dict(min=-1, max=1)),
                           dofs=DOFS)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.name, 'uniform')
        self.assertEqual(rule._min, -1)
        self.assertEqual(rule._max, 1)
        self.assertEqual(rule._mean, 0)

        rule = create_rule(policy=dict(name='uniform',
                                       args=dict(min=3, max=5, mean=4)),
                           dofs=DOFS)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.name, 'uniform')
        self.assertEqual(rule._min, 3)
        self.assertEqual(rule._max, 5)
        self.assertEqual(rule._mean, 4)

        rule = create_rule(policy=dict(name='choice',
                                       args=dict(values=[1, 2, 3])),
                           dofs=DOFS)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.name, 'from_set')
        self.assertEqual(rule._values, [1, 2, 3])

        rule = create_rule(policy=dict(name='value', args=10), dofs=DOFS)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.name, 'value')
        self.assertEqual(rule._value, 10)
    def test_within_workspace_rule(self):
        cm = ConstraintsManager.get_instance()
        self.assertTrue(cm.add(**WORKSPACE_CONSTRAINT))
        self.assertIn(WORKSPACE_CONSTRAINT['name'], cm.tags)

        rule = create_rule('workspace', workspace=WORKSPACE_CONSTRAINT['name'])

        self.assertIsNotNone(rule)

        pose = rule.get_pose()
        constraint = cm.get(WORKSPACE_CONSTRAINT['name'])
        self.assertTrue(constraint.contains_point([pose.x, pose.y]))

        rule.dofs = dict(x=True, y=True, z=True)
        pose = rule.get_pose()
        constraint = cm.get(WORKSPACE_CONSTRAINT['name'])
        self.assertTrue(constraint.contains_point([pose.x, pose.y]))
    def test_fixed_value_rule(self):
        value = random.random()
        rule = create_rule('value', value=value)

        self.assertIsNotNone(rule)
        self.assertEqual(rule.value, value)

        for tag in rule.dofs:
            self.assertFalse(rule.dofs[tag])

        for tag in DOF_TAGS:
            dofs = dict()
            for t in DOF_TAGS:
                dofs[t] = t == tag
            rule.dofs = dofs
            pose = rule.get_pose()
            for t in DOF_TAGS:
                if t == tag:
                    self.assertTrue(np.isclose(getattr(pose, t), value))
                else:
                    if t in ['x', 'y', 'z']:
                        self.assertEqual(getattr(pose, t), 0)