Ejemplo n.º 1
0
class Library(Block):
    def accept_visitor(self, v, **kwargs):
        return v.VisitLibrary(self, **kwargs)

    def __init__(self, library_manager, builder, builddata, name):
        super(Library, self).__init__(library_manager=library_manager,
                                      builder=builder,
                                      name=name)
        import neurounits.ast as ast

        self._function_defs = LookUpDict(
            builddata.funcdefs,
            accepted_obj_types=(ast.FunctionDef, ast.BuiltInFunction))
        self._symbolicconstants = LookUpDict(
            builddata.symbolicconstants,
            accepted_obj_types=(ast.SymbolicConstant, ))
        self._eqn_assignment = LookUpDict(
            builddata.assignments,
            accepted_obj_types=(ast.EqnAssignmentByRegime, ))

    def get_terminal_obj(self, symbol):

        possible_objs = LookUpDict(self.assignedvalues).get_objs_by(symbol=symbol)+ \
                        LookUpDict(self.symbolicconstants).get_objs_by(symbol=symbol)+ \
                        LookUpDict(self.functiondefs).get_objs_by(funcname=symbol)

        if not len(possible_objs) == 1:
            raise KeyError("Can't find terminal: %s" % symbol)

        return possible_objs[0]

    @property
    def functiondefs(self):
        return self._function_defs

    @property
    def symbolicconstants(self):
        return sorted(self._symbolicconstants, key=lambda a: a.symbol)

    @property
    def assignments(self):
        return list(iter(self._eqn_assignment))

    @property
    def assignedvalues(self):
        return sorted(list(self._eqn_assignment.get_objects_attibutes('lhs')),
                      key=lambda a: a.symbol)
Ejemplo n.º 2
0
class Library(Block):

    def accept_visitor(self, v, **kwargs):
        return v.VisitLibrary(self, **kwargs)

    def __init__(self,  library_manager, builder, builddata,name):
        super(Library,self).__init__(library_manager=library_manager, builder=builder, name=name)
        import neurounits.ast as ast

        self._function_defs = LookUpDict( builddata.funcdefs, accepted_obj_types=(ast.FunctionDef, ast.BuiltInFunction) )
        self._symbolicconstants = LookUpDict( builddata.symbolicconstants, accepted_obj_types=(ast.SymbolicConstant, ) )
        self._eqn_assignment = LookUpDict( builddata.assignments, accepted_obj_types=(ast.EqnAssignmentByRegime,) )

    def get_terminal_obj(self, symbol):

        possible_objs = LookUpDict(self.assignedvalues).get_objs_by(symbol=symbol)+ \
                        LookUpDict(self.symbolicconstants).get_objs_by(symbol=symbol)+ \
                        LookUpDict(self.functiondefs).get_objs_by(funcname=symbol)





        if not len(possible_objs) == 1:
            raise KeyError("Can't find terminal: %s" % symbol)

        return possible_objs[0]


    @property
    def functiondefs(self):
        return self._function_defs

    @property
    def symbolicconstants(self):
        return sorted(self._symbolicconstants, key=lambda a: a.symbol)

    @property
    def assignments(self):
        return list( iter(self._eqn_assignment) )

    @property
    def assignedvalues(self):
        return sorted(list(self._eqn_assignment.get_objects_attibutes('lhs')), key=lambda a:a.symbol)
Ejemplo n.º 3
0
class ComponentNamespace(object):

    def is_root(self):
        return self.parent is None

    def __repr__(self):
        return '<Component namespace: %s>' % self.full_name

    def __init__(self, name, parent ):
        # name is local, not fully qualified:
        self.name = name
        self.parent = parent

        self.subnamespaces = LookUpDict(accepted_obj_types=ComponentNamespace)
        self.libraries = LookUpDict(accepted_obj_types=ast.Library)
        self.components = LookUpDict(accepted_obj_types=ast.NineMLComponent)
        self.interfaces = LookUpDict(accepted_obj_types=ast.Interface)

    def get_blocks(self,):
        return list( self.libraries) + list(self.components) + list(self.interfaces)

    @property
    def full_name(self,):
        if self.is_root():
            return ''
        elif self.parent.is_root():
            return self.name
        else:
            return self.parent.full_name + '.' + self.name

    def get_subnamespace(self, sub_namespace_name_tokens):
        if not self.subnamespaces.has_obj(name=sub_namespace_name_tokens[0]):
            sub_ns = ComponentNamespace(name=sub_namespace_name_tokens[0], parent=self)
            self.subnamespaces._add_item(sub_ns)
        ns = self.subnamespaces.get_single_obj_by(name=sub_namespace_name_tokens[0])
        if len(sub_namespace_name_tokens) == 1:
            return ns
        else:
            return ns.get_subnamespace(sub_namespace_name_tokens[1:])




        assert False

    def add(self, obj):
        obj_toks = obj.name.split('.')
        ns_toks = self.full_name.split('.')

        n_more_obj_tokens = len(obj_toks) - len(ns_toks)
        assert n_more_obj_tokens > 0 or self.is_root()
        assert len(obj_toks) >=0

        # Both '<root>' and 'std' will have a single token:

        if self.is_root():
            if len(obj_toks) == 1:
                self.add_here(obj)
            else:
                sub_ns = self.get_subnamespace(sub_namespace_name_tokens=obj_toks[:-1])
                sub_ns.add(obj)

        else:
            # Namespace A.B, object A.B.d  (insert locally)
            # Namespace A.B, object A.B.C.d (insert in subnamespace)
            if n_more_obj_tokens == 0:
                assert False
            elif n_more_obj_tokens == 1:
                self.add_here(obj)
            else:
                sub_ns = self.get_subnamespace(sub_namespace_name_tokens=obj_toks[len(ns_toks):-1])
                sub_ns.add(obj)

        return






    def add_here(self, obj):
        ns_toks = self.full_name.split('.')
        obj_toks = obj.name.split('.')
        assert len(obj_toks) == len(ns_toks) +1 or (self.is_root() and len(obj_toks) == 1)

        assert not obj.name in self.libraries.get_objects_attibutes(attr='name')
        assert not obj.name in self.components.get_objects_attibutes(attr='name')
        assert not obj.name in self.interfaces.get_objects_attibutes(attr='name')

        if isinstance(obj, ast.NineMLComponent):
            self.components._add_item(obj)
        if isinstance(obj, ast.Library):
            self.libraries._add_item(obj)
        if isinstance(obj, ast.Interface):
            self.interfaces._add_item(obj)


    def get_all(self, components=True, libraries=True, interfaces=True):

        objs =  []
        if components:
            objs.extend(self.components)
        if libraries:
            objs.extend(self.libraries)
        if interfaces:
            objs.extend(self.interfaces)

        for ns in self.subnamespaces:
            objs.extend( ns.get_all(components=components, libraries=libraries, interfaces=interfaces) )
        return objs
