def model_to_entity(cls, onto: Ontology, subject: models.Model) -> OwlIndividual:
     indiv = OwlIndividual(name_instance(subject, onto.base_prefix))
     model_type: typing.Type[models.Model] = subject.__class__
     for rel in model_type._meta.get_fields():
         rel: typing.Union[ManyToOneRel, ManyToManyRel, AutoField]
         # if subject.__class__.__name__ == 'Genre':
         #     pprint([f.name for f in model_type._meta.get_fields() if not f.auto_created])
         if rel.name not in field_of(model_type):
             continue
         # if isinstance(rel, ManyToManyRel):
         #     raise AssertionError()
         # if isinstance(rel, ManyToOneRel):
         #     if rel.related_name is None:
         #         continue
         # SubGenre
         # pprint(field_of(model_type))
         if isinstance(rel, Field):
             field = rel.name
         else:
             field = rel.field.name
         if '.' in field:
             field = field.split('.')[-1]
         if rel.name in cls._exclude_ or '_ptr' in rel.name:
             continue
         if rel.name == 'id':
             continue
         relation_name = rel.name
         if not hasattr(subject, relation_name):
             continue
         s_object = getattr(subject, relation_name)
         if not s_object:
             continue
         # if rel.auto_created or object is None or object == '':
         #     continue
         if relation_name in ('label', 'comment'):
             predicate_name = absolutize_entity_name(relation_name, 'rdfs')
         else:
             predicate_name = absolutize_entity_name(relation_name, onto.base_prefix)
         if type(s_object).__name__ in ('ManyRelatedManager',):
             continue
         if type(s_object).__name__ in IGNORED_FIELD_PY_TYPES:
             # s_object = get_as_done(s_object)
             for element in list(s_object.all()):
                 assign_something(onto, subject, predicate_name, element, indiv)
         assign_something(onto, subject, predicate_name, s_object, indiv)
     for included_field, owl_field_name in cls._include_:
         s_object = getattr(subject, included_field)
         predicate_name = included_field
         assign_something(onto, subject, predicate_name, s_object, indiv)
         # if isinstance(object, models.Model):
         #     instance_name = f'{object.__class__.__name__}_{object.id}'
         #     assignee = absolutize_entity_name(instance_name, onto.base_prefix)
         # else:
         #     assignee = object  # str, int, built-ins
         # instance_of_class: OwlClass = onto.get_entity(subject.__class__.__name__)
         # indiv.be_type_of(instance_of_class)
         # indiv.add_property_assertion(predicate_name, assignee)
     assert not isinstance(indiv, str)
     return indiv
    def get_entity(self, entity_name: str, prefix: str = None) -> Union[OwlClass, OwlEntity, str, None]:
        """Gets an Entity with a given name

        Args:
            entity_name: The given name of an Entity
            prefix: A fallback prefix

        Returns:
            An OwlEntity with the given name. Can be an OwlClass or OwlProperty
        """
        if entity_name is None:
            return None
        if prefix is None:
            prefix = self.prefix
        modified_name = absolutize_entity_name(entity_name, prefix)
        if modified_name == OWL_THING or modified_name == f"{prefix}:Thing":
            return None
        try:
            if modified_name in self._missing_entities:
                self._missing_entities.remove(modified_name)
            return self.entities[modified_name]
        except KeyError:
            import re
            if not re.match(r'(.+) ([A-Za-z]+) (.+)', modified_name):
                self._missing_entities.add(modified_name)
            return modified_name
