Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_persistence_starsql_StarRdf | |
0.00% |
0 / 47 |
|
0.00% |
0 / 10 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPersistence | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| search | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| add | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addTripleCollection | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| remove | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getIterator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| triplesToValues | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| createSystemTripleQuery | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| 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 | declare(strict_types=1); |
| 22 | |
| 23 | use EasyRdf\Format; |
| 24 | use EasyRdf\Graph; |
| 25 | use Laudis\Neo4j\Databags\Statement; |
| 26 | use oat\generis\model\data\RdfInterface; |
| 27 | use WikibaseSolutions\CypherDSL\Query; |
| 28 | |
| 29 | class core_kernel_persistence_starsql_StarRdf implements RdfInterface |
| 30 | { |
| 31 | /** |
| 32 | * @var core_kernel_persistence_starsql_StarModel |
| 33 | */ |
| 34 | private $model; |
| 35 | |
| 36 | public function __construct(core_kernel_persistence_starsql_StarModel $model) |
| 37 | { |
| 38 | $this->model = $model; |
| 39 | } |
| 40 | |
| 41 | protected function getPersistence() |
| 42 | { |
| 43 | return $this->model->getPersistence(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * {@inheritDoc} |
| 48 | */ |
| 49 | public function get($subject, $predicate) |
| 50 | { |
| 51 | throw new common_Exception('Not implemented'); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * {@inheritDoc} |
| 56 | */ |
| 57 | public function search($predicate, $object) |
| 58 | { |
| 59 | throw new common_Exception('Not implemented'); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * {@inheritDoc} |
| 64 | */ |
| 65 | public function add(core_kernel_classes_Triple $triple) |
| 66 | { |
| 67 | $this->addTripleCollection([$triple]); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * {@inheritDoc} |
| 72 | */ |
| 73 | public function addTripleCollection(iterable $triples) |
| 74 | { |
| 75 | $nTriple = $this->triplesToValues($triples, Format::getFormat('ntriples')); |
| 76 | |
| 77 | $persistence = $this->getPersistence(); |
| 78 | $persistence->run( |
| 79 | 'CALL n10s.rdf.import.inline($nTriple,"N-Triples")', |
| 80 | ['nTriple' => $nTriple] |
| 81 | ); |
| 82 | |
| 83 | $systemTripleQuery = $this->createSystemTripleQuery($triples); |
| 84 | if ($systemTripleQuery instanceof Statement) { |
| 85 | $persistence->runStatement($systemTripleQuery); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * {@inheritDoc} |
| 91 | */ |
| 92 | public function remove(core_kernel_classes_Triple $triple) |
| 93 | { |
| 94 | $nTriple = $this->triplesToValues([$triple], Format::getFormat('ntriples')); |
| 95 | |
| 96 | $persistence = $this->getPersistence(); |
| 97 | $persistence->run( |
| 98 | 'CALL n10s.rdf.delete.inline($nTriple,"N-Triples")', |
| 99 | ['nTriple' => $nTriple] |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * {@inheritDoc} |
| 105 | */ |
| 106 | public function getIterator() |
| 107 | { |
| 108 | return new RecursiveIteratorIterator(new core_kernel_persistence_starsql_StarIterator($this->model)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @param iterable $tripleList |
| 113 | * @param Format $format |
| 114 | * |
| 115 | * @return string |
| 116 | */ |
| 117 | private function triplesToValues(iterable $tripleList, Format $format): string |
| 118 | { |
| 119 | $graph = new Graph(); |
| 120 | |
| 121 | /** @var core_kernel_classes_Triple $triple */ |
| 122 | foreach ($tripleList as $triple) { |
| 123 | if (!empty($triple->lg)) { |
| 124 | $graph->addLiteral( |
| 125 | $triple->subject, |
| 126 | $triple->predicate, |
| 127 | $triple->object, |
| 128 | $triple->lg |
| 129 | ); |
| 130 | } elseif (\common_Utils::isUri($triple->object)) { |
| 131 | $graph->addResource($triple->subject, $triple->predicate, $triple->object); |
| 132 | } else { |
| 133 | $graph->addLiteral($triple->subject, $triple->predicate, $triple->object); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return $graph->serialise($format); |
| 138 | } |
| 139 | |
| 140 | private function createSystemTripleQuery(iterable $tripleList): ?Statement |
| 141 | { |
| 142 | $systemSubjectList = []; |
| 143 | /** @var core_kernel_classes_Triple $triple */ |
| 144 | foreach ($tripleList as $triple) { |
| 145 | if ( |
| 146 | !empty($triple->modelid) |
| 147 | && $triple->modelid != \core_kernel_persistence_starsql_StarModel::DEFAULT_WRITABLE_MODEL |
| 148 | ) { |
| 149 | $systemSubjectList[$triple->subject] = true; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | $query = null; |
| 154 | if (!empty($systemSubjectList)) { |
| 155 | $systemNode = Query::node('Resource'); |
| 156 | $query = Query::new()->match($systemNode) |
| 157 | ->where($systemNode->property('uri')->in(array_keys($systemSubjectList))) |
| 158 | ->set($systemNode->labeled('System')); |
| 159 | |
| 160 | $query = Statement::create($query->build()); |
| 161 | } |
| 162 | |
| 163 | return $query; |
| 164 | } |
| 165 | } |