Пример #1
0
    def build_runnable(self, component, parent = None, id_ = None):
        """
        Build a runnable component from a component specification and add
        it to the simulation.

        @param component: Component specification
        @type component: lems.model.component.FatComponent

        @param parent: Parent runnable component.
        @type parent: lems.sim.runnable.Runnable

        @param id_: Optional id for therunnable. If it's not passed in,
        the runnable will inherit the id of the component.

        @raise SimBuildError: Raised when a component reference cannot be
        resolved.
        """
        if self.debug: print("++++++++ Calling build_runnable of %s with parent %s"%(component, parent))

        if id_ == None:
            runnable = Runnable(component.id, component, parent)
        else:
            runnable = Runnable(id_, component, parent)

        simulation = component.simulation

        record_target_backup = self.current_record_target
        data_output_backup = self.current_data_output

        do = None
        for d in simulation.data_displays:
            do = d
        if do == None:
            for d in simulation.data_writers:
                do = d

        if do != None:
            self.current_data_output = do

        for parameter in component.parameters:
            runnable.add_instance_variable(parameter.name, parameter.numeric_value)

        derived_parameter_code = []
        
        derived_parameter_ordering = order_derived_parameters(component)
        
        for dpn in derived_parameter_ordering:
            derived_parameter = component.derived_parameters[dpn]
            runnable.add_derived_variable(derived_parameter.name)
            
            expression = self.build_expression_from_tree(runnable,
                                                        None,
                                                        derived_parameter.expression_tree)
            
            derived_parameter_code += ['self.{0} = ({1})'.format(
                        derived_parameter.name,
                        expression)]
            derived_parameter_code += ['self.{0}_shadow = ({1})'.format(
                        derived_parameter.name,
                        expression)]
       
        suffix = ''
        runnable.add_method('update_derived_parameters' + suffix, ['self'],
                            derived_parameter_code)
            
        for constant in component.constants:
            runnable.add_instance_variable(constant.name, constant.numeric_value)

        for text in component.texts:
            runnable.add_text_variable(text.name, text.value)
            
        for link in component.links:
            runnable.add_text_variable(link.name, link.value)

        for ep in component.event_ports:
            if ep.direction.lower() == 'in':
                runnable.add_event_in_port(ep.name)
            else:
                runnable.add_event_out_port(ep.name)

                
        dynamics = component.dynamics
        self.add_dynamics_1(component, runnable, dynamics, dynamics)

        for regime in dynamics.regimes:
            self.add_dynamics_1(component, runnable, regime, dynamics)
            
            if regime.initial:
                runnable.current_regime = regime.name

            rn = regime.name
            if rn not in runnable.regimes:
                runnable.add_regime(RunnableRegime(rn))
            r = runnable.regimes[rn]
            suffix = '_regime_' + rn

            if runnable.__dict__.has_key('update_state_variables' + suffix): 
                  r.update_state_variables = runnable.__dict__['update_state_variables' + suffix]
                  
            if runnable.__dict__.has_key('update_derived_variables' + suffix): 
                r.update_derived_variables = runnable.__dict__['update_derived_variables' + suffix]
            
            if runnable.__dict__.has_key('run_startup_event_handlers' + suffix): 
                r.run_startup_event_handlers = runnable.__dict__['run_startup_event_handlers' + suffix]
            
            if runnable.__dict__.has_key('run_preprocessing_event_handlers' + suffix): 
                r.run_preprocessing_event_handlers = runnable.__dict__['run_preprocessing_event_handlers' + suffix]
            
            if runnable.__dict__.has_key('run_postprocessing_event_handlers' + suffix): 
                r.run_postprocessing_event_handlers = runnable.__dict__['run_postprocessing_event_handlers' + suffix]

        self.process_simulation_specs(component, runnable, component.simulation)

        for child in component.child_components:
            child_runnable = self.build_runnable(child, runnable)
            runnable.add_child(child.id, child_runnable)

            for children in component.children:
                #GG - These conditions need more debugging.
                if children.type in child.types:
                    runnable.add_child_typeref(children.type, child_runnable)
                if children.multiple:
                    if children.type in child.types:
                        runnable.add_child_to_group(children.name, child_runnable)
                else:
                    if child_runnable.id == children.name:
                        runnable.add_child_typeref(children.name, child_runnable)

        for attachment in component.attachments:
            runnable.make_attachment(attachment.type, attachment.name)

        self.build_structure(component, runnable, component.structure)

        dynamics = component.dynamics
        self.add_dynamics_2(component, runnable,
                            dynamics, dynamics)
        for regime in dynamics.regimes:
            self.add_dynamics_2(component, runnable, regime, dynamics)

            if regime.name not in runnable.regimes:
                runnable.add_regime(RunnableRegime(regime.name))
            r = runnable.regimes[regime.name]
            suffix = '_regime_' + regime.name

            if runnable.__dict__.has_key('update_kinetic_scheme' + suffix): 
                r.update_kinetic_scheme = runnable.__dict__['update_kinetic_scheme' + suffix]

        self.add_recording_behavior(component, runnable)

        self.current_data_output = data_output_backup
        self.current_record_target = record_target_backup

        return runnable
