Showing
1 changed file
with
266 additions
and
266 deletions
... | @@ -3,115 +3,64 @@ snakes.plugins.load(['gv', 'ops'], 'snakes.nets', 'nets') | ... | @@ -3,115 +3,64 @@ snakes.plugins.load(['gv', 'ops'], 'snakes.nets', 'nets') |
3 | from nets import * | 3 | from nets import * |
4 | 4 | ||
5 | 5 | ||
6 | - | ||
7 | -############################################################################### | ||
8 | ############################################################################### | 6 | ############################################################################### |
9 | -############################### INIT ########################################## | 7 | +## AUXILIARY FUNCTIONS ######################################################## |
10 | - | 8 | + |
11 | -# entities: tuple of name of the entities, initial level, tuple of decays 0 | 9 | +def clockt(obligatory, name, ls, us, lambdas, deltas, inputlist, D): |
12 | -# denotes unbounded decay (omega) | 10 | + '''This function computes the action of clock + decay + obligatory |
13 | -#entities = ( ('B',4, (0,2,2,2,3)), ('P',0, (0,0)), ('C',0, (0,0)), ('G',0, (0,0))) | 11 | + activities. |
14 | - | 12 | + obligatory = set of obligatory activities |
15 | - | 13 | + name = entity concerned |
16 | -#entities = ( ('Sugar',1, (0,2)), ('Aspartame',0, (0,2)), ('Glycemia',2, (0,2,2,2)), ('Glucagon',0, (0,2)), ('Insulin',0,(0,2,2))) | 14 | + ls = ls of entity |
17 | - | 15 | + us = us of entity |
18 | - | 16 | + lambdas = lambdas of entity |
19 | -entities = ( ('s1',0, (0,1)), ('s2',0, (0,1)), ('s3',0, (0,1)) ) | 17 | + deltas = list of the decays duration of entity |
20 | - | 18 | + inputlist = tuples for all other entities''' |
21 | - | 19 | + |
22 | - | 20 | + #print (obligatory, name, ls, us, lambdas, deltas, inputlist,D) |
23 | -# Activities: Tuple of (activators, inhibitors, results, duration) | ||
24 | -# activators, inhibitors are dictionaries of pairs (entity, level) | ||
25 | -# results are dictionaries of pairs (entity, +z) | ||
26 | - | ||
27 | -# potential activities | ||
28 | -#potential = ((dict([('P',0)]),dict([('P',1)]),dict([('P',1)]),0), | ||
29 | -# (dict([('P',1)]),dict(),dict([('P',-1)]),0), | ||
30 | -# (dict([('C',0)]),dict([('C',1)]),dict([('C',1)]),0), | ||
31 | -# (dict([('C',1)]),dict(),dict([('C',-1)]),0), | ||
32 | -# (dict([('G',0)]),dict([('G',1)]),dict([('G',1)]),0), | ||
33 | -# (dict([('G',1)]),dict(),dict([('G',-1)]),0) ) | ||
34 | - | ||
35 | -# potential = ((dict([('Sugar',1)]),dict(),dict([('Insulin',1),('Glycemia',1)]),0), | ||
36 | -# (dict([('Aspartame',1)]),dict(),dict([('Insulin',1)]),0), | ||
37 | -# (dict(),dict([('Glycemia',1)]),dict([('Glucagon',1)]),0), | ||
38 | -# (dict([('Glycemia',3)]),dict(),dict([('Insulin',1)]),0), | ||
39 | -# (dict([('Insulin',2)]),dict(),dict([('Glycemia',-1)]),0), | ||
40 | -# (dict([('Insulin',1),('Glycemia',3)]), dict(), dict([('Glycemia',-1)]),0), | ||
41 | -# (dict([('Insulin',1)]),dict([('Glycemia',2)]),dict([('Glycemia',-1)]),0), | ||
42 | -# (dict([('Glucagon',1)]),dict(),dict([('Glycemia',+1)]),0) ) | ||
43 | - | ||
44 | -potential = ( (dict(),dict([('s1',1)]),dict([('s2',1)]), 1), | ||
45 | - (dict(),dict([('s2',1)]),dict([('s3',1)]), 1), | ||
46 | - (dict(),dict([('s3',1)]),dict([('s1',1)]), 1) ) | ||
47 | - | ||
48 | - | ||
49 | - | ||
50 | -# obligatory activities | ||
51 | -#obligatory = ( (dict([('P',1)]),dict(),dict([('B',1)]),1), | ||
52 | -# (dict([('C',1)]),dict(),dict([('B',-1)]),3), | ||
53 | -# (dict([('G',1)]),dict(),dict([('B',-2)]),3)) | ||
54 | - | ||
55 | -obligatory = () | ||
56 | -############################### END ########################################## | ||
57 | -############################################################################### | ||
58 | -############################################################################### | ||
59 | - | ||
60 | - | ||
61 | -############################################################################### | ||
62 | -############################################################################### | ||
63 | -############################ AUXILIARY FUNCTIONS ############################## | ||
64 | - | ||
65 | -# This function computes the action of clock + decay + obligatory activities | ||
66 | -# obligatory = set of obligatory activities | ||
67 | -# name = entity concerned | ||
68 | -# ls = ls of entity | ||
69 | -# us = us of entity | ||
70 | -# lambdas = lambdas of entity | ||
71 | -# deltas = list of the decays duration of entity | ||
72 | -# inputlist = tuples for all other entities | ||
73 | -def clockt (obligatory, name, ls, us, lambdas, deltas, inputlist,D) : | ||
74 | - | ||
75 | - | ||
76 | - print (obligatory, name, ls, us, lambdas, deltas, inputlist,D) | ||
77 | l1=ls | 21 | l1=ls |
78 | u1=us | 22 | u1=us |
79 | lambda1=[] | 23 | lambda1=[] |
80 | 24 | ||
81 | # progression of time in lambda | 25 | # progression of time in lambda |
82 | - for i in range(0,len(lambdas)): lambda1.append(min(lambdas[i]+1, D)) | 26 | + for i in range(0,len(lambdas)): |
27 | + lambda1.append(min(lambdas[i]+1, D)) | ||
83 | 28 | ||
84 | # progression of time for u (only for bounded levels) | 29 | # progression of time for u (only for bounded levels) |
85 | - if deltas[ls] <> 0 : u1=us+1 | 30 | + if deltas[ls] <> 0: |
31 | + u1=us+1 | ||
86 | 32 | ||
87 | # decay | 33 | # decay |
88 | - if us+1 > deltas[ls] : | 34 | + if us+1 > deltas[ls]: |
89 | l1 = max(0, ls -1) | 35 | l1 = max(0, ls -1) |
90 | u1 = 0 | 36 | u1 = 0 |
91 | 37 | ||
92 | # search of obligatory activities where entity name is in results | 38 | # search of obligatory activities where entity name is in results |
93 | act = [] | 39 | act = [] |
94 | - for alpha in range(0, len(obligatory)) : | 40 | + for alpha in range(0, len(obligatory)): |
95 | obname = 'ob'+str(alpha) | 41 | obname = 'ob'+str(alpha) |
96 | - if name in obligatory[alpha][2] and inputlist[obname]>= obligatory[alpha][3]: | 42 | + if name in obligatory[alpha][2] \ |
43 | + and inputlist[obname]>= obligatory[alpha][3]: | ||
97 | act.append(obligatory[alpha]) | 44 | act.append(obligatory[alpha]) |
98 | 45 | ||
99 | - | ||
100 | - | ||
101 | # computation of the effect on entity name | 46 | # computation of the effect on entity name |
102 | - for alpha in range(0, len(act)) : | 47 | + for alpha in range(0, len(act)): |
103 | # check if the obligatory activity is enabled or not | 48 | # check if the obligatory activity is enabled or not |
104 | check = 0 | 49 | check = 0 |
105 | activators = act[alpha][0] | 50 | activators = act[alpha][0] |
106 | - for ent in activators : | 51 | + for ent in activators: |
107 | t = inputlist[ent] | 52 | t = inputlist[ent] |
108 | - if t[0] >= activators[ent] and t[2][activators[ent]] >= act[alpha][3] : check = 1 | 53 | + if t[0] >= activators[ent] \ |
54 | + and t[2][activators[ent]] >= act[alpha][3]: | ||
55 | + check = 1 | ||
109 | inhibitors = act[alpha][1] | 56 | inhibitors = act[alpha][1] |
110 | - for ent in inhibitors : | 57 | + for ent in inhibitors: |
111 | t = inputlist[ent] | 58 | t = inputlist[ent] |
112 | - if t[0] < inhibitors[ent] and t[2][inhibitors[ent]] >= act[alpha][3] : check = 1 | 59 | + if t[0] < inhibitors[ent] \ |
60 | + and t[2][inhibitors[ent]] >= act[alpha][3]: | ||
61 | + check = 1 | ||
113 | # if enabled compute the effect | 62 | # if enabled compute the effect |
114 | - if check : | 63 | + if check: |
115 | z = act[alpha][2][name] | 64 | z = act[alpha][2][name] |
116 | l1 = max(0, min(l1 + z, len(lambda1)-1)) | 65 | l1 = max(0, min(l1 + z, len(lambda1)-1)) |
117 | u1 = 0 | 66 | u1 = 0 |
... | @@ -119,213 +68,264 @@ def clockt (obligatory, name, ls, us, lambdas, deltas, inputlist,D) : | ... | @@ -119,213 +68,264 @@ def clockt (obligatory, name, ls, us, lambdas, deltas, inputlist,D) : |
119 | # update lambda with the proper dates | 68 | # update lambda with the proper dates |
120 | temp = l1 - ls | 69 | temp = l1 - ls |
121 | if temp > 0 : | 70 | if temp > 0 : |
122 | - for i in range(ls+1,l1) : lambda1[i]=0 | 71 | + for i in range(ls+1,l1): |
123 | - if temp < 0 : | 72 | + lambda1[i]=0 |
124 | - for i in range(l1+1,ls) : lambda1[i]=0 | 73 | + if temp < 0: |
74 | + for i in range(l1+1,ls): | ||
75 | + lambda1[i]=0 | ||
125 | 76 | ||
126 | return (l1, u1, tuple(lambda1)) | 77 | return (l1, u1, tuple(lambda1)) |
127 | 78 | ||
128 | 79 | ||
129 | -# This function computes the action of clock on obligatory activities places | ||
130 | -# obligatory = set of obligatory activities | ||
131 | -# name = obligatory activity under consideration | ||
132 | -# w = current value | ||
133 | -# inputlist = tuples for all other entities | ||
134 | def clockbetat (obligatory, name, w, inputlist,D) : | 80 | def clockbetat (obligatory, name, w, inputlist,D) : |
81 | + '''This function computes the action of clock on obligatory activities | ||
82 | + places. | ||
83 | + obligatory = set of obligatory activities | ||
84 | + name = obligatory activity under consideration | ||
85 | + w = current value | ||
86 | + inputlist = tuples for all other entities''' | ||
87 | + | ||
135 | check = 0 | 88 | check = 0 |
136 | activators = obligatory[name][0] | 89 | activators = obligatory[name][0] |
137 | - for ent in activators : | 90 | + for ent in activators: |
138 | t = inputlist[ent] | 91 | t = inputlist[ent] |
139 | - if t[0] >= activators[ent] and t[2][activators[ent]] >= obligatory[name][3] : check = 1 | 92 | + if t[0] >= activators[ent] \ |
93 | + and t[2][activators[ent]] >= obligatory[name][3]: | ||
94 | + check = 1 | ||
140 | inhibitors = obligatory[name][1] | 95 | inhibitors = obligatory[name][1] |
141 | - for ent in inhibitors : | 96 | + for ent in inhibitors: |
142 | t = inputlist[ent] | 97 | t = inputlist[ent] |
143 | - if t[0] < inhibitors[ent] and t[2][inhibitors[ent]] >= obligatory[name][3] : check = 1 | 98 | + if t[0] < inhibitors[ent] \ |
144 | - # if enabled compute the effect | 99 | + and t[2][inhibitors[ent]] >= obligatory[name][3]: |
145 | - if check and w >= obligatory[name][3] : return(0) | 100 | + check = 1 |
146 | - else: return(min(w+1, D)) | 101 | + # if enabled compute the effect |
147 | - | 102 | + if check and w >= obligatory[name][3]: |
148 | - | 103 | + return(0) |
149 | - | 104 | + else: |
150 | - | 105 | + return(min(w+1, D)) |
151 | 106 | ||
152 | -# this function computes the action on an entity of a potential activity | ||
153 | -# name = entity under consideration | ||
154 | -# lp, up, lambdap = its values | ||
155 | -# R = set of results of the activity | ||
156 | def potentialt (name, lp, up, lambdap, R) : | 107 | def potentialt (name, lp, up, lambdap, R) : |
157 | - print (name, lp, up, lambdap, R) | 108 | + '''This function computes the action on an entity of a potential activity. |
158 | - # entity is a result? | 109 | + name = entity under consideration |
159 | - if (name in R) : | 110 | + lp, up, lambdap = its values |
111 | + R = set of results of the activity''' | ||
112 | + | ||
113 | + #print (name, lp, up, lambdap, R) | ||
114 | + # is entity a result? | ||
115 | + if name in R: | ||
160 | lambda2 = list(lambdap) | 116 | lambda2 = list(lambdap) |
161 | levelp = max(0,min(len(lambdap)-1, lp+R[name])) | 117 | levelp = max(0,min(len(lambdap)-1, lp+R[name])) |
162 | change = levelp-lp | 118 | change = levelp-lp |
163 | - if change > 0 : | 119 | + if change > 0: |
164 | for i in range(lp+1,levelp+1): | 120 | for i in range(lp+1,levelp+1): |
165 | lambda2[i]=0 | 121 | lambda2[i]=0 |
166 | - if change < 0 : | 122 | + if change < 0: |
167 | for i in range(levelp+1,lp+1): | 123 | for i in range(levelp+1,lp+1): |
168 | lambda2[i]=0 | 124 | lambda2[i]=0 |
169 | - return (levelp, 0,tuple(lambda2)) | 125 | + return (levelp, 0, tuple(lambda2)) |
170 | else: | 126 | else: |
171 | return (lp, up, lambdap) | 127 | return (lp, up, lambdap) |
172 | 128 | ||
173 | -############################## END #################################### | 129 | +## END ######################################################################## |
174 | ############################################################################### | 130 | ############################################################################### |
175 | -############################################################################### | ||
176 | - | ||
177 | - | ||
178 | -####################### MAIN ################################################## | ||
179 | - | ||
180 | -# compute maximal duration of activities | ||
181 | -D=0 | ||
182 | -for alpha in potential : D = max(D, alpha[3]) | ||
183 | - | ||
184 | -for alpha in obligatory : D = max(D, alpha[3]) | ||
185 | - | ||
186 | -n = PetriNet('andy') | ||
187 | - | ||
188 | -n.globals["obligatory"] = obligatory | ||
189 | -n.globals["D"] = D | ||
190 | -n.globals["clockt"] = clockt | ||
191 | -n.globals["clockbetat"] = clockbetat | ||
192 | -n.globals["potentialt"] = potentialt | ||
193 | - | ||
194 | -################# Places for entities | ||
195 | -for i in range(0,len(entities)): | ||
196 | - name=entities[i][0] | ||
197 | - level = entities[i][1] | ||
198 | - deltas = entities[i][2] | ||
199 | - vector = [0]*len(deltas) | ||
200 | - n.add_place(Place(name, [(level,0, tuple(vector))])) | ||
201 | - | ||
202 | -################# clock transition | ||
203 | -inputlist = dict() | ||
204 | -n.globals["inputlist"] = inputlist | ||
205 | - | ||
206 | -n.add_transition(Transition('tc')) | ||
207 | - | ||
208 | - | ||
209 | -# connect all obligatory clocks | ||
210 | -for i in range(0,len(obligatory)): | ||
211 | - # transition name | ||
212 | - obname = 'ob'+str(i) | ||
213 | - # for every obligatory activity connect corresponding place to clock | ||
214 | - n.add_place(Place('p'+obname, [0])) | ||
215 | - n.add_input('p'+obname, 'tc', Variable('w'+obname)) | ||
216 | - inputlist.update({obname:'w'+obname}) | ||
217 | - | ||
218 | - | ||
219 | -# all entities are connected | ||
220 | -for i in range(0,len(entities)): | ||
221 | - name=entities[i][0] | ||
222 | - deltas = entities[i][2] | ||
223 | - n.globals["deltas"+name] = deltas | ||
224 | - n.globals[name] = name | ||
225 | - n.add_input(name, 'tc', Tuple([Variable('l'+name), Variable('u'+name), Variable('lambda'+name) ])) | ||
226 | - inputlist.update({name:['l'+name, 'u'+name, 'lambda'+name ]}) | ||
227 | - | ||
228 | - | ||
229 | - | ||
230 | -for i in range(0,len(entities)): | ||
231 | - name=entities[i][0] | ||
232 | - n.add_output(name, 'tc', Expression("clockt(obligatory,"+name+",l"+name+',u'+name+',lambda'+name+',deltas'+name+',inputlist,D)')) | ||
233 | - | ||
234 | - | ||
235 | -for i in range(0,len(obligatory)): | ||
236 | - obname = 'ob'+str(i) | ||
237 | - # for every obligatory activity connect corresponding place to clock | ||
238 | - n.add_output('p'+obname, 'tc', Expression("clockbetat(obligatory,"+str(i)+',w'+obname+',inputlist,D)')) | ||
239 | - | ||
240 | - | ||
241 | -## potential activities | ||
242 | -for i in range(0,len(potential)): | ||
243 | - # transition name | ||
244 | - trname = 'tr'+str(i) | ||
245 | - | ||
246 | - # for every potential activity connect corresponding place to clock | ||
247 | - n.add_place(Place('p'+trname, [0])) | ||
248 | - n.add_input('p'+trname, 'tc', Variable('w'+trname)) | ||
249 | - n.add_output('p'+trname, 'tc', Expression('min(D,w'+trname+'+1)')) | ||
250 | - | ||
251 | - | ||
252 | - activators = potential[i][0] | ||
253 | - inhibitors = potential[i][1] | ||
254 | - results = potential[i][2] | ||
255 | - print results | ||
256 | - n.globals["results"+trname] = results | ||
257 | - duration = potential[i][3] | ||
258 | - | ||
259 | - # compute entities involved in the activity | ||
260 | - nameactivators = activators.keys() | ||
261 | - nameinhib = inhibitors.keys() | ||
262 | - nameresults = results.keys() | ||
263 | - names = [] | ||
264 | - # check they appear only once | ||
265 | - for i in nameactivators : names.append(i) | ||
266 | - for i in nameinhib: | ||
267 | - if not (i in activators) : names.append(i) | ||
268 | - for i in nameresults: | ||
269 | - if not ( (i in activators) or (i in inhibitors)) : names.append(i) | ||
270 | - | ||
271 | - # compute guard of the activity | ||
272 | - # activity may be executed once every dur | ||
273 | - guard = 'w>='+str(duration) | ||
274 | - | ||
275 | - # activators | ||
276 | - for j in range(0,len(nameactivators)) : | ||
277 | - spec = nameactivators[j] | ||
278 | - level = str(activators[nameactivators[j]]) | ||
279 | - guard += ' and l'+spec+'>= '+ level + ' and lambda' +spec+'['+level+']>='+str(duration) | ||
280 | - | ||
281 | - # inhibitors | ||
282 | - for j in range(0,len(nameinhib)) : | ||
283 | - spec = nameinhib[j] | ||
284 | - level = str(inhibitors[nameinhib[j]]) | ||
285 | - guard += ' and l'+spec+'< '+ level + ' and lambda' +spec+'['+level+']>='+str(duration) | ||
286 | - | ||
287 | - n.add_transition(Transition(trname, Expression(guard))) | ||
288 | - n.add_input('p'+trname, trname, Variable('w')) | ||
289 | - n.add_output('p'+trname, trname, Expression('0')) | ||
290 | - | ||
291 | - # arcs of the transition from and to involved entities | ||
292 | - for j in range(0,len(names)) : | ||
293 | - n.add_input(names[j], trname, Tuple([Variable('l'+names[j]), Variable('u'+names[j]), Variable('lambda'+names[j]) ])) | ||
294 | - n.add_output(names[j], trname, Expression("potentialt(" +names[j]+",l"+names[j]+',u'+names[j]+',lambda'+names[j]+', results'+trname+')')) | ||
295 | 131 | ||
296 | 132 | ||
133 | +############################################################################### | ||
134 | +## MAIN ####################################################################### | ||
135 | +def andy2snakes(entities, potential, obligatory): | ||
136 | + # compute maximal duration of activities | ||
137 | + D=0 | ||
138 | + for alpha in potential : D = max(D, alpha[3]) | ||
139 | + | ||
140 | + for alpha in obligatory : D = max(D, alpha[3]) | ||
141 | + | ||
142 | + n = PetriNet('andy') | ||
143 | + | ||
144 | + n.globals["obligatory"] = obligatory | ||
145 | + n.globals["D"] = D | ||
146 | + n.globals["clockt"] = clockt | ||
147 | + n.globals["clockbetat"] = clockbetat | ||
148 | + n.globals["potentialt"] = potentialt | ||
149 | + | ||
150 | + ################# Places for entities | ||
151 | + for i in range(0,len(entities)): | ||
152 | + name=entities[i][0] | ||
153 | + level = entities[i][1] | ||
154 | + deltas = entities[i][2] | ||
155 | + vector = [0]*len(deltas) | ||
156 | + n.add_place(Place(name, [(level,0, tuple(vector))])) | ||
157 | + | ||
158 | + ################# clock transition | ||
159 | + inputlist = dict() | ||
160 | + n.globals["inputlist"] = inputlist | ||
161 | + | ||
162 | + n.add_transition(Transition('tc')) | ||
163 | + | ||
164 | + | ||
165 | + # connect all obligatory clocks | ||
166 | + for i in range(0,len(obligatory)): | ||
167 | + # transition name | ||
168 | + obname = 'ob'+str(i) | ||
169 | + # for every obligatory activity connect corresponding place to clock | ||
170 | + n.add_place(Place('p'+obname, [0])) | ||
171 | + n.add_input('p'+obname, 'tc', Variable('w'+obname)) | ||
172 | + inputlist.update({obname:'w'+obname}) | ||
173 | + | ||
174 | + | ||
175 | + # all entities are connected | ||
176 | + for i in range(0,len(entities)): | ||
177 | + name=entities[i][0] | ||
178 | + deltas = entities[i][2] | ||
179 | + n.globals["deltas"+name] = deltas | ||
180 | + n.globals[name] = name | ||
181 | + n.add_input(name, 'tc', Tuple([Variable('l'+name), Variable('u'+name), Variable('lambda'+name) ])) | ||
182 | + inputlist.update({name:['l'+name, 'u'+name, 'lambda'+name ]}) | ||
183 | + | ||
184 | + | ||
185 | + | ||
186 | + for i in range(0,len(entities)): | ||
187 | + name=entities[i][0] | ||
188 | + n.add_output(name, 'tc', Expression("clockt(obligatory,"+name+",l"+name+',u'+name+',lambda'+name+',deltas'+name+',inputlist,D)')) | ||
189 | + | ||
190 | + | ||
191 | + for i in range(0,len(obligatory)): | ||
192 | + obname = 'ob'+str(i) | ||
193 | + # for every obligatory activity connect corresponding place to clock | ||
194 | + n.add_output('p'+obname, 'tc', Expression("clockbetat(obligatory,"+str(i)+',w'+obname+',inputlist,D)')) | ||
195 | + | ||
196 | + | ||
197 | + ## potential activities | ||
198 | + for i in range(0,len(potential)): | ||
199 | + # transition name | ||
200 | + trname = 'tr'+str(i) | ||
201 | + | ||
202 | + # for every potential activity connect corresponding place to clock | ||
203 | + n.add_place(Place('p'+trname, [0])) | ||
204 | + n.add_input('p'+trname, 'tc', Variable('w'+trname)) | ||
205 | + n.add_output('p'+trname, 'tc', Expression('min(D,w'+trname+'+1)')) | ||
206 | + | ||
207 | + | ||
208 | + activators = potential[i][0] | ||
209 | + inhibitors = potential[i][1] | ||
210 | + results = potential[i][2] | ||
211 | + print results | ||
212 | + n.globals["results"+trname] = results | ||
213 | + duration = potential[i][3] | ||
214 | + | ||
215 | + # compute entities involved in the activity | ||
216 | + nameactivators = activators.keys() | ||
217 | + nameinhib = inhibitors.keys() | ||
218 | + nameresults = results.keys() | ||
219 | + names = [] | ||
220 | + # check they appear only once | ||
221 | + for i in nameactivators : names.append(i) | ||
222 | + for i in nameinhib: | ||
223 | + if not (i in activators) : names.append(i) | ||
224 | + for i in nameresults: | ||
225 | + if not ( (i in activators) or (i in inhibitors)) : names.append(i) | ||
226 | + | ||
227 | + # compute guard of the activity | ||
228 | + # activity may be executed once every dur | ||
229 | + guard = 'w>='+str(duration) | ||
230 | + | ||
231 | + # activators | ||
232 | + for j in range(0,len(nameactivators)) : | ||
233 | + spec = nameactivators[j] | ||
234 | + level = str(activators[nameactivators[j]]) | ||
235 | + guard += ' and l'+spec+'>= '+ level + ' and lambda' +spec+'['+level+']>='+str(duration) | ||
236 | + | ||
237 | + # inhibitors | ||
238 | + for j in range(0,len(nameinhib)) : | ||
239 | + spec = nameinhib[j] | ||
240 | + level = str(inhibitors[nameinhib[j]]) | ||
241 | + guard += ' and l'+spec+'< '+ level + ' and lambda' +spec+'['+level+']>='+str(duration) | ||
242 | + | ||
243 | + n.add_transition(Transition(trname, Expression(guard))) | ||
244 | + n.add_input('p'+trname, trname, Variable('w')) | ||
245 | + n.add_output('p'+trname, trname, Expression('0')) | ||
246 | + | ||
247 | + # arcs of the transition from and to involved entities | ||
248 | + for j in range(0,len(names)) : | ||
249 | + n.add_input(names[j], trname, Tuple([Variable('l'+names[j]), Variable('u'+names[j]), Variable('lambda'+names[j]) ])) | ||
250 | + n.add_output(names[j], trname, Expression("potentialt(" +names[j]+",l"+names[j]+',u'+names[j]+',lambda'+names[j]+', results'+trname+')')) | ||
251 | + | ||
252 | + return n | ||
297 | 253 | ||
298 | ######## depict Petri net | 254 | ######## depict Petri net |
299 | -n.draw("repress.ps") | 255 | +def draw_net(net, out_name='repress'): |
300 | - | 256 | + net.draw(out_name+'.ps') |
301 | -s = StateGraph(n) | 257 | + |
302 | -s.build() | 258 | +def draw_stategraph(net, entities_names, out_name='repressgraph', |
303 | - | 259 | + with_dot=True): |
304 | -def node_attr (state, graph, attr) : | 260 | + def node_attr (state, graph, attr) : |
305 | - # attr['label'] = str(state) | 261 | + marking = graph[state] |
306 | - marking = graph[state] | 262 | + attr["label"] = ":".join( str(list(marking(s))[0][0]) |
307 | - attr["label"] = ":".join(str(list(marking(s))[0][0]) | 263 | + for s in entities_names ) |
308 | - for s in ("s1", "s2", "s3")) | 264 | + def edge_attr (trans, mode, attr) : |
309 | - | 265 | + attr["label"] = trans.name |
310 | -def edge_attr (trans, mode, attr) : | 266 | + |
311 | - attr["label"] = trans.name | 267 | + s = StateGraph(net) |
312 | - | 268 | + s.build() |
313 | -s.draw('repressgraph.ps', node_attr=node_attr, edge_attr=edge_attr, | 269 | + |
314 | - engine="dot") | 270 | + s.draw(out_name+'.ps', node_attr=node_attr, edge_attr=edge_attr, |
315 | - | 271 | + engine='dot') |
316 | -g = s.draw(None, node_attr=node_attr, edge_attr=edge_attr, engine="dot") | 272 | + |
317 | -with open("repressgraph.dot", "w") as out : | 273 | + if with_dot: |
318 | - out.write(g.dot()) | 274 | + g = s.draw(None, node_attr=node_attr, edge_attr=edge_attr, |
319 | - | 275 | + engine='dot') |
320 | -g.render("repressgraph-layout.dot", engine="dot") | 276 | + with open(out_name+".dot", "w") as out: |
321 | - | 277 | + out.write(g.dot()) |
322 | -# t=n.transition('tr0') | 278 | + g.render(out_name+"-layout.dot", engine="dot") |
323 | -# m=t.modes() | 279 | + |
324 | -# print m | 280 | +if __name__=='__main__': |
325 | -# t.fire(m[0]) | 281 | + # entities: tuple of name of the entities, initial level, tuple of decays 0 |
326 | -# n.draw('repr1.ps') | 282 | + # denotes unbounded decay (omega) |
327 | -# t1=n.transition('tc') | 283 | + # examples: |
328 | -# m1=t1.modes() | 284 | + # entities = ( ('B',4, (0,2,2,2,3)), ('P',0, (0,0)), ('C',0, (0,0)), |
329 | -# print m1 | 285 | + # ('G',0, (0,0)) ) |
330 | -# t1.fire(m1[0]) | 286 | + # entities = ( ('Sugar',1, (0,2)), ('Aspartame',0, (0,2)), |
331 | -# n.draw('repr1.ps') | 287 | + # ('Glycemia',2, (0,2,2,2)), ('Glucagon',0, (0,2)), |
288 | + # ('Insulin',0,(0,2,2)) ) | ||
289 | + | ||
290 | + entities = ( ('s1',0, (0,1)), ('s2',0, (0,1)), ('s3',0, (0,1)) ) | ||
291 | + | ||
292 | + # Activities: Tuple of (activators, inhibitors, results, duration) | ||
293 | + # activators, inhibitors are dictionaries of pairs | ||
294 | + # (entity, level) | ||
295 | + # results are dictionaries of pairs (entity, +z) | ||
296 | + | ||
297 | + # potential activities examples: | ||
298 | + # potential = ( (dict([('P',0)]),dict([('P',1)]),dict([('P',1)]),0), | ||
299 | + # (dict([('P',1)]),dict(),dict([('P',-1)]),0), | ||
300 | + # (dict([('C',0)]),dict([('C',1)]),dict([('C',1)]),0), | ||
301 | + # (dict([('C',1)]),dict(),dict([('C',-1)]),0), | ||
302 | + # (dict([('G',0)]),dict([('G',1)]),dict([('G',1)]),0), | ||
303 | + # (dict([('G',1)]),dict(),dict([('G',-1)]),0) ) | ||
304 | + # potential = ( (dict([('Sugar',1)]),dict(), | ||
305 | + # dict([('Insulin',1),('Glycemia',1)]),0), | ||
306 | + # (dict([('Aspartame',1)]),dict(),dict([('Insulin',1)]),0), | ||
307 | + # (dict(),dict([('Glycemia',1)]),dict([('Glucagon',1)]),0), | ||
308 | + # (dict([('Glycemia',3)]),dict(),dict([('Insulin',1)]),0), | ||
309 | + # (dict([('Insulin',2)]),dict(),dict([('Glycemia',-1)]),0), | ||
310 | + # (dict([('Insulin',1),('Glycemia',3)]), dict(), | ||
311 | + # dict([('Glycemia',-1)]),0), | ||
312 | + # (dict([('Insulin',1)]),dict([('Glycemia',2)]), | ||
313 | + # dict([('Glycemia',-1)]),0), | ||
314 | + # (dict([('Glucagon',1)]),dict(),dict([('Glycemia',+1)]),0) | ||
315 | + # ) | ||
316 | + | ||
317 | + potential = ( (dict(), dict([('s1',1)]), dict([('s2',1)]), 1), | ||
318 | + (dict(), dict([('s2',1)]), dict([('s3',1)]), 1), | ||
319 | + (dict(), dict([('s3',1)]), dict([('s1',1)]), 1) ) | ||
320 | + | ||
321 | + # obligatory activities examples: | ||
322 | + # obligatory = ( (dict([('P',1)]),dict(),dict([('B',1)]),1), | ||
323 | + # (dict([('C',1)]),dict(),dict([('B',-1)]),3), | ||
324 | + # (dict([('G',1)]),dict(),dict([('B',-2)]),3)) | ||
325 | + | ||
326 | + obligatory = () | ||
327 | + | ||
328 | + net = andy2snakes(entities, potential, obligatory) | ||
329 | + draw_net(net, out_name="repress") | ||
330 | + draw_stategraph(net, ("s1", "s2", "s3"), out_name="repressgraph") | ||
331 | + | ... | ... |
-
Please register or login to post a comment