Resolve structure specifications.

def resolve_structure(self, fc, ct):
        """
        Resolve structure specifications.
        """
        if self.debug: print("++++++++ Resolving structure of (%s) with %s"%(fc, ct))
        for w in ct.structure.withs:
            try:
                if w.instance == 'parent' or w.instance == 'this':
                    w2 = With(w.instance, w.as_)
                else:
                    w2 = With(fc.paths[w.instance].value,
                              w.as_)
            except:
                raise ModelError("Unable to resolve With parameters for "
                                 "'{0}' in component '{1}'",
                                 w.as_, fc.id)
            fc.structure.add(w2)
            
        if len(ct.structure.tunnels) > 0:
            raise ModelError("Tunnel is not yet supported in PyLEMS!");
            
        for fe in ct.structure.for_eachs:
            fc.structure.add_for_each(fe)
            
        for ev in ct.structure.event_connections:
            try:
                
                from_inst = fc.structure.withs[ev.from_].instance
                to_inst = fc.structure.withs[ev.to].instance
                
                if self.debug: print("EC..: "+from_inst+" to "+to_inst+ " in "+str(fc.paths))
                
                if len(fc.texts) > 0 or len(fc.paths) > 0:
                    
                    source_port = fc.texts[ev.source_port].value if ev.source_port and len(ev.source_port)>0 and ev.source_port in fc.texts else None
                    target_port = fc.texts[ev.target_port].value if ev.target_port and len(ev.target_port)>0 and ev.target_port in fc.texts else None
                    
                    if self.debug: print("sp: %s"%source_port)
                    if self.debug: print("tp: %s"%target_port)

                    receiver = None

                    # TODO: Get more efficient way to find parent comp
                    if '../' in ev.receiver:
                        receiver_id = None
                        parent_attr = ev.receiver[3:]
                        if self.debug: print("Finding %s in the parent of: %s (%i)"%(parent_attr, fc, id(fc)))
                        
                        for comp in self.components.values():
                            if self.debug: print(" - Checking %s (%i)" %(comp.id,id(comp))) 
                            for child in comp.children:
                                if self.debug: print("    - Checking %s (%i)" %(child.id,id(child))) 
                                for child2 in child.children:
                                    if child2.id == fc.id and child2.type == fc.type and child.id == fc.parent_id:
                                        if self.debug: print("    - Got it?: %s (%i), child: %s"%(child.id, id(child), child2))
                                        receiver_id = child.parameters[parent_attr]
                                        if self.debug: print("Got it: "+receiver_id)
                                        break

                        if receiver_id is not None:            
                            for comp in self.fat_components:
                                if comp.id == receiver_id:
                                        receiver = comp
                                        if self.debug: print("receiver is: %s"%receiver)

                    if self.debug: print("rec1: %s"%receiver)
                    if not receiver:
                        receiver = fc.component_references[ev.receiver].referenced_component if ev.receiver else None
                    receiver_container = fc.texts[ev.receiver_container].value if (fc.texts and ev.receiver_container) else ''

                    if self.debug: print("rec2: %s"%receiver)
                    if len(receiver_container)==0:
                        # TODO: remove this hard coded check!
                        receiver_container = 'synapses'

                else:
                    #if from_inst == 'parent':
                        #par = fc.component_references[ev.receiver]
                    
                    if self.debug: 
                        print("+++++++++++++++++++")
                        print(ev.toxml())
                        print(ev.source_port)
                        print(fc)
                    source_port = ev.source_port
                    target_port = ev.target_port
                    receiver = None
                    receiver_container = None
                    
                ev2 = EventConnection(from_inst,
                                      to_inst,
                                      source_port,
                                      target_port,
                                      receiver,
                                      receiver_container)
                if self.debug: 
                    print("Created EC: "+ev2.toxml())
                    print(receiver)
                    print(receiver_container)
            except:
                logging.exception("Something awful happened!")
                raise ModelError("Unable to resolve event connection parameters in component '{0}'",fc)
            fc.structure.add(ev2)
                
        for ch in ct.structure.child_instances:
            try:
                if self.debug: print(ch.toxml())
                if '../' in ch.component:
                    parent = self.get_parent_component(fc)
                    if self.debug: print("Parent: %s"%parent)
                    comp_ref = ch.component[3:]
                    if self.debug: print("comp_ref: %s"%comp_ref)
                    comp_id = parent.parameters[comp_ref]
                    comp = self.fat_components[comp_id]
                    ch2 = ChildInstance(ch.component, comp)
                else:    
                    ref_comp = fc.component_references[ch.component].referenced_component
                    ch2 = ChildInstance(ch.component, ref_comp)
            except Exception as e:
                if self.debug: print(e)
                raise ModelError("Unable to resolve child instance parameters for "
                                 "'{0}' in component '{1}'",
                                 ch.component, fc.id)
            fc.structure.add(ch2)

        for mi in ct.structure.multi_instantiates:
            try:
                if mi.component:
                    mi2 = MultiInstantiate(component=fc.component_references[mi.component].referenced_component,
                                           number=int(fc.parameters[mi.number].numeric_value))
                else:
                    mi2 = MultiInstantiate(component_type=fc.component_references[mi.component_type].referenced_component,
                                           number=int(fc.parameters[mi.number].numeric_value))                    
            except:
                raise ModelError("Unable to resolve multi-instantiate parameters for "
                                 "'{0}' in component '{1}'",
                                 mi.component, fc)
            fc.structure.add(mi2)