Пример #2
0
    def build_runnable(self, component, parent = None, id_ = None):
        """
        Build a runnable component from a component specification and add
        it to the simulation.

        @param component: Component specification
        @type component: lems.model.component.FatComponent

        @param parent: Parent runnable component.
        @type parent: lems.sim.runnable.Runnable

        @param id_: Optional id for therunnable. If it's not passed in,
        the runnable will inherit the id of the component.

        @raise SimBuildError: Raised when a component reference cannot be
        resolved.
        """
        if self.debug: print("++++++++ Calling build_runnable of %s with parent %s"%(component, parent))

        if id_ == None:
            runnable = Runnable(component.id, component, parent)
        else:
            runnable = Runnable(id_, component, parent)

        simulation = component.simulation

        record_target_backup = self.current_record_target
        data_output_backup = self.current_data_output

        do = None
        for d in simulation.data_displays:
            do = d
        if do == None:
            for d in simulation.data_writers:
                do = d

        if do != None:
            self.current_data_output = do

        for parameter in component.parameters:
            runnable.add_instance_variable(parameter.name, parameter.numeric_value)
            
        
        for property in component.properties:
            print("\n\n*****************************************************************\n\n"+
                  "   Property element is not stable in PyLEMS yet, see https://github.com/LEMS/pylems/issues/16\n\n"+
                  "   Used in: %s\n\n"%property.toxml()+
                  "*****************************************************************\n\n\n")
            runnable.add_instance_variable(property.name, property.default_value)

        derived_parameter_code = []
        
        derived_parameter_ordering = order_derived_parameters(component)
        
        for dpn in derived_parameter_ordering:
            derived_parameter = component.derived_parameters[dpn]
            runnable.add_derived_variable(derived_parameter.name)
            
            expression = self.build_expression_from_tree(runnable,
                                                        None,
                                                        derived_parameter.expression_tree)
            
            derived_parameter_code += ['self.{0} = ({1})'.format(
                        derived_parameter.name,
                        expression)]
            derived_parameter_code += ['self.{0}_shadow = ({1})'.format(
                        derived_parameter.name,
                        expression)]
       
        suffix = ''
        runnable.add_method('update_derived_parameters' + suffix, ['self'],
                            derived_parameter_code)
            
        for constant in component.constants:
            runnable.add_instance_variable(constant.name, constant.numeric_value)

        for text in component.texts:
            runnable.add_text_variable(text.name, text.value)
            
        for link in component.links:
            runnable.add_text_variable(link.name, link.value)

        for ep in component.event_ports:
            if ep.direction.lower() == 'in':
                runnable.add_event_in_port(ep.name)
            else:
                runnable.add_event_out_port(ep.name)

                
        dynamics = component.dynamics
        self.add_dynamics_1(component, runnable, dynamics, dynamics)

        for regime in dynamics.regimes:
            self.add_dynamics_1(component, runnable, regime, dynamics)
            
            if regime.initial:
                runnable.current_regime = regime.name

            rn = regime.name
            if rn not in runnable.regimes:
                runnable.add_regime(RunnableRegime(rn))
            r = runnable.regimes[rn]
            suffix = '_regime_' + rn

            if runnable.__dict__.has_key('update_state_variables' + suffix): 
                  r.update_state_variables = runnable.__dict__['update_state_variables' + suffix]
                  
            if runnable.__dict__.has_key('update_derived_variables' + suffix): 
                r.update_derived_variables = runnable.__dict__['update_derived_variables' + suffix]
            
            if runnable.__dict__.has_key('run_startup_event_handlers' + suffix): 
                r.run_startup_event_handlers = runnable.__dict__['run_startup_event_handlers' + suffix]
            
            if runnable.__dict__.has_key('run_preprocessing_event_handlers' + suffix): 
                r.run_preprocessing_event_handlers = runnable.__dict__['run_preprocessing_event_handlers' + suffix]
            
            if runnable.__dict__.has_key('run_postprocessing_event_handlers' + suffix): 
                r.run_postprocessing_event_handlers = runnable.__dict__['run_postprocessing_event_handlers' + suffix]

        self.process_simulation_specs(component, runnable, component.simulation)

        for child in component.child_components:
            child_runnable = self.build_runnable(child, runnable)
            runnable.add_child(child.id, child_runnable)

            for children in component.children:
                #GG - These conditions need more debugging.
                if children.type in child.types:
                    runnable.add_child_typeref(children.type, child_runnable)
                if children.multiple:
                    if children.type in child.types:
                        runnable.add_child_to_group(children.name, child_runnable)
                else:
                    if child_runnable.id == children.name:
                        runnable.add_child_typeref(children.name, child_runnable)

        for attachment in component.attachments:
            runnable.make_attachment(attachment.type, attachment.name)

        self.build_structure(component, runnable, component.structure)

        dynamics = component.dynamics
        self.add_dynamics_2(component, runnable,
                            dynamics, dynamics)
        for regime in dynamics.regimes:
            self.add_dynamics_2(component, runnable, regime, dynamics)

            if regime.name not in runnable.regimes:
                runnable.add_regime(RunnableRegime(regime.name))
            r = runnable.regimes[regime.name]
            suffix = '_regime_' + regime.name

            if runnable.__dict__.has_key('update_kinetic_scheme' + suffix): 
                r.update_kinetic_scheme = runnable.__dict__['update_kinetic_scheme' + suffix]

        self.add_recording_behavior(component, runnable)

        self.current_data_output = data_output_backup
        self.current_record_target = record_target_backup

        return runnable
