Skip to content

Commit

Permalink
CONSTRUCT -> SELECT
Browse files Browse the repository at this point in the history
  • Loading branch information
arcangelo7 committed Mar 27, 2024
1 parent cdc8ce6 commit f96cc71
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
20 changes: 10 additions & 10 deletions oc_ocdm/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

import rdflib
from rdflib import RDF, ConjunctiveGraph, Graph, URIRef
from SPARQLWrapper import XML, SPARQLWrapper
from SPARQLWrapper import JSON, SPARQLWrapper

from oc_ocdm.graph.graph_entity import GraphEntity
from oc_ocdm.support.reporter import Reporter
from oc_ocdm.support.support import build_graph_from_results

if TYPE_CHECKING:
from typing import Any, Dict, List, Optional, Set
Expand Down Expand Up @@ -125,16 +126,14 @@ def _try_parse(self, graph: ConjunctiveGraph, file_obj, formats: List[str]) -> b
@staticmethod
def get_graph_from_subject(graph: Graph, subject: URIRef) -> Graph:
g: Graph = Graph(identifier=graph.identifier)
for p, o in graph.predicate_objects(subject):
if isinstance(o, rdflib.term.Literal):
o = rdflib.term.Literal(lexical_or_value=str(o), datatype=None)
for p, o in graph.predicate_objects(subject, unique=True):
g.add((subject, p, o))
return g

@staticmethod
def _extract_subjects(graph: Graph) -> Set[URIRef]:
subjects: Set[URIRef] = set()
for s in graph.subjects():
for s in graph.subjects(unique=True):
subjects.add(s)
return subjects

Expand Down Expand Up @@ -166,8 +165,9 @@ def graph_validation(self, graph: Graph, closed: bool = False) -> Graph:
return valid_graph

