-
Notifications
You must be signed in to change notification settings - Fork 35
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
feat: RDF Object Mapper PoC #3447
Draft
hectoras
wants to merge
7
commits into
develop
Choose a base branch
from
experimental/rdf-object-mapper
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f782b5
feat: PoC for object mapper
hectoras a94388e
chore: Cleanup
hectoras bf481dd
refactor: Clean things up a little bit
hectoras ef77ec4
refactor: Clean things up a little bit
hectoras 6775eb7
chore: Cleanup
hectoras d84dba5
chore: Cleanup
hectoras 194dfb0
chore: Update notes
hectoras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
models/classes/RdfObjectMapper/Annotation/RdfAttributeMapping.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?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) 2022 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
namespace oat\tao\model\RdfObjectMapper\Annotation; | ||
|
||
use core_kernel_classes_Property; | ||
use core_kernel_classes_Resource; | ||
use Psr\Log\LoggerInterface; | ||
use ReflectionProperty; | ||
|
||
//#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
/** | ||
*@Annotation | ||
*/ | ||
class RdfAttributeMapping | ||
{ | ||
public /*string*/ $propertyUri; | ||
public /*string*/ $attributeType = 'resource'; | ||
public /*string*/ $mappedField = null; | ||
|
||
// the commented out constructors are in case we use PHP 8 annotations | ||
/*public function __construct( | ||
string $propertyUri, | ||
string $attributeType = 'resource', | ||
string $mappedField = '' | ||
) | ||
{ | ||
$this->propertyUri = $propertyUri; | ||
$this->attributeType = $attributeType; | ||
$this->mappedField = $mappedField; | ||
}*/ | ||
|
||
/*public function __construct() //($data, $b, $c) | ||
{ | ||
//$this->attributeType = $attributeType; | ||
|
||
//echo "Called RdfAttributeMapping ctor with ".var_export($data,true); | ||
}*/ | ||
|
||
// You may see keeping hydrate() here in the annotation class as a good | ||
// thing (as in "higher cohesion", keeping the value initialization in the | ||
// annotation implementation) or bad thing (as in "higher coupling", | ||
// preventing having more than one behaviour (implementation) for a given | ||
// annotation). | ||
// | ||
// Maybe we'll want to allow overriding the behaviour for a given annotation | ||
// somehow in the future (for example, from extensions)). | ||
// | ||
public function hydrate( | ||
LoggerInterface $logger, | ||
ReflectionProperty $property, | ||
core_kernel_classes_Resource $src, | ||
object $targetObject | ||
): void | ||
{ | ||
$logger->debug(__CLASS__ . " maps a value to the property"); | ||
|
||
$values = $src->getPropertyValues( | ||
new core_kernel_classes_Property($this->propertyUri) | ||
); | ||
|
||
if(count($values) == 0) { | ||
$logger->warning(__CLASS__ . " no value to map"); | ||
return; | ||
} | ||
|
||
if(count($values) > 1) { | ||
$logger->warning(__CLASS__ . "too many values to map"); | ||
return; | ||
} | ||
|
||
if(count($values) == 1) { | ||
$value = current($values); | ||
$logger->info( | ||
sprintf("%s Mapping value %s into %s", | ||
__CLASS__, | ||
$value, | ||
$property->getName() | ||
) | ||
); | ||
|
||
$property->setAccessible(true); | ||
$property->setValue($targetObject, $value); | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
models/classes/RdfObjectMapper/Annotation/RdfResourceAttributeMapping.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?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) 2022 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
namespace oat\tao\model\RdfObjectMapper\Annotation; | ||
|
||
use core_kernel_classes_Resource; | ||
use LogicException; | ||
use Psr\Log\LoggerInterface; | ||
use ReflectionProperty; | ||
|
||
//#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
/** | ||
*@Annotation | ||
*/ | ||
class RdfResourceAttributeMapping | ||
{ | ||
public /*int*/ $type = 0; | ||
|
||
// the commented out constructors are in case we use PHP 8 annotations | ||
//public function __construct(int $attributeType) | ||
/*public function __construct($data) | ||
{ | ||
//$this->attributeType = $attributeType; | ||
|
||
//echo "Called RdfResourceAttributeMapping ctor with ".var_export($data,true); | ||
}*/ | ||
|
||
public function hydrate( | ||
LoggerInterface $logger, | ||
ReflectionProperty $property, | ||
core_kernel_classes_Resource $src, | ||
object $targetObject | ||
): void | ||
{ | ||
$logger->debug( | ||
__CLASS__ . | ||
' maps a (direct) value from the resource class to the property' | ||
); | ||
|
||
switch ($this->type) | ||
{ | ||
case RdfResourceAttributeType::LABEL: | ||
$value = $src->getLabel(); | ||
break; | ||
case RdfResourceAttributeType::COMMENT: | ||
$value = $src->getComment(); | ||
break; | ||
case RdfResourceAttributeType::URI: | ||
$value = $src->getUri(); | ||
break; | ||
default: | ||
throw new LogicException( | ||
"Unknown ".__CLASS__."::type value: ". | ||
$this->type | ||
); | ||
} | ||
|
||
$logger->info( | ||
sprintf("%s Mapping value %s into %s", | ||
__CLASS__, | ||
$value, | ||
$property->getName() | ||
) | ||
); | ||
|
||
if (version_compare(PHP_VERSION, '8.1.0', '<')) { | ||
// Not needed starting PHP 8.1 (it has become a no-op since then) | ||
$property->setAccessible(true); | ||
} | ||
|
||
$property->setValue($targetObject, $value); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
models/classes/RdfObjectMapper/Annotation/RdfResourceAttributeType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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) 2022 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
namespace oat\tao\model\RdfObjectMapper\Annotation; | ||
|
||
class RdfResourceAttributeType | ||
{ | ||
public const URI = 1; | ||
public const LABEL = 2; | ||
public const COMMENT = 3; | ||
} |
31 changes: 31 additions & 0 deletions
31
models/classes/RdfObjectMapper/Contract/RdfObjectMapperInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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) 2022 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
namespace oat\tao\model\RdfObjectMapper\Contract; | ||
|
||
use core_kernel_classes_Resource; | ||
|
||
interface RdfObjectMapperInterface | ||
{ | ||
public function mapResource( | ||
core_kernel_classes_Resource $resource, | ||
string $targetClass | ||
): object; | ||
} |
119 changes: 119 additions & 0 deletions
119
models/classes/RdfObjectMapper/Example/UserSettingsMappedType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?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) 2021 (original work) Open Assessment Technologies SA. | ||
*/ | ||
|
||
namespace oat\tao\model\RdfObjectMapper\Example; | ||
|
||
use oat\generis\model\GenerisRdf; | ||
use oat\tao\model\user\UserSettingsInterface; | ||
|
||
use oat\tao\model\RdfObjectMapper\Annotation\RdfAttributeMapping; | ||
use oat\tao\model\RdfObjectMapper\Annotation\RdfResourceAttributeMapping; | ||
use oat\tao\model\RdfObjectMapper\Annotation\RdfResourceAttributeType; | ||
|
||
|
||
/** | ||
* Example object type with RDF mapping annotations. | ||
* | ||
* This would be "userland code" for the mapper (i.e. objects using annotations | ||
* are to be located in other models/namespaces (tao/models/classes/user etc) | ||
*/ | ||
class UserSettingsMappedType implements UserSettingsInterface | ||
{ | ||
//#[RdfResourceAttributeMapping(RdfResourceAttributeMapping::URI)] | ||
/** @RdfResourceAttributeMapping(type = RdfResourceAttributeType::URI) */ | ||
private /*string*/ $userUri; | ||
|
||
//#[RdfResourceAttributeMapping(RdfResourceAttributeMapping::LABEL)] | ||
/** @RdfResourceAttributeMapping(type = RdfResourceAttributeType::LABEL) */ | ||
private /*string*/ $userLabel; | ||
|
||
//#[RdfResourceAttributeMapping(RdfResourceAttributeMapping::COMMENT)] | ||
/** @RdfResourceAttributeMapping(type = RdfResourceAttributeType::COMMENT) */ | ||
private /*string*/ $userComment; | ||
|
||
/*#[RdfAttributeMapping( | ||
GenerisRdf::PROPERTY_USER_UILG, | ||
'resource', | ||
|
||
// field returned for non-literal properties. | ||
// Can be | ||
// - 'alias' (string) | ||
// - 'range' (core_kernel_classes_ContainerCollection, calls $property->getRange()) | ||
// - 'resource' (complete resource instance) | ||
// - 'uri' (resource URI as string) | ||
'uri' | ||
)]*/ | ||
|
||
/** @RdfAttributeMapping( | ||
* propertyUri = GenerisRdf::PROPERTY_USER_UILG, | ||
* attributeType = "resource", | ||
* mappedField = "uri") | ||
*/ | ||
private /*?string*/ $uiLanguageCode; | ||
|
||
/** | ||
* @RdfAttributeMapping( | ||
* propertyUri = GenerisRdf::PROPERTY_USER_DEFLG, | ||
* attributeType = "resource", | ||
* mappedField = "uri") | ||
*/ | ||
private /*?string*/ $dataLanguage; | ||
|
||
/** | ||
* property returned as a core_kernel_classes_Literal instance, | ||
* will be converted to string | ||
* @todo Use RdfResourceAttributeType (or a new class) constants for | ||
* attributeType | ||
* @RdfAttributeMapping( | ||
* GenerisRdf::PROPERTY_USER_TIMEZONE, | ||
* attributeType = "literal") | ||
*/ | ||
private /*string*/ $timezone; | ||
|
||
public function getUri(): string | ||
{ | ||
return $this->userUri; | ||
} | ||
|
||
public function getLabel(): string | ||
{ | ||
return $this->userLabel; | ||
} | ||
|
||
public function getComment(): string | ||
{ | ||
return $this->userComment; | ||
} | ||
|
||
public function getUILanguageCode(): ?string | ||
{ | ||
return $this->uiLanguageCode; | ||
} | ||
|
||
public function getDataLanguageCode(): ?string | ||
{ | ||
return $this->dataLanguage; | ||
} | ||
|
||
public function getTimezone(): string | ||
{ | ||
return $this->timezone; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having only
LITERAL
(for copying values as-is) andRESOURCE
(for URIs pointing to other resources) may suffice.The former values types here were matching exactly the fields used for the PoC (basically attributes currently found in
core_kernel_classes_Resource
; as well as having aURI
type to hold resource URIs (for example, URIs used as primary identifiers for resources).This may be combined with PHP 7.4+ property type hints as well to validate things like conversions to integer etc.