Esempio n. 1
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $status = BT\Task\Handler::process($this->task, $exchange);
     switch ($status) {
         case BT\Task\Status::ERROR:
             if ($this->policy->getValue('error')) {
                 return BT\Task\Status::FAILED;
             }
             return $status;
         case BT\Task\Status::INACTIVE:
             if ($this->policy->getValue('inactive')) {
                 return BT\Task\Status::FAILED;
             }
             return $status;
         case BT\Task\Status::ACTIVE:
             if ($this->policy->getValue('active')) {
                 return BT\Task\Status::FAILED;
             }
             return $status;
         case BT\Task\Status::SUCCESS:
             if ($this->policy->getValue('success')) {
                 return BT\Task\Status::FAILED;
             }
             return $status;
     }
     return $status;
 }
Esempio n. 2
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $status = BT\Task\Handler::process($this->task, $exchange);
     if ($status == BT\Task\Status::SUCCESS) {
         $this->task->reset();
     }
     return $status;
 }
Esempio n. 3
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $limit = Core\Convert::toInteger($this->policy->getValue('limit'));
     if ($this->calls < $limit) {
         $status = BT\Task\Handler::process($this->task, $exchange);
         $this->calls++;
         return $status;
     }
     return BT\Task\Status::FAILED;
 }
Esempio n. 4
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $callable = explode(',', $this->policy->getValue('callable'));
     $options = Core\Convert::toInteger($this->policy->getValue('options'));
     $probability = Core\Convert::toDouble($this->policy->hasKey('odds')) * $options;
     if (call_user_func($callable, array(1, $options)) <= $probability) {
         $status = BT\Task\Handler::process($this->task, $exchange);
         return $status;
     }
     return BT\Task\Status::ACTIVE;
 }
Esempio n. 5
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $until = $this->policy->getValue('until');
     do {
         $status = BT\Task\Handler::process($this->task, $exchange);
         if (!in_array($status, array(BT\Task\Status::SUCCESS, BT\Task\Status::FAILED))) {
             return $status;
         }
     } while ($status == $until);
     return $until;
 }
Esempio n. 6
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $interval = Core\Convert::toInteger($this->policy->getValue('interval')) / 1000;
     // milliseconds => seconds
     if (microtime(true) >= $this->next_time) {
         $status = BT\Task\Handler::process($this->task, $exchange);
         $this->next_time += $interval;
         return $status;
     }
     return BT\Task\Status::ACTIVE;
 }
Esempio n. 7
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $max_count = Core\Convert::toInteger($this->policy->getValue('max_count'));
     if ($this->counter < $max_count) {
         $this->counter++;
         return BT\Task\Status::ACTIVE;
     }
     $this->counter = 0;
     $status = BT\Task\Handler::process($this->task, $exchange);
     return $status;
 }
Esempio n. 8
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $callable = explode(',', $this->policy->getValue('callable'));
     $count = $this->tasks->count();
     if ($count > 0) {
         $index = call_user_func($callable, array(0, $count));
         $task = $this->tasks->getValue($index);
         return BT\Task\Handler::process($task, $exchange);
     }
     return BT\Task\Status::ERROR;
 }
Esempio n. 9
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $status = BT\Task\Handler::process($this->task, $exchange);
     if ($status == BT\Task\Status::SUCCESS) {
         return BT\Task\Status::FAILED;
     } else {
         if ($status == BT\Task\Status::FAILED) {
             return BT\Task\Status::SUCCESS;
         }
     }
     return $status;
 }
Esempio n. 10
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $shuffle = Core\Convert::toBoolean($this->policy->getValue('shuffle'));
     if ($shuffle) {
         $this->tasks->shuffle();
     }
     $index = Core\Convert::toInteger($this->policy->getValue('index'));
     if ($this->tasks->hasIndex($index)) {
         return BT\Task\Handler::process($this->tasks->getValue($index), $exchange);
     }
     return BT\Task\Status::ERROR;
 }
Esempio n. 11
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $delay = Core\Convert::toInteger($this->policy->getValue('delay')) / 1000;
     // milliseconds => seconds
     $deltaT = microtime(true) - $this->start_time;
     if ($deltaT >= $delay) {
         $duration = Core\Convert::toInteger($this->policy->getValue('duration')) / 1000;
         // milliseconds => seconds
         if ($deltaT < $delay + $duration) {
             return BT\Task\Handler::process($this->task, $exchange);
         }
     }
     return BT\Task\Status::INACTIVE;
 }
