Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REL-1197: Rdf export/import from neo4j #1068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/kernel/persistence/file/FileModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function toFile($filePath, $triples)
if (!empty($triple->lg)) {
$graph->addLiteral($triple->subject, $triple->predicate, $triple->object, $triple->lg);
} elseif (\common_Utils::isUri($triple->object)) {
$graph->add($triple->subject, $triple->predicate, $triple->object);
$graph->addResource($triple->subject, $triple->predicate, $triple->object);
} else {
$graph->addLiteral($triple->subject, $triple->predicate, $triple->object);
}
Expand Down
23 changes: 13 additions & 10 deletions core/kernel/persistence/smoothsql/class.SmoothRdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ public function get($subject, $predicate)
throw new \common_Exception('Not implemented');
}

/**
* (non-PHPdoc)
* @see \oat\generis\model\data\RdfInterface::search()
*/
public function search($predicate, $object)
{
throw new \common_Exception('Not implemented');
}

/**
* (non-PHPdoc)
* @see \oat\generis\model\data\RdfInterface::add()
Expand Down Expand Up @@ -155,18 +164,12 @@ public function remove(core_kernel_classes_Triple $triple)
);
}

/**
* (non-PHPdoc)
* @see \oat\generis\model\data\RdfInterface::search()
*/
public function search($predicate, $object)
{
throw new \common_Exception('Not implemented');
}

public function getIterator()
{
return new core_kernel_persistence_smoothsql_SmoothIterator($this->getPersistence());
return new core_kernel_persistence_smoothsql_SmoothIterator(
$this->getPersistence(),
array_diff($this->model->getReadableModels(), ['1'])
);
}

/**
Expand Down
36 changes: 36 additions & 0 deletions core/kernel/persistence/starsql/FlatRecursiveIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA;
*
*/

namespace oat\generis\model\kernel\persistence\starsql;

use RecursiveIterator;

