code: def build_foreach(self, component, runnable, foreach, name_mappings = {}):
          if self.debug: print("\n++++++++ Calling build_foreach of %s with runnable %s, parent %s, name_mappings: %s"%(component.id, runnable.id, runnable.parent, name_mappings))
          target_array = runnable.resolve_path(foreach.instances)
          for target_runnable in target_array:
              if self.debug: print("Applying contents of for_each to %s, as %s"%(target_runnable.id, foreach.as_))
              name_mappings[foreach.as_] = target_runnable
              for fe2 in foreach.for_eachs:
                  target_array2 = runnable.resolve_path(fe2.instances)
                  for target_runnable2 in target_array2:
                      name_mappings[fe2.as_] = target_runnable2
                      self.build_foreach(component, runnable, fe2, name_mappings)
              for ec in foreach.event_connections:
                  source = name_mappings[ec.from_]
                  target = name_mappings[ec.to]
                  source_port = ec.source_port
                  target_port = ec.target_port
                  if not source_port:
                      if len(source.event_out_ports) == 1:
                          source_port = source.event_out_ports[0]
                      else:
                          raise SimBuildError(("No source event port "
                                               "uniquely identifiable"
                                               " in '{0}'").format(source.id))
                  if not target_port:
                      if len(target.event_in_ports) == 1:
                          target_port = target.event_in_ports[0]
                      else:
                          raise SimBuildError(("No destination event port "
                                               "uniquely identifiable "
                                               "in '{0}'").format(target))
                  if self.debug: print("register_event_out_callback\n   Source: %s, %s (port: %s) \n   -> %s, %s (port: %s)"%(source, id(source), source_port, target, id(target), target_port))
                  source.register_event_out_callback(\
                      source_port, lambda: target.inc_event_in(target_port))