Esempio n. 12
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $count = $this->tasks->count();
     if ($count > 0) {
         $shuffle = Core\Convert::toBoolean($this->policy->getValue('shuffle'));
         if ($shuffle) {
             $this->tasks->shuffle();
         }
         $inactivesCt = 0;
         $successesCt = 0;
         $successesMax = min(Core\Convert::toInteger($this->policy->getValue('successes')), $count);
         $failuresCt = 0;
         $failuresMax = min(Core\Convert::toInteger($this->policy->getValue('failures')), $count);
         foreach ($this->tasks as $task) {
             $status = BT\Task\Handler::process($task, $exchange);
             switch ($status) {
                 case BT\Task\Status::INACTIVE:
                     $inactivesCt++;
                     break;
                 case BT\Task\Status::ACTIVE:
                     break;
                 case BT\Task\Status::SUCCESS:
                     $successesCt++;
                     if ($successesCt >= $successesMax) {
                         return BT\Task\Status::SUCCESS;
                     }
                     break;
                 case BT\Task\Status::FAILED:
                     $failuresCt++;
                     if ($failuresCt >= $failuresMax) {
                         return BT\Task\Status::FAILED;
                     }
                     break;
                 case BT\Task\Status::ERROR:
                 case BT\Task\Status::QUIT:
                     return $status;
             }
         }
         if ($inactivesCt != $count) {
             return BT\Task\Status::ACTIVE;
         }
     }
     return BT\Task\Status::INACTIVE;
 }
Esempio n. 13
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $shuffle = Core\Convert::toBoolean($this->policy->getValue('shuffle'));
     if ($shuffle) {
         $this->tasks->shuffle();
     }
     $inactives = 0;
     foreach ($this->tasks as $task) {
         $status = BT\Task\Handler::process($task, $exchange);
         if ($status == BT\Task\Status::INACTIVE) {
             $inactives++;
         } else {
             if ($status != BT\Task\Status::SUCCESS) {
                 return $status;
             }
         }
     }
     return $inactives < $this->tasks->count() ? BT\Task\Status::SUCCESS : BT\Task\Status::INACTIVE;
 }
Esempio n. 14
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $id = Core\Convert::toString($this->policy->getValue('id'));
     if ($this->blackboard->hasKey($id)) {
         $hashCode = $this->blackboard->getValue($id);
         if ($hashCode == $this->task->__hashCode()) {
             $status = BT\Task\Handler::process($this->task, $exchange);
             if ($status != BT\Task\Status::ACTIVE) {
                 $this->blackboard->removeKey($id);
             }
             return $status;
         }
         return BT\Task\Status::ACTIVE;
     } else {
         $status = BT\Task\Handler::process($this->task, $exchange);
         if ($status == BT\Task\Status::ACTIVE) {
             $this->blackboard->putEntry($id, $this->task->__hashCode());
         }
         return $status;
     }
 }
Esempio n. 15
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $steps = Core\Convert::toInteger($this->policy->getValue('steps'));
     if ($this->policy->getValue('reverse')) {
         // direction
         for ($i = $steps - 1; $i >= 0; $i--) {
             $status = BT\Task\Handler::process($this->task, $exchange);
             if (!in_array($status, array(BT\Task\Status::SUCCESS, BT\Task\Status::FAILED, BT\Task\Status::ERROR, BT\Task\Status::QUIT))) {
                 return $status;
             }
         }
     } else {
         for ($i = 0; $i < $steps; $i++) {
             $status = BT\Task\Handler::process($this->task, $exchange);
             if (!in_array($status, array(BT\Task\Status::SUCCESS, BT\Task\Status::FAILED, BT\Task\Status::ERROR, BT\Task\Status::QUIT))) {
                 return $status;
             }
         }
     }
     return BT\Task\Status::SUCCESS;
 }
Esempio n. 16
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $inactives = 0;
     while ($this->state < $this->tasks->count()) {
         $status = BT\Task\Handler::process($this->tasks->getValue($this->state), $exchange);
         if ($status == BT\Task\Status::INACTIVE) {
             $inactives++;
         } else {
             if ($status == BT\Task\Status::ACTIVE) {
                 return $status;
             } else {
                 if ($status != BT\Task\Status::SUCCESS) {
                     $this->state = 0;
                     return $status;
                 }
             }
         }
         $this->state++;
     }
     $this->state = 0;
     return $inactives < $this->tasks->count() ? BT\Task\Status::SUCCESS : BT\Task\Status::INACTIVE;
 }
