Skip to content

Commit

Permalink
Use default values when no mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jan 12, 2016
1 parent da161b0 commit e9c076f
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 162 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "qtism/qtism",
"description": "OAT QTI Software Module Library",
"type": "library",
"version": "0.9.18",
"version": "0.9.19",
"authors": [
{
"name": "Open Assessment Technologies S.A.",
Expand Down
129 changes: 63 additions & 66 deletions qtism/runtime/expressions/MapResponsePointProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,73 +86,70 @@ public function process() {
if ($var instanceof ResponseVariable) {
$areaMapping = $var->getAreaMapping();

if (!is_null($areaMapping)) {
if (is_null($areaMapping)) {
return new Float(0.0);
}

// Correct cardinality ?
if ($var->getBaseType() === BaseType::POINT && ($var->isSingle() || $var->isMultiple())) {
// We can begin!
// -- Null value, nothing will match
if ($var->isNull()) {
return new Float($areaMapping->getDefaultValue());
}
if ($var->isSingle()) {
$val = new MultipleContainer(BaseType::POINT, array($state[$identifier]));
}
else {
$val = $state[$identifier];
}
$result = 0;
$mapped = array();
foreach ($val as $point) {
foreach ($areaMapping->getAreaMapEntries() as $areaMapEntry) {
// Correct cardinality ?
if ($var->getBaseType() === BaseType::POINT && ($var->isSingle() || $var->isMultiple())) {
// We can begin!
// -- Null value, nothing will match
if ($var->isNull()) {
return new Float($areaMapping->getDefaultValue());
}
if ($var->isSingle()) {
$val = new MultipleContainer(BaseType::POINT, array($state[$identifier]));
}
else {
$val = $state[$identifier];
}
$result = 0;
$mapped = array();
foreach ($val as $point) {
foreach ($areaMapping->getAreaMapEntries() as $areaMapEntry) {

$coords = $areaMapEntry->getCoords();

if (!in_array($coords, $mapped) && $coords->inside($point)) {
$mapped[] = $coords;
$result += $areaMapEntry->getMappedValue();
}
}
}

// If no relevant mapping found, return the default.
if (count($mapped) === 0) {
return new Float($areaMapping->getDefaultValue());
}
else {
// Check upper and lower bound.
if ($areaMapping->hasLowerBound() && $result < $areaMapping->getLowerBound()) {
return new Float($areaMapping->getLowerBound());
}
else if ($areaMapping->hasUpperBound() && $result > $areaMapping->getUpperBound()) {
return new Float($areaMapping->getUpperBound());
}
else {
return new Float(floatval($result));
}
}
}
else {
if ($var->isRecord()) {
$msg = "The MapResponsePoint expression cannot be applied to RECORD variables.";
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::WRONG_VARIABLE_BASETYPE);
}
else {
$strBaseType = BaseType::getNameByConstant($var->getBaseType());
$msg = "The MapResponsePoint expression applies only on variables with baseType 'point', baseType '${strBaseType}' given.";
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::WRONG_VARIABLE_BASETYPE);
}
}
}
else {
$msg = "Variable with identifier '${identifier}' has no areaMapping.";
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::INCONSISTENT_VARIABLE);
}
$coords = $areaMapEntry->getCoords();

if (!in_array($coords, $mapped) && $coords->inside($point)) {
$mapped[] = $coords;
$result += $areaMapEntry->getMappedValue();
}
}
}

// If no relevant mapping found, return the default.
if (count($mapped) === 0) {
return new Float($areaMapping->getDefaultValue());
}
else {
// Check upper and lower bound.
if ($areaMapping->hasLowerBound() && $result < $areaMapping->getLowerBound()) {
return new Float($areaMapping->getLowerBound());
}
else if ($areaMapping->hasUpperBound() && $result > $areaMapping->getUpperBound()) {
return new Float($areaMapping->getUpperBound());
}
else {
return new Float(floatval($result));
}
}
}
else {
if ($var->isRecord()) {
$msg = "The MapResponsePoint expression cannot be applied to RECORD variables.";
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::WRONG_VARIABLE_BASETYPE);
}
else {
$strBaseType = BaseType::getNameByConstant($var->getBaseType());
$msg = "The MapResponsePoint expression applies only on variables with baseType 'point', baseType '${strBaseType}' given.";
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::WRONG_VARIABLE_BASETYPE);
}
}
}
else {
$msg = "The variable with identifier '${identifier}' is not a ResponseVariable.";
Expand All @@ -164,4 +161,4 @@ public function process() {
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::NONEXISTENT_VARIABLE);
}
}
}
}
173 changes: 84 additions & 89 deletions qtism/runtime/expressions/MapResponseProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,95 +90,90 @@ public function process() {

$mapping = $variable->getMapping();

if (!is_null($mapping)) {
if (is_null($mapping)) {
return new Float(0.0);
}

if ($variable->isSingle()) {

foreach ($mapping->getMapEntries() as $mapEntry) {

$val = $state[$identifier];
$mapKey = $mapEntry->getMapKey();

if ($val instanceof String && $mapEntry->isCaseSensitive() === false) {
$val = mb_strtolower($val->getValue(), 'UTF-8');
$mapKey = mb_strtolower($mapKey, 'UTF-8');
}

if ($val instanceof Comparable && $val->equals($mapKey) || $val === $mapKey) {
// relevant mapping found.
$mappedValue = $mapEntry->getMappedValue();
return new Float($mappedValue);
}
}

// No relevant mapping found, return mapping default.
return new Float($mapping->getDefaultValue());
}
else if ($variable->isMultiple()) {

$result = 0.0;

if (!is_null($variable->getValue())) {
$mapped = array(); // already mapped keys.
$mapEntries = $mapping->getMapEntries();

foreach ($variable->getValue() as $val) {

for ($i = 0; $i < count($mapEntries); $i++) {

$mapKey = $rawMapKey = $mapEntries[$i]->getMapKey();

if ($val instanceof String && $mapEntries[$i]->isCaseSensitive() === false) {
$val = mb_strtolower($val->getValue(), 'UTF-8');
$mapKey = mb_strtolower($mapKey, 'UTF-8');
}

if (($val instanceof Comparable && $val->equals($mapKey) === true) || $val === $mapKey) {
if (in_array($rawMapKey, $mapped, true) === false) {
$result += $mapEntries[$i]->getMappedValue();
$mapped[] = $rawMapKey;

}
// else...
// This value has already been mapped.

break;
}
}

if ($i >= count($mapEntries)) {
// No explicit mapping found for source value $val.
$result += $mapping->getDefaultValue();
}
}

// When mapping a container, try to apply lower or upper bound.
if ($mapping->hasLowerBound() && $result < $mapping->getLowerBound()) {
return new Float($mapping->getLowerBound());
}
else if ($mapping->hasUpperBound() && $result > $mapping->getUpperBound()) {
return new Float($mapping->getUpperBound());
}
else {
return new Float($result);
}
}
else {
// Returns a 0.0 result.
return new Float($result);
}


}
else {
$msg = "MapResponse cannot be applied on a RECORD container.";
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::WRONG_VARIABLE_BASETYPE);
}
}
else {
$msg = "The target variable has no mapping while processing MapResponse.";
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::INCONSISTENT_VARIABLE);
}
if ($variable->isSingle()) {

foreach ($mapping->getMapEntries() as $mapEntry) {

$val = $state[$identifier];
$mapKey = $mapEntry->getMapKey();

if ($val instanceof String && $mapEntry->isCaseSensitive() === false) {
$val = mb_strtolower($val->getValue(), 'UTF-8');
$mapKey = mb_strtolower($mapKey, 'UTF-8');
}

if ($val instanceof Comparable && $val->equals($mapKey) || $val === $mapKey) {
// relevant mapping found.
$mappedValue = $mapEntry->getMappedValue();
return new Float($mappedValue);
}
}

// No relevant mapping found, return mapping default.
return new Float($mapping->getDefaultValue());
}
else if ($variable->isMultiple()) {

$result = 0.0;

if (!is_null($variable->getValue())) {
$mapped = array(); // already mapped keys.
$mapEntries = $mapping->getMapEntries();

foreach ($variable->getValue() as $val) {

for ($i = 0; $i < count($mapEntries); $i++) {

$mapKey = $rawMapKey = $mapEntries[$i]->getMapKey();

if ($val instanceof String && $mapEntries[$i]->isCaseSensitive() === false) {
$val = mb_strtolower($val->getValue(), 'UTF-8');
$mapKey = mb_strtolower($mapKey, 'UTF-8');
}

if (($val instanceof Comparable && $val->equals($mapKey) === true) || $val === $mapKey) {
if (in_array($rawMapKey, $mapped, true) === false) {
$result += $mapEntries[$i]->getMappedValue();
$mapped[] = $rawMapKey;

}
// else...
// This value has already been mapped.

break;
}
}

if ($i >= count($mapEntries)) {
// No explicit mapping found for source value $val.
$result += $mapping->getDefaultValue();
}
}

// When mapping a container, try to apply lower or upper bound.
if ($mapping->hasLowerBound() && $result < $mapping->getLowerBound()) {
return new Float($mapping->getLowerBound());
}
else if ($mapping->hasUpperBound() && $result > $mapping->getUpperBound()) {
return new Float($mapping->getUpperBound());
}
else {
return new Float($result);
}
}
else {
// Returns a 0.0 result.
return new Float($result);
}
}
else {
$msg = "MapResponse cannot be applied on a RECORD container.";
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::WRONG_VARIABLE_BASETYPE);
}
}
else {
$msg = "The target variable must be a ResponseVariable, OutcomeVariable given while processing MapResponse.";
Expand All @@ -190,4 +185,4 @@ public function process() {
throw new ExpressionProcessingException($msg, $this, ExpressionProcessingException::NONEXISTENT_VARIABLE);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ public function testNoAreaMapping() {
$variable = ResponseVariable::createFromDataModel($variableDeclaration);
$processor = new MapResponsePointProcessor($expr);
$processor->setState(new State(array($variable)));

$this->setExpectedException("qtism\\runtime\\expressions\\ExpressionProcessingException");

$result = $processor->process();
$this->assertEquals(0.0, $result->getValue());
}

public function testWrongVariableType() {
Expand Down Expand Up @@ -212,4 +212,4 @@ public function testUpperBoundOverflow() {
$this->assertInstanceOf('qtism\\common\\datatypes\\Float', $result);
$this->assertEquals(5, $result->getValue());
}
}
}
6 changes: 3 additions & 3 deletions test/qtism/runtime/expressions/MapResponseProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ public function testVariableNotDefined() {
}

public function testNoMapping() {
$this->setExpectedException("qtism\\runtime\\expressions\\ExpressionProcessingException");
$variableDeclaration = $this->createComponentFromXml('<responseDeclaration identifier="response1" baseType="duration" cardinality="multiple"/>');
$variable = ResponseVariable::createFromDataModel($variableDeclaration);
$mapResponseExpr = $this->createComponentFromXml('<mapResponse identifier="response1"/>');
$mapResponseProcessor = new MapResponseProcessor($mapResponseExpr);

$mapResponseProcessor->setState(new State(array($variable)));
$mapResponseProcessor->process();
$result = $mapResponseProcessor->process();
$this->assertEquals(0.0, $result->getValue());
}

public function testMultipleCardinalityIdentifierToFloat() {
Expand Down Expand Up @@ -225,4 +225,4 @@ public function testOutcomeDeclaration() {
$mapResponseProcessor->setState(new State(array($variable)));
$mapResponseProcessor->process();
}
}
}

0 comments on commit e9c076f

Please sign in to comment.