Go to the documentation of this file.00001
00002
00003 from pi_trees_lib.pi_trees_lib import *
00004 import time
00005
00006 class WeightedRandomExample():
00007 def __init__(self):
00008
00009 BEHAVE = Sequence("behave")
00010
00011
00012 WEIGHTED_RANDOM_TASKS = WeightedRandomSelector("Weighted Random Selector", weights=[3, 2, 1], reset_after=True)
00013
00014
00015 TASK1 = Message("High_Probability", "Highest Probability Task")
00016 TASK2 = Message("Medium_Probability", "Medium Probability Task")
00017 TASK3 = Message("Low_Probability", "Lowest Probability Task")
00018
00019
00020 WEIGHTED_RANDOM_TASKS.add_child(TASK1)
00021 WEIGHTED_RANDOM_TASKS.add_child(TASK2)
00022 WEIGHTED_RANDOM_TASKS.add_child(TASK3)
00023
00024 LOOP = Loop("Loop_Forever", iterations=-1)
00025
00026 LOOP.add_child(WEIGHTED_RANDOM_TASKS)
00027
00028
00029 BEHAVE.add_child(LOOP)
00030
00031
00032 print "Behavior Tree Structure"
00033 print_tree(BEHAVE)
00034
00035
00036 while True:
00037 status = BEHAVE.run()
00038 if status == TaskStatus.SUCCESS:
00039 print "Finished running tree."
00040 break
00041
00042
00043 class Message(Task):
00044 def __init__(self, name, message, *args, **kwargs):
00045 super(Message, self).__init__(name, *args, **kwargs)
00046
00047 self.name = name
00048 self.message = message
00049 print "Creating message task: ", self.message
00050
00051 def run(self):
00052 print self.message
00053 time.sleep(1)
00054
00055 return TaskStatus.SUCCESS
00056
00057
00058 if __name__ == '__main__':
00059 tree = WeightedRandomExample()
00060