Skip to content

Commit

Permalink
Revert "update sparql to fetch all data and object properties"
Browse files Browse the repository at this point in the history
This reverts commit 42c420a.
  • Loading branch information
Treesarj committed Aug 28, 2024
1 parent 42c420a commit 7d41315
Showing 1 changed file with 35 additions and 66 deletions.
101 changes: 35 additions & 66 deletions oteapi_dlite/strategies/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,27 +242,15 @@ class URIs within a specified RDF graph.
{% macro sparql_query(class_names, graph_uri) %}
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?parentClass
WHERE {
GRAPH <{{ graph_uri }}> {
{
# Subclass traversal to find parent classes
?class rdfs:subClassOf* ?parentClass .
FILTER(
{% for class_name in class_names -%}
?class = <{{ class_name }}>{{ " ||" if not loop.last }}
{% endfor %}
)
}
UNION
{
# Explicitly include owl:Thing as a parent class
VALUES ?class { {% for class_name in class_names -%} <{{ class_name }}> {% endfor %} }
BIND(owl:Thing AS ?parentClass)
}
}
GRAPH <{{ graph_uri }}> {
?class rdfs:subClassOf* ?parentClass .
FILTER(
{% for class_name in class_names -%}
?class = <{{ class_name }}>{{ " ||" if not loop.last }}
{% endfor %})
}
}
{% endmacro %}
"""
Expand Down Expand Up @@ -333,53 +321,33 @@ def fetch_and_populate_graph(
sparql.setReturnFormat(JSON)

query = f"""
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fno: <https://w3id.org/function/ontology#>
PREFIX emmo: <http://emmo.info/domain-mappings#>
PREFIX oteio: <http://emmo.info/oteio#>
SELECT DISTINCT ?subject ?predicate ?object
WHERE {{
GRAPH <{graph_uri}> {{
# Match all subclasses of the parent class
?subclass rdfs:subClassOf* <{parent_node}> .
# Retrieve all relevant triples for these subclasses and their individuals
{{
# Subclasses themselves and their relationships
?subclass ?predicate ?object .
BIND(?subclass AS ?subject)
}} UNION {{
# Individuals of these subclasses and their properties
?subject rdf:type ?subclass .
?subject ?predicate ?object .
}}
# Ensure subject, predicate, and object are not empty
FILTER(BOUND(?subject) && BOUND(?predicate) && BOUND(?object))
# Filter by the specific predicates
FILTER (?predicate IN (
rdfs:label,
rdf:type,
rdf:about,
owl:propertyDisjointWith,
fno:expects,
fno:predicate,
fno:type,
fno:returns,
fno:executes,
oteio:hasPythonFunctionName,
oteio:hasPythonModuleName,
oteio:hasPypiPackageName,
emmo:mapsTo))
}}
}}
"""
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX fno: <https://w3id.org/function/ontology#>
SELECT ?subject ?predicate ?object
WHERE {{
GRAPH <{graph_uri}> {{
?subject ?predicate ?object .
?subject rdfs:subClassOf* <{parent_node}> .
FILTER (?predicate IN (
rdfs:subClassOf,
skos:prefLabel,
rdfs:subPropertyOf,
rdfs:domain,
rdfs:range,
rdf:type,
owl:propertyDisjointWith,
fno:expects,
fno:predicate,
fno:type,
fno:returns,
fno:executes))
}}
}}
"""
sparql.setQuery(query)

results = sparql.query().convert()
Expand All @@ -391,6 +359,7 @@ def fetch_and_populate_graph(
rdflib.URIRef(result["object"]["value"]),
)
)

logger.info("Graph populated with fetched triples.")

except SPARQLWrapperException as wrapper_error:
Expand Down

0 comments on commit 7d41315

Please sign in to comment.