public function closeTransaction($succesMessage = 'Updated', $checkAllConjucts = true, $databaseCommit = false, $setNewContent = true) { $session = Session::singleton(); Hooks::callHooks('preDatabaseCloseTransaction', get_defined_vars()); Notifications::addLog('========================= CLOSING TRANSACTION =========================', 'DATABASE'); if ($checkAllConjucts) { Notifications::addLog("Check all conjuncts", 'DATABASE'); // Evaluate all invariant conjuncts. Conjuncts are cached. $invariantRulesHold = RuleEngine::checkInvariantRules(); // Evaluate all signal conjuncts. Conjuncts are cached RuleEngine::checkProcessRules(); } else { Notifications::addLog("Check all affected conjuncts", 'DATABASE'); // Evaluate all affected invariant conjuncts. Conjuncts are cached. $invariantRulesHold = RuleEngine::checkInvariantRules(RuleEngine::getAffectedInvConjuncts($this->affectedConcepts, $this->affectedRelations), true); // Evaluate all affected signal conjuncts. Conjuncts are cached RuleEngine::checkConjuncts(RuleEngine::getAffectedSigConjuncts($this->affectedConcepts, $this->affectedRelations), true); // Check only those process rules that are relevant for the activate roles RuleEngine::checkProcessRules($session); } unset($this->affectedConcepts, $this->affectedRelations); $this->affectedConcepts = array(); $this->affectedRelations = array(); if ($setNewContent && isset($session->atom)) { $session->atom->setNewContent($session->interface); } // e.g. not needed in Atom::delete() function if ($invariantRulesHold && $databaseCommit) { $this->commitTransaction(); // commit database transaction Notifications::addSuccess($succesMessage); } elseif (Config::get('ignoreInvariantViolations', 'transactions') && COMMIT_INV_VIOLATIONS) { $this->commitTransaction(); Notifications::addError("Transaction committed with invariant violations"); } elseif ($invariantRulesHold) { $this->rollbackTransaction(); // rollback database transaction Notifications::addInfo($succesMessage); } else { $this->rollbackTransaction(); // rollback database transaction } Hooks::callHooks('postDatabaseCloseTransaction', get_defined_vars()); return $invariantRulesHold; }
public static function getSignalsFromDB($conjunctIds) { $db = Database::singleton(); $result = array(); $conjunctIds = array_unique($conjunctIds); // remove duplicates if (count($conjunctIds) > 0) { // TODO: DB Query can be changed to WHERE `conjId` IN (<conjId1>, <conjId2>, etc) $query = "SELECT * FROM `__all_signals__` WHERE " . implode(' OR ', array_map(function ($conjunctId) { return "`conjId` = '{$conjunctId}'"; }, $conjunctIds)); $result = $db->Exe($query); } else { Notifications::addInfo("No conjunctIds provided (can be that this role does not maintain any rule)"); } return $result; }
public static function checkRule($rule, $cacheConjuncts = true) { $db = Database::singleton(); $violations = array(); Notifications::addLog("Checking rule '" . $rule['name'] . "'", 'RuleEngine'); try { foreach ($rule['conjunctIds'] as $conjunctId) { $result = array_merge((array) $result, RuleEngine::checkConjunct($conjunctId, $cacheConjuncts)); } if (count($result) == 0) { Notifications::addInfo("Rule '" . $rule['name'] . "' holds", 'RuleEngineRulesThatHold', 'Rules that hold'); } else { $violations = $result; } return $violations; } catch (Exception $e) { Notifications::addError("While evaluating rule '" . $rule['name'] . "': " . $e->getMessage()); } }
/** * @url GET resource/{concept}/{srcAtomId}/{interfaceId} * @url GET resource/{concept}/{srcAtomId}/{interfaceId}/{tgtAtomId} * @param string $concept * @param string $srcAtomId * @param string $interfaceId * @param string $tgtAtomId * @param array $roleIds * @param boolean $inclLinktoData * @param string $arrayType * @param boolean $metaData */ public function getAtom($concept, $srcAtomId, $interfaceId, $tgtAtomId = null, $roleIds = null, $inclLinktoData = false, $arrayType = "assoc", $metaData = true) { try { $session = Session::singleton(); $session->activateRoles($roleIds); $session->setInterface($interfaceId); $result = array(); if ($session->interface->srcConcept != $concept) { throw new Exception("Concept '{$concept}' cannot be used as source concept for interface '" . $session->interface->label . "'", 400); } if (!$session->interface->crudR) { throw new Exception("GET is not allowed for interface " . $session->interface->label, 405); } $atom = new Atom($srcAtomId, $concept); if (!$atom->atomExists()) { throw new Exception("Resource '{$srcAtomId}' not found", 404); } $result = (array) $atom->getContent($session->interface, true, $tgtAtomId, $inclLinktoData, $arrayType, $metaData); if (empty($result)) { Notifications::addInfo("No results found"); } if (is_null($tgtAtomId)) { // return array of atoms (i.e. tgtAtoms of the interface given srcAtomId) return array_values($result); // array_values transforms assoc array to non-assoc array } else { // return 1 atom (i.e. tgtAtomId) return current($result); } } catch (Exception $e) { throw new RestException($e->getCode(), $e->getMessage()); } }
public static function fixViolations($rule, $violations) { if (count($violations)) { Notifications::addLog('ExecEngine fixing violations for rule: ' . $rule['name'], 'ExecEngine'); foreach ($violations as $violation) { $theMessage = ExecEngine::getPairView($violation['src'], $rule['srcConcept'], $violation['tgt'], $rule['tgtConcept'], $rule['pairView']); // This function tries to return a string with all NULL bytes, HTML and PHP tags stripped from a given str. Strip_tags() is binary safe. // $theCleanMessage = strip_tags($theMessage); // Determine actions/functions to be taken $functionsToBeCalled = explode('{EX}', $theMessage); // Execute actions/functions foreach ($functionsToBeCalled as $functionToBeCalled) { if (empty($functionToBeCalled)) { continue; } // skips to the next iteration if $functionToBeCalled is empty. This is the case when violation text starts with delimiter {EX} // Determine delimiter if (substr($functionToBeCalled, 0, 2) == '_;') { $delimiter = '_;'; $functionToBeCalled = substr($functionToBeCalled, 2); } else { $delimiter = ';'; } $params = explode($delimiter, $functionToBeCalled); // Split off variables $params = array_map('trim', $params); // Trim all params $params = array_map('phpArgumentInterpreter', $params); // Evaluate phpArguments, using phpArgumentInterpreter function $function = array_shift($params); // First parameter is function name $classMethod = (array) explode('::', $function); if (function_exists($function) || method_exists($classMethod[0], $classMethod[1])) { $successMessage = call_user_func_array($function, $params); Notifications::addLog($successMessage, 'ExecEngine'); } else { $errorMessage = "Function '" . $function . "' does not exists. Create function with " . count($params) . " parameters"; throw new Exception($errorMessage, 500); } } } Notifications::addInfo(self::$roleName . ' fixed violations for rule: ' . $rule['name'], 'ExecEngineSuccessMessage', self::$roleName . ' fixed violations'); } }