Пример #1
0
  def __new__(MetaClass, name, superclasses, obj_dict):
    namespace = obj_dict.get("namespace") or (CURRENT_NAMESPACES.get() and CURRENT_NAMESPACES.get()[-1]) or superclasses[0].namespace
    storid    = obj_dict.get("storid")    or namespace.world._abbreviate("%s%s" % (namespace.base_iri, name))
    
    if "is_a" in obj_dict:
      _is_a = [*superclasses, *obj_dict["is_a"]]
      superclasses = MetaClass._find_base_classes(_is_a) or (Thing,)
    else:
      if len(superclasses) > 1:
        _is_a = superclasses = MetaClass._find_base_classes(superclasses) or (Thing,)
      else:
        _is_a = superclasses
        
    if LOADING:
      Class = namespace.world._entities.get (storid)
    else:
      Class = namespace.world._get_by_storid(storid)
      
    equivalent_to = obj_dict.pop("equivalent_to", None)
    
    if Class is None:
      if not LOADING:
        for base in _is_a:
          if isinstance(base, Construct):
            base = base._set_ontology_copy_if_needed(namespace.ontology, _is_a)
              
      _is_a = CallbackList(_is_a, None, MetaClass._class_is_a_changed)
      obj_dict.update(
        _name          = name,
        namespace      = namespace,
        storid         = storid,
        is_a           = _is_a,
        _equivalent_to = None,
      )

      Class = namespace.world._entities[storid] = _is_a._obj = type.__new__(MetaClass, name, superclasses, obj_dict)
      _cache_entity(Class)
      
      if not LOADING:
        namespace.ontology._add_obj_triple_spo(storid, rdf_type, MetaClass._owl_type)
        for parent in _is_a: Class._add_is_a_triple(parent)
        
    else:
      if not MetaClass is Class.__class__: Class.__class__ = MetaClass
      if (Class.is_a != _is_a) and (_is_a != (Thing,)):
        Class.is_a.extend([i for i in _is_a if not i in Class.is_a])
        
    if equivalent_to:
      if isinstance(equivalent_to, list): Class.equivalent_to.extend(equivalent_to)
      
    return Class
Пример #2
0
    def __new__(Class, name=None, namespace=None, **kargs):
        if name:
            if isinstance(name, Thing):
                namespace = name.namespace
                name = name.name
            else:
                namespace = namespace or (
                    CURRENT_NAMESPACES.get()
                    and CURRENT_NAMESPACES.get()[-1]) or Class.namespace
            if LOADING or (name == ""):
                already_existing = None
            else:
                already_existing = namespace.world["%s%s" %
                                                   (namespace.base_iri, name)]

            if not already_existing is None:
                if not isinstance(already_existing, Class):
                    if isinstance(Class, FusionClass):
                        Classes = Class.__bases__
                    else:
                        Classes = (Class, )
                    for C in Classes:
                        if not C in already_existing.is_a:
                            already_existing.is_a._append(C)
                            if not LOADING:
                                already_existing.namespace.ontology._add_obj_triple_spo(
                                    already_existing.storid, rdf_type,
                                    C.storid)

                bases = ThingClass._find_base_classes(already_existing.is_a)
                if len(bases) == 1:
                    already_existing.__class__ = bases[0]
                else:
                    already_existing.__class__ = FusionClass._get_fusion_class(
                        bases)

                if not LOADING:
                    for attr, value in kargs.items():
                        setattr(already_existing, attr, value)

                return already_existing

        #_cache_entity(entity)
        #return object.__new__(Class)
        #print(namespace, kargs)
        return _cache_entity(object.__new__(Class))