/** * 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; } }
/** * @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()); }
/** * @param Rule $rule * @param WorkingMemory $workingMemory * * @return array */ private function execAction(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(); }; $code = trim($rule->getAction()); $code = $code . (preg_match('/;$/i', $code) ? '' : ';'); return $executor($code); }