def assign_something(onto: Ontology, subject, predicate_name: str, s_object, indiv: OwlIndividual):
    print(f'{subject} {predicate_name} {s_object}.')
    if isinstance(s_object, models.Model):
        instance_name = f'{s_object.__class__.__name__}_{s_object.id}'
        assignee = absolutize_entity_name(instance_name, onto.base_prefix)
    else:
        assignee = s_object  # str, int, built-ins
    instance_of_class: OwlClass = onto.get_entity(subject.__class__.__name__)
    indiv.be_type_of(instance_of_class)
    indiv.add_property_assertion(predicate_name, assignee)
    def load_from_spec(cls, spec_filename: str) -> 'OntogenConverter':
        """Creates an abstract Ontology from a specs file with the given filename

        Args:
            spec_filename: The filename of a specs file in YAML
        """
        self = cls()
        with open(spec_filename) as f:
            self._dct = yaml.load(f, Loader=yaml.Loader)
            root = self._dct
        self._check_eligible_version(root)
        self._deal_with_iris(root)
        temp_classes = []

        for base in BASE_ENTITIES:
            cls = base
            q = base.get_owl_type_entity_name()
            if q not in root:
                continue
            classes = root[q]
            for class_entity_name in classes:
                if class_entity_name == OWL_THING:
                    continue
                entity_name = absolutize_entity_name(class_entity_name, self.prefix)
                obj = cls(entity_name)
                sub = classes[class_entity_name]
                if isinstance(sub, dict):
                    obj.from_dict(sub)
                if base == OwlClass:
                    obj._internal_dict = sub
                    for prop in PROPERTY_ENTITIES:
                        if prop not in sub:
                            continue
                        prop_class = sub[prop]
                        for prop_name in prop_class:
                            prop_qualifier = absolutize_entity_name(prop_name, self.prefix)
                            obj.defined_properties[prop_qualifier] = self.get_entity(prop_name)
                    temp_classes.append(obj)
                self._ontology.add_entity(obj)
        self._add_individuals(root)
        self._ontology.from_dict(root)
        self._load_class_descriptions(tuple(self.entities.values()))
        return self
 def _from_internals_to_dict(self) -> dict:
     onto = self._ontology
     classes: List[Thing] = list(onto.implementation.classes())
     props: List[Thing] = list(onto.implementation.properties())
     individuals: List[Thing] = list(onto.implementation.individuals())
     all_three = classes + props + individuals
     for cls in classes:
         p = onto.lookup_prefix(cls.namespace.base_iri)
         e = absolutize_entity_name(cls.name, p)
         c = OwlClass(e)
         for p in Thing.get_properties(cls):
             if isinstance(p, AnnotationPropertyClass):
                 c.retrieve_property(p.name, cls, self._ontology.lookup_prefix(p.namespace.base_iri))
         c.parent_class_names = [absolutize_entity_name(s.name, self.ontology.base_prefix) for s in cls.is_a]
         self.entities[e] = c
     for prop in props:
         p = onto.lookup_prefix(prop.namespace.base_iri)
         e = absolutize_entity_name(prop.name, p)
         if isinstance(prop, DataPropertyClass):
             dp = OwlDataProperty(e)
             self.entities[e] = dp
             dp.parent_class_names = [absolutize_entity_name(s.name, self.ontology.base_prefix) for s in prop.is_a]
         elif isinstance(prop, ObjectPropertyClass):
             op = OwlObjectProperty(e)
             self.entities[e] = op
             op.parent_class_names = [absolutize_entity_name(s.name, self.ontology.base_prefix) for s in prop.is_a]
     for individual in individuals:
         p = onto.lookup_prefix(individual.namespace.base_iri)
         e = absolutize_entity_name(individual.name, p)
         i = OwlIndividual(e)
         for x in dir(individual):
             p = getattr(individual, x)
             if isinstance(p, ClassValueList) or isinstance(p, IndividualValueList):
                 e = absolutize_entity_name(x, onto.base_prefix)
                 for a in p:
                     i.add_property_assertion(e, str(a))
         for inst_of in individual.is_instance_of:
             p = onto.lookup_prefix(inst_of.namespace.base_iri)
             e2 = absolutize_entity_name(inst_of.name, p)
             i.be_type_of(self.get_entity(e2))
         self.entities[e] = i
     dct = {}
     assign_optional_dct(dct, OWL_CLASS, {abs_name: self.ontology.classes[abs_name].to_dict() for abs_name in self.ontology.classes})
     assign_optional_dct(dct, OWL_OBJECT_PROPERTY, {abs_name: self.ontology.object_properties[abs_name].to_dict()
                                                    for abs_name in self.ontology.object_properties})
     assign_optional_dct(dct, OWL_DATA_PROPERTY, {abs_name: self.ontology.data_properties[abs_name].to_dict()
                                                  for abs_name in self.ontology.data_properties})
     assign_optional_dct(dct, OWL_INDIVIDUAL, {abs_name: self.ontology.individuals[abs_name].to_dict()
                                               for abs_name in self.individuals})
     return dct
 def _add_individuals(self, base_dict: dict):
     individuals = base_dict.get(OWL_INDIVIDUAL, {})
     for individual in individuals:
         ind = OwlIndividual(individual)
         for t in individuals[individual]:
             values = individuals[individual][t]
             if t == RDF_TYPE:
                 for value in values:
                     entity = self.get_entity(value, self.prefix)
                     ind.be_type_of(entity)
             elif t == "relations":
                 for key, values in values.items():
                     for val in values:
                         ind.add_property_assertion(absolutize_entity_name(key, self.prefix), val)
         self.individuals[individual] = ind
         self._ontology.add_entity(ind)
 def retrieve_property(self, builtin_name: str, obj, prefix):
     val = getattr(obj, builtin_name)
     self.add_property_assertion(
         absolutize_entity_name(builtin_name, prefix), val)
 def add_entity(self, entity: OwlEntity):
     self.entities[absolutize_entity_name(entity.name)] = entity