Skip to content

Commit

Permalink
Fix all type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
multimeric committed Dec 18, 2024
1 parent 517f475 commit c512723
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions schema_automator/importers/rdfs_import_engine.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Dict, Iterable, List, Any, Optional
from typing import Dict, Iterable, List, Any
import typing
from collections import defaultdict

Expand All @@ -9,17 +9,15 @@
SchemaDefinition,
SlotDefinition,
ClassDefinition,
Prefix
)
# from funowl.converters.functional_converter import to_python
# from funowl import *

from dataclasses import dataclass, field

from linkml_runtime.utils.formatutils import underscore
from linkml_runtime.utils.introspection import package_schemaview
from rdflib import Graph, RDF, OWL, URIRef, RDFS, SKOS, SDO, Namespace, Literal
from schema_automator.importers.import_engine import ImportEngine
from schema_automator.utils.schemautils import write_schema


HTTP_SDO = Namespace("http://schema.org/")
Expand Down Expand Up @@ -124,27 +122,48 @@ def convert(
if k == "schema" and v != "http://schema.org/":
continue
sb.add_prefix(k, v, replace_if_present=True)
if default_prefix is not None and schema.prefixes is not None :
if default_prefix is not None and isinstance(schema.prefixes, dict):
schema.default_prefix = default_prefix
if model_uri is not None and default_prefix not in schema.prefixes:
sb.add_prefix(default_prefix, model_uri, replace_if_present=True)
schema.id = schema.prefixes[default_prefix].prefix_reference
prefix = schema.prefixes[default_prefix]
if isinstance(prefix, Prefix):
schema.id = prefix.prefix_reference
cls_slots = defaultdict(list)

for slot in self.generate_rdfs_properties(g, cls_slots):
sb.add_slot(slot)
for cls in self.process_rdfs_classes(g, cls_slots):
sb.add_class(cls)

if identifier is not None:
if identifier is not None and isinstance(schema.slots, dict) and isinstance(schema.classes, dict):
id_slot = SlotDefinition(identifier, identifier=True, range="uriorcurie")
schema.slots[identifier] = id_slot
for c in schema.classes.values():
if not c.is_a and not c.mixins:
if identifier not in c.slots:
c.slots.append(identifier)
if isinstance(c, ClassDefinition) and isinstance(c.slots, list):
if not c.is_a and not c.mixins:
if identifier not in c.slots:
c.slots.append(identifier)
self.fix_missing(schema)
return schema

def fix_missing(self, schema: SchemaDefinition) -> None:
"""
For some properties we have a `subproperty_of` that references a slot that doesn't exist.
This removes such links.
For example with `schema:name`, we have a `subPropertyOf` that references `rdfs:label`, which is from
the RDFS metamodel that we don't currently import.
"""
if not isinstance(schema.slots, dict):
raise ValueError("SchemaBuilder schema must have slots as a dict")
slot_names: set[str] = set(schema.slots.keys())
for slot in schema.slots.values():
if not isinstance(slot, SlotDefinition):
raise ValueError(f"Slot {slot} is not a SlotDefinition")
if slot.subproperty_of is not None and slot.subproperty_of not in slot_names:
logging.warning(f"Slot {slot.name} has subproperty_of {slot.subproperty_of}, but that slot is missing")
slot.subproperty_of = None

def process_rdfs_classes(
self,
g: Graph,
Expand Down

0 comments on commit c512723

Please sign in to comment.