/**
  * @param KnowledgeBase $knowledgeBase
  * @param WorkingMemory $workingMemory
  *
  * @return RuleRunDecorator[]
  */
 protected function getMatchedRules(KnowledgeBase $knowledgeBase, WorkingMemory $workingMemory)
 {
     $this->inferenceProfiler && $this->inferenceProfiler->startIteration();
     $this->inferenceProfiler && $this->inferenceProfiler->startMatchingRules();
     /** @var RuleRunDecorator[] $matchedRules */
     $matchedRules = [];
     /** @var Rule $rule */
     foreach ($knowledgeBase->getRules() as $rule) {
         // if there are already matched rules and they're higher priority dismatch this one
         if (sizeof($matchedRules) > 0 && $rule->getPriority() < $matchedRules[0]->getPriority()) {
             $this->inferenceProfiler && $this->inferenceProfiler->addMatchingRuleCheck($rule, 'Skip by priority');
             break;
         }
         // skip already executed rules
         if ($workingMemory->isExecuted($rule)) {
             $this->inferenceProfiler && $this->inferenceProfiler->addMatchingRuleCheck($rule, 'Already executed');
             continue;
         }
         $rule = new RuleRunDecorator($rule, $workingMemory);
         $this->inferenceProfiler && $this->inferenceProfiler->addMatchingRuleCheck($rule);
         // skip if condition is not true
         if ($this->ruleExecutor->checkCondition($rule, $workingMemory)) {
             $this->inferenceProfiler && $this->inferenceProfiler->setMatchingRuleCheckResult('Rule matches');
             $matchedRules[] = $rule;
         } else {
             $this->inferenceProfiler && $this->inferenceProfiler->setMatchingRuleCheckResult('Rule does not match');
         }
     }
     $this->inferenceProfiler && $this->inferenceProfiler->endMatchingRules();
     return $matchedRules;
 }