-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(xmlupload): change serialisation of values with rdflib (#1274)
- Loading branch information
1 parent
a8b1b68
commit ec14df4
Showing
3 changed files
with
106 additions
and
38 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/dsp_tools/commands/xmlupload/models/serialise/serialise_rdf_value.py
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,56 @@ | ||
from abc import ABC | ||
from abc import abstractmethod | ||
from dataclasses import dataclass | ||
|
||
from rdflib import RDF | ||
from rdflib import BNode | ||
from rdflib import Graph | ||
from rdflib import Literal | ||
from rdflib import Namespace | ||
from rdflib import URIRef | ||
|
||
KNORA_API = Namespace("http://api.knora.org/ontology/knora-api/v2#") | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ValueRDF(ABC): | ||
resource_bn: BNode | ||
prop_name: URIRef | ||
value: Literal | URIRef | ||
permissions: Literal | None | ||
comment: Literal | None | ||
|
||
@abstractmethod | ||
def as_graph(self) -> Graph: | ||
"""Creates the value as rdflib graph""" | ||
|
||
def _get_generic_graph(self, val_bn: BNode) -> Graph: | ||
g = Graph() | ||
g.add((self.resource_bn, self.prop_name, val_bn)) | ||
if self.permissions: | ||
g.add((val_bn, KNORA_API.hasPermissions, self.permissions)) | ||
if self.comment: | ||
g.add((val_bn, KNORA_API.valueHasComment, self.comment)) | ||
return g | ||
|
||
|
||
class BooleanValueRDF(ValueRDF): | ||
value: Literal | ||
|
||
def as_graph(self) -> Graph: | ||
val_bn = BNode() | ||
g = self._get_generic_graph(val_bn) | ||
g.add((val_bn, RDF.type, KNORA_API.BooleanValue)) | ||
g.add((val_bn, KNORA_API.booleanValueAsBoolean, self.value)) | ||
return g | ||
|
||
|
||
class IntValueRDF(ValueRDF): | ||
value: Literal | ||
|
||
def as_graph(self) -> Graph: | ||
val_bn = BNode() | ||
g = self._get_generic_graph(val_bn) | ||
g.add((val_bn, RDF.type, KNORA_API.IntValue)) | ||
g.add((val_bn, KNORA_API.intValueAsInt, self.value)) | ||
return g |
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
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