From b51c0de37d80443538eba78418d6c94d83296b7f Mon Sep 17 00:00:00 2001 From: "Francesca.L.Bleken@sintef.no" Date: Wed, 1 Apr 2026 10:54:47 +0200 Subject: [PATCH] New logic in told with key datamodel. If already a class: subClassOf --- tests/datadoc/test_dataset.py | 23 ++++++++++++++++++++++- tripper/datadoc/dataset.py | 13 ++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/datadoc/test_dataset.py b/tests/datadoc/test_dataset.py index 2e7f79da..91398dc6 100644 --- a/tests/datadoc/test_dataset.py +++ b/tests/datadoc/test_dataset.py @@ -23,7 +23,7 @@ def test_told(): # pylint: disable=too-many-statements from pathlib import Path - from tripper import DCAT, DCTERMS, RDF, Triplestore + from tripper import DCAT, DCTERMS, OWL, RDF, RDFS, Triplestore from tripper.datadoc.dataset import store, told from tripper.datadoc.errors import InvalidDatadocError from tripper.utils import en @@ -229,6 +229,27 @@ def test_told(): d8 = told(descrH) assert d8["@type"] == "owl:Class" + # A dataset type should remain a class and use datamodel as subclass. + descrI = { + "@id": "ex:TechnicalDataset", + "@type": "owl:Class", + "title": "Technical dataset type", + "datamodel": "ex:BaseDataModel", + } + d9 = told(descrI) + assert d9["@id"] == "ex:TechnicalDataset" + assert d9["@type"] == "owl:Class" + assert d9["datamodel"] == "ex:BaseDataModel" + assert d9["subClassOf"] == "ex:BaseDataModel" + + # Test store() on descrI + ts.remove() + store(ts, descrI, prefixes=prefixes) + EX = ts.namespaces["ex"] + assert ts.has(EX.TechnicalDataset, RDF.type, OWL.Class) + assert ts.has(EX.TechnicalDataset, RDFS.subClassOf, EX.BaseDataModel) + assert not ts.has(EX.TechnicalDataset, RDF.type, EX.BaseDataModel) + # Multi-rep with invalid root keyword descrG = { "prefixes": {"laz": "http://lazarus.org/reincarnated/data"}, diff --git a/tripper/datadoc/dataset.py b/tripper/datadoc/dataset.py index d63986ff..9d8efacd 100644 --- a/tripper/datadoc/dataset.py +++ b/tripper/datadoc/dataset.py @@ -361,8 +361,19 @@ def _deduplicate_types(d): ] add(d, k, [tuple(t) for t in lst]) elif k == "datamodel": - add(d, "@type", v) + # Check if d is is of type @type owl:Class, if not, add the + # datamodel as a @type. Otherwise, the datamodel will be + # added as a subclass of the class. + is_class = OWL.Class in { + expand(t) if isinstance(t, str) else t + for t in asseq(get(d, "@type", ())) + } + if is_class: + add(d, "subClassOf", v) + else: + add(d, "@type", v) d[k] = v + # # The below works fine. It is commented out since it is doubtable # whether it is a good idea to invent new shortcuts for json-ld.