Commit daff4f51 authored by Élodie Thiéblin's avatar Élodie Thiéblin
Browse files

fix: retrieve property domain/range from parent properties if exist

needed for skos
parent 5da386066a22
......@@ -61,6 +61,57 @@
return f"{prefix}{fragment}"
def property_range_domain_uris(owl_model, property_uri, domain=True):
domain_range_property = "rdfs:domain"
if not domain:
domain_range_property = "rdfs:range"
query_domains = f"""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT DISTINCT ?uri WHERE {{
{{<{property_uri}> {domain_range_property} ?uri.}}
UNION
{{<{property_uri}> {domain_range_property} ?z.
?z owl:unionOf ?uri.}}
}}
"""
domain_uris = []
# first, check if the property has its own domain defined
for domain_res in owl_model.query(query_domains):
domain_uri = domain_res.uri
if isinstance(domain_uri, BNode):
continue
domain_uris.append(domain_uri)
# if the property has its own domain/range defined
# it is more specific than its parents so return it
if len(domain_uris) > 0:
return domain_uris
# if it has no specification, perhaps its parents do
query_parent_domains = f"""
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT DISTINCT ?uri WHERE {{
<{property_uri}> rdfs:subPropertyOf+ ?prop.
{{?prop {domain_range_property} ?uri.}}
UNION
{{?prop {domain_range_property} ?z.
?z owl:unionOf ?uri.}}
}}
"""
for parent_domain_res in owl_model.query(query_parent_domains):
domain_uri = parent_domain_res.uri
if isinstance(domain_uri, BNode):
continue
domain_uris.append(domain_uri)
return domain_uris
def yams_domain_from_urirefs(
uris: Iterable[URIRef], all_entity_types: Iterable[str]
) -> List[str]:
......@@ -129,8 +180,8 @@
if superior_classes:
if len(superior_classes) > 1:
print(
f"Warning: multi-heritage is not support yet. {class_fragment} will specialize ",
f"{superior_classes[0]} and {', '.join(superior_classes[1::])} ",
f"Warning: multi-heritage is not support yet. {class_fragment} will specialize",
f"{superior_classes[0]} and {', '.join(superior_classes[1::])}",
"specialization will be ignored",
)
entity_schema._specialized_type = superior_classes[0]
......@@ -166,4 +217,5 @@
(None, None, RDFS.Literal),
)
yams_type = LITERAL_TYPES_TO_YAMS_TYPES[literal_type]
domain_fragments = yams_domain_from_urirefs(
......@@ -169,10 +221,5 @@
domain_fragments = yams_domain_from_urirefs(
(
domain_uri
for _, _, domain_uri in owl_model.triples(
(datatype_property_uri, RDFS.domain, None)
)
),
property_range_domain_uris(owl_model, datatype_property_uri, domain=True),
entitytype_fragment_to_uri.keys(),
)
for domain_fragment in domain_fragments:
......@@ -207,4 +254,5 @@
schema.add_relation_type(RelationType(object_property_uri_fragment))
relationtype_fragment_to_uri[object_property_uri_fragment] = object_property_uri
all_types = list(entitytype_fragment_to_uri.keys())
domain_fragments = yams_domain_from_urirefs(
......@@ -210,9 +258,4 @@
domain_fragments = yams_domain_from_urirefs(
(
domain_uri
for _, _, domain_uri in owl_model.triples(
(object_property_uri, RDFS.domain, None)
)
),
property_range_domain_uris(owl_model, object_property_uri, domain=True),
all_types,
)
......@@ -217,3 +260,4 @@
all_types,
)
range_fragments = yams_domain_from_urirefs(
......@@ -219,9 +263,4 @@
range_fragments = yams_domain_from_urirefs(
(
range_uri
for _, _, range_uri in owl_model.triples(
(object_property_uri, RDFS.range, None)
)
),
property_range_domain_uris(owl_model, object_property_uri, domain=False),
all_types,
)
......@@ -226,5 +265,6 @@
all_types,
)
for domain_fragment, range_fragment in itertools.product(
domain_fragments, range_fragments
):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment