/** * @param Rule[] $rules * @param WorkingMemory $workingMemory * * @return Rule */ public function selectPreferredRule(array $rules, WorkingMemory $workingMemory) { $random = rand(0, sizeof($rules) - 1); // Calculate a random number $selectedRule = $rules[$random]; $this->inferenceProfiler && $this->inferenceProfiler->setIterationSelectedRule($selectedRule, 'Rule selected randomly'); return $selectedRule; }
/** * @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; }
/** * @param Rule $rule * @param WorkingMemory $workingMemory * * @return array */ public function execute(Rule $rule, WorkingMemory $workingMemory) { if ($rule instanceof RuleRunDecorator && $rule->hasSuccessCombinationRules()) { $workingMemory->setExecuted($rule->getRule()); foreach ($rule->getSuccessCombinationRules() as $combinationRule) { $this->inferenceProfiler && $this->inferenceProfiler->addIterationRuleExecution($combinationRule); $workingMemory->setAllFacts($this->execAction($combinationRule, $workingMemory)); $workingMemory->setExecuted($combinationRule); } } else { $this->inferenceProfiler && $this->inferenceProfiler->addIterationRuleExecution($rule); $workingMemory->setAllFacts($this->execAction($rule, $workingMemory)); $workingMemory->setExecuted($rule->getRule()); } }