Esempio n. 17
0
 /**
  * This method executes the tasks define in the pipeline.
  *
  * @access public
  * @param BT\Exchange $exchange                             the given exchange to process
  * @param string $id                                        the id of behavior tree to run
  * @return integer                                          the status code
  */
 public function run(BT\Exchange $exchange, $id = 'BEHAVE')
 {
     // http://aigamedev.com/open/article/popular-behavior-tree-design/
     $factory = new Spring\XMLObjectFactory(Spring\Data\XML::load($this->file));
     $registry = $factory->getParser()->getRegistry();
     $registry->putEntry(array('action', BT\Task::NAMESPACE_URI), new BT\Object\Factory\ActionElement());
     $registry->putEntry(array('blackboard', BT\Task::NAMESPACE_URI), new BT\Object\Factory\BlackboardElement());
     $registry->putEntry(array('branch', BT\Task::NAMESPACE_URI), new BT\Object\Factory\BranchElement());
     $registry->putEntry(array('breakpoint', BT\Task::NAMESPACE_URI), new BT\Object\Factory\BreakpointElement());
     $registry->putEntry(array('composite', BT\Task::NAMESPACE_URI), new BT\Object\Factory\CompositeElement());
     $registry->putEntry(array('condition', BT\Task::NAMESPACE_URI), new BT\Object\Factory\ConditionElement());
     $registry->putEntry(array('decorator', BT\Task::NAMESPACE_URI), new BT\Object\Factory\DecoratorElement());
     $registry->putEntry(array('entry', BT\Task::NAMESPACE_URI), new BT\Object\Factory\EntryElement());
     $registry->putEntry(array('leaf', BT\Task::NAMESPACE_URI), new BT\Object\Factory\LeafElement());
     $registry->putEntry(array('logger', BT\Task::NAMESPACE_URI), new BT\Object\Factory\LoggerElement());
     $registry->putEntry(array('parallel', BT\Task::NAMESPACE_URI), new BT\Object\Factory\ParallelElement());
     $registry->putEntry(array('picker', BT\Task::NAMESPACE_URI), new BT\Object\Factory\PickerElement());
     $registry->putEntry(array('ref', BT\Task::NAMESPACE_URI), new BT\Object\Factory\RefElement());
     $registry->putEntry(array('resetter', BT\Task::NAMESPACE_URI), new BT\Object\Factory\ResetterElement());
     $registry->putEntry(array('selector', BT\Task::NAMESPACE_URI), new BT\Object\Factory\SelectorElement());
     $registry->putEntry(array('semaphore', BT\Task::NAMESPACE_URI), new BT\Object\Factory\SemaphoreElement());
     $registry->putEntry(array('sequence', BT\Task::NAMESPACE_URI), new BT\Object\Factory\SequenceElement());
     $registry->putEntry(array('policy', BT\Task::NAMESPACE_URI), new BT\Object\Factory\PolicyElement());
     $registry->putEntry(array('stub', BT\Task::NAMESPACE_URI), new BT\Object\Factory\StubElement());
     $registry->putEntry(array('tasks', BT\Task::NAMESPACE_URI), new BT\Object\Factory\TasksElement());
     $registry->putEntry(array('ticker', BT\Task::NAMESPACE_URI), new BT\Object\Factory\TickerElement());
     $registry->putEntry(array('timer', BT\Task::NAMESPACE_URI), new BT\Object\Factory\TimerElement());
     if ($factory->hasObject($id)) {
         $object = $factory->getObject($id);
         if ($object instanceof BT\Task) {
             return BT\Task\Handler::process($object, $exchange);
         }
     }
     return BT\Task\Status::ERROR;
 }
Esempio n. 18
0
 /**
  * This method processes the models and returns the status.
  *
  * @access public
  * @param BT\Exchange $exchange                             the exchange given to process
  * @return integer                                          the status code
  */
 public function process(BT\Exchange $exchange)
 {
     $status = BT\Task\Handler::process($this->task, $exchange);
     if ($this->isEnabled()) {
         switch ($status) {
             case BT\Task\Status::INACTIVE:
                 $this->logger->add($this->level, 'Task: :task Status: Inactive', array(':task' => $this->task));
                 break;
             case BT\Task\Status::ACTIVE:
                 $this->logger->add($this->level, 'Task: :task Status: Active', array(':task' => $this->task));
                 break;
             case BT\Task\Status::SUCCESS:
                 $this->logger->add($this->level, 'Task: :task Status: Success', array(':task' => $this->task));
                 break;
             case BT\Task\Status::FAILED:
                 $this->logger->add($this->level, 'Task: :task Status: Failed', array(':task' => $this->task));
                 break;
             case BT\Task\Status::ERROR:
                 $this->logger->add($this->level, 'Task: :task Status: Error', array(':task' => $this->task));
                 break;
             case BT\Task\Status::QUIT:
                 $this->logger->add($this->level, 'Task: :task Status: Quit', array(':task' => $this->task));
                 break;
         }
     }
     return $status;
 }