Пример #3
0
    def build_runnable(self, component, parent=None):
        """
        Build a runnable component from a component specification and add
        it to the simulation.

        @param component: Component specification
        @type component: lems.model.component.Component

        @param parent: Parent runnable component.
        @type parent: lems.sim.runnable.Runnable

        @raise SimBuildError: Raised when a component reference cannot be
        resolved.
        """

        runnable = Runnable(component.id, parent)

        context = component.context
        record_target_backup = self.current_record_target

        for pn in context.parameters:
            p = context.parameters[pn]
            if p.numeric_value:
                runnable.add_instance_variable(p.name, p.numeric_value)
            else:
                pass
                ## if p.dimension == '__component_ref__':
                ##     ref = context.parent.lookup_component(p.value)
                ##     if ref == None:
                ##         raise SimBuildError(('Unable to resolve component '
                ##                              'reference {0}').\
                ##                             format(component_name))
                ##     self.sim.add_runnable(ref.id, self.build_runnable(ref))

        for port in context.event_in_ports:
            runnable.add_event_in_port(port)
        for port in context.event_out_ports:
            runnable.add_event_out_port(port)

        if context.selected_behavior_profile:
            self.add_runnable_behavior(component, runnable,
                                       context.selected_behavior_profile)
        else:
            runnable.add_method('update_state_variables', ['self', 'dt'], [])
            runnable.add_method('run_preprocessing_event_handlers', ['self'],
                                [])
            runnable.add_method('run_postprocessing_event_handlers', ['self'],
                                [])

        for cn in context.components:
            child = context.components[cn]
            runnable.add_child(child.id, self.build_runnable(child, runnable))

        self.build_structure(component, runnable, context.structure)

        if context.selected_behavior_profile:
            self.add_recording_behavior(component, runnable,
                                        context.selected_behavior_profile)

        self.current_record_target = record_target_backup

        return runnable
