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.

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