Skip to content

Commit b8c2430

Browse files
committed
Adding referenceTo and fixing properties of data type OBJECT
1 parent fed647f commit b8c2430

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

bam_masterdata/cli/entities_to_rdf.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from structlog._config import BoundLoggerLazyProxy
77

88
import click
9-
from rdflib import Literal, Namespace
9+
from rdflib import BNode, Literal, Namespace
1010
from rdflib.namespace import DC, OWL, RDF, RDFS, SKOS
1111

12-
from bam_masterdata.utils import import_module
12+
from bam_masterdata.utils import code_to_class_name, import_module
1313

1414
BAM = Namespace("http://bam.de/masterdata/")
1515

@@ -39,29 +39,33 @@ def rdf_graph_init(g: "Graph") -> None:
3939
bam_props_uri = {
4040
BAM["hasMandatoryProperty"]: [
4141
(RDF.type, OWL.ObjectProperty),
42-
(RDFS.subPropertyOf, OWL.topObjectProperty),
43-
(RDFS.domain, OWL.Thing),
42+
(RDFS.domain, BAM.ObjectType),
43+
(RDFS.range, BAM.PropertyType),
4444
(SKOS.prefLabel, Literal("hasMandatoryProperty", lang="en")),
4545
],
4646
BAM["hasOptionalProperty"]: [
4747
(RDF.type, OWL.ObjectProperty),
48-
(RDFS.subPropertyOf, OWL.topObjectProperty),
49-
(RDFS.domain, OWL.Thing),
48+
(RDFS.domain, BAM.ObjectType),
49+
(RDFS.range, BAM.PropertyType),
5050
(SKOS.prefLabel, Literal("hasOptionalProperty", lang="en")),
5151
],
52+
BAM["referenceTo"]: [
53+
(RDF.type, OWL.ObjectProperty),
54+
(RDFS.domain, BAM.PropertyType), # Restricting domain to PropertyType
55+
(RDFS.range, BAM.ObjectType), # Explicitly setting range to ObjectType
56+
(SKOS.prefLabel, Literal("referenceTo", lang="en")),
57+
],
5258
}
5359
for prop_uri, obj_properties in bam_props_uri.items():
54-
for prop in obj_properties:
55-
g.add((prop_uri, prop[0], prop[1]))
60+
for prop in obj_properties: # type: ignore
61+
g.add((prop_uri, prop[0], prop[1])) # type: ignore
5662

5763
# Adding base entity types objects
5864
for entity in ["PropertyType", "ObjectType", "CollectionType", "DatasetType"]:
5965
entity_uri = BAM[entity]
6066
g.add((entity_uri, RDF.type, OWL.Class))
6167
g.add((entity_uri, SKOS.prefLabel, Literal(entity, lang="en")))
6268

63-
return g
64-
6569

6670
def entities_to_rdf(
6771
graph: "Graph", module_path: str, logger: "BoundLoggerLazyProxy"
@@ -104,6 +108,19 @@ def entities_to_rdf(
104108
)
105109
graph.add((prop_uri, SKOS.altLabel, Literal(obj.property_label, lang="en")))
106110
graph.add((prop_uri, DC.type, Literal(obj.data_type.value)))
111+
if obj.data_type.value == "OBJECT":
112+
# entity_ref_uri = BAM[code_to_class_name(obj.object_code)]
113+
# graph.add((prop_uri, BAM.referenceTo, entity_ref_uri))
114+
entity_ref_uri = BAM[code_to_class_name(obj.object_code)]
115+
116+
# Create a restriction with referenceTo
117+
restriction = BNode()
118+
graph.add((restriction, RDF.type, OWL.Restriction))
119+
graph.add((restriction, OWL.onProperty, BAM["referenceTo"]))
120+
graph.add((restriction, OWL.someValuesFrom, entity_ref_uri))
121+
122+
# Add the restriction as a subclass of the property
123+
graph.add((prop_uri, RDFS.subClassOf, restriction))
107124
return None
108125

109126
# All other datamodel modules

0 commit comments

Comments
 (0)