Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 100 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_rules_Term | |
0.00% |
0 / 100 |
|
0.00% |
0 / 8 |
812 | |
0.00% |
0 / 1 |
| evaluate | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
72 | |||
| evalutateSetOperation | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| evaluateSPX | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
| evaluateXPO | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| evaluateSet | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| evaluateConst | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| evaluateOperation | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| evaluateArithmOperation | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | use oat\generis\model\OntologyRdf; |
| 26 | use oat\generis\model\RulesRdf; |
| 27 | |
| 28 | /** |
| 29 | * Short description of class core_kernel_rules_Term |
| 30 | * |
| 31 | * @access public |
| 32 | * @author firstname and lastname of author, <author@example.org> |
| 33 | * @package generis |
| 34 | |
| 35 | */ |
| 36 | class core_kernel_rules_Term extends core_kernel_classes_Resource |
| 37 | { |
| 38 | // --- ASSOCIATIONS --- |
| 39 | |
| 40 | |
| 41 | // --- ATTRIBUTES --- |
| 42 | |
| 43 | // --- OPERATIONS --- |
| 44 | |
| 45 | /** |
| 46 | * Short description of method evaluate |
| 47 | * |
| 48 | * @access public |
| 49 | * @author firstname and lastname of author, <author@example.org> |
| 50 | * @param array $variable |
| 51 | * @return mixed |
| 52 | */ |
| 53 | public function evaluate($variable = []) |
| 54 | { |
| 55 | |
| 56 | common_Logger::i('Evaluating Term uri : ' . $this->getUri(), ['Generis Term']); |
| 57 | common_Logger::i('Evaluating Term name : ' . $this->getLabel(), ['Generis Term']); |
| 58 | $termType = $this->getUniquePropertyValue(new core_kernel_classes_Property(OntologyRdf::RDF_TYPE)); |
| 59 | common_Logger::d('Term s type : ' . $termType->getUri(), ['Generis Term']); |
| 60 | switch ($termType->getUri()) { |
| 61 | case RulesRdf::CLASS_TERM: |
| 62 | throw new common_Exception("Forbidden Type of Term"); |
| 63 | |
| 64 | break; |
| 65 | |
| 66 | |
| 67 | case RulesRdf::CLASS_TERM_SUJET_PREDICATE_X: |
| 68 | $returnValue = $this->evaluateSPX($variable); |
| 69 | break; |
| 70 | |
| 71 | |
| 72 | case RulesRdf::CLASS_URI_TERM_X_PREDICATE_OBJECT: |
| 73 | $returnValue = $this->evaluateXPO(); |
| 74 | break; |
| 75 | |
| 76 | |
| 77 | case RulesRdf::CLASS_URI_CONSTRUCTED_SET: |
| 78 | $returnValue = $this->evaluateSet(); |
| 79 | break; |
| 80 | |
| 81 | |
| 82 | case RulesRdf::CLASS_TERM_CONST: |
| 83 | $returnValue = $this->evaluateConst(); |
| 84 | break; |
| 85 | |
| 86 | |
| 87 | case RulesRdf::CLASS_OPERATION: |
| 88 | $returnValue = $this->evaluateOperation($variable); |
| 89 | break; |
| 90 | |
| 91 | |
| 92 | default: |
| 93 | throw new common_Exception('problem evaluating Term'); |
| 94 | } |
| 95 | |
| 96 | return $returnValue; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Short description of method evalutateSetOperation |
| 102 | * |
| 103 | * @access public |
| 104 | * @author firstname and lastname of author, <author@example.org> |
| 105 | * @param Resource $setOperator |
| 106 | * @param Collection $actualSet |
| 107 | * @param ContainerCollection $newSet |
| 108 | * @return core_kernel_classes_ContainerCollection |
| 109 | */ |
| 110 | public function evalutateSetOperation( |
| 111 | core_kernel_classes_Resource $setOperator, |
| 112 | common_Collection $actualSet, |
| 113 | core_kernel_classes_ContainerCollection $newSet |
| 114 | ) { |
| 115 | $returnValue = null; |
| 116 | if ($setOperator->getUri() == RulesRdf::INSTANCE_OPERATOR_UNION) { |
| 117 | $returnValue = $actualSet->union($newSet); |
| 118 | } elseif ($setOperator->getUri() == RulesRdf::INSTANCE_OPERATOR_INTERSECT) { |
| 119 | $returnValue = $actualSet->intersect($newSet); |
| 120 | } else { |
| 121 | throw new common_Exception('unknow set operator'); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | return $returnValue; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Short description of method evaluateSPX |
| 130 | * |
| 131 | * @access protected |
| 132 | * @author firstname and lastname of author, <author@example.org> |
| 133 | * @param array $variable |
| 134 | * @return mixed |
| 135 | */ |
| 136 | protected function evaluateSPX($variable = []) |
| 137 | { |
| 138 | |
| 139 | common_Logger::d('SPX TYPE', ['Generis Term evaluateSPX']); |
| 140 | $resource = $this->getUniquePropertyValue(new core_kernel_classes_Property(RulesRdf::PROPERTY_TERM_SPX_SUBJET)); |
| 141 | if ($resource instanceof core_kernel_classes_Resource) { |
| 142 | if (array_key_exists($resource->getUri(), $variable)) { |
| 143 | common_Logger::d('Variable uri : ' . $resource->getUri() . ' found', ['Generis Term evaluateSPX']); |
| 144 | common_Logger::d('Variable name : ' . $resource->getLabel() . ' found', ['Generis Term evaluateSPX']); |
| 145 | $resource = new core_kernel_classes_Resource($variable[$resource->getUri()]); |
| 146 | common_Logger::d('Variable repaced uri : ' . $resource->getUri(), ['Generis Term evaluateSPX']); |
| 147 | common_Logger::d('Variable repaced name : ' . $resource->getLabel(), ['Generis Term evaluateSPX']); |
| 148 | } |
| 149 | |
| 150 | try { |
| 151 | $propertyInstance = $this->getUniquePropertyValue( |
| 152 | new core_kernel_classes_Property(RulesRdf::PROPERTY_TERM_SPX_PREDICATE) |
| 153 | ); |
| 154 | } catch (common_Exception $e) { |
| 155 | echo $e; |
| 156 | var_dump($this); |
| 157 | die('unable to get property value in Term'); |
| 158 | } |
| 159 | |
| 160 | $property = new core_kernel_classes_Property($propertyInstance->getUri()); |
| 161 | common_Logger::d('Property uri ' . $property->getUri(), ['Generis Term evaluateSPX']); |
| 162 | common_Logger::d('Property name ' . $property->getLabel(), ['Generis Term evaluateSPX']); |
| 163 | $returnValue = $resource->getPropertyValuesCollection($property); |
| 164 | common_Logger::d($returnValue->count() . ' values returned ', ['Generis Term evaluateSPX']); |
| 165 | if ($returnValue->isEmpty()) { |
| 166 | $newEmptyTerm = new core_kernel_rules_Term(RulesRdf::INSTANCE_TERM_IS_NULL, __METHOD__); |
| 167 | common_Logger::d('Empty Term Created', ['Generis Term evaluateSPX']); |
| 168 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_TERM_VALUE); |
| 169 | $returnValue = $newEmptyTerm->getUniquePropertyValue($property); |
| 170 | } else { |
| 171 | if ($returnValue->count() == 1) { |
| 172 | $returnValue = $returnValue->get(0); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | return $returnValue; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Short description of method evaluateXPO |
| 181 | * |
| 182 | * @access protected |
| 183 | * @author firstname and lastname of author, <author@example.org> |
| 184 | * @return mixed |
| 185 | */ |
| 186 | protected function evaluateXPO() |
| 187 | { |
| 188 | |
| 189 | common_Logger::d('XPO TYPE', ['Generis Term evaluateXPO']); |
| 190 | $classTerm = new core_kernel_classes_Class(RulesRdf::CLASS_URI_TERM_X_PREDICATE_OBJECT); |
| 191 | $obj = $this->getUniquePropertyValue(new core_kernel_classes_Property(RulesRdf::PROPERTY_TERM_XPO_OBJECT)); |
| 192 | $pred = $this->getUniquePropertyValue(new core_kernel_classes_Property(RulesRdf::PROPERTY_TERM_XPO_PREDICATE)); |
| 193 | if ($obj instanceof core_kernel_classes_Literal) { |
| 194 | $objValue = $obj->literal; |
| 195 | } |
| 196 | if ($obj instanceof core_kernel_classes_Resource) { |
| 197 | $objValue = $pred->getUri(); |
| 198 | } |
| 199 | |
| 200 | $returnValue = new core_kernel_classes_ContainerCollection(new common_Object(__METHOD__)); |
| 201 | $terms = $classTerm->searchInstances([$pred->getUri() => $objValue], ['like' => false]); |
| 202 | foreach ($terms as $term) { |
| 203 | $returnValue->add($term); |
| 204 | } |
| 205 | return $returnValue; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Short description of method evaluateSet |
| 210 | * |
| 211 | * @access protected |
| 212 | * @author firstname and lastname of author, <author@example.org> |
| 213 | * @return mixed |
| 214 | */ |
| 215 | protected function evaluateSet() |
| 216 | { |
| 217 | |
| 218 | common_Logger::d('Constructed Set TYPE', ['Generis Term evaluateSet']); |
| 219 | $operator = $this->getUniquePropertyValue(new core_kernel_classes_Property(RulesRdf::PROPERTY_SET_OPERATOR)); |
| 220 | $subSets = $this->getPropertyValuesCollection(new core_kernel_classes_Property(RulesRdf::PROPERTY_SUBSET)); |
| 221 | $returnValue = new core_kernel_classes_ContainerCollection($this); |
| 222 | foreach ($subSets->getIterator() as $aSet) { |
| 223 | if ($aSet instanceof core_kernel_classes_Resource) { |
| 224 | $newSet = new core_kernel_rules_Term($aSet->getUri()); |
| 225 | $resultSet = $newSet->evaluate(); |
| 226 | if ($resultSet instanceof core_kernel_classes_ContainerCollection) { |
| 227 | $returnValue = $this->evalutateSetOperation($operator, $returnValue, $resultSet); |
| 228 | } else { |
| 229 | $collection = new core_kernel_classes_ContainerCollection($this); |
| 230 | $collection->add($resultSet); |
| 231 | $returnValue = $this->evalutateSetOperation($operator, $returnValue, $collection); |
| 232 | } |
| 233 | } else { |
| 234 | throw new common_Exception('Bad Type , waiting for a Resource '); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return $returnValue; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Short description of method evaluateConst |
| 243 | * |
| 244 | * @access protected |
| 245 | * @author firstname and lastname of author, <author@example.org> |
| 246 | * @return mixed |
| 247 | */ |
| 248 | protected function evaluateConst() |
| 249 | { |
| 250 | |
| 251 | common_Logger::d('CONSTANTE TYPE', ['Generis Term evaluateConst']); |
| 252 | $property = new core_kernel_classes_Property(RulesRdf::PROPERTY_TERM_VALUE); |
| 253 | return $this->getUniquePropertyValue($property); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Short description of method evaluateOperation |
| 258 | * |
| 259 | * @access protected |
| 260 | * @author firstname and lastname of author, <author@example.org> |
| 261 | * @param array variable |
| 262 | * @return mixed |
| 263 | */ |
| 264 | protected function evaluateOperation($variable = []) |
| 265 | { |
| 266 | |
| 267 | common_Logger::d('OPERATION TYPE', ['Generis Term evaluateOperation']); |
| 268 | return $this->evaluateArithmOperation($variable); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Short description of method evaluateArtihmOperation |
| 273 | * |
| 274 | * @access public |
| 275 | * @author firstname and lastname of author, <author@example.org> |
| 276 | * @param array $variable |
| 277 | * @return mixed |
| 278 | */ |
| 279 | public function evaluateArithmOperation($variable = []) |
| 280 | { |
| 281 | |
| 282 | $operation = new core_kernel_rules_Operation($this->getUri(), __METHOD__); |
| 283 | return $operation->evaluate($variable); |
| 284 | } |
| 285 | } |