/** * Set the QTI Data Model Rule object to be processed. * * @param \qtism\runtime\rules\Rule $rule * @throws \InvalidArgumentException If $rule is not compliant with the rule processor implementation. */ public function setRule(Rule $rule) { $expectedType = $this->getRuleType(); if (Reflection::isInstanceOf($rule, $expectedType) === true) { $this->rule = $rule; } else { $procClass = get_class($this); $givenType = get_class($rule); $msg = "The {$procClass} Rule Processor only processes {$expectedType} Rule objects, {$givenType} given."; throw new InvalidArgumentException($msg); } }
/** * Set the QTI Data Model Expression to be processed. * * @param \qtism\data\expressions\Expression $expression A QTI Data Model Expression object. * @throws \InvalidArgumentException If $expression is not a subclass nor implements the Expression type returned by the getExpressionType method. */ public function setExpression(Expression $expression) { $expectedType = $this->getExpressionType(); if (ReflectionUtils::isInstanceOf($expression, $expectedType) === true) { $this->expression = $expression; } else { $procClass = get_class($this); $givenType = get_class($expression); $msg = "The {$procClass} Expression Processor only processes {$expectedType} Expression objects, {$givenType} given."; throw new InvalidArgumentException($msg); } $this->expression = $expression; }
/** * Process the setOutcomeValue/setTemplateValue rule. * * A RuleProcessingException will be thrown if: * * * The variable does not exist. * * The requested variable is not an OutcomeVariable/TemplateVariable. * * The variable's baseType does not match the baseType of the affected value. * * An error occurs while processing the related expression. * * @throws \qtism\runtime\rules\RuleProcessingException If one of the error described above arise. */ public function process() { $state = $this->getState(); $rule = $this->getRule(); $identifier = $rule->getIdentifier(); $var = $state->getVariable($identifier); if (is_null($var) === true) { $msg = "No variable with identifier '{$identifier}' to be set in the current state."; throw new RuleProcessingException($msg, $this, RuleProcessingException::NONEXISTENT_VARIABLE); } elseif (Reflection::isInstanceOf($var, $this->getVariableType()) === false) { $msg = "The variable to set '{$identifier}' is not an instance of '" . $this->getVariableType() . "'."; throw new RuleProcessingException($msg, $this, RuleProcessingException::WRONG_VARIABLE_TYPE); } // Process the expression. // Its result will be the value to set to the target variable. try { $expressionEngine = new ExpressionEngine($rule->getExpression(), $state); $val = $expressionEngine->process(); } catch (ExpressionProcessingException $e) { $msg = "An error occured while processing the expression bound with the '" . Reflection::shortClassName($rule) . "' rule."; throw new RuleProcessingException($msg, $this, RuleProcessingException::RUNTIME_ERROR, $e); } // The variable exists, its new value is processed. try { // juggle a little bit (int -> float, float -> int) if ($val !== null && $var->getCardinality() === Cardinality::SINGLE) { $baseType = $var->getBaseType(); if ($baseType === BaseType::INTEGER && $val instanceof Float) { $val = new Integer(intval($val->getValue())); } elseif ($baseType === BaseType::FLOAT && $val instanceof Integer) { $val = new Float(floatval($val->getValue())); } } $var->setValue($val); } catch (InvalidArgumentException $e) { $varBaseType = BaseType::getNameByConstant($var->getBaseType()) === false ? 'noBaseType' : BaseType::getNameByConstant($var->getBaseType()); $varCardinality = Cardinality::getNameByConstant($var->getCardinality()); // The affected value does not match the baseType of the variable $var. $msg = "Unable to set value {$val} to variable '{$identifier}' (cardinality = {$varCardinality}, baseType = {$varBaseType})."; throw new RuleProcessingException($msg, $this, RuleProcessingException::WRONG_VARIABLE_BASETYPE, $e); } }