|
6 | 6 | from structlog._config import BoundLoggerLazyProxy
|
7 | 7 |
|
8 | 8 | import click
|
9 |
| -from rdflib import Literal, Namespace |
| 9 | +from rdflib import BNode, Literal, Namespace |
10 | 10 | from rdflib.namespace import DC, OWL, RDF, RDFS, SKOS
|
11 | 11 |
|
12 |
| -from bam_masterdata.utils import import_module |
| 12 | +from bam_masterdata.utils import code_to_class_name, import_module |
13 | 13 |
|
14 | 14 | BAM = Namespace("http://bam.de/masterdata/")
|
15 | 15 |
|
@@ -39,29 +39,33 @@ def rdf_graph_init(g: "Graph") -> None:
|
39 | 39 | bam_props_uri = {
|
40 | 40 | BAM["hasMandatoryProperty"]: [
|
41 | 41 | (RDF.type, OWL.ObjectProperty),
|
42 |
| - (RDFS.subPropertyOf, OWL.topObjectProperty), |
43 |
| - (RDFS.domain, OWL.Thing), |
| 42 | + (RDFS.domain, BAM.ObjectType), |
| 43 | + (RDFS.range, BAM.PropertyType), |
44 | 44 | (SKOS.prefLabel, Literal("hasMandatoryProperty", lang="en")),
|
45 | 45 | ],
|
46 | 46 | BAM["hasOptionalProperty"]: [
|
47 | 47 | (RDF.type, OWL.ObjectProperty),
|
48 |
| - (RDFS.subPropertyOf, OWL.topObjectProperty), |
49 |
| - (RDFS.domain, OWL.Thing), |
| 48 | + (RDFS.domain, BAM.ObjectType), |
| 49 | + (RDFS.range, BAM.PropertyType), |
50 | 50 | (SKOS.prefLabel, Literal("hasOptionalProperty", lang="en")),
|
51 | 51 | ],
|
| 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 | + ], |
52 | 58 | }
|
53 | 59 | 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 |
56 | 62 |
|
57 | 63 | # Adding base entity types objects
|
58 | 64 | for entity in ["PropertyType", "ObjectType", "CollectionType", "DatasetType"]:
|
59 | 65 | entity_uri = BAM[entity]
|
60 | 66 | g.add((entity_uri, RDF.type, OWL.Class))
|
61 | 67 | g.add((entity_uri, SKOS.prefLabel, Literal(entity, lang="en")))
|
62 | 68 |
|
63 |
| - return g |
64 |
| - |
65 | 69 |
|
66 | 70 | def entities_to_rdf(
|
67 | 71 | graph: "Graph", module_path: str, logger: "BoundLoggerLazyProxy"
|
@@ -104,6 +108,19 @@ def entities_to_rdf(
|
104 | 108 | )
|
105 | 109 | graph.add((prop_uri, SKOS.altLabel, Literal(obj.property_label, lang="en")))
|
106 | 110 | 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)) |
107 | 124 | return None
|
108 | 125 |
|
109 | 126 | # All other datamodel modules
|
|
0 commit comments