def editPRates(self, src, dst, newPRates): Log.addLogMessage( Log.INFO, 'Production rates for edge ' + str( (src, dst)) + ' updated to ' + str(newPRates)) self.graphData[src][dst]['prates'] = newPRates newPRates = str(newPRates) self.graphData.updatePRates((src, dst), newPRates)
def editCRates(self, src, dst, newCRates): Log.addLogMessage( Log.INFO, 'Consumption rates for edge ' + str( (src, dst)) + ' updated to ' + str(newCRates)) self.graphData[src][dst]['crates'] = newCRates newCRates = str(newCRates) self.graphData.updateCRates((src, dst), newCRates)
def editTokens(self, src, dst, newTokens): Log.addLogMessage(Log.INFO, 'Tokens for edge ' + str((src, dst)) + ' updated to ' + str(newTokens)) self.graphData[src][dst]['tkns'] = newTokens newTokens = str(newTokens) self.graphData.updateTokens((src, dst), newTokens) #Also store the state after the change (Makes it more visual and makes it possible to step back to) self.graphData._storestate()
def editTokens(self, src, dst, newTokens): Log.addLogMessage( Log.INFO, 'Tokens for edge ' + str( (src, dst)) + ' updated to ' + str(newTokens)) self.graphData[src][dst]['tkns'] = newTokens newTokens = str(newTokens) self.graphData.updateTokens((src, dst), newTokens) #Also store the state after the change (Makes it more visual and makes it possible to step back to) self.graphData._storestate()
def setClashCodeActionTriggered(self): clashCodeStr = self.clashCode newClashCode, ok = QInputDialog.getMultiLineText(self.widget, 'CLaSH code for ' + self.nodeName, 'CLaSH code:', text=clashCodeStr) if ok: try: # TODO add validation of code self.clashCode = newClashCode self.widget.editClashCode(self.nodeName, newClashCode) except: Log.addLogMessage(Log.ERROR, 'Invalid CLaSH code') QMessageBox.critical(self.widget, 'Error', 'Invalid CLaSH code')
def setNodeActiontriggered(self): functionStr = str(self.nodeFunction) newFunctionStr, ok = QInputDialog.getText(self.widget, 'Edit node function', 'Function:', text = functionStr) if ok: try: newFunction = eval(newFunctionStr) self.nodeFunction = newFunctionStr self.widget.editNodeFunction(self.nodeName, newFunctionStr) except: Log.addLogMessage(Log.ERROR, 'Invalid node function') QMessageBox.critical(self.widget, 'Error', 'Invalid node function')
def setTokenActiontriggered(self): tokenStr = str(self.tokenValues) newTokenStr, ok = QInputDialog.getText(self.widget, 'Edit tokens', 'Tokens:', text = tokenStr) if ok: try: newTokens = eval(newTokenStr) self.newTokenValues(newTokens) self.widget.editTokens(self.src, self.dst, newTokens) except: Log.addLogMessage(Log.ERROR, 'List of tokens not valid.') QMessageBox.critical(self.widget, 'Error', 'List of tokens not valid.')
def setCRatesActiontriggered(self): cRatesStr = str(self.cRates) newCRatesStr, ok = QInputDialog.getText(self.tokenCluster.widget, 'Edit consumption rates', 'Consumption rate:', text = cRatesStr) if ok: try: newCRates = eval(newCRatesStr) self.cRates = newCRates self.tokenCluster.widget.editCRates(self.tokenCluster.src, self.tokenCluster.dst, newCRates) except: Log.addLogMessage(Log.ERROR, 'Consumption rate not valid.') QMessageBox.critical(self.tokenCluster.widget, 'Error', 'Consumption rate not valid.')
def setTokenActiontriggered(self): tokenStr = str(self.tokenValues) newTokenStr, ok = QInputDialog.getText(self.widget, 'Edit tokens', 'Tokens:', text=tokenStr) if ok: try: newTokens = eval(newTokenStr) self.newTokenValues(newTokens) self.widget.editTokens(self.src, self.dst, newTokens) except: Log.addLogMessage(Log.ERROR, 'List of tokens not valid.') QMessageBox.critical(self.widget, 'Error', 'List of tokens not valid.')
def setNodeActiontriggered(self): functionStr = str(self.nodeFunction) newFunctionStr, ok = QInputDialog.getText(self.widget, 'Edit node function', 'Function:', text=functionStr) if ok: try: newFunction = eval(newFunctionStr) self.nodeFunction = newFunctionStr self.widget.editNodeFunction(self.nodeName, newFunctionStr) except: Log.addLogMessage(Log.ERROR, 'Invalid node function') QMessageBox.critical(self.widget, 'Error', 'Invalid node function')
def setClashCodeActionTriggered(self): clashCodeStr = self.clashCode newClashCode, ok = QInputDialog.getMultiLineText(self.widget, 'CLaSH code for ' + self.nodeName, 'CLaSH code:', text=clashCodeStr) if ok: try: # TODO add validation of code self.clashCode = newClashCode self.widget.editClashCode(self.nodeName, newClashCode) except: Log.addLogMessage(Log.ERROR, 'Invalid CLaSH code') QMessageBox.critical(self.widget, 'Error', 'Invalid CLaSH code')
def buttonClicked(self): MAX_ITERATIONS = 128 if self.sender() == self.btnReset: self.graph.reset() elif self.sender() == self.btnBack: self.graph.back() elif self.sender() == self.btnNext: self.graph.step() else: # btnRunUntill button clicked src, dst = self.cmbEdges.currentText().split(' → ') n = 0 while str(self.graph[src][dst]['tkns']) != ( '[' + self.editContains.text() + ']') and n < MAX_ITERATIONS: self.graph.step() self.graphWidget.updateTokensGraph() n += 1 if n == MAX_ITERATIONS: Log.addLogMessage(Log.WARNING, 'Stop condition never met') self.lblStateNr.setText('State: ' + str(self.graph.stateCount() - 1)) self.btnBack.setEnabled(self.graph.stateCount() > 1) self.btnReset.setEnabled(self.graph.stateCount() > 1) # Display the new state of the graph in the GraphWidget self.graphWidget.updateTokensGraph() # update the signal window such that all tokens ad activiations are shown for n, data in self.graph.nodefirings.items(): self.swid.updateSignal(n, data) for (src, dst), data in self.graph.edgestates.items(): name = src + ' → ' + dst self.swid.updateSignal(name, data) # Nasty way to make sure the widget scrolls to the end # required because the max value of the scrollbar is not enought due # to delayed resizing of the tableWidget... QTimer.singleShot(1, self.swid.scrollToEnd)
def buttonClicked(self): MAX_ITERATIONS = 128 if self.sender() == self.btnReset: self.graph.reset() elif self.sender() == self.btnBack: self.graph.back() elif self.sender() == self.btnNext: self.graph.step() else: # btnRunUntill button clicked src, dst = self.cmbEdges.currentText().split(' → ') n = 0 while str(self.graph[src][dst]['tkns']) != ('[' + self.editContains.text() + ']') and n < MAX_ITERATIONS: self.graph.step() self.graphWidget.updateTokensGraph() n += 1 if n == MAX_ITERATIONS: Log.addLogMessage(Log.WARNING, 'Stop condition never met') self.lblStateNr.setText('State: ' + str(self.graph.stateCount() - 1)) self.btnBack.setEnabled(self.graph.stateCount() > 1) self.btnReset.setEnabled(self.graph.stateCount() > 1) # Display the new state of the graph in the GraphWidget self.graphWidget.updateTokensGraph() # update the signal window such that all tokens ad activiations are shown for n, data in self.graph.nodefirings.items(): self.swid.updateSignal(n, data) for (src, dst), data in self.graph.edgestates.items(): name = src + ' → ' + dst self.swid.updateSignal(name, data) # Nasty way to make sure the widget scrolls to the end # required because the max value of the scrollbar is not enought due # to delayed resizing of the tableWidget... QTimer.singleShot(1, self.swid.scrollToEnd)
def editClashCode(self, nodeName, newClashCode): Log.addLogMessage(Log.INFO, 'CLaSH code of ' + nodeName + ' updated') self.graphData.node[nodeName]['clashcode'] = newClashCode self.graphData.updateClashCode(nodeName, newClashCode)
def editNodeFunction(self, nodeName, newFunction): Log.addLogMessage( Log.INFO, 'Function of node ' + nodeName + ' updated to ' + str(newFunction)) self.graphData.node[nodeName]['funcstr'] = newFunction self.graphData.updateNodeFunction(nodeName, newFunction)
def loadFromFile(self, filename): """ Loads a graph from a JSON file. Parameters ---------- filename : string with file path Raises ------ ValueError When the graph is inconsistent as detected by the validateGraph() method. """ Log.addLogMessage(Log.INFO, 'Opened grap ' + filename) self.filename = filename with open(filename, 'r') as f: jsonstr = f.read() jsondata = json.loads(jsonstr) # make sure the name becomes camelcase without spaces: required by CLaSH namestr = jsondata['name'].strip() namestr = string.capwords(namestr) namestr = namestr.replace(' ', '') self.name = namestr # Load the predefined clash types when available if 'clashtypes' in jsondata.keys(): self.clashtypes = jsondata['clashtypes'] # Load all nodes and their attributes for jsnode in jsondata['nodes']: nodeName = jsnode['name'] nodeFunction = jsnode['function'] nodeClashCode = '' if 'clashcode' in jsnode.keys(): nodeClashCode = jsnode['clashcode'] nodeColor = self.DEFAULT_NODE_COLOR if 'color' in jsnode.keys(): nodeColor = jsnode['color'] nodePosition = jsnode['pos'][0], jsnode['pos'][1] self.add_node(nodeName, nodeFunction, nodePosition, clashcode=nodeClashCode, color=nodeColor) # Load all edges and their attributes for jsedge in jsondata['edges']: edgeSource = jsedge['src'] edgeDestination = jsedge['dst'] edgeResNumber = jsedge['resnr'] edgeArgNumber = jsedge['argnr'] edgePRates = CSDFGraph._flattenRateList(jsedge.get('prates', [1])) edgeCRates = CSDFGraph._flattenRateList(jsedge.get('crates', [1])) edgeTokens = jsedge.get('tkns', []) edgeColor = self.DEFAULT_EDGE_COLOR if 'color' in jsedge.keys(): edgeColor = jsedge['color'] self.add_edge( edgeSource, edgeDestination, edgeResNumber, edgeArgNumber, edgePRates, edgeCRates, edgeTokens, color=edgeColor) # Now that the graph is construcuted, validate it: self.validateGraph()
def editPRates(self, src, dst, newPRates): Log.addLogMessage(Log.INFO, 'Production rates for edge ' + str((src, dst)) + ' updated to ' + str(newPRates)) self.graphData[src][dst]['prates'] = newPRates newPRates = str(newPRates) self.graphData.updatePRates((src, dst), newPRates)
def editNodeFunction(self, nodeName, newFunction): Log.addLogMessage(Log.INFO, 'Function of node ' + nodeName + ' updated to ' + str(newFunction)) self.graphData.node[nodeName]['funcstr'] = newFunction self.graphData.updateNodeFunction(nodeName, newFunction)
def storeToFile(self, filename=''): """ Stores the current graph in a JSON file. Parameters ---------- filename : string with filepath filename is an optional argument containing the file in which graph is stored. When this argument is not used, the graph is stored in the file from which it was initially read. """ if filename == '': # no file name given so use file from which this graph is made fname = self.filename else: fname = filename # Put all info into a temporary dictionary which will be transformed into a json string graphDict = OrderedDict({}) # First save graph properties/attributes: name and predefined CLaSH types graphDict['name'] = self.name if self.clashtypes is not None: graphDict['clashtypes'] = self.clashtypes # Store all the nodes of the graph in the temporary dictionary nodesList = [] for nname in self.nodes(): nodedict = OrderedDict({}) nodedict['name'] = nname nodedict['function'] = self.node[nname]['funcstr'] if self.node[nname]['clashcode'] != '': nodedict['clashcode'] = self.node[nname]['clashcode'] nodedict['pos'] = list(self.node[nname]['pos']) nodedict['color'] = self.node[nname]['color'] nodesList.append(nodedict) # add all nodes to temporary dict in form of a list graphDict['nodes'] = nodesList # Store all the edges of the graph in the temporary dictionary edgesList = [] for srcname, dstname in self.edges(): edgedict = OrderedDict({}) edgedict['src'] = srcname edgedict['dst'] = dstname edgedict['resnr'] = self[srcname][dstname]['res'] edgedict['argnr'] = self[srcname][dstname]['arg'] edgedict['prates'] = self[srcname][dstname]['prates'] edgedict['crates'] = self[srcname][dstname]['crates'] edgedict['tkns'] = self[srcname][dstname]['tkns'] edgedict['color'] = self[srcname][dstname]['color'] edgesList.append(edgedict) # add all edges to temporary dict in form of a list graphDict['edges'] = edgesList # Last but not leat, write the graph to the file with open(fname, 'w') as outfile: json.dump(graphDict, outfile, indent=4) Log.addLogMessage(Log.INFO, 'Saved graph ' + fname)
def editClashCode(self, nodeName, newClashCode): Log.addLogMessage(Log.INFO,'CLaSH code of ' + nodeName + ' updated') self.graphData.node[nodeName]['clashcode'] = newClashCode self.graphData.updateClashCode(nodeName, newClashCode)
def loadFromFile(self, filename): """ Loads a graph from a JSON file. Parameters ---------- filename : string with file path Raises ------ ValueError When the graph is inconsistent as detected by the validateGraph() method. """ Log.addLogMessage(Log.INFO, 'Opened grap ' + filename) self.filename = filename with open(filename, 'r') as f: jsonstr = f.read() jsondata = json.loads(jsonstr) # make sure the name becomes camelcase without spaces: required by CLaSH namestr = jsondata['name'].strip() namestr = string.capwords(namestr) namestr = namestr.replace(' ', '') self.name = namestr # Load the predefined clash types when available if 'clashtypes' in jsondata.keys(): self.clashtypes = jsondata['clashtypes'] # Load all nodes and their attributes for jsnode in jsondata['nodes']: nodeName = jsnode['name'] nodeFunction = jsnode['function'] nodeClashCode = '' if 'clashcode' in jsnode.keys(): nodeClashCode = jsnode['clashcode'] nodeColor = self.DEFAULT_NODE_COLOR if 'color' in jsnode.keys(): nodeColor = jsnode['color'] nodePosition = jsnode['pos'][0], jsnode['pos'][1] self.add_node(nodeName, nodeFunction, nodePosition, clashcode=nodeClashCode, color=nodeColor) # Load all edges and their attributes for jsedge in jsondata['edges']: edgeSource = jsedge['src'] edgeDestination = jsedge['dst'] edgeResNumber = jsedge['resnr'] edgeArgNumber = jsedge['argnr'] edgePRates = CSDFGraph._flattenRateList(jsedge.get('prates', [1])) edgeCRates = CSDFGraph._flattenRateList(jsedge.get('crates', [1])) edgeTokens = jsedge.get('tkns', []) edgeColor = self.DEFAULT_EDGE_COLOR if 'color' in jsedge.keys(): edgeColor = jsedge['color'] self.add_edge(edgeSource, edgeDestination, edgeResNumber, edgeArgNumber, edgePRates, edgeCRates, edgeTokens, color=edgeColor) # Now that the graph is construcuted, validate it: self.validateGraph()
def editCRates(self, src, dst, newCRates): Log.addLogMessage(Log.INFO, 'Consumption rates for edge ' + str((src, dst)) + ' updated to ' + str(newCRates)) self.graphData[src][dst]['crates'] = newCRates newCRates = str(newCRates) self.graphData.updateCRates((src, dst), newCRates)
def storeToFile(self, filename=''): """ Stores the current graph in a JSON file. Parameters ---------- filename : string with filepath filename is an optional argument containing the file in which graph is stored. When this argument is not used, the graph is stored in the file from which it was initially read. """ if filename == '': # no file name given so use file from which this graph is made fname = self.filename else: fname = filename # Put all info into a temporary dictionary which will be transformed into a json string graphDict = OrderedDict({}) # First save graph properties/attributes: name and predefined CLaSH types graphDict['name'] = self.name if self.clashtypes is not None: graphDict['clashtypes'] = self.clashtypes # Store all the nodes of the graph in the temporary dictionary nodesList = [] for nname in self.nodes(): nodedict = OrderedDict({}) nodedict['name'] = nname nodedict['function'] = self.node[nname]['funcstr'] if self.node[nname]['clashcode'] != '': nodedict['clashcode'] = self.node[nname]['clashcode'] nodedict['pos'] = list(self.node[nname]['pos']) nodedict['color'] = self.node[nname]['color'] nodesList.append(nodedict) # add all nodes to temporary dict in form of a list graphDict['nodes'] = nodesList # Store all the edges of the graph in the temporary dictionary edgesList = [] for srcname, dstname in self.edges(): edgedict = OrderedDict({}) edgedict['src'] = srcname edgedict['dst'] = dstname edgedict['resnr'] = self[srcname][dstname]['res'] edgedict['argnr'] = self[srcname][dstname]['arg'] edgedict['prates'] = self[srcname][dstname]['prates'] edgedict['crates'] = self[srcname][dstname]['crates'] edgedict['tkns'] = self[srcname][dstname]['tkns'] edgedict['color'] = self[srcname][dstname]['color'] edgesList.append(edgedict) # add all edges to temporary dict in form of a list graphDict['edges'] = edgesList # Last but not leat, write the graph to the file with open(fname, 'w') as outfile: json.dump(graphDict, outfile, indent=4) Log.addLogMessage(Log.INFO, 'Saved graph ' + fname)