-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.py
120 lines (87 loc) · 4.15 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Widget to display simulation data of a CSDF graph.
author: Sander Giesselink
"""
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QColor
from PyQt5.QtCore import Qt
from node import Node
from edge import Edge
from tokenCluster import TokenCluster
class Graph(QWidget):
def __init__(self, graphWidget, scene, view):
super().__init__()
self.graphWidget = graphWidget
self.scene = scene
self.view = view
self.edgeList = []
self.nodeList = []
self.clusterList = []
def clearGraph(self):
#Delete all items from the scene and clear lists
for i in range(len(self.nodeList)):
self.scene.removeItem(self.nodeList[i])
for i in range(len(self.edgeList)):
self.scene.removeItem(self.edgeList[i])
for i in range(len(self.clusterList)):
self.clusterList[i].deleteTokens()
self.scene.removeItem(self.clusterList[i])
self.nodeList.clear()
self.edgeList.clear()
self.clusterList.clear()
def addNode(self, x, y, name, func=[], clashCode='', color=Qt.red):
newNode = Node(self, self.view, name, func, clashCode, color)
newNode.setPos(x, y)
newNode.setZValue(0)
#Add node to the scene and list
self.scene.addItem(newNode)
self.nodeList.append(newNode)
def addEdge(self, beginPoint, endPoint, beginSide, endSide, edgeSelfLoops, src, dst, tokenValues, pRates, cRates, color):
newEdge = Edge(beginPoint, endPoint, beginSide, endSide, edgeSelfLoops, pRates, cRates, color)
#Place edges always behind nodes
newEdge.setZValue(1)
#Give edge a cluster of tokens
tokenCluster = TokenCluster(self, self.scene, self.view, newEdge, src, dst, tokenValues)
self.scene.addItem(tokenCluster)
self.clusterList.append(tokenCluster)
#Add edge to the scene and list
self.scene.addItem(newEdge)
self.edgeList.append(newEdge)
return newEdge
def addEdgeToNodes(self, beginNodeIndex, endNodeIndex, beginSide, endSide, src = '', dst = '', tokenValues = [], pRates = [0], cRates = [0], resnr = '', argnr = '', color=QColor(200,100,50)):
beginNode = self.nodeList[beginNodeIndex]
endNode = self.nodeList[endNodeIndex]
#Get points on the nodes that the edge can connect to
beginPoint = beginNode.getIOPointForEdge(beginSide, 2)
endPoint = endNode.getIOPointForEdge(endSide, 1)
#Set the beginNode as an output
beginNode.setIOType(beginSide, 2, resnr)
#Set the endNode as an input
endNode.setIOType(endSide, 1, argnr)
#Add new IO ports to nodes for future edges
beginNode.addNewIO(beginSide, 0)
endNode.addNewIO(endSide, 0)
#Create edge between the 2 nodes
edge = self.addEdge(beginPoint, endPoint, beginSide, endSide, beginNode == endNode, src, dst, tokenValues, pRates, cRates, color)
#Give both nodes a reference to the created edge
beginNode.addEdge(edge, 'begin')
endNode.addEdge(edge, 'end')
def updateTokens(self, edgeIndex, tokenValues):
self.scene.lockScene = True
self.clusterList[edgeIndex].newTokenValues(tokenValues)
def editTokens(self, src, dst, newTokens):
self.graphWidget.editTokens(src, dst, newTokens)
def editNodeFunction(self, name, newFunction):
self.graphWidget.editNodeFunction(name, newFunction)
def editNodePosition(self, name, newPos):
self.graphWidget.editNodePosition(name, newPos)
def editClashCode(self, name, newClashCode):
self.graphWidget.editClashCode(name, newClashCode)
def editPRates(self, src, dst, newPRates):
self.graphWidget.editPRates(src, dst, newPRates)
def editCRates(self, src, dst, newCRates):
self.graphWidget.editCRates(src, dst, newCRates)
def getFireCount(self, src_dst, node):
return self.graphWidget.getFireCount(src_dst, node)