/**
  * @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));
     }
 }
Exemplo n.º 2
0
 /**
  * @param bool   $flyValue
  * @param string $sizeValue
  * @param int    $legsValue
  * @param string $expectedAnimal
  * @param string $expectedSound
  * @param array  $expectedRules
  *
  * @dataProvider soundsDataProvider
  */
 public function testSounds($flyValue, $sizeValue, $legsValue, $expectedAnimal, $expectedSound, array $expectedRules)
 {
     $this->knowledgeBase->addFact('fly', $flyValue);
     $this->knowledgeBase->addFact('size', $sizeValue);
     $this->knowledgeBase->addFact('legs', $legsValue);
     $workingMemory = $this->engine->run($this->knowledgeBase);
     $facts = $this->knowledgeBase->getFacts();
     $this->assertSimpleFact('fly', $flyValue, $facts);
     $this->assertSimpleFact('size', $sizeValue, $facts);
     $this->assertSimpleFact('legs', $legsValue, $facts);
     $this->assertSimpleFact('animal', $expectedAnimal, $facts);
     $this->assertSimpleFact('sound', $expectedSound, $facts);
     $expectedRulesObjects = [];
     foreach ($expectedRules as $expectedRuleKey) {
         $expectedRulesObjects[] = $this->rules[$expectedRuleKey];
     }
     $this->assertExplanation($expectedRulesObjects, $workingMemory);
 }
Exemplo n.º 3
0
 /**
  * Test Parent and children
  */
 public function testParentAndChildren()
 {
     $joe = new \stdClass();
     $joe->name = 'Joe';
     $joe->dad = null;
     $joe->siblings = [];
     $joe->grandpas = [];
     $john = new \stdClass();
     $joe->children = [$john];
     $john->name = 'John';
     $john->dad = $joe;
     $john->children = [];
     $john->siblings = [];
     $john->grandpas = [];
     $peter = new \stdClass();
     $peter->name = 'Peter';
     $peter->dad = $john;
     $peter->children = [];
     $peter->siblings = [];
     $peter->grandpas = [];
     $mery = new \stdClass();
     $mery->name = 'Mery';
     $mery->dad = $john;
     $mery->children = [];
     $mery->siblings = [];
     $mery->grandpas = [];
     $this->knowledgeBase->addFact('joe', $joe);
     $this->knowledgeBase->addFact('john', $john);
     $this->knowledgeBase->addFact('peter', $peter);
     $this->knowledgeBase->addFact('mery', $mery);
     $workingMemory = $this->engine->run($this->knowledgeBase);
     $facts = $this->knowledgeBase->getFacts();
     $this->assertTrue(in_array($mery, $peter->siblings));
     $this->assertTrue(in_array($peter, $mery->siblings));
     $this->assertTrue(in_array($joe, $mery->grandpas));
     $this->assertTrue(in_array($joe, $peter->grandpas));
 }
 /**
  * @param KnowledgeBase $knowledgeBase
  *
  * @return WorkingMemory
  */
 public function run(KnowledgeBase $knowledgeBase)
 {
     $workingMemory = new WorkingMemory();
     $workingMemory->setFacts($knowledgeBase->getFacts());
     $this->inferenceProfiler && $this->inferenceProfiler->startInference($knowledgeBase->getFacts());
     while ($matchedRules = $this->getMatchedRules($knowledgeBase, $workingMemory)) {
         /** @var RuleRunDecorator $selectedRuleDecorator */
         $selectedRuleDecorator = $this->conflictResolutionStrategy->selectPreferredRule($matchedRules, $workingMemory);
         $this->ruleExecutor->execute($selectedRuleDecorator, $workingMemory);
     }
     $this->inferenceProfiler && $this->inferenceProfiler->endInference($knowledgeBase->getFacts());
     $knowledgeBase->setFacts($workingMemory->getAllFacts());
     return $workingMemory;
 }