def actualize_assertions(self, inst):
     for set_prop in self.properties_with_values:
         v = self.properties_with_values[set_prop]
         val = self._prepare_assertion_value(set_prop, v)
         if isinstance(val, list):
             # set_prop in BUILTIN_NAMES and
             for i, v in enumerate(val):
                 if isinstance(v, str):
                     if len(v) > 3 and ("^^" in v or "@" in v):
                         try:
                             split_values = re.split(
                                 r'(?:(.+)\^\^(.+)@(.+)|(.+)\^\^(.+))', v)
                             if split_values[1] is None and split_values[
                                     3] is None:
                                 v, data_type = (split_values[4],
                                                 split_values[5])
                                 val[i] = DATATYPE_MAP[data_type](v)
                             elif len(split_values
                                      ) > 2 and split_values[1] is not None:
                                 lit, lang = (split_values[1],
                                              split_values[3])
                                 val[i] = locstr(lit, lang)
                             else:
                                 v, data_type = (split_values[4],
                                                 split_values[5])
                                 val[i] = DATATYPE_MAP[data_type](v)
                         except IndexError:
                             raise ValueError(f'No such found - {v}')
         if ":" in set_prop:
             set_prop = shorten_entity_name(set_prop)
         try:
             if isinstance(val, list):
                 setattr(inst, set_prop, val)
             else:
                 setattr(inst, set_prop, val)
         except AttributeError:
             pass
Ejemplo n.º 2
0
def pl(s):
    """Returns `s` as a plain literal string."""
    return owlready2.locstr(s, lang='')
Ejemplo n.º 3
0
def en(s):
    """Returns `s` as an English location string."""
    return owlready2.locstr(s, lang='en')
Ejemplo n.º 4
0
    def sync_attributes(self, name_policy=None, name_prefix='',
                        class_docstring='comment', sync_imported=False):
        """This method is intended to be called after you have added new
        classes (typically via Python) to make sure that attributes like
        `label` and `comments` are defined.

        If a class, object property, data property or annotation
        property in the current ontology has no label, the name of
        the corresponding Python class will be assigned as label.

        If a class, object property, data property or annotation
        property has no comment, it will be assigned the docstring of
        the corresponding Python class.

        `name_policy` specify wether and how the names in the ontology
        should be updated.  Valid values are:
          None          not changed
          "uuid"        `name_prefix` followed by a global unique id (UUID).
          "sequential"  `name_prefix` followed a sequantial number.
        EMMO conventions imply ``name_policy=='uuid'``.

        If `sync_imported` is true, all imported ontologies are also
        updated.

        The `class_docstring` argument specifies the annotation that
        class docstrings are mapped to.  Defaults to "comment".
        """
        for cls in itertools.chain(
                self.classes(), self.object_properties(),
                self.data_properties(), self.annotation_properties()):
            if not hasattr(cls, 'prefLabel'):
                # no prefLabel - create new annotation property..
                with self:
                    class prefLabel(owlready2.label):
                        pass
                cls.prefLabel = [locstr(cls.__name__, lang='en')]
            elif not cls.prefLabel:
                cls.prefLabel.append(locstr(cls.__name__, lang='en'))
            if class_docstring and hasattr(cls, '__doc__') and cls.__doc__:
                getattr(cls, class_docstring).append(
                    locstr(inspect.cleandoc(cls.__doc__), lang='en'))

        for ind in self.individuals():
            if not hasattr(ind, 'prefLabel'):
                # no prefLabel - create new annotation property..
                with self:
                    class prefLabel(owlready2.label):  # noqa: F811
                        pass
                ind.prefLabel = [locstr(ind.name, lang='en')]
            elif not ind.prefLabel:
                ind.prefLabel.append(locstr(ind.name, lang='en'))

        chain = itertools.chain(
            self.classes(), self.individuals(), self.object_properties(),
            self.data_properties(), self.annotation_properties())
        if name_policy == 'uuid':
            for obj in chain:
                obj.name = name_prefix + str(uuid.uuid5(uuid.NAMESPACE_DNS,
                                                        obj.name))
        elif name_policy == 'sequential':
            for obj in chain:
                n = 0
                while f'{self.base_iri}{name_prefix}{n}' in self:
                    n += 1
                obj.name = name_prefix + str(n)
        elif name_policy is not None:
            raise TypeError('invalid name_policy: %r' % (name_policy, ))

        if sync_imported:
            for onto in self.imported_ontologies:
                onto.sync_attributes()