/**
  * @param KnowledgeBase $knowledgeBase
  */
 public function load(KnowledgeBase $knowledgeBase)
 {
     foreach ($this->data['facts'] as $name => $value) {
         $knowledgeBase->add(Fact::factory($name, $value));
     }
     foreach ($this->data['rules'] as $condition => $data) {
         if (is_string($data)) {
             $action = $data;
             $priority = 0;
         } else {
             $action = $data['action'];
             $priority = isset($data['priority']) ? $data['priority'] : 0;
         }
         $knowledgeBase->add(Rule::factory($condition, $condition, $action, $priority));
     }
 }
Пример #2
0
 public function execute()
 {
     $this->context->pseudo_args($this->args);
     // Create a rule
     $rule = Rule::factory($this->context, $this->callback, $this->args);
     // Execute the rule, and if no errors are returned, perform the actions
     if ($rule->execute() === $this->check_against) {
         // Use the parent for executing actions
         $parent = $this->context->parent(Formo::PARENT);
         foreach ($this->action_fields as $key => $field) {
             // Fix the args
             $this->context->pseudo_args($this->action_args[$key]);
             // Perform each action
             $callback = array($parent->find($field), $this->action_callbacks[$key]);
             call_user_func_array($callback, $this->action_args[$key]);
         }
     }
 }
Пример #3
0
 public function rule($field, $callback, array $args = NULL)
 {
     $context = $field = $field === NULL ? $this : $this->find($field);
     $this->make_context($context, $callback);
     // Add the rule
     $field->add_validator('rules', Rule::factory($field, $context, $callback, $args));
     return $this;
 }