Exemplo n.º 1
0
 def _create_goal(self, sensor, effect, goal_name, activator_name):
     """
     Generate goals, which made the manager trying to work infinitely on the given effect,
      until the network is stopped. Therefore the goal shouldn't reachable (except the goal for boolean effects)
     :param sensor: instance of type Sensor
     :param effect: instance of type  Effect
     :param goal_name: unique name for the goal
     :return: a goal, which causes the manager to work on the effect during the whole time
     """
     if effect.sensor_type == str(bool):
         desired_value = True if effect.indicator > 0 else False
         activator = BooleanActivator(name=activator_name,
                                      desiredValue=desired_value)
         condition = Condition(activator=activator, sensor=sensor)
         return OfflineGoal(name=goal_name,
                            planner_prefix=self.get_manager_prefix(),
                            permanent=True,
                            conditions={condition})
     if effect.sensor_type == str(int) or effect.sensor_type == str(float):
         activator = GreedyActivator(maximize=effect.indicator > 0,
                                     step_size=abs(effect.indicator),
                                     name=activator_name)
         condition = Condition(activator=activator, sensor=sensor)
         return OfflineGoal(goal_name,
                            planner_prefix=self.get_manager_prefix(),
                            permanent=True,
                            conditions={condition})
     raise RuntimeError(
         msg='Cant create goal for effect type \'' + effect.sensor_type +
         '\'. Overwrite the method _create_goal to handle the type')
Exemplo n.º 2
0
    def test_activator(self):
        method_prefix = self.__message_prefix + "TestOfflineGoal"
        planner_prefix = method_prefix + "Manager"
        m = Manager(activationThreshold=7, prefix=planner_prefix)

        topic_name = 'IncreaseTopicTest/Topic'

        sensor = TopicSensor(topic=topic_name, name='IncreaseTopicTestSensor', message_type=Int32,
                             initial_value=0)
        activator = GreedyActivator()
        condition = Condition(sensor=sensor, activator=activator)

        IncreaserBehavior(topic_name=topic_name, effect_name=sensor.name,
                          createLogFiles=True, planner_prefix=planner_prefix)
        goal = OfflineGoal(name=self.__message_prefix + 'CentralGoal', planner_prefix=planner_prefix)
        goal.add_condition(condition)
        m.add_goal(goal)

        number_of_steps = 15
        for x in range(0, number_of_steps + 1 , 1):
            m.step()
            rospy.sleep(0.1)

        # it takes 2 steps until the activation has increased
        expected_behaviour_steps = number_of_steps - 2
        self.assertEquals(expected_behaviour_steps, sensor.latestValue)
Exemplo n.º 3
0
    def test_offline_goal(self):

        method_prefix = self.__message_prefix + "TestOfflineGoal"
        planner_prefix = method_prefix + "/Manager"
        m = Manager(activationThreshold=7, prefix=planner_prefix)

        topic_name = method_prefix + '/Topic'

        sensor = SimpleTopicSensor(topic=topic_name,
                                   message_type=Bool,
                                   initial_value=False)
        condition = Condition(sensor, BooleanActivator())

        pddl_function_name = condition.getFunctionNames()[0]
        SetTrueBehavior(effect_name=pddl_function_name,
                        topic_name=topic_name,
                        name=method_prefix + "SetTrue",
                        plannerPrefix=planner_prefix)
        goal = OfflineGoal('CentralGoal', planner_prefix=planner_prefix)
        goal.add_condition(condition)
        m.add_goal(goal)
        for x in range(0, 3, 1):
            m.step()
            rospy.sleep(0.1)

        goal.fetchStatus(3)

        self.assertTrue(goal.satisfied, 'Goal is not satisfied')

        goal.unregister()
Exemplo n.º 4
0
    def test_conditions_in_multiple_levels(self):
        """
        Testing conditions that are used as well on the highest manager hierarchy level as well as in a sub manager of
        a NetworkBehaviour. In particular one conditions is used as precondition, the other one as goal condition.
        """

        method_prefix = self.__message_prefix + "/test_conditions_in_multiple_levels"

        pre_con_sensor = Sensor(name="Precon_sensor", initial_value=False)
        pre_con = Condition(pre_con_sensor, BooleanActivator(desiredValue=True))

        topic_name = method_prefix + '/Topic'
        sensor = TopicSensor(topic=topic_name, message_type=Int32, initial_value=0)
        condition = Condition(sensor, ThresholdActivator(thresholdValue=3))

        planner_prefix = method_prefix + "/Manager"
        m = Manager(activationThreshold=7, prefix=planner_prefix)
        goal = OfflineGoal('CentralGoal', planner_prefix=planner_prefix)
        goal.add_condition(condition)
        m.add_goal(goal)

        effect = Effect(sensor_name=sensor.name, indicator=1, sensor_type=int, activator_name=condition.activator.name)

        first_level_network = NetworkBehaviour(name=method_prefix + '/FirstLevel', planner_prefix=planner_prefix,
                                               createLogFiles=True)
        first_level_network.add_effects([effect])
        first_level_network.add_precondition(pre_con)

        goal_with_same_cond = OfflineGoal('CentralGoal2', planner_prefix=planner_prefix)
        goal_with_same_cond.add_condition(condition)
        first_level_network.add_goal(goal_with_same_cond)

        increaser_behavior = IncreaserBehavior(effect_name=sensor.name, topic_name=topic_name,
                                               name=method_prefix + "TopicIncreaser",
                                               planner_prefix=first_level_network.get_manager_prefix())

        increaser_behavior.add_precondition(pre_con)

        # activate the first_level_network increaser_Behavior
        for x in range(0, 3, 1):
            self.assertFalse(first_level_network._isExecuting)
            m.step()
            pre_con_sensor.update(True)
            rospy.sleep(0.1)

        self.assertTrue(first_level_network._isExecuting)

        for x in range(0, 4, 1):
            m.step()
            rospy.sleep(0.1)

        self.assertTrue(increaser_behavior._isExecuting)
