Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 163 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_rules_Expression | |
0.00% |
0 / 163 |
|
0.00% |
0 / 13 |
5256 | |
0.00% |
0 / 1 |
| evaluate | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| getLogicalOperator | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getTerminalExpression | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getFirstExpression | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getSecondExpression | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| expEval | |
0.00% |
0 / 65 |
|
0.00% |
0 / 1 |
992 | |||
| evalEquals | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| evalDifferent | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| evalInfEquals | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| evalInf | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| evalSup | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| evalSupEquals | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| operatorEval | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
132 | |||
| 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_Expression |
| 30 | * |
| 31 | * @access public |
| 32 | * @author firstname and lastname of author, <author@example.org> |
| 33 | * @package generis |
| 34 | |
| 35 | */ |
| 36 | class core_kernel_rules_Expression extends core_kernel_classes_Resource |
| 37 | { |
| 38 | // --- ASSOCIATIONS --- |
| 39 | |
| 40 | |
| 41 | // --- ATTRIBUTES --- |
| 42 | |
| 43 | /** |
| 44 | * Short description of attribute logicalOperator |
| 45 | * |
| 46 | * @access private |
| 47 | * @var Resource |
| 48 | */ |
| 49 | private $logicalOperator = null; |
| 50 | /** |
| 51 | * Short description of attribute firstExpression |
| 52 | * |
| 53 | * @access private |
| 54 | * @var Expression |
| 55 | */ |
| 56 | private $firstExpression = null; |
| 57 | /** |
| 58 | * Short description of attribute secondExpression |
| 59 | * |
| 60 | * @access private |
| 61 | * @var Expression |
| 62 | */ |
| 63 | private $secondExpression = null; |
| 64 | // --- OPERATIONS --- |
| 65 | |
| 66 | /** |
| 67 | * Short description of method evaluate |
| 68 | * |
| 69 | * @access public |
| 70 | * @author firstname and lastname of author, <author@example.org> |
| 71 | * @param array variable |
| 72 | * @return boolean |
| 73 | */ |
| 74 | public function evaluate($variable = []) |
| 75 | { |
| 76 | $returnValue = (bool) false; |
| 77 | common_Logger::i('Evaluating Expression uri: ' . $this->getUri(), ['Generis Expression']); |
| 78 | common_Logger::i('Evaluating Expression name: ' . $this->getLabel(), ['Generis Expression']); |
| 79 | if ($this->getUri() == RulesRdf::INSTANCE_EXPRESSION_TRUE) { |
| 80 | return true; |
| 81 | } |
| 82 | if ($this->getUri() == RulesRdf::INSTANCE_EXPRESSION_FALSE) { |
| 83 | return false; |
| 84 | } |
| 85 | $returnValue = $this->expEval($variable); |
| 86 | $logValue = $returnValue ? ' TRUE ' : ' FALSE '; |
| 87 | common_Logger::i('Value : ' . $logValue, ['Generis Expression']); |
| 88 | return (bool) $returnValue; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Short description of method getLogicalOperator |
| 93 | * |
| 94 | * @access public |
| 95 | * @author firstname and lastname of author, <author@example.org> |
| 96 | * @return core_kernel_classes_Resource |
| 97 | */ |
| 98 | public function getLogicalOperator() |
| 99 | { |
| 100 | $returnValue = null; |
| 101 | if (empty($this->logicalOperator)) { |
| 102 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_HASLOGICALOPERATOR); |
| 103 | $this->logicalOperator = $this->getUniquePropertyValue($property); |
| 104 | } |
| 105 | $returnValue = $this->logicalOperator; |
| 106 | return $returnValue; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Short description of method getTerminalExpression |
| 111 | * |
| 112 | * @access public |
| 113 | * @author firstname and lastname of author, <author@example.org> |
| 114 | * @return core_kernel_rules_Term |
| 115 | */ |
| 116 | public function getTerminalExpression() |
| 117 | { |
| 118 | $returnValue = null; |
| 119 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_TERMINAL_EXPRESSION); |
| 120 | $propertyValue = $this->getUniquePropertyValue($property); |
| 121 | if ($propertyValue instanceof core_kernel_classes_Resource) { |
| 122 | $returnValue = new core_kernel_rules_Term($propertyValue->getUri()); |
| 123 | $returnValue->debug = __METHOD__; |
| 124 | } else { |
| 125 | throw new common_Exception('property retrieve should be a Resource'); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | return $returnValue; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Short description of method getFirstExpression |
| 134 | * |
| 135 | * @access public |
| 136 | * @author firstname and lastname of author, <author@example.org> |
| 137 | * @return core_kernel_rules_Expression |
| 138 | */ |
| 139 | public function getFirstExpression() |
| 140 | { |
| 141 | $returnValue = null; |
| 142 | if (empty($this->firstExpression)) { |
| 143 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_FIRST_EXPRESSION); |
| 144 | $this->firstExpression = new core_kernel_rules_Expression( |
| 145 | $this->getUniquePropertyValue($property)->getUri() |
| 146 | ); |
| 147 | } |
| 148 | $returnValue = $this->firstExpression; |
| 149 | return $returnValue; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Short description of method getSecondExpression |
| 154 | * |
| 155 | * @access public |
| 156 | * @author firstname and lastname of author, <author@example.org> |
| 157 | * @return core_kernel_rules_Expression |
| 158 | */ |
| 159 | public function getSecondExpression() |
| 160 | { |
| 161 | $returnValue = null; |
| 162 | if (empty($this->secondExpression)) { |
| 163 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_SECOND_EXPRESSION); |
| 164 | $this->secondExpression = new core_kernel_rules_Expression( |
| 165 | $this->getUniquePropertyValue($property)->getUri() |
| 166 | ); |
| 167 | } |
| 168 | $returnValue = $this->secondExpression; |
| 169 | return $returnValue; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Short description of method expEval |
| 174 | * |
| 175 | * @access public |
| 176 | * @author firstname and lastname of author, <author@example.org> |
| 177 | * @param array variable |
| 178 | * @return mixed |
| 179 | */ |
| 180 | public function expEval($variable = []) |
| 181 | { |
| 182 | |
| 183 | $terminalExpression = $this->getTerminalExpression(); |
| 184 | |
| 185 | |
| 186 | if ($terminalExpression->getUri() == RulesRdf::INSTANCE_EMPTY_TERM_URI) { |
| 187 | $firstPart = $this->getFirstExpression()->expEval($variable) ; |
| 188 | |
| 189 | if ($this->getLogicalOperator()->getUri() == RulesRdf::INSTANCE_AND_OPERATOR) { |
| 190 | if ($firstPart == false) { |
| 191 | common_Logger::i('CUT : first Expression == FALSE and OPERATOR = AND', ['Generis Expression']); |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | if ($this->getLogicalOperator()->getUri() == RulesRdf::INSTANCE_OR_OPERATOR) { |
| 196 | if ($firstPart == true) { |
| 197 | common_Logger::i('CUT : first Expression == TRUE and OPERATOR = OR', ['Generis Expression']); |
| 198 | return true; |
| 199 | } |
| 200 | } |
| 201 | $secondPart = $this->getSecondExpression()->expEval($variable); |
| 202 | |
| 203 | //if both part are simple value |
| 204 | if ( |
| 205 | $firstPart instanceof core_kernel_classes_Container |
| 206 | && $secondPart instanceof core_kernel_classes_Container |
| 207 | ) { |
| 208 | common_Logger::d('Both Part are Container', ['Generis Expression']); |
| 209 | $returnValue = $this->operatorEval($firstPart, $secondPart); |
| 210 | } elseif ( |
| 211 | //both are vector |
| 212 | $firstPart instanceof core_kernel_classes_ContainerCollection |
| 213 | && $secondPart instanceof core_kernel_classes_ContainerCollection |
| 214 | ) { |
| 215 | $returnValue = false; |
| 216 | foreach ($firstPart->getIterator() as $subLeftPart) { |
| 217 | //analyse left collection and remove any container in it which is not literal |
| 218 | if (!($subLeftPart instanceof core_kernel_classes_Resource)) { |
| 219 | foreach ($secondPart->getIterator() as $subRightPart) { |
| 220 | //analyse right collection and remove any container in it which is not literal |
| 221 | if (!($subRightPart instanceof core_kernel_classes_Resource)) { |
| 222 | //print_r($subLeftPart);print_r($subRightPart); |
| 223 | |
| 224 | $returnValue = $returnValue || $this->operatorEval($subLeftPart, $subRightPart); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | //die("the evaluation is ". $returnValue); |
| 230 | |
| 231 | //throw new common_Exception('not implemented yet', __FILE__,__LINE__); |
| 232 | } elseif ( |
| 233 | // first is a vector second is a value |
| 234 | $firstPart instanceof core_kernel_classes_ContainerCollection |
| 235 | && $secondPart instanceof core_kernel_classes_Container |
| 236 | ) { |
| 237 | $tempResult = false; |
| 238 | foreach ($firstPart->getIterator() as $container) { |
| 239 | common_Logger::d( |
| 240 | 'FirstPart Part is ContainerCollection Second is Container', |
| 241 | ['Generis Expression'] |
| 242 | ); |
| 243 | //TODO For now consider that if only one value of the table return true, |
| 244 | |
| 245 | //TODO exist unique need to be added |
| 246 | |
| 247 | if ($this->getLogicalOperator()->getUri() != RulesRdf::INSTANCE_DIFFERENT_OPERATOR_URI) { |
| 248 | $tempResult = $tempResult || $this->operatorEval($container, $secondPart); |
| 249 | } else { |
| 250 | if ($this->operatorEval($container, $secondPart)) { |
| 251 | $tempResult = true; |
| 252 | } else { |
| 253 | break; |
| 254 | $tempResult = false; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | $returnValue = $tempResult; |
| 259 | } elseif ( |
| 260 | // first is a value second is a vector |
| 261 | $firstPart instanceof core_kernel_classes_Container |
| 262 | && $secondPart instanceof core_kernel_classes_ContainerCollection |
| 263 | ) { |
| 264 | foreach ($secondPart->getIterator() as $container) { |
| 265 | common_Logger::d( |
| 266 | 'FirstPart Part Container is Second is ContainerCollection', |
| 267 | ['Generis Expression'] |
| 268 | ); |
| 269 | |
| 270 | //TODO For now consider that all value of the table need to be equal to return true, , |
| 271 | //TODO exist unique need to be added |
| 272 | $tempResult = $tempResult && $this->operatorEval($firstPart, $container); |
| 273 | } |
| 274 | $returnValue = $tempResult; |
| 275 | } else { //case we compare boolean |
| 276 | common_Logger::d('Both part are boolean', ['Generis Expression']); |
| 277 | |
| 278 | switch ($this->getLogicalOperator()->getUri()) { |
| 279 | case RulesRdf::INSTANCE_OR_OPERATOR: |
| 280 | $returnValue = $firstPart || $secondPart ; |
| 281 | |
| 282 | break; |
| 283 | |
| 284 | |
| 285 | case RulesRdf::INSTANCE_AND_OPERATOR: |
| 286 | $returnValue = $firstPart && $secondPart ; |
| 287 | break; |
| 288 | |
| 289 | |
| 290 | default: |
| 291 | var_dump($this); |
| 292 | throw new common_Exception('Expression ' . $this->getLabel() . ' do not have knowm operator'); |
| 293 | } |
| 294 | } |
| 295 | } else { |
| 296 | if ($terminalExpression != null) { |
| 297 | common_Logger::d('Evaluating Terminal Expression', ['Generis Expression']); |
| 298 | $returnValue = $terminalExpression->evaluate($variable); |
| 299 | common_Logger::d('Result : ' . $returnValue, ['Generis Expression']); |
| 300 | } else { |
| 301 | throw new common_Exception('terminal expression is null'); |
| 302 | } |
| 303 | } |
| 304 | return $returnValue; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Short description of method evalEquals |
| 309 | * |
| 310 | * @access public |
| 311 | * @author firstname and lastname of author, <author@example.org> |
| 312 | * @param Literal first |
| 313 | * @param Literal second |
| 314 | * @return boolean |
| 315 | */ |
| 316 | public function evalEquals(core_kernel_classes_Literal $first, core_kernel_classes_Literal $second) |
| 317 | { |
| 318 | $returnValue = (bool) false; |
| 319 | if ($first instanceof core_kernel_classes_Literal && $second instanceof core_kernel_classes_Literal) { |
| 320 | $returnValue = $first->literal == $second->literal; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | return (bool) $returnValue; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Short description of method evalDifferent |
| 329 | * |
| 330 | * @access public |
| 331 | * @author firstname and lastname of author, <author@example.org> |
| 332 | * @param Literal first |
| 333 | * @param Literal second |
| 334 | * @return boolean |
| 335 | */ |
| 336 | public function evalDifferent(core_kernel_classes_Literal $first, core_kernel_classes_Literal $second) |
| 337 | { |
| 338 | $returnValue = (bool) false; |
| 339 | if ($first instanceof core_kernel_classes_Literal && $second instanceof core_kernel_classes_Literal) { |
| 340 | $returnValue = $first->literal != $second->literal; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | return (bool) $returnValue; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Short description of method evalInfEquals |
| 349 | * |
| 350 | * @access public |
| 351 | * @author firstname and lastname of author, <author@example.org> |
| 352 | * @param Literal first |
| 353 | * @param Literal second |
| 354 | * @return boolean |
| 355 | */ |
| 356 | public function evalInfEquals(core_kernel_classes_Literal $first, core_kernel_classes_Literal $second) |
| 357 | { |
| 358 | $returnValue = (bool) false; |
| 359 | if ($first instanceof core_kernel_classes_Literal && $second instanceof core_kernel_classes_Literal) { |
| 360 | $returnValue = $first->literal <= $second->literal; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | |
| 365 | return (bool) $returnValue; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Short description of method evalInf |
| 370 | * |
| 371 | * @access public |
| 372 | * @author firstname and lastname of author, <author@example.org> |
| 373 | * @param Literal first |
| 374 | * @param Literal second |
| 375 | * @return boolean |
| 376 | */ |
| 377 | public function evalInf(core_kernel_classes_Literal $first, core_kernel_classes_Literal $second) |
| 378 | { |
| 379 | $returnValue = (bool) false; |
| 380 | if ($first instanceof core_kernel_classes_Literal && $second instanceof core_kernel_classes_Literal) { |
| 381 | $returnValue = $first->literal < $second->literal; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | return (bool) $returnValue; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Short description of method evalSup |
| 390 | * |
| 391 | * @access public |
| 392 | * @author firstname and lastname of author, <author@example.org> |
| 393 | * @param Literal first |
| 394 | * @param Literal second |
| 395 | * @return boolean |
| 396 | */ |
| 397 | public function evalSup(core_kernel_classes_Literal $first, core_kernel_classes_Literal $second) |
| 398 | { |
| 399 | $returnValue = (bool) false; |
| 400 | if ($first instanceof core_kernel_classes_Literal && $second instanceof core_kernel_classes_Literal) { |
| 401 | $returnValue = $first->literal > $second->literal; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | return (bool) $returnValue; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Short description of method evalSupEquals |
| 410 | * |
| 411 | * @access public |
| 412 | * @author firstname and lastname of author, <author@example.org> |
| 413 | * @param Literal first |
| 414 | * @param Literal second |
| 415 | * @return boolean |
| 416 | */ |
| 417 | public function evalSupEquals(core_kernel_classes_Literal $first, core_kernel_classes_Literal $second) |
| 418 | { |
| 419 | $returnValue = (bool) false; |
| 420 | if ($first instanceof core_kernel_classes_Literal && $second instanceof core_kernel_classes_Literal) { |
| 421 | $returnValue = $first->literal >= $second->literal; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | return (bool) $returnValue; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Short description of method operatorEval |
| 430 | * |
| 431 | * @access public |
| 432 | * @author firstname and lastname of author, <author@example.org> |
| 433 | * @param Container firstPart |
| 434 | * @param Container secondPart |
| 435 | * @return boolean |
| 436 | */ |
| 437 | public function operatorEval(core_kernel_classes_Container $firstPart, core_kernel_classes_Container $secondPart) |
| 438 | { |
| 439 | $returnValue = (bool) false; |
| 440 | if ($firstPart instanceof core_kernel_classes_Resource) { |
| 441 | $firstPart = new core_kernel_classes_Literal($firstPart->getUri()); |
| 442 | } |
| 443 | if ($secondPart instanceof core_kernel_classes_Resource) { |
| 444 | $secondPart = new core_kernel_classes_Literal($secondPart->getUri()); |
| 445 | } |
| 446 | common_Logger::d('First Value : ' . $firstPart->literal, ['Generis Expression']); |
| 447 | common_Logger::d('Second Value : ' . $secondPart->literal, ['Generis Expression']); |
| 448 | common_Logger::d('Operator : ' . $this->getLogicalOperator()->getLabel(), ['Generis Expression']); |
| 449 | switch ($this->getLogicalOperator()->getUri()) { |
| 450 | case RulesRdf::INSTANCE_EQUALS_OPERATOR_URI: |
| 451 | $returnValue = $this->evalEquals($firstPart, $secondPart); |
| 452 | break; |
| 453 | |
| 454 | |
| 455 | case RulesRdf::INSTANCE_DIFFERENT_OPERATOR_URI: |
| 456 | $returnValue = $this->evalDifferent($firstPart, $secondPart); |
| 457 | break; |
| 458 | |
| 459 | |
| 460 | |
| 461 | case RulesRdf::INSTANCE_SUP_EQ_OPERATOR_URI: |
| 462 | $returnValue = $this->evalSupEquals($firstPart, $secondPart); |
| 463 | break; |
| 464 | |
| 465 | |
| 466 | case RulesRdf::INSTANCE_INF_EQ_OPERATOR_URI: |
| 467 | $returnValue = $this->evalInfEquals($firstPart, $secondPart); |
| 468 | break; |
| 469 | |
| 470 | |
| 471 | case RulesRdf::INSTANCE_SUP_OPERATOR_URI: |
| 472 | $returnValue = $this->evalSup($firstPart, $secondPart); |
| 473 | break; |
| 474 | |
| 475 | |
| 476 | case RulesRdf::INSTANCE_INF_OPERATOR_URI: |
| 477 | $returnValue = $this->evalInf($firstPart, $secondPart); |
| 478 | break; |
| 479 | |
| 480 | |
| 481 | |
| 482 | default: |
| 483 | var_dump($this); |
| 484 | throw new common_Exception('Expression ' . $this->getLabel() . ' do not have knowm operator'); |
| 485 | } |
| 486 | |
| 487 | $logValue = $returnValue ? ' TRUE ' : ' FALSE '; |
| 488 | common_Logger::d('Expression Value : ' . $logValue, ['Generis Expression']); |
| 489 | return (bool) $returnValue; |
| 490 | } |
| 491 | } |