Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SearchTokenGenerator | |
0.00% |
0 / 33 |
|
0.00% |
0 / 5 |
240 | |
0.00% |
0 / 1 |
| generateTokens | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| getProperties | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getPropertiesByClass | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getIndexes | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getTokenizerStrings | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| 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) 2014-2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\search; |
| 22 | |
| 23 | use common_exception_InconsistentData; |
| 24 | use core_kernel_classes_Property; |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\generis\model\OntologyRdfs; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use oat\tao\model\search\index\OntologyIndex; |
| 29 | use oat\tao\model\search\tokenizer\ResourceClasses; |
| 30 | use oat\tao\model\search\tokenizer\ResourceTokenizer; |
| 31 | use oat\tao\model\search\tokenizer\PropertyValueTokenizer; |
| 32 | use oat\generis\model\OntologyAwareTrait; |
| 33 | |
| 34 | /** |
| 35 | * @author Joel Bout <joel@taotesting.com> |
| 36 | */ |
| 37 | class SearchTokenGenerator extends ConfigurableService |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | |
| 41 | /** @var OntologyIndex[][] */ |
| 42 | protected $indexMap = []; |
| 43 | |
| 44 | /** @var core_kernel_classes_Property[][] */ |
| 45 | protected $propertyCache = []; |
| 46 | |
| 47 | /** |
| 48 | * @throws common_exception_InconsistentData |
| 49 | * |
| 50 | * @return array[][] Returns an array of subArrays containing [index, strings] |
| 51 | */ |
| 52 | public function generateTokens(core_kernel_classes_Resource $resource): array |
| 53 | { |
| 54 | $tokens = []; |
| 55 | |
| 56 | foreach ($this->getProperties($resource) as $property) { |
| 57 | $indexes = $this->getIndexes($property); |
| 58 | |
| 59 | if (!empty($indexes)) { |
| 60 | foreach ($indexes as $index) { |
| 61 | $tokens[] = [$index, $this->getTokenizerStrings($index, $resource, $property)]; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return $tokens; |
| 67 | } |
| 68 | |
| 69 | protected function getProperties(core_kernel_classes_Resource $resource) |
| 70 | { |
| 71 | $classProperties = [$this->getProperty(OntologyRdfs::RDFS_LABEL)]; |
| 72 | foreach ($resource->getTypes() as $type) { |
| 73 | $classProperties = array_merge($classProperties, $this->getPropertiesByClass($type)); |
| 74 | } |
| 75 | |
| 76 | return $classProperties; |
| 77 | } |
| 78 | |
| 79 | protected function getPropertiesByClass(\core_kernel_classes_Class $type) |
| 80 | { |
| 81 | if (!isset($this->propertyCache[$type->getUri()])) { |
| 82 | $this->propertyCache[$type->getUri()] = $type->getProperties(true); |
| 83 | // alternativly use non recursiv and union with getPropertiesByClass of parentclasses |
| 84 | } |
| 85 | return $this->propertyCache[$type->getUri()]; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @return OntologyIndex[] |
| 90 | */ |
| 91 | protected function getIndexes(core_kernel_classes_Property $property): array |
| 92 | { |
| 93 | if (!isset($this->indexMap[$property->getUri()])) { |
| 94 | $this->indexMap[$property->getUri()] = []; |
| 95 | $indexes = $property->getPropertyValues($this->getProperty(OntologyIndex::PROPERTY_INDEX)); |
| 96 | foreach ($indexes as $indexUri) { |
| 97 | $this->indexMap[$property->getUri()][] = new OntologyIndex($indexUri); |
| 98 | } |
| 99 | } |
| 100 | return $this->indexMap[$property->getUri()]; |
| 101 | } |
| 102 | |
| 103 | private function getTokenizerStrings( |
| 104 | OntologyIndex $index, |
| 105 | core_kernel_classes_Resource $resource, |
| 106 | core_kernel_classes_Property $property |
| 107 | ): array { |
| 108 | $tokenizer = $index->getTokenizer(); |
| 109 | |
| 110 | if ($tokenizer instanceof ResourceTokenizer) { |
| 111 | return $tokenizer->getStrings($resource); |
| 112 | } |
| 113 | |
| 114 | if ($tokenizer instanceof PropertyValueTokenizer) { |
| 115 | return $tokenizer instanceof ResourceClasses |
| 116 | ? $tokenizer->getStrings($resource) |
| 117 | : $tokenizer->getStrings($resource->getPropertyValues($property)); |
| 118 | } |
| 119 | |
| 120 | throw new common_exception_InconsistentData( |
| 121 | sprintf( |
| 122 | 'Unsupported tokenizer %s', |
| 123 | get_class($tokenizer) |
| 124 | ) |
| 125 | ); |
| 126 | } |
| 127 | } |