Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 44 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_persistence_starsql_StarModel | |
0.00% |
0 / 44 |
|
0.00% |
0 / 15 |
420 | |
0.00% |
0 / 1 |
| getResource | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getClass | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getProperty | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| isWritable | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getPersistence | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getCache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRdfInterface | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRdfsInterface | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSearchInterface | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getNewTripleModelId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getReadableModels | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getWritableModels | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addReadableModel | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getReadableModelIds | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getUpdatableModelIds | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2023 (original work) Open Assessment Technologies SA ; |
| 19 | */ |
| 20 | |
| 21 | use oat\generis\model\data\ModelManager; |
| 22 | use oat\generis\model\data\Ontology; |
| 23 | use oat\generis\model\kernel\persistence\Cacheable; |
| 24 | use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService; |
| 25 | use oat\oatbox\cache\PropertyCache; |
| 26 | use oat\oatbox\cache\SimpleCache; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | |
| 29 | class core_kernel_persistence_starsql_StarModel extends ConfigurableService implements Ontology, Cacheable |
| 30 | { |
| 31 | public const OPTION_PERSISTENCE = 'persistence'; |
| 32 | public const OPTION_READABLE_MODELS = 'readable'; |
| 33 | public const OPTION_WRITEABLE_MODELS = 'writeable'; |
| 34 | public const OPTION_NEW_TRIPLE_MODEL = 'addTo'; |
| 35 | public const OPTION_SEARCH_SERVICE = 'search'; |
| 36 | |
| 37 | public const DEFAULT_WRITABLE_MODEL = 1; |
| 38 | public const DEFAULT_READ_ONLY_MODEL = 2; |
| 39 | |
| 40 | /** |
| 41 | * Persistence to use for the smoothmodel |
| 42 | * |
| 43 | * @var common_persistence_SqlPersistence |
| 44 | */ |
| 45 | private $persistence; |
| 46 | |
| 47 | public function getResource($uri) |
| 48 | { |
| 49 | $resource = new \core_kernel_classes_Resource($uri); |
| 50 | $resource->setModel($this); |
| 51 | return $resource; |
| 52 | } |
| 53 | |
| 54 | public function getClass($uri) |
| 55 | { |
| 56 | $class = new \core_kernel_classes_Class($uri); |
| 57 | $class->setModel($this); |
| 58 | return $class; |
| 59 | } |
| 60 | |
| 61 | public function getProperty($uri) |
| 62 | { |
| 63 | $property = new \core_kernel_classes_Property($uri); |
| 64 | $property->setModel($this); |
| 65 | return $property; |
| 66 | } |
| 67 | |
| 68 | public function isWritable(core_kernel_classes_Resource $resource): bool |
| 69 | { |
| 70 | $writableModels = $this->getWritableModels(); |
| 71 | |
| 72 | /** @var core_kernel_classes_Triple $triple */ |
| 73 | foreach ($resource->getRdfTriples() as $triple) { |
| 74 | if (!in_array((int)$triple->modelid, $writableModels, true)) { |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | public function getPersistence(): common_persistence_GraphPersistence |
| 83 | { |
| 84 | if (is_null($this->persistence)) { |
| 85 | $this->persistence = $this->getServiceLocator() |
| 86 | ->get(common_persistence_Manager::SERVICE_ID) |
| 87 | ->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE)); |
| 88 | } |
| 89 | return $this->persistence; |
| 90 | } |
| 91 | |
| 92 | public function getCache(): SimpleCache |
| 93 | { |
| 94 | return $this->getServiceLocator()->get(PropertyCache::SERVICE_ID); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * (non-PHPdoc) |
| 99 | * @see \oat\generis\model\data\Model::getRdfInterface() |
| 100 | */ |
| 101 | public function getRdfInterface() |
| 102 | { |
| 103 | return new core_kernel_persistence_starsql_StarRdf($this); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * (non-PHPdoc) |
| 108 | * @see \oat\generis\model\data\Model::getRdfsInterface() |
| 109 | */ |
| 110 | public function getRdfsInterface() |
| 111 | { |
| 112 | return new core_kernel_persistence_starsql_StarRdfs($this); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @return ComplexSearchService |
| 117 | */ |
| 118 | public function getSearchInterface() |
| 119 | { |
| 120 | $search = $this->getServiceLocator()->get($this->getOption(self::OPTION_SEARCH_SERVICE)); |
| 121 | $search->setModel($this); |
| 122 | return $search; |
| 123 | } |
| 124 | |
| 125 | // Manage the sudmodels of the smooth mode |
| 126 | |
| 127 | /** |
| 128 | * Returns the id of the model to add to |
| 129 | * |
| 130 | * @return string |
| 131 | */ |
| 132 | public function getNewTripleModelId() |
| 133 | { |
| 134 | return $this->getOption(self::OPTION_NEW_TRIPLE_MODEL); |
| 135 | } |
| 136 | |
| 137 | public function getReadableModels() |
| 138 | { |
| 139 | return $this->getOption(self::OPTION_READABLE_MODELS); |
| 140 | } |
| 141 | |
| 142 | public function getWritableModels() |
| 143 | { |
| 144 | return $this->getOption(self::OPTION_WRITEABLE_MODELS); |
| 145 | } |
| 146 | |
| 147 | // |
| 148 | // Deprecated functions |
| 149 | // |
| 150 | |
| 151 | /** |
| 152 | * Defines a model as readable |
| 153 | * |
| 154 | * @param string $id |
| 155 | */ |
| 156 | public function addReadableModel($id) |
| 157 | { |
| 158 | common_Logger::i('ADDING MODEL ' . $id); |
| 159 | |
| 160 | $readables = $this->getOption(self::OPTION_READABLE_MODELS); |
| 161 | $this->setOption(self::OPTION_READABLE_MODELS, array_unique(array_merge($readables, [$id]))); |
| 162 | |
| 163 | // update in persistence |
| 164 | ModelManager::setModel($this); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns the submodel ids that are readable |
| 169 | * |
| 170 | * @return array() |
| 171 | * @deprecated |
| 172 | */ |
| 173 | public static function getReadableModelIds() |
| 174 | { |
| 175 | $model = ModelManager::getModel(); |
| 176 | if (!$model instanceof self) { |
| 177 | throw new common_exception_Error( |
| 178 | __FUNCTION__ . ' called on ' . get_class($model) . ' model implementation' |
| 179 | ); |
| 180 | } |
| 181 | return $model->getReadableModels(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns the submodel ids that are updatable |
| 186 | * |
| 187 | * @return array() |
| 188 | * @deprecated |
| 189 | */ |
| 190 | public static function getUpdatableModelIds() |
| 191 | { |
| 192 | $model = ModelManager::getModel(); |
| 193 | if (!$model instanceof self) { |
| 194 | throw new common_exception_Error( |
| 195 | __FUNCTION__ . ' called on ' . get_class($model) . ' model implementation' |
| 196 | ); |
| 197 | } |
| 198 | return $model->getWritableModels(); |
| 199 | } |
| 200 | } |