@staticmethod
def import_entities_from_graph(g_set: GraphSet, graph: Graph, resp_agent: str,
def import_entities_from_graph(g_set: GraphSet, results: List[Dict], resp_agent: str,
enable_validation: bool = False, closed: bool = False) -> List[GraphEntity]:
graph = build_graph_from_results(results)
if enable_validation:
reader = Reader()
graph = reader.graph_validation(graph, closed)
Expand Down Expand Up @@ -226,12 +226,12 @@ def import_entities_from_graph(g_set: GraphSet, graph: Graph, resp_agent: str,
def import_entity_from_triplestore(g_set: GraphSet, ts_url: str, res: URIRef, resp_agent: str,
enable_validation: bool = False) -> GraphEntity:
sparql: SPARQLWrapper = SPARQLWrapper(ts_url)
query: str = f"CONSTRUCT {{<{res}> ?p ?o}} WHERE {{<{res}> ?p ?o}}"
query: str = f"SELECT ?s ?p ?o WHERE {{BIND (<{res}> AS ?s). ?s ?p ?o.}}"
sparql.setQuery(query)
sparql.setMethod('GET')
sparql.setReturnFormat(XML)
result: ConjunctiveGraph = sparql.queryAndConvert()
if result is not None:
sparql.setReturnFormat(JSON)
result: ConjunctiveGraph = sparql.queryAndConvert()['results']['bindings']
if result:
imported_entities: List[GraphEntity] = Reader.import_entities_from_graph(g_set, result,
resp_agent, enable_validation)
if len(imported_entities) <= 0:
Expand Down
16 changes: 15 additions & 1 deletion oc_ocdm/support/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import re
from datetime import datetime
from typing import TYPE_CHECKING
from rdflib import URIRef, Graph

if TYPE_CHECKING:
from typing import Optional, List, Tuple, Match, Dict, Set
from rdflib import URIRef, Graph
from oc_ocdm.graph.entities.bibliographic.bibliographic_resource import BibliographicResource
from oc_ocdm.graph.entities.bibliographic.responsible_agent import ResponsibleAgent
from oc_ocdm.graph.entities.bibliographic.agent_role import AgentRole
Expand Down Expand Up @@ -362,6 +362,20 @@ def has_supplier_prefix(res: URIRef, base_iri: str) -> bool:
string_iri: str = str(res)
return re.search(r"^%s[a-z][a-z]/0" % base_iri, string_iri) is not None

def build_graph_from_results(results: List[Dict]) -> Graph:
graph = Graph()
for triple in results:
s = URIRef(triple['s']['value'])
p = URIRef(triple['p']['value'])
if triple['o']['type'] == 'uri':
o = URIRef(triple['o']['value'])
else:
datatype = triple['o'].get('datatype', None)
datatype = URIRef(datatype) if datatype is not None else None
o = Literal(triple['o']['value'], datatype=datatype)
graph.add((s, p, o))
return graph


def is_dataset(res: URIRef) -> bool:
string_iri: str = str(res)
Expand Down
22 changes: 19 additions & 3 deletions oc_ocdm/test/resources/test_shacle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest

from pyshacl import validate
from rdflib import ConjunctiveGraph, Graph
from rdflib import ConjunctiveGraph, Graph, URIRef

from oc_ocdm.graph.graph_set import GraphSet
from oc_ocdm.reader import Reader
Expand Down Expand Up @@ -33,7 +33,15 @@ def test_reader(self):
g_set = GraphSet(base_iri='https://w3id.org/oc/meta/')
g = ConjunctiveGraph()
g.parse(source=os.path.join('oc_ocdm', 'test', 'resources', 'data_reader.json'))
reader.import_entities_from_graph(g_set, g, resp_agent='https://orcid.org/0000-0002-8420-0696', enable_validation=True, closed=False)
results = []
for triple in g:
o = triple[2]
o_type = 'uri' if isinstance(o, URIRef) else 'literal'
if o_type == 'literal' and o.datatype:
results.append({'s': {'type': 'uri', 'value': str(triple[0])}, 'p': {'type': 'uri', 'value': str(triple[1])}, 'o': {'type': o_type, 'value': str(triple[2]), 'datatype': str(o.datatype)}})
else:
results.append({'s': {'type': 'uri', 'value': str(triple[0])}, 'p': {'type': 'uri', 'value': str(triple[1])}, 'o': {'type': o_type, 'value': str(triple[2])}})
reader.import_entities_from_graph(g_set, results, resp_agent='https://orcid.org/0000-0002-8420-0696', enable_validation=True, closed=False)
self.assertEqual(set(str(s) for s in g_set.res_to_entity.keys()), {
'https://w3id.org/oc/meta/br/060209',
'https://w3id.org/oc/meta/br/060182',
Expand All @@ -49,7 +57,15 @@ def test_reader_invalid(self):
g_set = GraphSet(base_iri='https://w3id.org/oc/meta/')
g = ConjunctiveGraph()
g.parse(source=os.path.join('oc_ocdm', 'test', 'resources', 'data_reader_invalid.json'))
reader.import_entities_from_graph(g_set, g, resp_agent='https://orcid.org/0000-0002-8420-0696', enable_validation=True, closed=False)
results = []
for triple in g:
o = triple[2]
o_type = 'uri' if isinstance(o, URIRef) else 'literal'
if o_type == 'literal' and o.datatype:
results.append({'s': {'type': 'uri', 'value': str(triple[0])}, 'p': {'type': 'uri', 'value': str(triple[1])}, 'o': {'type': o_type, 'value': str(triple[2]), 'datatype': str(o.datatype)}})
else:
results.append({'s': {'type': 'uri', 'value': str(triple[0])}, 'p': {'type': 'uri', 'value': str(triple[1])}, 'o': {'type': o_type, 'value': str(triple[2])}})
reader.import_entities_from_graph(g_set, results, resp_agent='https://orcid.org/0000-0002-8420-0696', enable_validation=True, closed=False)
self.assertEqual(set(str(s) for s in g_set.res_to_entity.keys()), {
'https://w3id.org/oc/meta/br/060182',
'https://w3id.org/oc/meta/ar/06034124',
Expand Down

0 comments on commit f96cc71

Please sign in to comment.