From 22f6da540f017df08b96490b68a23f19f3a9c57c Mon Sep 17 00:00:00 2001 From: flange <38500-flange@users.noreply.git.rwth-aachen.de> Date: Tue, 12 Nov 2024 16:51:28 +0100 Subject: [PATCH] exclude deprecated disciplines --- project/dalia/curation/suggest/disciplines.py | 17 ++++++++-- .../curation/suggest/test_disciplines.py | 31 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/project/dalia/curation/suggest/disciplines.py b/project/dalia/curation/suggest/disciplines.py index efad3c0..172aba7 100644 --- a/project/dalia/curation/suggest/disciplines.py +++ b/project/dalia/curation/suggest/disciplines.py @@ -1,10 +1,16 @@ from typing import List, Tuple -from rdflib import Literal, RDF, SKOS, URIRef, Variable +from rdflib import Literal, OWL, RDF, SKOS, URIRef, Variable from project.dalia.api_models.api_models import CurationSuggestDisciplinesResultItem from project.dalia.query.utils import query_ontologies_dataset -from project.dalia.query_builder.query_builder import FILTER, FunctionExpressions, Operators, QueryBuilder +from project.dalia.query_builder.query_builder import ( + FILTER, + FILTER_EXISTS, + FunctionExpressions, + Operators, + QueryBuilder, +) # Note: This algorithm is quite inefficient and results in a lot of SPARQL queries. # Another idea would be to pull all disciplines and their narrower/broader nodes at once, e.g. via @@ -19,6 +25,9 @@ from project.dalia.query_builder.query_builder import FILTER, FunctionExpression # ?broaderDiscipline skos:narrower ?discipline . # } # ?discipline a skos:Concept . +# FILTER NOT EXISTS { +# ?discipline owl:deprecated true . +# } # ?discipline skos:prefLabel ?label . # FILTER(LANG(?label) = "en") . # ?discipline skos:notation ?notation . @@ -57,6 +66,10 @@ def _prepare_query_to_get_disciplines_metadata(discipline_selection_bgp: Tuple) ).WHERE( discipline_selection_bgp, (var_discipline, RDF.type, SKOS.Concept), + FILTER_EXISTS( + (var_discipline, OWL.deprecated, Literal(True)), + state=False + ), (var_discipline, SKOS.prefLabel, var_label), FILTER( Operators.EQ( diff --git a/tests/project/dalia/curation/suggest/test_disciplines.py b/tests/project/dalia/curation/suggest/test_disciplines.py index 8dd4c38..8625a45 100644 --- a/tests/project/dalia/curation/suggest/test_disciplines.py +++ b/tests/project/dalia/curation/suggest/test_disciplines.py @@ -1,10 +1,16 @@ +from typing import List + from django.urls import reverse from rdflib import URIRef from rest_framework import status from project.dalia.api_models.api_models import CurationSuggestDisciplinesResultItem -from project.dalia.curation.suggest.disciplines import get_child_disciplines_of_discipline, get_top_disciplines, \ - prepare_query_to_get_all_narrower_disciplines_of_discipline, prepare_query_to_get_all_top_disciplines +from project.dalia.curation.suggest.disciplines import ( + get_child_disciplines_of_discipline, + get_top_disciplines, + prepare_query_to_get_all_narrower_disciplines_of_discipline, + prepare_query_to_get_all_top_disciplines, +) from project.dalia.serializers import CurationSuggestDisciplinesResultItemSerializer from tests.project.dalia.utils import dedent_and_normalize, normalize @@ -17,6 +23,9 @@ def test_prepare_query_to_get_all_top_disciplines(): WHERE { ?discipline <http://www.w3.org/2004/02/skos/core#topConceptOf> <https://w3id.org/kim/hochschulfaechersystematik/scheme> . ?discipline <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept> . + FILTER NOT EXISTS { + ?discipline <http://www.w3.org/2002/07/owl#deprecated> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> . + } . ?discipline <http://www.w3.org/2004/02/skos/core#prefLabel> ?label . FILTER ( LANG ( ?label ) = "en" ) . ?discipline <http://www.w3.org/2004/02/skos/core#notation> ?notation . @@ -33,6 +42,9 @@ def test_prepare_query_to_get_all_narrower_disciplines_of_discipline(): WHERE { <abc> <http://www.w3.org/2004/02/skos/core#narrower> ?discipline . ?discipline <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept> . + FILTER NOT EXISTS { + ?discipline <http://www.w3.org/2002/07/owl#deprecated> "true"^^<http://www.w3.org/2001/XMLSchema#boolean> . + } . ?discipline <http://www.w3.org/2004/02/skos/core#prefLabel> ?label . FILTER ( LANG ( ?label ) = "en" ) . ?discipline <http://www.w3.org/2004/02/skos/core#notation> ?notation . @@ -142,3 +154,18 @@ def test_get_on_CurationSuggestDisciplinesView_returns_200_and_the_disciplines_t assert n242.label == "Interior Architecture" assert n242.value == "https://w3id.org/kim/hochschulfaechersystematik/n242" assert len(n242.children) == 0 + + assert _has_item_with_uri(data, "https://w3id.org/kim/hochschulfaechersystematik/n077") + assert _has_item_with_uri(data, "https://w3id.org/kim/hochschulfaechersystematik/n194") + + # deprecated items are not in the returned data + assert not _has_item_with_uri(data, "https://w3id.org/kim/hochschulfaechersystematik/n241") + assert not _has_item_with_uri(data, "https://w3id.org/kim/hochschulfaechersystematik/n237") + + +# DFS in nested data structure +def _has_item_with_uri(data: List[CurationSuggestDisciplinesResultItem], uri: str) -> bool: + for item in data: + if item.value == uri or _has_item_with_uri(item.children, uri): + return True + return False -- GitLab