synchgraph.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. '''This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
  2. Copyright 2011 by the AToMPM team and licensed under the LGPL
  3. See COPYING.lesser and README.md in the root of this project for full details'''
  4. from petrinet import *
  5. from threading import *
  6. import igraph as ig
  7. import Queue as q
  8. from random import choice
  9. class synchgraph:
  10. id = 0
  11. def __init__(self,modnum,M0):
  12. self.modnum = modnum
  13. self.sg = ig.Graph(0,directed=True)
  14. self.M0 = M0
  15. self.sg.add_vertices(1)
  16. self.sg.vs[0]['M'] = M0
  17. self.last = M0
  18. def summary(self):
  19. ig.summary(self.sg)
  20. def statePresent(self, M):
  21. for v in self.sg.vs:
  22. #for key,value in v['M']:
  23. if all(v['M'] == M):
  24. #print 'marking present in synchgraph'
  25. return v.index
  26. return -1
  27. def statePresentReach(self, M):
  28. for v in self.sg.vs:
  29. #for key,value in v['M']:
  30. if all(v['SM'] == M):
  31. #print 'marking present in synchgraph'
  32. return v.index
  33. return -1
  34. def addMarkingBatch(self,T,from_prod,to_prod):
  35. new = None
  36. for i in range(len(from_prod[0])):
  37. new = self.addMarking(from_prod[0][i],to_prod[0][i],T,self.last)
  38. self.last = new
  39. #self.graph(synchgraph.id);
  40. #synchgraph.id+=1
  41. def addMarking(self,Ms,Mnew,Arc,last):
  42. fr = self.statePresent(last)
  43. to = self.statePresent(Mnew)
  44. #self.last = Mnew
  45. if not to == -1:
  46. self.sg.add_edges([(fr,to)])
  47. self.sg.es[self.sg.get_eid(fr, to)]['T'] = '%s,%s'%(Ms,Arc)
  48. else:
  49. self.sg.add_vertices(1)
  50. to = self.sg.vcount()-1
  51. self.sg.vs[to]['M'] = Mnew
  52. self.sg.add_edges([(fr,to)])
  53. self.sg.es[self.sg.get_eid(fr, to)]['T'] = '%s,%s'%(Ms,Arc)
  54. return Mnew
  55. def markSCC(self, modules):
  56. for v in self.sg.vs:
  57. newval = []
  58. for value in v['M']:
  59. ls = value.split('-')
  60. new = '%s-%d'%(ls[0],modules[ls[0]].getSCCvid(ls[1]))
  61. newval.append(new)
  62. v['SM'] = newval
  63. def graph(self,id=None):
  64. key = choice(range(20))
  65. vattr=''
  66. eattr = ''
  67. nodes = {}
  68. graph = pydot.Dot(key, graph_type='digraph')
  69. dateTag = datetime.datetime.now().strftime("%Y-%b-%d_%H-%M-%S")
  70. for v in self.sg.vs:
  71. #sort(v['M'])
  72. # vattr +='('
  73. # i = len(v['M'])
  74. # leng = i
  75. # j=0
  76. if 'SM' in self.sg.vs.attribute_names():
  77. vattr+='ssc\n';
  78. for value in v['SM']:
  79. # if leng == 1:
  80. # if 'SCC' in self.sg.vs.attribute_names():
  81. # vattr +='SCC-%s\n'%v['SCC']
  82. # vattr = 'fstate%d'%choice(range(100))
  83. # else:
  84. #if int(value) > 0:
  85. # if 'SCC' in self.sg.vs.attribute_names():
  86. # vattr +='SCC-%s\n'%v['SCC']
  87. vattr += '%s'%(value.capitalize())
  88. else:
  89. for value in v['M']:
  90. # if leng == 1:
  91. # if 'SCC' in self.sg.vs.attribute_names():
  92. # vattr +='SCC-%s\n'%v['SCC']
  93. # vattr = 'fstate%d'%choice(range(100))
  94. # else:
  95. #if int(value) > 0:
  96. # if 'SCC' in self.sg.vs.attribute_names():
  97. # vattr +='SCC-%s\n'%v['SCC']
  98. vattr += '%s'%(value.capitalize())
  99. # if not i-1 == 0:
  100. # pass#vattr+=','
  101. # i -=1
  102. #j+=1
  103. #vattr +=')'
  104. nodes[v.index] = pydot.Node(vattr)
  105. graph.add_node(nodes[v.index])
  106. vattr = ''
  107. for e in self.sg.es:
  108. #pass
  109. #need to have later
  110. graph.add_edge(pydot.Edge(nodes[e.source],nodes[e.target],label=e['T']))
  111. #graph.write_svg('graphs/STATE%s%d%s.svg'%(self.key,choice(range(100)),dateTag))
  112. if id == None:
  113. graph.write_svg('../graphs/SYNCH%s%d%s.svg'%(key,choice(range(100)),dateTag))
  114. else:
  115. graph.write_svg('../graphs/SYNCH%d.svg'%(id))
  116. # def process(self,packet):
  117. # if packet.ismarking():
  118. # self.marking[packet.key()] = packet.payload()
  119. # else:
  120. #
  121. # if len(self.marking) == self.modnum and not self.wait:
  122. # self.createNode()
  123. #
  124. # def run(self):
  125. # #get initial states from the queue
  126. # while not self.done:
  127. # packet = self.queue.get(block=True, timeout=None)
  128. # self.process(packet)