public static function checkInvariantRules($invariantConjuctsIds = null, $cacheConjuncts = true) { $invariantRulesHold = true; // default // check invariant rules Notifications::addLog('------------------------- CHECKING INVARIANT RULES -------------------------', 'RuleEngine'); // If $allInvariantConjuctsIds is provided (i.e. not null, which is something different than an empty array), check only those invariant conjuncts if (!is_null($invariantConjuctsIds)) { Notifications::addLog("Checking provided conjuncts: " . implode(', ', $invariantConjuctsIds), 'RuleEngine'); foreach ((array) $invariantConjuctsIds as $conjunctId) { $violations = RuleEngine::checkConjunct($conjunctId, $cacheConjuncts); foreach ((array) $violations as $violation) { $invariantRulesHold = false; $conjunct = RuleEngine::getConjunct($conjunctId); foreach ($conjunct['invariantRuleNames'] as $ruleName) { $rule = RuleEngine::getRule($ruleName); Notifications::addInvariant($rule, $violation['src'], $violation['tgt']); } } } // Otherwise check all invariantConjuncts } else { Notifications::addLog("Checking all invariant rules", 'RuleEngine'); foreach (RuleEngine::getAllInvariantRulesNames() as $ruleName) { $rule = RuleEngine::getRule($ruleName); $violations = RuleEngine::checkRule($rule, $cacheConjuncts); foreach ((array) $violations as $violation) { $invariantRulesHold = false; Notifications::addInvariant($rule, $violation['src'], $violation['tgt']); } } } return $invariantRulesHold; }
public static function run($allRules = false) { $database = Database::singleton(); Notifications::addLog('------------------------- EXEC ENGINE STARTED -------------------------', 'ExecEngine'); // Load the execEngine functions (security hazard :P) $files = getDirectoryList(__DIR__ . '/functions'); foreach ($files as $file) { if (substr($file, -3) !== 'php') { continue; } require_once __DIR__ . '/functions/' . $file; Notifications::addLog('Included file: ' . __DIR__ . '/functions/' . $file, 'ExecEngine'); } self::$roleName = Config::get('execEngineRoleName', 'execEngine'); $role = Role::getRoleByName(self::$roleName); $maxRunCount = Config::get('maxRunCount', 'execEngine'); self::$runCount = 0; self::$autoRerun = Config::get('autoRerun', 'execEngine'); if ($role) { // Get all rules that are maintained by the ExecEngine while (self::$doRun) { self::$doRun = false; self::$runCount++; if (self::$runCount > $maxRunCount) { throw new Exception('Maximum reruns exceeded for ExecEngine (rules with violations:' . implode(', ', $rulesThatHaveViolations) . ')', 500); } Notifications::addLog("ExecEngine run #" . self::$runCount . " (auto rerun: " . var_export(self::$autoRerun, true) . ") for role '" . $role->label . "'", 'ExecEngine'); // Determine affected rules that must be checked by the exec engine $affectedConjuncts = (array) RuleEngine::getAffectedInvConjuncts($database->getAffectedConcepts(), $database->getAffectedRelations()); $affectedConjuncts = array_merge($affectedConjuncts, (array) RuleEngine::getAffectedSigConjuncts($database->getAffectedConcepts(), $database->getAffectedRelations())); $affectedRules = array(); foreach ($affectedConjuncts as $conjunctId) { $conjunct = RuleEngine::getConjunct($conjunctId); foreach ($conjunct['invariantRuleNames'] as $ruleName) { $affectedRules[] = $ruleName; } foreach ($conjunct['signalRuleNames'] as $ruleName) { $affectedRules[] = $ruleName; } } // Check rules $rulesThatHaveViolations = array(); foreach ($role->maintains() as $ruleName) { if (!in_array($ruleName, $affectedRules) && !$allRules) { continue; } // skip this rule $rule = RuleEngine::getRule($ruleName); $violations = RuleEngine::checkRule($rule, false); if (count($violations)) { $rulesThatHaveViolations[] = $rule['name']; // Fix violations for every rule ExecEngine::fixViolations($rule, $violations); // Conjunct violations are not cached, because they are fixed by the ExecEngine // If $autoRerun, set $doRun to true because violations have been fixed (this may fire other execEngine rules) if (self::$autoRerun) { self::$doRun = true; } } } } } else { Notifications::addInfo("ExecEngine extension included but role '" . self::$roleName . "' not found."); } Notifications::addLog('------------------------- END OF EXEC ENGINE -------------------------', 'ExecEngine'); }