Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_rules_Operation | |
0.00% |
0 / 72 |
|
0.00% |
0 / 5 |
506 | |
0.00% |
0 / 1 |
| getFirstOperation | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getSecondOperation | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| getOperator | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| evaluate | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
90 | |||
| evaluateRecursiveOperation | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2007-2010 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO-QUAL); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2017 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT) |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | use oat\generis\model\RulesRdf; |
| 27 | |
| 28 | /** |
| 29 | * Short description of class core_kernel_rules_Operation |
| 30 | * |
| 31 | * @access public |
| 32 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 33 | * @package generis |
| 34 | |
| 35 | */ |
| 36 | class core_kernel_rules_Operation extends core_kernel_rules_Term |
| 37 | { |
| 38 | // --- ASSOCIATIONS --- |
| 39 | |
| 40 | |
| 41 | // --- ATTRIBUTES --- |
| 42 | |
| 43 | /** |
| 44 | * Short description of attribute firstOperation |
| 45 | * |
| 46 | * @access private |
| 47 | * @var Term |
| 48 | */ |
| 49 | private $firstOperation = null; |
| 50 | /** |
| 51 | * Short description of attribute secondOperation |
| 52 | * |
| 53 | * @access private |
| 54 | * @var Term |
| 55 | */ |
| 56 | private $secondOperation = null; |
| 57 | /** |
| 58 | * Short description of attribute arithmeticOperator |
| 59 | * |
| 60 | * @access private |
| 61 | * @var Resource |
| 62 | */ |
| 63 | private $arithmeticOperator = null; |
| 64 | // --- OPERATIONS --- |
| 65 | |
| 66 | /** |
| 67 | * Short description of method getFirstOperation |
| 68 | * |
| 69 | * @access public |
| 70 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 71 | * @return core_kernel_rules_Term |
| 72 | */ |
| 73 | public function getFirstOperation() |
| 74 | { |
| 75 | $returnValue = null; |
| 76 | if (empty($this->firstOperation)) { |
| 77 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_OPERATION_FIRST_OP); |
| 78 | $resource = $this->getUniquePropertyValue($property); |
| 79 | $this->firstOperation = new core_kernel_rules_Term($resource->getUri()); |
| 80 | } |
| 81 | $returnValue = $this->firstOperation; |
| 82 | return $returnValue; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Short description of method getSecondOperation |
| 87 | * |
| 88 | * @access public |
| 89 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 90 | * @return core_kernel_rules_Term |
| 91 | */ |
| 92 | public function getSecondOperation() |
| 93 | { |
| 94 | $returnValue = null; |
| 95 | if (empty($this->secondOperation)) { |
| 96 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_OPERATION_SECOND_OP); |
| 97 | $resource = $this->getUniquePropertyValue($property); |
| 98 | $this->secondOperation = new core_kernel_rules_Term($resource->getUri()); |
| 99 | } |
| 100 | $returnValue = $this->secondOperation; |
| 101 | return $returnValue; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Short description of method getOperator |
| 106 | * |
| 107 | * @access public |
| 108 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 109 | * @return core_kernel_classes_Resource |
| 110 | */ |
| 111 | public function getOperator() |
| 112 | { |
| 113 | $returnValue = null; |
| 114 | if (empty($this->arithmeticOperator)) { |
| 115 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_OPERATION_OPERATOR); |
| 116 | $this->arithmeticOperator = $this->getUniquePropertyValue($property); |
| 117 | } |
| 118 | $returnValue = $this->arithmeticOperator; |
| 119 | return $returnValue; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Short description of method evaluate |
| 124 | * |
| 125 | * @access public |
| 126 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 127 | * @param array variable |
| 128 | * @return mixed |
| 129 | */ |
| 130 | public function evaluate($variable = []) |
| 131 | { |
| 132 | |
| 133 | common_Logger::i('Evaluating Operation uri : ' . $this->getUri(), ['Generis Operation']); |
| 134 | common_Logger::i('Evaluating Operation name : ' . $this->getLabel(), ['Generis Operation']); |
| 135 | $operator = $this->getOperator(); |
| 136 | common_Logger::d('Operator uri: ' . $operator->getUri(), ['Generis Operation']); |
| 137 | common_Logger::d('Operator name: ' . $operator->getLabel(), ['Generis Operation']); |
| 138 | $firstPart = $this->getFirstOperation()->evaluate($variable); |
| 139 | $secondPart = $this->getSecondOperation()->evaluate($variable); |
| 140 | if ($firstPart instanceof core_kernel_classes_ContainerCollection) { |
| 141 | //if we have more than one result we only take the Literal label |
| 142 | $nbLiteral = 0; |
| 143 | $iterator = $firstPart->getIterator(); |
| 144 | foreach ($iterator as $first) { |
| 145 | if ($first instanceof core_kernel_classes_Literal) { |
| 146 | $firstPart = $first; |
| 147 | $nbLiteral++; |
| 148 | } |
| 149 | } |
| 150 | if ($nbLiteral != 1) { |
| 151 | var_dump($iterator); |
| 152 | throw new common_Exception('more than one Literal Retreive during evaluation'); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | if ($secondPart instanceof core_kernel_classes_ContainerCollection) { |
| 159 | //if we have more than one result we only take the resource label |
| 160 | $nbLiteral = 0; |
| 161 | $iterator = $secondPart->getIterator(); |
| 162 | foreach ($secondPart->getIterator() as $second) { |
| 163 | if ($second instanceof core_kernel_classes_Literal) { |
| 164 | $secondPart = $second; |
| 165 | $nbLiteral++; |
| 166 | } |
| 167 | } |
| 168 | if ($nbLiteral != 1) { |
| 169 | var_dump($iterator); |
| 170 | throw new common_Exception('more than one Literal Retreive during evaluation'); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | common_Logger::d('First Part : ', ['Generis Operation']); |
| 175 | common_Logger::d('Second Part : ' . $secondPart, ['Generis Operation']); |
| 176 | $returnValue = $this->evaluateRecursiveOperation($firstPart, $secondPart, $operator); |
| 177 | common_Logger::i('Operation value: ' . $returnValue, ['Generis Operation']); |
| 178 | |
| 179 | return $returnValue; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Short description of method evaluateRecursiveOperation |
| 184 | * |
| 185 | * @access public |
| 186 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 187 | * @param Literal first |
| 188 | * @param Literal second |
| 189 | * @param Resource operator |
| 190 | * @return mixed |
| 191 | */ |
| 192 | public function evaluateRecursiveOperation( |
| 193 | core_kernel_classes_Literal $first, |
| 194 | core_kernel_classes_Literal $second, |
| 195 | core_kernel_classes_Resource $operator |
| 196 | ) { |
| 197 | |
| 198 | |
| 199 | switch ($operator->getUri()) { |
| 200 | case RulesRdf::INSTANCE_OPERATOR_ADD: |
| 201 | $returnValue = new core_kernel_classes_Literal($first->literal + $second->literal); |
| 202 | break; |
| 203 | |
| 204 | |
| 205 | case RulesRdf::INSTANCE_OPERATOR_MINUS: |
| 206 | $returnValue = new core_kernel_classes_Literal($first->literal - $second->literal); |
| 207 | break; |
| 208 | |
| 209 | |
| 210 | case RulesRdf::INSTANCE_OPERATOR_MULTIPLY: |
| 211 | $returnValue = new core_kernel_classes_Literal($first->literal * $second->literal); |
| 212 | break; |
| 213 | |
| 214 | |
| 215 | case RulesRdf::INSTANCE_OPERATOR_DIVISION: |
| 216 | $returnValue = new core_kernel_classes_Literal($first->literal / $second->literal); |
| 217 | break; |
| 218 | |
| 219 | |
| 220 | case RulesRdf::INSTANCE_OPERATOR_CONCAT: |
| 221 | // FIXME Hotfix for the concat operator. Can't find why traling spaces are not |
| 222 | // kept intact when using concat. |
| 223 | // ex: 'february ' CONCAT '2008' -> 'february2008' instead of 'february 2008'. |
| 224 | $returnValue = new core_kernel_classes_Literal($first->literal . ' ' . $second->literal); |
| 225 | break; |
| 226 | |
| 227 | |
| 228 | |
| 229 | default: |
| 230 | throw new common_Exception('problem evaluating operation, operator do not match with operands'); |
| 231 | } |
| 232 | $returnValue->debug = __METHOD__; |
| 233 | return $returnValue; |
| 234 | } |
| 235 | } |