/** * @param Rule $rule * @param WorkingMemory $workingMemory * * @return array */ public function execute(Rule $rule, WorkingMemory $workingMemory) { $facts = $workingMemory->getAllFacts(); /** * @param string $_action * @return array */ $executor = function ($_action) use($facts) { extract($facts); unset($facts); eval($_action); unset($_action); return get_defined_vars(); }; $workingMemory->setAllFacts($executor($rule->getAction())); $workingMemory->setExecuted($rule->getRule()); }
/** * 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); } }
/** * 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); } }
/** * @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()); } }
/** * @param Rule $rule */ public function addIterationRuleExecution(Rule $rule) { $this->currentIterationExecution = sizeof($this->inferences[$this->currentInference]['iterations'][$this->currentIteration]['executions']); $this->inferences[$this->currentInference]['iterations'][$this->currentIteration]['executions'][$this->currentIterationExecution] = ['name' => $rule->getName(), 'action' => $rule->getAction()]; }
/** * 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; } }