Commit 505c8f18 authored by Fabien Amarger's avatar Fabien Amarger
Browse files

feat: Generate entitytype fragment to uri and same for relationtype

parent ef9c11eca047
import subprocess
import re
import os
from typing import Iterable, Optional, List
from typing import Iterable, Optional, List, Dict
from argparse import ArgumentParser
import itertools
......@@ -53,7 +53,8 @@
def owl_model_to_yams(owl_model: Graph, instance_name: str) -> Schema:
ETYPE_URI = {}
entitytype_fragment_to_uri: Dict[str, str] = {}
relationtype_fragment_to_uri: Dict[str, str] = {}
# 0. Create a new YAMS Schema
schema = Schema(instance_name)
register_base_types(schema)
......@@ -69,7 +70,8 @@
continue
if class_fragment in schema:
raise ValueError(
f"Error: class {class_uri} and {ETYPE_URI[class_fragment]} use the same fragment"
f"Error: class {class_uri} and {entitytype_fragment_to_uri[class_fragment]} "
"use the same fragment"
)
schema.add_entity_type(EntityType(class_fragment))
entity_schema = schema._entities[class_fragment]
......@@ -73,7 +75,7 @@
)
schema.add_entity_type(EntityType(class_fragment))
entity_schema = schema._entities[class_fragment]
ETYPE_URI[class_fragment] = class_uri
entitytype_fragment_to_uri[class_fragment] = class_uri
superior_classes = []
for _, _, superior_class_uri in owl_model.triples(
(class_uri, RDFS.subClassOf, None)
......@@ -97,6 +99,7 @@
continue
if datatype_property_uri_fragment in schema:
raise ValueError(
f"Error: attribute {datatype_property_uri} already defined"
f"Error: attribute {datatype_property_uri} already defined "
f"({relationtype_fragment_to_uri[datatype_property_uri_fragment]})"
)
schema.add_relation_type(RelationType(datatype_property_uri_fragment))
......@@ -101,5 +104,8 @@
)
schema.add_relation_type(RelationType(datatype_property_uri_fragment))
relationtype_fragment_to_uri[
datatype_property_uri_fragment
] = datatype_property_uri
# take first range, if no range use RDFS.Literal
_, _, literal_type = next(
owl_model.triples((datatype_property_uri, RDFS.range, None)),
......@@ -113,7 +119,7 @@
(datatype_property_uri, RDFS.domain, None)
)
),
ETYPE_URI.keys(),
entitytype_fragment_to_uri.keys(),
)
for domain_fragment in domain_fragments:
schema.add_relation_def(
......@@ -134,5 +140,8 @@
print(f"Warning: object property {object_property_uri} could not be parsed")
continue
if object_property_uri_fragment in schema:
raise ValueError(f"Error: relation {object_property_uri} already defined")
raise ValueError(
f"Error: relation {object_property_uri} already defined "
f"({relationtype_fragment_to_uri[object_property_uri_fragment]})"
)
schema.add_relation_type(RelationType(object_property_uri_fragment))
......@@ -138,5 +147,6 @@
schema.add_relation_type(RelationType(object_property_uri_fragment))
all_types = list(ETYPE_URI.keys())
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(
(
domain_uri
......
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