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

fix: fix prefix and add annotation properties

parent c7e39927902e
......@@ -10,6 +10,7 @@
import itertools
from rdflib import Graph, RDF, RDFS, OWL, XSD, URIRef # type: ignore
from urllib.parse import urlparse
from rdflib.term import BNode
from yams.schema import Schema # type: ignore
from yams.serialize import serialize_to_python # type: ignore
from yams.buildobjs import ( # type: ignore
......@@ -32,7 +33,7 @@
XSD.float: "Float",
}
with open(f"{here}/prefixes.json","r") as fobj:
with open(f"{here}/prefixes.json", "r") as fobj:
PREFIXES = json.load(fobj)
......@@ -55,6 +56,8 @@
prefix = url.hostname.replace("www.", "").replace(".", "_") + "_"
if titling:
prefix = prefix.title().replace("_", "")
else:
fragment = fragment.lower()
return f"{prefix}{fragment}"
......@@ -63,6 +66,8 @@
) -> List[str]:
fragments: List[str] = []
for uri in uris:
if isinstance(uri, BNode):
continue
fragment = fragment_from_uri(uri, titling=True)
if fragment is not None:
fragments.append(fragment)
......@@ -85,4 +90,14 @@
# p = s._entities["Person"] # normalement p = s.entity_schema_for("Person")
# p.check({'name':1})
class_query = """
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?uri WHERE {
{?uri a owl:Class}
UNION {?rangeprop rdfs:range ?uri.}
UNION {?domainprop rdfs:domain ?uri.}
}
"""
# 1. fetch all classes
......@@ -88,5 +103,8 @@
# 1. fetch all classes
for class_uri, _, _ in owl_model.triples((None, RDF.type, OWL.Class)):
for class_res in owl_model.query(class_query):
class_uri = class_res.uri
if isinstance(class_uri, BNode):
continue
class_fragment = fragment_from_uri(class_uri, titling=True)
if class_fragment is None:
print(f"Warning: class {class_uri} could not be parsed")
......@@ -113,11 +131,16 @@
# 2. fetch all datatype properties
for (
datatype_property_uri,
_,
_,
) in owl_model.triples((None, RDF.type, OWL.DatatypeProperty)):
datatype_property_query = """
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?uri WHERE {
{?uri a owl:DatatypeProperty}
UNION {?uri a owl:AnnotationProperty.}
}
"""
for datatype_property_res in owl_model.query(datatype_property_query):
datatype_property_uri = datatype_property_res.uri
datatype_property_uri_fragment = fragment_from_uri(datatype_property_uri)
if datatype_property_uri_fragment is None:
print(f"Warning: data property {datatype_property_uri} could not be parsed")
......@@ -147,6 +170,11 @@
entitytype_fragment_to_uri.keys(),
)
for domain_fragment in domain_fragments:
# if the parent of the domain is already in domain_fragments
domain_parent = schema[domain_fragment].specializes()
if domain_parent is not None and domain_parent.type in domain_fragments:
continue
schema.add_relation_def(
RelationDefinition(
domain_fragment, datatype_property_uri_fragment, yams_type
......
......@@ -19,6 +19,7 @@
"rdflib": ">=5.0.0, <6.0.0",
"Jinja2": None,
"black": None,
"requests": None,
}
install_requires = ["{0} {1}".format(d, v or "").strip() for d, v in requires.items()]
......
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