Beispiel #1
0
class _Nexus(object):
    """
    Internal to Actuator
    
    A nexus is where, for a given instance of a system, models and their
    instances can be recorded and then looked up by other components. In
    particular, model instances can be looked up by either the class of the
    instance, or by a base class of the instance. This way, one model instance can
    find another instance by way of the instance class or base class. This can
    be used when processing reference selection expressions or context
    expressions.
    """
    def __init__(self):
        self.mapper = ClassMapper()
        
    def capture_model_to_instance(self, model_class, instance):
        """
        Associate a model class to an instance of that class. Actually causes a series
        of associations to be created, following the inheritance chain for the
        class and mapping each base class to the instance, as we can't be sure
        which class will be used to try to locate the instance, the specific one
        or a more general one. Mapping terminates when ModelBase is encountered
        in the mro.
        """
        self.mapper[model_class] = instance
        for base in model_class.__mro__:
            if base in [ModelBase, _NexusMember]:
                break
            if issubclass(base, (ModelBase, _NexusMember)):
                self.mapper[base] = instance
        
    def find_instance(self, model_class):
        return self.mapper.get(model_class)
    
    def merge_from(self, other_nexus):
        self.mapper.update(other_nexus.mapper)
Beispiel #2
0
 def __init__(self):
     self.mapper = ClassMapper()