예제 #1
0
 def handle(self):
     """
     Handle the request. It should contain the directory, model name and tranformation name.
     The handle will make a Himesis of the model
     The handle will make a Himesis of the rules and a schedule
     The handle will execute the transformation
     The handle will export the transformed model to Simulink
     @return: None
     """
     sys.stdout.write( "started thread")
     self.mh =  self.server.getFromPool()
     self.wfile.write('RDY')
     data = self.rfile.readline().rstrip('\n')
     self.wfile.write('OK')
     sys.stdout.write(data)
     path,model,transformation = data.split(';')
     self.mh.chDir(path)
     # Model to Himesis
     modelToHimesis = SimulinkModelToHimesis(self.mh,model,path)
     modelToHimesis.SimulinkModelToHimesis()
     # Transfornation to T-Core
     transformationToHimesis = SimulinkTransformationToHimesis(transformation,path,self.mh)
     transformationToHimesis.SimulinkTransformationModelToHimesis()
     # Execute the Transformation
     exec("from Server.temp."+ Himesis.standardize_name(model) + " import "+ Himesis.standardize_name(model))
     exec("import Server.temp.T_" + transformation)
     packet = Packet()
     exec("packet.graph = "+ Himesis.standardize_name(model) + "()")
     exec('packet = Server.temp.T_'+ transformation +'.packet_in(packet)')
     #export:
     simulinkExport = SimulinkExporter(model,self.mh,packet.graph)
     simulinkExport.exportSimulink()
     self.wfile.write('finished')
예제 #2
0
    def run(self, path, model, opt_name):

        if not self.mh == None:

            start = time.time()
            self.mh.chDir(path)

            # turn Simulink model into himesis graph
            modelToHimesis = SimulinkModelToHimesis(self.mh, model, path)
            modelToHimesis.SimulinkModelToHimesis()

            end = time.time()
            print("Time taken to import from Simulink: " + str(end - start) + " seconds")

            # draw model
            # print("Start drawing")
            # self.mh.drawSystem(model, "~/")
            # print("End drawing")

        start = time.clock()
        hToCBD = HimesisToCBD()
        self.model = hToCBD.convertFile("himesis/" + Himesis.standardize_name(model) + ".py")
        end = time.clock()
        print("Time taken for Himesis to CBD: " + str(end - start) + " seconds")

        # TODO: only needed for dep graph, should remove this
        start = time.clock()
        self.simulator = CBDsimulator(self.model)
        end = time.clock()
        print("Time taken to build simulator: " + str(end - start) + " seconds")

        opt = opt_name(self.simulator, self.mh)
        self.model = opt.optimize(self.model)

        start = time.clock()
        CBDToH = CBDToHimesis()
        h = CBDToH.convert(self.model)
        h2 = CBDToH.convert(self.model, False)
        h.compile("himesis/")
        end = time.clock()
        print("Time taken for CBD to Himesis: " + str(end - start) + " seconds")

        if not self.mh == None:

            start = time.clock()
            simulinkExport = SimulinkExporter(model, self.mh, h)
            simulinkExport.exportSimulink()
            end = time.clock()
            print("Time taken to export to Simulink: " + str(end - start) + " seconds")

            self.mh.endLib()
예제 #3
0
    def transform(self, model, analysis):
    
        #do code transformation
        if self.useModelTransformation:
            
            transformation = "flattening"
            path = "./examples/"
            
            model_name = model.getBlockName()
            model_name = Himesis.standardize_name(model_name)
            
            if not self.mh == None:
                # Transfornation to T-Core
                transformationToHimesis = SimulinkTransformationToHimesis(transformation, path, self.mh)
                transformationToHimesis.SimulinkTransformationModelToHimesis()
            
            
            # Execute the Transformation
            model_graph = self.get_object('./himesis/' + model_name + ".py")
            
            packet = Packet()
            packet.graph = model_graph
            

            exec("import temp.T_" + transformation)
            exec('packet = temp.T_'+ transformation +'.packet_in(packet)')
            
            print(packet)

        else:
        
            for depth in sorted(analysis.keys()):
            
                subsystem = analysis[depth]
                print("Subsystem Name: " + subsystem.getBlockName() + " at depth: " + str(depth))
                
                if len(subsystem.linksIN) == 0:
                    continue
                    
                #TODO: Make these pretty
                self.fix_incoming_edges(model, subsystem)
                self.fix_outgoing_edges(model, subsystem)
                self.remove_subsystem(model, subsystem)

                break
                
        return model
예제 #4
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
예제 #5
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)