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()
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')
def test_interruptable_behaviour(self): """ Test behaviour interruptable property """ method_prefix = self.__message_prefix + "test_interruptable_behaviour" planner_prefix = method_prefix + "Manager" m = Manager(activationThreshold=7, prefix=planner_prefix) topic_name_1 = method_prefix + '/sensor_1' non_interruptable_sensor = SimpleTopicSensor(topic=topic_name_1, message_type=Int32, initial_value=False) non_interruptable_condition = Condition(non_interruptable_sensor, GreedyActivator()) condition_function_name = non_interruptable_condition.getFunctionNames()[0] non_interruptable_behaviour = IncreaserBehavior(effect_name=condition_function_name, topic_name=topic_name_1, name=method_prefix + "TopicIncreaser", plannerPrefix=planner_prefix, interruptable=False) topic_name_2 = method_prefix + '/sensor_2' interruptable_sensor = SimpleTopicSensor(topic=topic_name_2, message_type=Int32, initial_value=False) interruptable_condition = Condition(interruptable_sensor, GreedyActivator()) condition_function_name2 = interruptable_condition.getFunctionNames()[0] interruptable_behaviour = IncreaserBehavior(effect_name=condition_function_name2, topic_name=topic_name_2, name=method_prefix + "TopicIncreaser2", plannerPrefix=planner_prefix, interruptable=True) enable_sensor = Sensor(name='enable_sensor', initial_value=True) enable_cond = Condition(enable_sensor, BooleanActivator()) non_interruptable_behaviour.addPrecondition(enable_cond) goal = GoalBase(method_prefix + 'CentralGoal', plannerPrefix=planner_prefix, permanent=True) goal.addCondition(non_interruptable_condition) goal.addCondition(interruptable_condition) # first normal operation: every behaviour runs as expected for x in range(0, 4, 1): m.step() rospy.sleep(0.1) self.assertTrue(non_interruptable_behaviour._isExecuting, "Non-Interruptable Behaviour is not executed") self.assertTrue(interruptable_behaviour._isExecuting, "Interruptable Behaviour is not executed") #disable non_interruptable_behaviour precondition and check if it is affected enable_sensor.update(False) for x in range(0, 1, 1): m.step() rospy.sleep(0.1) self.assertTrue(non_interruptable_behaviour._isExecuting, "Non-Interruptable Behaviour is not executed") self.assertTrue(interruptable_behaviour._isExecuting, "Interruptable Behaviour is not executed") #disable precondition of interruptable behaviour and check if it is disabled as well interruptable_behaviour.addPrecondition(enable_cond) for x in range(0, 1, 1): m.step() rospy.sleep(0.1) self.assertTrue(non_interruptable_behaviour._isExecuting, "Non-Interruptable Behaviour is not executed") self.assertFalse(interruptable_behaviour._isExecuting, "Interruptable Behaviour is executed")
def test_independent_behaviour(self): """ Test behaviour property independentFromPlanner """ method_prefix = self.__message_prefix + "test_independent_behaviour" planner_prefix = method_prefix + "Manager" m = Manager(activationThreshold=7, prefix=planner_prefix) topic_name_1 = method_prefix + '/sensor_1' sensor = SimpleTopicSensor(topic=topic_name_1, message_type=Bool, initial_value=False) condition = Condition(sensor, BooleanActivator()) condition_function_name = condition.getFunctionNames()[0] #independentFromPlanner and effects independent_behaviour = SetTrueBehavior(effect_name=condition_function_name, topic_name=topic_name_1, name=method_prefix + "SetTrue", plannerPrefix=planner_prefix, independentFromPlanner=True) #independentFromPlanner and no effects independent_behaviour2 = SetTrueBehavior(effect_name=None, topic_name=topic_name_1, name=method_prefix + "SetTrue2", plannerPrefix=planner_prefix, independentFromPlanner=True) # not independentFromPlanner and no effects independent_behaviour3 = SetTrueBehavior(effect_name=None, topic_name=topic_name_1, name=method_prefix + "SetTrue3", plannerPrefix=planner_prefix, independentFromPlanner=False) goal = GoalBase(method_prefix + 'CentralGoal', plannerPrefix=planner_prefix) goal.addCondition(condition) for x in range(0, 3, 1): m.step() rospy.sleep(0.1) goal_proxy = m.goals[0] goal_proxy.fetchStatus(3) self.assertTrue(goal_proxy.satisfied, 'Goal is not satisfied') self.assertTrue(independent_behaviour._isExecuting, "independent_behaviour is not executed") self.assertFalse(independent_behaviour2.was_executed, "independent_behaviour2 was executed") self.assertFalse(independent_behaviour3.was_executed, "independent_behaviour2 was executed")
def test_inverted_activation(self): planner_prefix = "condition_elements_test" m = Manager(activationThreshold=7, prefix=planner_prefix, createLogFiles=True) satisfaction_threshold = 0.8 sensor1 = Sensor() activator_increasing = LinearActivator(zeroActivationValue=0, fullActivationValue=1) activator_decreasing = LinearActivator(zeroActivationValue=1, fullActivationValue=0) condition_increasing = Condition(sensor1, activator_increasing) condition_decreasing = Condition(sensor1, activator_decreasing) sensor1.update(newValue=0.8) condition_increasing.sync() condition_increasing.updateComputation() condition_decreasing.sync() condition_decreasing.updateComputation() wish_increasing = condition_increasing.getWishes()[0] wish_decreasing = condition_decreasing.getWishes()[0] self.assertAlmostEqual(0.2, wish_increasing.indicator, delta=0.001) self.assertAlmostEqual(-0.8, wish_decreasing.indicator, delta=0.001) increasing_precondition_precon_pddl = condition_increasing.getPreconditionPDDL( satisfaction_threshold=satisfaction_threshold) decreasing_precondition_precon_pddl = condition_decreasing.getPreconditionPDDL( satisfaction_threshold=satisfaction_threshold) increasing_precondition_state_pddl = condition_increasing.getStatePDDL( )[0] decreasing_precondition_state_pddl = condition_decreasing.getStatePDDL( )[0] sensor2 = Sensor() sensor2.update(newValue=0.4) average_condition = AverageSensorSatisfactionCondition( sensors=[sensor1, sensor2], activator=activator_decreasing) average_condition.sync() average_condition.updateComputation() wish_average = average_condition.getWishes() average_precon_pddl = average_condition.getPreconditionPDDL( satisfaction_threshold=satisfaction_threshold) average_state = average_condition.getStatePDDL() behaviour1 = BehaviourBase("behaviour_1", plannerPrefix=planner_prefix) behaviour1.add_effect( Effect(sensor_name=sensor1.name, indicator=-0.1, sensor_type=float)) behaviour1.addPrecondition(condition_increasing) behaviour2 = BehaviourBase("behaviour_2", plannerPrefix=planner_prefix) behaviour2.add_effect( Effect(sensor_name=sensor1.name, indicator=0.1, sensor_type=float)) behaviour2.addPrecondition(condition_decreasing) behaviour2.addPrecondition(average_condition) m.step() m.step() b1_state_pddl = behaviour1.getStatePDDL() b2_state_pddl = behaviour2.getStatePDDL() b1_action_pddl = behaviour1.getActionPDDL() b2_action_pddl = behaviour2.getActionPDDL() for x in range(0, 3, 1): m.step() rospy.sleep(0.1)
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')