Skip to content
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

update sparql #17

Merged
merged 14 commits into from
Sep 2, 2024
64 changes: 42 additions & 22 deletions oteapi_dlite/strategies/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,18 @@ class URIs within a specified RDF graph.
try:
template_str = """
{% 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#>
SELECT ?parentClass
WHERE {
GRAPH <{{ graph_uri }}> {
?class rdfs:subClassOf* ?parentClass .
FILTER(
{% for class_name in class_names -%}
?class = <{{ class_name }}>{{ " ||" if not loop.last }}
{% endfor %})
}
}
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?parentClass
WHERE {
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 @@ -326,26 +326,47 @@ def fetch_and_populate_graph(
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 ?subject ?predicate ?object
SELECT DISTINCT ?subject ?predicate ?object
WHERE {{
GRAPH <{graph_uri}> {{
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 .
?subject rdfs:subClassOf* <{parent_node}> .
}}

# Ensure subject, predicate, and object are not empty
FILTER(BOUND(?subject) && BOUND(?predicate) && BOUND(?object))

# Filter by the specific predicates
FILTER (?predicate IN (
rdfs:subClassOf,
Treesarj marked this conversation as resolved.
Show resolved Hide resolved
skos:prefLabel,
rdfs:subPropertyOf,
rdfs:domain,
rdfs:range,
rdfs:label,
rdf:type,
rdf:about,
owl:propertyDisjointWith,
fno:expects,
fno:predicate,
fno:type,
fno:returns,
fno:executes))
}}
fno:executes,
oteio:hasPythonFunctionName,
oteio:hasPythonModuleName,
oteio:hasPypiPackageName,
emmo:mapsTo))
}}
}}
"""
sparql.setQuery(query)
Expand All @@ -359,7 +380,6 @@ def fetch_and_populate_graph(
rdflib.URIRef(result["object"]["value"]),
)
)

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

except SPARQLWrapperException as wrapper_error:
Expand Down
Loading