예제 #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
예제 #2
0
 def convert(self, h_graph):
 
     graph_name = h_graph.name
     
     #graph_to_dot(graph_name + "_from_simulink", h_graph)
     
     cbd = CBD(graph_name)
     cbd.delta_t = 0.001
     
     module = __import__('SimulinkCBD')
     
     for i in range(len(h_graph.vs)):
         block = h_graph.vs[i]
         block_name = self.getBlockID(block, i)
         
         block_type = block["mm__"]
         
         try:
             block_class = getattr(module, 'Simulink_' + block_type + "Block")
             
         except Exception:
             print("HimesisToCBD Error: Unknown Simulink block type '" + block_type + "'!")  
             block_class = getattr(module, 'Simulink_GenericBlock')
             
         cbd.addBlock(block_class(block_name, block))
         #print(block)
         
     edges_to_delete = []
     for edgeId in range(h_graph.ecount()):
         source = h_graph.es[edgeId].source
         target = h_graph.es[edgeId].target
         
         #print("Source: " + str(source))
         #print("Target: " + str(target))
         
         
         
         source_block = h_graph.vs[source]
         source_block_name = self.getBlockID(source_block, source)
         
         target_block = h_graph.vs[target]
         target_block_name = self.getBlockID(target_block, target)
         
         #fix edge direction for inputs
         #TODO: Fix for inport and contains blocks
         if ("Port_Input" in target_block_name or "Block_Inport" in target_block_name) and not "__Relation__" in source_block_name:
         
             cbd.addConnection(target_block_name, source_block_name)
             
             #remove the incorrect edge
             edges_to_delete.append(h_graph.es[edgeId])
             
             h_graph.add_edge(target, source)
         else:
             cbd.addConnection(source_block_name, target_block_name)
     
     #delete the edges that were fixed
     h_graph.delete_edges(edges_to_delete)
         
     graph_to_dot(graph_name, h_graph)
     
     #cbd.dump()
     return cbd