def endvisit_neuron(self, node): # before following checks occur, we need to ensure several simple properties CoCosManager.post_symbol_table_builder_checks( node, after_ast_rewrite=self.after_ast_rewrite_) # the following part is done in order to mark conductance based buffers as such. if node.get_input_blocks() is not None and node.get_equations_blocks() is not None and \ len(node.get_equations_blocks().get_declarations()) > 0: # this case should be prevented, since several input blocks result in a incorrect model if isinstance(node.get_input_blocks(), list): buffers = (buffer for bufferA in node.get_input_blocks() for buffer in bufferA.get_input_ports()) else: buffers = ( buffer for buffer in node.get_input_blocks().get_input_ports()) from pynestml.meta_model.ast_kernel import ASTKernel # todo: ode declarations are not used, is this correct? # ode_declarations = (decl for decl in node.get_equations_blocks().get_declarations() if # not isinstance(decl, ASTKernel)) # now update the equations if node.get_equations_blocks() is not None and len( node.get_equations_blocks().get_declarations()) > 0: equation_block = node.get_equations_blocks() assign_ode_to_variables(equation_block) if not self.after_ast_rewrite_: CoCosManager.post_ode_specification_checks(node) Logger.set_current_node(None) return
def visitSynapse(self, ctx): from pynestml.generated.PyNestMLLexer import PyNestMLLexer name = str(ctx.NAME()) if ctx.NAME() is not None else None body = self.visit( ctx.synapseBody()) if ctx.synapseBody() is not None else None # after we have constructed the meta_model of the neuron, # we can ensure some basic properties which should always hold # we have to check if each type of block is defined at most once (except for function), and that input,output # and update are defined once if hasattr(ctx.start.source[1], 'fileName'): artifact_name = ntpath.basename(ctx.start.source[1].fileName) else: artifact_name = 'parsed from string' synapse = ASTNodeFactory.create_ast_synapse( name=name + FrontendConfiguration.suffix, body=body, source_position=create_source_pos(ctx), artifact_name=artifact_name) # update the comments update_node_comments(synapse, self.__comments.visit(ctx)) # in order to enable the logger to print correct messages set as the source the corresponding neuron Logger.set_current_node(synapse) CoCoEachSynapseBlockUniqueAndDefined.check_co_co(node=synapse) # now the meta_model seems to be correct, return it Logger.set_current_node(synapse) return synapse
def visit_neuron(self, node): """ Private method: Used to visit a single neuron and create the corresponding global as well as local scopes. :return: a single neuron. :rtype: ast_neuron """ # set current processed neuron Logger.set_current_node(node) code, message = Messages.get_start_building_symbol_table() Logger.log_message(node=node, code=code, error_position=node.get_source_position(), message=message, log_level=LoggingLevel.INFO) scope = Scope(scope_type=ScopeType.GLOBAL, source_position=node.get_source_position()) node.update_scope(scope) node.get_body().update_scope(scope) # now first, we add all predefined elements to the scope variables = PredefinedVariables.get_variables() functions = PredefinedFunctions.get_function_symbols() types = PredefinedTypes.get_types() for symbol in variables.keys(): node.get_scope().add_symbol(variables[symbol]) for symbol in functions.keys(): node.get_scope().add_symbol(functions[symbol]) for symbol in types.keys(): node.get_scope().add_symbol(types[symbol])
def visit_synapse(self, node): """ Private method: Used to visit a single synapse and create the corresponding global as well as local scopes. :return: a single synapse. :rtype: ast_synapse """ # set current processed synapse # Logger.set_current_synapse(node) Logger.set_current_node(node) code, message = Messages.get_start_building_symbol_table() Logger.log_message(node=node, code=code, error_position=node.get_source_position(), message=message, log_level=LoggingLevel.INFO) # before starting the work on the synapse, make everything which was implicit explicit # but if we have a model without an equations block, just skip this step scope = Scope(scope_type=ScopeType.GLOBAL, source_position=node.get_source_position()) node.update_scope(scope) node.get_body().update_scope(scope) # now first, we add all predefined elements to the scope variables = PredefinedVariables.get_variables() functions = PredefinedFunctions.get_function_symbols() types = PredefinedTypes.get_types() for symbol in variables.keys(): node.get_scope().add_symbol(variables[symbol]) for symbol in functions.keys(): node.get_scope().add_symbol(functions[symbol]) for symbol in types.keys(): node.get_scope().add_symbol(types[symbol])
def endvisit_neuron(self, node): # before following checks occur, we need to ensure several simple properties CoCosManager.post_symbol_table_builder_checks(node, after_ast_rewrite=self.after_ast_rewrite_) # update the equations if node.get_equations_blocks() is not None and len(node.get_equations_blocks().get_declarations()) > 0: equation_block = node.get_equations_blocks() assign_ode_to_variables(equation_block) Logger.set_current_node(None)
def test(self): Logger.init_logger(LoggingLevel.INFO) model = ModelParser.parse_model( os.path.join( os.path.realpath( os.path.join(os.path.dirname(__file__), 'resources', 'ExpressionTypeTest.nestml')))) Logger.set_current_node(model.get_neuron_list()[0]) model.accept(ExpressionTestVisitor()) # ExpressionTestVisitor().handle(model) Logger.set_current_node(None) self.assertEqual( len( Logger.get_all_messages_of_level_and_or_node( model.get_neuron_list()[0], LoggingLevel.ERROR)), 2)
def visitSynapse(self, ctx): from pynestml.generated.PyNestMLLexer import PyNestMLLexer name = str(ctx.NAME()) if ctx.NAME() is not None else None body = self.visit(ctx.synapseBody()) if ctx.synapseBody() is not None else None # after we have constructed the meta_model of the neuron, # we can ensure some basic properties which should always hold # we have to check if each type of block is defined at most once (except for function), and that input,output # and update are defined once if hasattr(ctx.start.source[1], 'fileName'): artifact_name = ntpath.basename(ctx.start.source[1].fileName) else: artifact_name = 'parsed from string' synapse = ASTNodeFactory.create_ast_synapse(name=name + FrontendConfiguration.suffix, body=body, source_position=create_source_pos(ctx), artifact_name=artifact_name) # # find the @heterogeneous/@homogeneous magic keyword """for parameter_block in body.get_parameter_blocks(): for i, astDeclaration in enumerate(parameter_block.declarations): assert not (PyNestMLLexer.MAGIC_KEYWORD_HOMOGENEOUS in astDeclaration.get_variables() \ and PyNestMLLexer.MAGIC_KEYWORD_HOMOGENEOUS in astDeclaration.get_variables()), PyNestMLLexer.MAGIC_KEYWORD_HETEROGENEOUS + " and " + PyNestMLLexer.MAGIC_KEYWORD_HOMOGENEOUS + " keywords cannot be combined" if PyNestMLLexer.MAGIC_KEYWORD_HETEROGENEOUS in astDeclaration.get_magic_keywords(): assert len(astDeclaration.get_variables()) == 1, PyNestMLLexer.MAGIC_KEYWORD_HETEROGENEOUS + " keyword can only apply to a single variable" # synapse.set_default_weight(astDeclaration.get_variables()[0]) synapse.set_parameter_heterogeneous(var=astDeclaration.get_variables()[0]) elif PyNestMLLexer.MAGIC_KEYWORD_HOMOGENEOUS in astDeclaration.get_magic_keywords(): assert len(astDeclaration.get_variables()) == 1, PyNestMLLexer.MAGIC_KEYWORD_HOMOGENEOUS + " keyword can only apply to a single variable" synapse.set_parameter_homogeneous(var=astDeclaration.get_variables()[0])""" # update the comments update_node_comments(synapse, self.__comments.visit(ctx)) # in order to enable the logger to print correct messages set as the source the corresponding neuron Logger.set_current_node(synapse) CoCoEachSynapseBlockUniqueAndDefined.check_co_co(node=synapse) # now the meta_model seems to be correct, return it Logger.set_current_node(synapse) return synapse
def endvisit_synapse(self, node): # before following checks occur, we need to ensure several simple properties CoCosManager.post_symbol_table_builder_checks(node) Logger.set_current_node(None)