예제 #1
0
 /**
  * Prepares tests
  */
 public function setup()
 {
     $this->engine = new InferenceEngine();
     $this->rules = [];
     $this->rules['r1'] = Rule::factory('r1', '$1->dad == $2', '$2->children[] = $1;');
     $this->rules['r2'] = Rule::factory('r2', '$1->dad == $2->dad', '$1->siblings[] = $2; $2->siblings[] = $1;');
     $this->rules['r3'] = Rule::factory('r3', '$1->dad == $2 && $2->dad', '$1->grandpas[] = $2->dad;');
     $this->knowledgeBase = new KnowledgeBase();
     foreach ($this->rules as $rule) {
         $this->knowledgeBase->addRule($rule);
     }
 }
예제 #2
0
 /**
  * Prepares tests
  */
 public function setup()
 {
     $this->engine = new InferenceEngine(new ExpressionLanguageRuleExecutor());
     $this->rules = [];
     $this->rules['r1'] = Rule::factory('r1', 'animal == "bee"', '$sound = "buzz";');
     $this->rules['r2'] = Rule::factory('r2', 'animal == "bird"', '$sound = "tweet";');
     $this->rules['r3'] = Rule::factory('r3', 'animal == "hen"', '$sound = "cluck";');
     $this->rules['r4'] = Rule::factory('r4', 'animal == "rat"', '$sound = "squeak";');
     $this->rules['r5'] = Rule::factory('r5', 'fly and size == "small"', '$animal = "bee";');
     $this->rules['r6'] = Rule::factory('r6', 'fly and size == "large" and legs==2', '$animal = "bird";');
     $this->rules['r7'] = Rule::factory('r7', 'legs==4', '$animal = "rat";');
     $this->rules['r8'] = Rule::factory('r8', 'not fly and legs==2', '$animal = "hen";');
     $this->knowledgeBase = new KnowledgeBase();
     foreach ($this->rules as $rule) {
         $this->knowledgeBase->addRule($rule);
     }
 }
 /**
  * Calculate possible wildcards
  */
 protected function calculateWildcardCombinations()
 {
     $wildCards = $this->getConditionWildcards();
     if (sizeof($wildCards) > 2) {
         throw new \Exception('More than 2 wildcards is not yet supported');
     }
     $combinations = $this->combinations($this->workingMemory->getAllFacts(), sizeof($wildCards));
     foreach ($combinations as $i => $combination) {
         $proccesedCondition = $this->parseCode($this->getCondition(), $this->getConditionWildcards(), $combination);
         $proccesedAction = $this->parseCode($this->getAction(), $this->getConditionWildcards(), $combination);
         $rule = Rule::factory($this->getName() . '__' . $i, $proccesedCondition, $proccesedAction, $this->getPriority());
         $this->wildcardCombinationRules[] = $rule;
     }
 }