Пример #4
0
    def build_runnable(self, component, parent = None, id_ = None):
        """
        Build a runnable component from a component specification and add
        it to the simulation.

        @param component: Component specification
        @type component: lems.model.component.Component

        @param parent: Parent runnable component.
        @type parent: lems.sim.runnable.Runnable

        @param id_: Optional id for therunnable. If it's not passed in,
        the runnable will inherit the id of the component.

        @raise SimBuildError: Raised when a component reference cannot be
        resolved.
        """

        if id_ == None:
            runnable = Runnable(component.id, component, parent)
        else:
            runnable = Runnable(id_, component, parent)

        context = component.context
        record_target_backup = self.current_record_target

        for pn in context.parameters:
            p = context.parameters[pn]
            if p.numeric_value != None:
                runnable.add_instance_variable(p.name, p.numeric_value)
            elif p.dimension in ['__link__', '__text__']:
                runnable.add_text_variable(p.name, p.value)

        for port in context.event_in_ports:
            runnable.add_event_in_port(port)
        for port in context.event_out_ports:
            runnable.add_event_out_port(port)

        if context.selected_dynamics_profile:
            dynamics = context.selected_dynamics_profile
            self.add_dynamics_1(component, runnable,
                                dynamics.default_regime, dynamics.default_regime)

            for rn in dynamics.regimes:
                regime = dynamics.regimes[rn]
                self.add_dynamics_1(component, runnable, regime, dynamics.default_regime)

                if regime.initial:
                    runnable.current_regime = regime.name

                if rn not in runnable.regimes:
                    runnable.add_regime(Regime(rn))
                r = runnable.regimes[rn]
                suffix = '_regime_' + rn

                r.update_state_variables = runnable.__dict__['update_state_variables'
                                                             + suffix]
                r.update_derived_variables = runnable.__dict__['update_derived_variables'
                                                               + suffix]
                r.run_startup_event_handlers = runnable.__dict__['run_startup_event_handlers'
                                                                 + suffix]
                r.run_preprocessing_event_handlers = runnable.__dict__['run_preprocessing_event_handlers'
                                                                       + suffix]
                r.run_postprocessing_event_handlers = runnable.__dict__['run_postprocessing_event_handlers'
                                                                        + suffix]
        else:
            runnable.add_method('update_state_variables', ['self', 'dt'],
                                [])
            runnable.add_method('update_derived_variables', ['self'],
                                [])
            runnable.add_method('run_startup_event_handlers', ['self'],
                                [])
            runnable.add_method('run_preprocessing_event_handlers', ['self'],
                                [])
            runnable.add_method('run_postprocessing_event_handlers', ['self'],
                                [])

        self.process_simulation_specs(component, runnable, context.simulation)

        #for cn in context.components:
        for cn in context.components_ordering:
            child = context.components[cn]
            child_runnable = self.build_runnable(child, runnable)
            runnable.add_child(child.id, child_runnable)

            for cdn in context.children_defs:
                cdt = context.children_defs[cdn]
                #if cdt == child.component_type:
                if child.is_type(cdt):
                    runnable.add_child_to_group(cdn, child_runnable)

        for type_ in context.attachments:
            name = context.attachments[type_]
            runnable.make_attachment(type_, name)

        self.build_structure(component, runnable, context.structure)

        if context.selected_dynamics_profile:
            dynamics = context.selected_dynamics_profile
            self.add_dynamics_2(component, runnable,
                                dynamics.default_regime, dynamics.default_regime)
            for rn in dynamics.regimes:
                regime = dynamics.regimes[rn]
                self.add_dynamics_2(component, runnable, regime, dynamics.default_regime)

                if rn not in runnable.regimes:
                    runnable.add_regime(Regime(rn))
                r = runnable.regimes[rn]
                suffix = '_regime_' + rn

                r.update_kinetic_scheme = runnable.__dict__['update_kinetic_scheme'
                                                            + suffix]
        else:
            runnable.add_method('update_kinetic_scheme', ['self', 'dt'],
                                [])

        self.add_recording_behavior(component, runnable)

        self.current_record_target = record_target_backup

        return runnable
Пример #5
0
    def build_runnable(self, component, parent=None):
        """
        Build a runnable component from a component specification and add
        it to the simulation.

        @param component: Component specification
        @type component: lems.model.component.Component

        @param parent: Parent runnable component.
        @type parent: lems.sim.runnable.Runnable

        @raise SimBuildError: Raised when a component reference cannot be
        resolved.
        """

        runnable = Runnable(component.id, parent)

        context = component.context
        record_target_backup = self.current_record_target

        for pn in context.parameters:
            p = context.parameters[pn]
            if p.numeric_value:
                runnable.add_instance_variable(p.name, p.numeric_value)
            else:
                pass
                ## if p.dimension == '__component_ref__':
                ##     ref = context.parent.lookup_component(p.value)
                ##     if ref == None:
                ##         raise SimBuildError(('Unable to resolve component '
                ##                              'reference {0}').\
                ##                             format(component_name))
                ##     self.sim.add_runnable(ref.id, self.build_runnable(ref))

        for port in context.event_in_ports:
            runnable.add_event_in_port(port)
        for port in context.event_out_ports:
            runnable.add_event_out_port(port)

        if context.selected_behavior_profile:
            self.add_runnable_behavior(component, runnable, context.selected_behavior_profile)
        else:
            runnable.add_method("update_state_variables", ["self", "dt"], [])
            runnable.add_method("run_preprocessing_event_handlers", ["self"], [])
            runnable.add_method("run_postprocessing_event_handlers", ["self"], [])

        for cn in context.components:
            child = context.components[cn]
            runnable.add_child(child.id, self.build_runnable(child, runnable))

        self.build_structure(component, runnable, context.structure)

        if context.selected_behavior_profile:
            self.add_recording_behavior(component, runnable, context.selected_behavior_profile)

        self.current_record_target = record_target_backup

        return runnable