Exemplo n.º 5
0
 def setUpClass(cls):
     print("Needs a running roscore")
     super(GoalWrapperTest, cls).setUpClass()
     rospy.init_node("test_node")
     cls.manager = Manager(prefix="Test_manager")
     cls.sensor = TopicSensor(name="test_sensor",
                              topic="/sensor_topic",
                              message_type=Bool,
                              initial_value=False)
     cls.test_condition = Condition(cls.sensor, BooleanActivator())
     cls.comparison_goal = OfflineGoal(name="comparison_goal",
                                       planner_prefix="Test_manager")
     cls.manager_name = "Test_manager"
Exemplo n.º 6
0
    def _create_goal(self, sensor, effect, goal_name):
        """
        Generate goals, which made the manager trying to work infinitely on the given effect,
         until the network is stopped. Therefore the goal shouldn't reachable (except the goal for boolean effects)
        :param sensor: instance of type Sensor
        :param effect: instance of type  Effect
        :param goal_name: unique name for the goal
        :return: a goal, which causes the manager to work on the effect during the whole time
        :raises RuntimeError: if the creation of a goal for an effect of this
                type is not possible
        """

        try:
            condition = create_condition_from_effect(effect=effect,
                                                     sensor=sensor)
            return OfflineGoal(name=goal_name,
                               planner_prefix=self.get_manager_prefix(),
                               permanent=True,
                               conditions={condition})
        except RuntimeError:
            raise RuntimeError(
                msg="Can't create goal for effect type '" +
                effect.sensor_type +
                "'.Overwrite the method _create_goal to handle the type")
Exemplo n.º 7
0
    def test_multiple_embedded_network_behaviors(self):
        """
        Tests the case, that one network behavior is embedded into another network behavior.
        The goal requires to receive an int (3) in a topic.
        """

        method_prefix = self.__message_prefix + "/test_multiple_embedded_network_behaviors"

        topic_name = method_prefix + '/Topic'
        sensor = SimpleTopicSensor(topic=topic_name,
                                   message_type=Int32,
                                   initial_value=0)
        condition = Condition(sensor, ThresholdActivator(thresholdValue=3))

        planner_prefix = method_prefix + "/Manager"
        m = Manager(activationThreshold=7, prefix=planner_prefix)
        goal = OfflineGoal('CentralGoal', planner_prefix=planner_prefix)
        goal.add_condition(condition)
        m.add_goal(goal)

        effect = Effect(sensor_name=sensor.name,
                        indicator=1,
                        sensor_type=int,
                        activator_name=condition.activator.name)

        first_level_network = NetworkBehavior(name=method_prefix +
                                              '/FirstLevel',
                                              plannerPrefix=planner_prefix,
                                              createLogFiles=True)
        first_level_network.add_effects_and_goals([(sensor, effect)])

        second_level_network = NetworkBehavior(
            name=method_prefix + '/SecondLevel',
            plannerPrefix=first_level_network.get_manager_prefix(),
            createLogFiles=True)
        # Doesnt matter, whether the effects are added via the constructor or the add method.
        # Both methods are used here, to demonstrate both ways.
        second_level_network.add_effects_and_goals([(sensor, effect)])

        pddl_function_name = condition.getFunctionNames()[0]
        increaser_behavior = IncreaserBehavior(
            effect_name=pddl_function_name,
            topic_name=topic_name,
            name=method_prefix + "TopicIncreaser",
            plannerPrefix=second_level_network.get_manager_prefix())

        # activate the first_level_network, second_level_network and increaser_Behavior
        for x in range(0, 3, 1):
            self.assertFalse(first_level_network._isExecuting)
            m.step()
            rospy.sleep(0.1)
        self.assertTrue(first_level_network._isExecuting)

        for x in range(0, 3, 1):
            self.assertFalse(second_level_network._isExecuting)
            first_level_network.do_step()
            rospy.sleep(0.1)
        self.assertTrue(second_level_network._isExecuting)

        for x in range(0, 3, 1):
            self.assertFalse(increaser_behavior._isExecuting)
            second_level_network.do_step()
            rospy.sleep(0.1)
        self.assertTrue(increaser_behavior._isExecuting)

        # Satisfy goal
        for step in range(0, 3, 1):
            sensor.sync()
            self.assertEqual(step, sensor.value)
            second_level_network.do_step()
            rospy.sleep(0.1)

        goal.fetchStatus(3)
        self.assertTrue(goal.satisfied, 'Goal is not satisfied')