Ejemplo n.º 1
0
    def convert(self, model, do_switch_hack = True):
        model_name = model.getBlockName() + "_opt"
        
        h = Himesis(name=model_name)
        h[Himesis.Constants.META_MODEL] = ['Simulink']  # the meta-model type name
        
        h["name"] = model_name
        
        model_blocks = model.getBlocks()
        block_id_dict = {}
        
        #need to avoid block name conflicts
        self.block_names = []
       
        
        
        for block in model_blocks:
            vertex = h.add_node()
            block_id_dict[block] = vertex
            
            self.get_attribs(h, vertex, block)

        for block in model_blocks:
            vertex = block_id_dict[block]
            
            
            for out_port in block.linksOUT:
            
                
                out_vertex = block_id_dict[out_port]
                
                target_block_name = out_port.getBlockName()
                source_block_name = block.getBlockName()
                
                
                #TODO: Remove switch hack
                if do_switch_hack and ("Port_Input" in source_block_name or "Block_Inport" in source_block_name) and not "__Relation__" in target_block_name:
                    h.add_edge(out_vertex, vertex)
                else:
                    h.add_edge(vertex, out_vertex)
            
            #h.vs[vertex][Himesis.Constants.META_MODEL] = block.getBlockName()
                    
            #print(vertex)
        
        if not do_switch_hack:
            model_name = model_name + "_real"
        
        graph_to_dot(model_name, h)
        
        return h
Ejemplo n.º 2
0
    def createSimulinkModel(self, blocks, connections, name,outputfolder):
        allNodes = self.getAllNodes(blocks)
        # init Himesis:
        numberOfNodes = len(allNodes)
        himesis = Himesis(name=name, num_nodes=numberOfNodes)
        himesis[Himesis.Constants.META_MODEL] = ['Simulink']  # the meta-model type name
        himesis["name"] = str(name)
        # attributes
        for node in allNodes:
            himesis.vs[node._id][Himesis.Constants.META_MODEL] = str(node.getType())   # the meta-model type element
            #fullname = node.fullPath
            #fullname = fullname.rsplit('/',1)
            theName = str(node.name).rsplit('/',1)[-1]
            himesis.vs[node._id]['Name'] = str(theName)
            # do other attributes:
            parameters = node.getParameters()
            for k,v in parameters.iteritems():
                himesis.vs[node._id][k] = v
        for block in blocks:
            if not isinstance(block, ContainsBlock):
                himesis.vs[block._id]['Position'] = block.Position
        # edges, we have edges from block to port and from the containment relations
        for block in blocks:
            if not isinstance(block, ContainsBlock):
                for inport in block.inports.values():
                    blockportvertex = himesis.add_node()
                    himesis.vs[blockportvertex][Himesis.Constants.META_MODEL] = S2HConstants.BLOCK_INPORT_RELATION
                    himesis.add_edge(block._id, blockportvertex)
                    himesis.add_edge(blockportvertex, inport._id)
                for outport in block.outports.values():
                    blockportvertex = himesis.add_node()
                    himesis.vs[blockportvertex][Himesis.Constants.META_MODEL] = S2HConstants.BLOCK_OUTPORT_RELATION
                    himesis.add_edge(block._id, blockportvertex)
                    himesis.add_edge(blockportvertex, outport._id)
                for enableport in block.enabled.values():
                    blockportvertex = himesis.add_node()
                    himesis.vs[blockportvertex][Himesis.Constants.META_MODEL] = S2HConstants.BLOCK_ENABLE_RELATION
                    himesis.add_edge(block._id, blockportvertex)
                    himesis.add_edge(blockportvertex, enableport._id)
                for triggerport in block.trigger.values():
                    blockportvertex = himesis.add_node()
                    himesis.vs[blockportvertex][Himesis.Constants.META_MODEL] = S2HConstants.BLOCK_TRIGGER_RELATION
                    himesis.add_edge(block._id, blockportvertex)
                    himesis.add_edge(blockportvertex, triggerport._id)
            else:
                if self.debugLevel > 0:
                    print block.subsystem
                    print block.block
                himesis.add_edge(block.subsystem._id, block._id)
                himesis.add_edge(block._id,block.block._id)
        #and from outport to inport
        createdLines = []
        #del connections.conTable[None]
        for linename,connectionsdict in connections.conTable.iteritems():
            if linename is not None:
                doIdelete = True
                if self.debugLevel > 0:
                    print linename
                theOutports = connectionsdict[OUTPORT]
                theInports = connectionsdict[INPORT]
                for outportEndpoint in theOutports:
                    opid = None
                    if hasattr(outportEndpoint.port, '_id'):
                        opid = outportEndpoint.port._id
                    if opid is None or opid == -1: # the line is on another level, or even it is not there anymore
                        doIdelete = False
                        break
                    for inportEndpoint in theInports:
                        ipid = inportEndpoint.port._id
                        if ipid is None or ipid == -1: # the line is on another level, or even it is not there anymore
                            break
                        # use the assigned id of the
                        convertex = himesis.add_node()
                        himesis.vs[convertex][Himesis.Constants.META_MODEL] = S2HConstants.PORT2PORT_RELATION
                        himesis.add_edge(opid, convertex)
                        himesis.add_edge(convertex, ipid)
                        inportEndpoint.port._id = None # We created this line, so we do not need it anymore
                    outportEndpoint.port._id = None # We created this linem
                if doIdelete:
                    createdLines.append(linename)
        for linename in createdLines:
            del connections.conTable[linename]

        #compile
        himesis.compile(outputfolder)