-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from rdflib.graph import URIRef, Literal | ||
from rdflib.namespace import XSD | ||
|
||
from ..globals.urls import ONTONS | ||
|
||
class JSONToRDF: | ||
def __init__(self, graph, graphurl): | ||
self.graph = graph | ||
self.graphurl = graphurl | ||
|
||
def _load_triple_into_graph(self, subject, propid, value): | ||
for val in value: | ||
valitem = None | ||
if val["@type"] == "uri": | ||
valitem = URIRef(val["@id"]) | ||
elif val["@type"] == "literal": | ||
dtype = val["@datatype"] | ||
if dtype: | ||
valitem = Literal(value, datatype=(XSD[dtype] if dtype in XSD else None)) | ||
else: | ||
valitem = Literal(value) | ||
if valitem: | ||
self.graph.add(( | ||
URIRef(subject), | ||
URIRef(propid), | ||
valitem, | ||
URIRef(self.graphurl) | ||
)) | ||
|
||
def _clear_subgraph(self): | ||
for ctx in self.graph.contexts(): | ||
id = str(ctx.identifier) | ||
if id == self.graphurl: | ||
self.graph.remove((None, None, None, id)) | ||
|
||
def load_data_in_graph(self, data): | ||
# Clear the subgraph | ||
self._clear_subgraph() | ||
|
||
# Load data | ||
for subject in data: | ||
for propid in data[subject]: | ||
value = data[subject][propid] | ||
self._load_triple_into_graph(subject, ONTONS + propid, value) |