class FlatRecursiveIterator extends \IteratorIterator implements RecursiveIterator
{
public function hasChildren()
{
return false;
}

public function getChildren()
{
return new \EmptyIterator();
}
}
10 changes: 5 additions & 5 deletions core/kernel/persistence/starsql/class.Class.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public function getSubClasses(core_kernel_classes_Class $resource, $recursive =
if (!empty($recursive)) {
$query = <<<CYPHER
MATCH (startNode:Resource {uri: \$uri})
MATCH path = (descendantNode)-[:`{$relationship}`*]->(startNode)
MATCH (descendantNode)-[:`{$relationship}`*]->(startNode)
RETURN descendantNode.uri
CYPHER;
} else {
$query = <<<CYPHER
MATCH (startNode:Resource {uri: \$uri})
MATCH path = (descendantNode)-[:`{$relationship}`]->(startNode)
MATCH (descendantNode)-[:`{$relationship}`]->(startNode)
RETURN descendantNode.uri
CYPHER;
}
Expand Down Expand Up @@ -94,13 +94,13 @@ public function getParentClasses(core_kernel_classes_Class $resource, $recursive
if (!empty($recursive)) {
$query = <<<CYPHER
MATCH (startNode:Resource {uri: \$uri})
MATCH path = (startNode)-[:`{$relationship}`*]->(ancestorNode)
MATCH (startNode)-[:`{$relationship}`*]->(ancestorNode)
RETURN ancestorNode.uri
CYPHER;
} else {
$query = <<<CYPHER
MATCH (startNode:Resource {uri: \$uri})
MATCH path = (startNode)-[:`{$relationship}`]->(ancestorNode)
MATCH (startNode)-[:`{$relationship}`]->(ancestorNode)
RETURN ancestorNode.uri
CYPHER;
}
Expand All @@ -122,7 +122,7 @@ public function getProperties(core_kernel_classes_Class $resource, $recursive =
$relationship = OntologyRdfs::RDFS_DOMAIN;
$query = <<<CYPHER
MATCH (startNode:Resource {uri: \$uri})
MATCH path = (descendantNode)-[:`{$relationship}`]->(startNode)
MATCH (descendantNode)-[:`{$relationship}`]->(startNode)
RETURN descendantNode.uri
CYPHER;
$results = $this->getPersistence()->run($query, ['uri' => $uri]);
Expand Down
29 changes: 17 additions & 12 deletions core/kernel/persistence/starsql/class.Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,10 @@ public function removePropertyValueByLg(core_kernel_classes_Resource $resource,

public function getRdfTriples(core_kernel_classes_Resource $resource): core_kernel_classes_ContainerCollection
{
$relationship = relationshipTo()->withVariable($relationshipVar = variable());
$relationship = relationshipTo()
->withVariable($relationshipVar = variable())
->withMinHops(0)
->withMaxHops(1);
$relatedNode = node()->withLabels(['Resource'])->withVariable($relatedNodeVar = variable());
$node = node()->withProperties(['uri' => $uriParameter = parameter()])
->withVariable($nodeVar = variable())
Expand All @@ -352,20 +355,20 @@ public function getRdfTriples(core_kernel_classes_Resource $resource): core_kern

$results = $this->getPersistence()->run($query, [$uriParameter->getParameter() => $resource->getUri()]);
$returnValue = new core_kernel_classes_ContainerCollection(new common_Object(__METHOD__));
$nodeProcessed = false;
foreach ($results as $result) {
$resultNode = $result->get($nodeVar->getName());
/** @var \Laudis\Neo4j\Types\CypherMap $resultRelationship */
$resultRelationship = $result->get($relationshipVar->getName());
$resultRelatedNode = $result->get($relatedNodeVar->getName());
$updatedAt = $resultNode->getProperty(TaoOntology::PROPERTY_UPDATED_AT);
// $updatedBy = $resultNode->getProperty(TaoOntology::PROPERTY_UPDATED_BY);
if (!$nodeProcessed) {
$returnValue = $this->buildTriplesFromNode($returnValue, $resource->getUri(), $updatedAt, $resultNode);
$returnValue = $this->buildTriplesFromNode($returnValue, $resource->getUri(), $resultNode);
$nodeProcessed = true;
}
if ($resultRelationship) {
if (!$resultRelationship->isEmpty()) {
$resultRelationship = $resultRelationship->first();
$triple = new core_kernel_classes_Triple();
$triple->subject = $resource->getUri();
$triple->epoch = $updatedAt;
$triple->predicate = $resultRelationship->getType();
$triple->object = $resultRelatedNode->getProperty('uri');
$returnValue->add($triple);
Expand Down Expand Up @@ -593,19 +596,21 @@ private function parseTranslatedLang($value): string
return $matches[2] ?? '';
}

private function buildTriplesFromNode(core_kernel_classes_ContainerCollection $tripleCollection, $uri, $updatedAt, $resultNode)
private function buildTriplesFromNode(core_kernel_classes_ContainerCollection $tripleCollection, $uri, $resultNode)
{
foreach ($resultNode->getProperties() as $propKey => $propValue) {
if ($propKey === 'uri') {
continue;
}
$triple = new core_kernel_classes_Triple();
$triple->subject = $uri;
// $triple->author = $updatedBy;
$triple->epoch = $updatedAt;
$triple->predicate = $propKey;
if (is_iterable($propValue)) {
foreach ($propValue as $value) {
$triple->lg = $this->parseTranslatedLang($value);
$triple->object = $this->parseTranslatedValue($value);
$tripleCollection->add($triple);
$langTriple = clone $triple;
$langTriple->lg = $this->parseTranslatedLang($value);
$langTriple->object = $this->parseTranslatedValue($value);
$tripleCollection->add($langTriple);
}
} else {
$triple->lg = $this->parseTranslatedLang($propValue);
Expand Down
69 changes: 69 additions & 0 deletions core/kernel/persistence/starsql/class.StarIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2023 (original work) Open Assessment Technologies SA
*
*/

use oat\generis\model\data\Model;
use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService;
use oat\generis\model\kernel\persistence\starsql\FlatRecursiveIterator;

/**
* Iterator over all triples
*
*/
class core_kernel_persistence_starsql_StarIterator extends \IteratorIterator implements \RecursiveIterator
{
public function __construct(Model $model)
{
/** @var ComplexSearchService $search */
$search = $model->getSearchInterface();
$query = $search->query();

$queryOptions = $query->getOptions();
$queryOptions['system_only'] = true;
$query->setOptions($queryOptions);

parent::__construct($search->getGateway()->search($query));
}

public function hasChildren()
{
return true;
}

public function current()
{
$current = parent::current();

if ($current instanceof \core_kernel_classes_Literal) {
$current = new \core_kernel_classes_Resource($current->literal);
}

return $current;
}


public function getChildren()
{
/** @var \core_kernel_classes_Resource $currentResource */
$currentResource = $this->current();

return new FlatRecursiveIterator($currentResource->getRdfTriples());
}
}
9 changes: 1 addition & 8 deletions core/kernel/persistence/starsql/class.StarModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
use oat\generis\model\kernel\persistence\smoothsql\install\SmoothRdsModel;
use oat\oatbox\cache\SimpleCache;

class core_kernel_persistence_starsql_StarModel extends ConfigurableService implements Ontology,
SchemaProviderInterface
class core_kernel_persistence_starsql_StarModel extends ConfigurableService implements Ontology
{
const OPTION_PERSISTENCE = 'persistence';
const OPTION_READABLE_MODELS = 'readable';
Expand Down Expand Up @@ -198,10 +197,4 @@ public static function getUpdatableModelIds()
}
return $model->getWritableModels();
}

public function provideSchema(SchemaCollection $schemaCollection)
{
// $schema = $schemaCollection->getSchema($this->getOption(self::OPTION_PERSISTENCE));
// SmoothRdsModel::addSmoothTables($schema);
}
}
Loading