Ejemplo n.º 4
0
class ComponentNamespace(object):
    def is_root(self):
        return self.parent is None

    def __repr__(self):
        return '<Component namespace: %s>' % self.full_name

    def __init__(self, name, parent):
        # name is local, not fully qualified:
        self.name = name
        self.parent = parent

        self.subnamespaces = LookUpDict(accepted_obj_types=ComponentNamespace)
        self.libraries = LookUpDict(accepted_obj_types=ast.Library)
        self.components = LookUpDict(accepted_obj_types=ast.NineMLComponent)
        self.interfaces = LookUpDict(accepted_obj_types=ast.Interface)

    def get_blocks(self, ):
        return list(self.libraries) + list(self.components) + list(
            self.interfaces)

    @property
    def full_name(self, ):
        if self.is_root():
            return ''
        elif self.parent.is_root():
            return self.name
        else:
            return self.parent.full_name + '.' + self.name

    def get_subnamespace(self, sub_namespace_name_tokens):
        if not self.subnamespaces.has_obj(name=sub_namespace_name_tokens[0]):
            sub_ns = ComponentNamespace(name=sub_namespace_name_tokens[0],
                                        parent=self)
            self.subnamespaces._add_item(sub_ns)
        ns = self.subnamespaces.get_single_obj_by(
            name=sub_namespace_name_tokens[0])
        if len(sub_namespace_name_tokens) == 1:
            return ns
        else:
            return ns.get_subnamespace(sub_namespace_name_tokens[1:])

        assert False

    def add(self, obj):
        obj_toks = obj.name.split('.')
        ns_toks = self.full_name.split('.')

        n_more_obj_tokens = len(obj_toks) - len(ns_toks)
        assert n_more_obj_tokens > 0 or self.is_root()
        assert len(obj_toks) >= 0

        # Both '<root>' and 'std' will have a single token:

        if self.is_root():
            if len(obj_toks) == 1:
                self.add_here(obj)
            else:
                sub_ns = self.get_subnamespace(
                    sub_namespace_name_tokens=obj_toks[:-1])
                sub_ns.add(obj)

        else:
            # Namespace A.B, object A.B.d  (insert locally)
            # Namespace A.B, object A.B.C.d (insert in subnamespace)
            if n_more_obj_tokens == 0:
                assert False
            elif n_more_obj_tokens == 1:
                self.add_here(obj)
            else:
                sub_ns = self.get_subnamespace(
                    sub_namespace_name_tokens=obj_toks[len(ns_toks):-1])
                sub_ns.add(obj)

        return

    def add_here(self, obj):
        ns_toks = self.full_name.split('.')
        obj_toks = obj.name.split('.')
        assert len(obj_toks) == len(ns_toks) + 1 or (self.is_root()
                                                     and len(obj_toks) == 1)

        assert not obj.name in self.libraries.get_objects_attibutes(
            attr='name')
        assert not obj.name in self.components.get_objects_attibutes(
            attr='name')
        assert not obj.name in self.interfaces.get_objects_attibutes(
            attr='name')

        if isinstance(obj, ast.NineMLComponent):
            self.components._add_item(obj)
        if isinstance(obj, ast.Library):
            self.libraries._add_item(obj)
        if isinstance(obj, ast.Interface):
            self.interfaces._add_item(obj)

    def get_all(self, components=True, libraries=True, interfaces=True):

        objs = []
        if components:
            objs.extend(self.components)
        if libraries:
            objs.extend(self.libraries)
        if interfaces:
            objs.extend(self.interfaces)

        for ns in self.subnamespaces:
            objs.extend(
                ns.get_all(components=components,
                           libraries=libraries,
                           interfaces=interfaces))
        return objs