From 41a9b5ce122053603ae71895051cddf1725c8c76 Mon Sep 17 00:00:00 2001 From: flange <38500-flange@users.noreply.git.rwth-aachen.de> Date: Thu, 24 Oct 2024 16:27:35 +0200 Subject: [PATCH] change the SPARQL query to find all communities for a given list of learning resources RDF modeling has changed from mo:hasCommunity to rec:recommender and/or bflr:supportinghost --- .../query/items/metadata/item_communities.py | 15 ++++-- .../rdf/namespace/bibframe_lite_relation.py | 9 ++++ project/dalia/rdf/namespace/rec.py | 9 ++++ .../items/metadata/test_item_communities.py | 51 +++++++++---------- 4 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 project/dalia/rdf/namespace/bibframe_lite_relation.py create mode 100644 project/dalia/rdf/namespace/rec.py diff --git a/project/dalia/query/items/metadata/item_communities.py b/project/dalia/query/items/metadata/item_communities.py index daa5d1c..b4d966f 100644 --- a/project/dalia/query/items/metadata/item_communities.py +++ b/project/dalia/query/items/metadata/item_communities.py @@ -6,8 +6,8 @@ from rdflib import DCTERMS, RDF, URIRef, Variable from project.dalia.api_models.api_models import Community from project.dalia.query.communities.one_to_one_metadata import get_one_to_one_metadata_for_communities from project.dalia.query.utils import query_dalia_dataset -from project.dalia.query_builder.query_builder import QueryBuilder, VALUES -from project.dalia.rdf.namespace import MoDalia, educor +from project.dalia.query_builder.query_builder import GROUP, QueryBuilder, UNION, VALUES +from project.dalia.rdf.namespace import MoDalia, bibframe_lite_relation, educor, rec _VARIABLES = { "lr": Variable("lr"), @@ -30,9 +30,18 @@ def prepare_query_for_learning_resource_to_communities_associations(resource_uri resource_uri_ref_blocks ), (var_lr, RDF.type, educor.EducationalResource), - (var_lr, MoDalia.hasCommunity, var_community), + # This GROUP-UNION pattern is equivalent to "?lr rec:recommender|bflr:supportinghost ?community" + # (AlternativePath expression in SPARQL). + GROUP( + (var_lr, rec.recommender, var_community), + ), + UNION( + (var_lr, bibframe_lite_relation.supportinghost, var_community), + ), (var_community, RDF.type, MoDalia.Community), (var_community, DCTERMS.title, var_community_title) + ).GROUP_BY( # prevents duplicates in case the same community is both rec:recommender and bflr:supportinghost + var_lr, var_community ).ORDER_BY( var_community_title ).build() diff --git a/project/dalia/rdf/namespace/bibframe_lite_relation.py b/project/dalia/rdf/namespace/bibframe_lite_relation.py new file mode 100644 index 0000000..7e1eb14 --- /dev/null +++ b/project/dalia/rdf/namespace/bibframe_lite_relation.py @@ -0,0 +1,9 @@ +""" +Terms from Bibframe Lite + Relation (http://bibfra.me/vocab/relation/) +""" +from rdflib import URIRef + +NS = "http://bibfra.me/vocab/relation/" + +# Properties +supportinghost = URIRef(NS + "supportinghost") diff --git a/project/dalia/rdf/namespace/rec.py b/project/dalia/rdf/namespace/rec.py new file mode 100644 index 0000000..941e226 --- /dev/null +++ b/project/dalia/rdf/namespace/rec.py @@ -0,0 +1,9 @@ +""" +Terms from The Recommendation Ontology (http://purl.org/ontology/rec/core#) +""" +from rdflib import URIRef + +NS = "http://purl.org/ontology/rec/core#" + +# Properties +recommender = URIRef(NS + "recommender") diff --git a/tests/project/dalia/query/items/metadata/test_item_communities.py b/tests/project/dalia/query/items/metadata/test_item_communities.py index 45ba36d..24af713 100644 --- a/tests/project/dalia/query/items/metadata/test_item_communities.py +++ b/tests/project/dalia/query/items/metadata/test_item_communities.py @@ -18,56 +18,53 @@ def test_prepare_query_for_learning_resource_to_communities_associations(): ( <def> ) } ?lr <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://github.com/tibonto/educor#EducationalResource> . - ?lr <https://purl.org/ontology/modalia#hasCommunity> ?community . + { + ?lr <http://purl.org/ontology/rec/core#recommender> ?community . + } + UNION { + ?lr <http://bibfra.me/vocab/relation/supportinghost> ?community . + } ?community <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://purl.org/ontology/modalia#Community> . ?community <http://purl.org/dc/terms/title> ?community_title . } + GROUP BY ?lr ?community ORDER BY ?community_title """) def test_get_learning_resource_to_communities_associations(triplestore): communities = { - "BERD@NFDI": URIRef("https://id.dalia.education/community/7ecf1a3e-e377-4f5c-ac70-78d498951843"), "ELIXIR": URIRef("https://id.dalia.education/community/c5d22eff-ec20-409d-b8d3-aa4e9cc2d6cb"), + "FAIRagro": URIRef("https://id.dalia.education/community/f6e6a7fc-552b-4d75-9fca-8e2b1b8ad624"), "KonsortSWD": URIRef("https://id.dalia.education/community/ca42e106-dc0e-44ae-bb87-417195fd7d39"), - "NFDI4Cat": URIRef("https://id.dalia.education/community/7783f91b-2496-4c1b-97ef-9db578d237ca"), - "NFDI4Chem": URIRef("https://id.dalia.education/community/bead62a8-c3c2-46d6-9eb1-ffeaba38d5bf"), - "NFDI4Ing": URIRef("https://id.dalia.education/community/4f646323-756e-41d0-bdb6-0767411a14b5"), - "Nationale Forschungsdateninfrastruktur (NFDI)": - URIRef("https://id.dalia.education/community/3dc37495-59bb-4505-851f-e09c5df8e356"), + "NFDI4Biodiversity": URIRef("https://id.dalia.education/community/aac7b1be-cf00-4fdc-a26a-e8e0e1410b18"), + "NFDI4Culture": URIRef("https://id.dalia.education/community/6a21dd4a-200c-44e3-85b8-68fb31d510af"), } learning_resources = { - 1: URIRef("https://id.dalia.education/learning-resource/e4384beb-abc6-4790-8579-5f45b1817b72"), - 2: URIRef("https://id.dalia.education/learning-resource/82a4e5f2-89cb-46b8-a017-a69d4ad48431"), - 3: URIRef("https://id.dalia.education/learning-resource/2105884b-1a1b-470d-a16a-1c6cd3efd804"), - 4: URIRef("https://id.dalia.education/learning-resource/00855e6b-ebc6-422b-a2d5-7caa58569a9b"), - 5: URIRef("https://id.dalia.education/learning-resource/9c9cea0c-2ca1-4789-a091-250e8fe46024"), + 1: URIRef("https://id.dalia.education/learning-resource/08a8e89b-c854-4c7f-bfc0-5df933ad66de"), + 2: URIRef("https://id.dalia.education/learning-resource/e5128165-4c65-4dca-a40f-d4dd73ed4908"), + 3: URIRef("https://id.dalia.education/learning-resource/d350bbb7-e949-4589-9a5f-a787859963e1"), + 4: URIRef("https://id.dalia.education/learning-resource/84e64329-43d9-4e0b-a1c5-0bd0c92d1133"), } lr_to_communities_uri_refs_associations = get_learning_resource_to_communities_associations([ - learning_resources[1], # KonsortSWD, Nationale Forschungsdateninfrastruktur (NFDI) + learning_resources[1], # ELIXIR, FAIRagro learning_resources[2], # no associated communities - learning_resources[3], # NFDI4Cat, NFDI4Chem, NFDI4Ing - learning_resources[4], # BERD@NFDI - learning_resources[5], # ELIXIR, NFDI4Chem + learning_resources[3], # FAIRagro, KonsortSWD, NFDI4Biodiversity + learning_resources[4], # NFDI4Culture ]) assert dict(lr_to_communities_uri_refs_associations) == { learning_resources[1]: [ - communities["KonsortSWD"], - communities["Nationale Forschungsdateninfrastruktur (NFDI)"], + communities["ELIXIR"], + communities["FAIRagro"], ], - learning_resources[3]: [ - communities["NFDI4Cat"], - communities["NFDI4Chem"], - communities["NFDI4Ing"], + learning_resources[3]: [ # TODO: Does the ordering by community_title work? + communities["NFDI4Biodiversity"], + communities["KonsortSWD"], + communities["FAIRagro"], ], learning_resources[4]: [ - communities["BERD@NFDI"], - ], - learning_resources[5]: [ - communities["ELIXIR"], - communities["NFDI4Chem"], + communities["NFDI4Culture"], ], } -- GitLab