-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPetrix2.js
380 lines (351 loc) · 11.1 KB
/
Petrix2.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// module PetriNets
var PetriNets = {
// returns a builder for composing Petri nets
builder: function(){
// processes are implemented as a matrix
// but details are hidden
var make_matrix = function( xdim, ydim ){
var rows = new Array();
for (var i=0; i<xdim; i++){
rows[i] = new Array();
}
return {
xlength: function(){ return xdim;},
ylength: function(){ return ydim; },
// get the value stored in (x,y)
get: function(x, y){
return rows[x][y];
},
// set a value in (x,y)
set: function(x, y, val){
rows[x][y] = val;
},
// return the row number x
get_row: function(x){
return rows[x];
},
};
};
// build a process from a matrix
var make_process = function( matrix ){
// a process is a matrix where entries are array of functions
// matrix[i][j] = set of processes (non deterministically) reachable performing input i and output j
return {
// name of the process
name:'unknown',
// returns the set of processes reachable consuming input i
go: function(name){
var result = new Array();
var row = matrix.get_row(name);
for ( i in row ){
if (row[i] !== undefined ){
result.concat( row[i] );
}
}
return result;
},
// returns the set of processes reachable from input-output action (i,j)
get: function(x, y){
return matrix.get(x,y);
},
// returns the number of input channels
get_input_size: function(){
return matrix.xlength();
},
// returns the number of output channels
get_output_size: function(){
return matrix.ylength();
},
// show the matrix representation of the process after a single step
print: function(){
var out = '';
// header
out += '\t';
for (var j=0; j<this.get_output_size(); j++){
out += j.toString(2) + '\t';
}
out += '\n';
for (var i=0; i<this.get_input_size(); i++){
// row number
out += i.toString(2) + '\t';
for (var j=0; j<this.get_output_size(); j++){
var array = this.get(i,j);
if (array !== undefined){
out += '{';
for (var k=0; k<array.length; k++){
// print(k);
var p = array[k];
// print( typeof p );
out += p().name + ', ';
}
out += '}';
} else {
out += '*';
}
out += '\t';
}
out += '\n';
}
return out;
},
};
};
// utility function, build an array made of a single process
var make_singleton = function(p){
var array = new Array();
var f = function(){ return p; };
array.push(f);
return array;
};
// definition of e and f
var empty_matrix = make_matrix(2,2);
var full_matrix = make_matrix(2,2);
var e = make_process(empty_matrix);
e.name = 'e';
var f = make_process(full_matrix);
f.name = 'f';
empty_matrix.set(0,0, make_singleton(e));
empty_matrix.set(0,1, undefined);
empty_matrix.set(1,0, make_singleton(f));
empty_matrix.set(1,1, undefined);
full_matrix.set(0,0, make_singleton(f));
full_matrix.set(0,1, make_singleton(e));
full_matrix.set(1,0, undefined);
full_matrix.set(1,1, make_singleton(f));
return {
// basic processes
// returns an empty place
empty: function(){
return e;
},
// returns a full place
full: function(){
return f;
},
// returns a source component
source: function(){
var matrix = make_matrix(1,2);
var p = make_process(matrix);
p.name = 'src';
matrix.set(0,0, make_singleton(p));
matrix.set(0,1, make_singleton(p));
return p;
},
// returns a sink component
sink: function(){
var matrix = make_matrix(2,1);
var p = make_process(matrix);
p.name = 'snk';
matrix.set(0,0, make_singleton(p));
matrix.set(1,0, make_singleton(p));
return p;
},
// returns an identity component (i.e. a wire)
id: function(){
var matrix = make_matrix(2,2);
var p = make_process(matrix);
p.name = "id";
matrix.set(0,0, make_singleton(p));
matrix.set(0,1, undefined);
matrix.set(1,0, undefined);
matrix.set(1,1, make_singleton(p));
return p;
},
// returns a xor split wiring
xor_split: function(){
var split = make_matrix(2,4);
var p = make_process(split);
p.name = 'xs';
split.set(0,0, make_singleton(p));
split.set(0,1, undefined);
split.set(0,2, undefined);
split.set(0,3, undefined);
split.set(1,0, undefined);
split.set(1,1, make_singleton(p));
split.set(1,2, make_singleton(p));
split.set(1,3, undefined);
return p;
},
// returns a xor merge wiring
xor_merge: function(){
var merge = make_matrix(4,2);
var p = make_process(merge);
p.name = 'xm';
merge.set(0,0, make_singleton(p));
merge.set(1,0, undefined);
merge.set(2,0, undefined);
merge.set(3,0, undefined);
merge.set(0,1, undefined);
merge.set(1,1, make_singleton(p));
merge.set(2,1, make_singleton(p));
merge.set(3,1, undefined);
return p;
},
// returns an and split wiring
and_split: function(){
var split = make_matrix(2,4);
var p = make_process(split);
p.name = 'as';
split.set(0,0, make_singleton(p));
split.set(0,1, undefined);
split.set(0,2, undefined);
split.set(0,3, undefined);
split.set(1,0, undefined);
split.set(1,1, undefined);
split.set(1,2, undefined);
split.set(1,3, make_singleton(p));
return p;
},
// returns an and merge wiring
and_merge: function(){
var merge = make_matrix(4,2);
var p = make_process(merge);
p.name = 'am';
merge.set(0,0, make_singleton(p));
merge.set(1,0, undefined);
merge.set(2,0, undefined);
merge.set(3,0, undefined);
merge.set(0,1, undefined);
merge.set(1,1, undefined);
merge.set(2,1, undefined);
merge.set(3,1, make_singleton(p));
return p;
},
// compositions
// returns the tensor product (parallel composition) of two processes
dot: function(p, q){
var that = this;
var get_next = function(parray, qarray){
var array = new Array();
var seq = function(p, q){ return function(){ return that.dot(p(),q());}; };
for (var i=0; i<parray.length; i++){
for (var j=0; j<qarray.length; j++){
var p1 = parray[i]; // funzione tipo f(){ return p;}
var p2 = qarray[j]; // funzione tipo f(){ return p;}
var s = seq(p1,p2); // funzione tipo f(){ return p1;p2 }
array.push( s );
}
}
return array;
};
var matrix = make_matrix(p.get_input_size()*q.get_input_size(), p.get_output_size()*q.get_output_size());
for ( var i=0; i<p.get_input_size(); i++){
for (var j=0; j<p.get_output_size(); j++){
var pel = p.get(i,j);
for (var z=0; z<q.get_input_size(); z++){
for (var w=0; w<q.get_output_size(); w++){
var qel = q.get(z,w);
if ( qel !== undefined && pel !== undefined ){
var next = get_next(pel, qel);
// print(next.length);
matrix.set( z+i*q.get_input_size(), w+j*q.get_output_size(), next);
} else {
matrix.set( z+i*q.get_input_size(), w+j*q.get_output_size(), undefined);
}
}
}
}
}
var new_process = make_process(matrix);
new_process.name = p.name + "|" + q.name;
return new_process;
},
// returns the sequential composition of two processes
seq: function(p, q){
var that = this;
var get_next = function(parray, qarray){
var array = new Array();
var seq = function(p, q){ return function(){ return that.seq(p(),q());}; };
for (var i=0; i<parray.length; i++){
for (var j=0; j<qarray.length; j++){
var p1 = parray[i]; // funzione tipo f(){ return p;}
var p2 = qarray[j]; // funzione tipo f(){ return p;}
var s = seq(p1,p2); // funzione tipo f(){ return p1;p2 }
array.push( s );
}
}
return array;
};
// print('seq(' + p.name +', ' + q.name + ')');
var matrix = make_matrix(p.get_input_size(), q.get_output_size());
// print(p.get_input_size() + ' ' + q.get_output_size());
for ( var i=0; i<p.get_input_size(); i++ ){ // per ogni riga
for (var j=0; j<q.get_output_size(); j++){ // per ogni colonna
// risultato della somma
var sum = new Array();
// print("starting sum " + i +", " + j);
// per ogni elemento riga/colonna
for (var z=0; z<p.get_output_size(); z++){
var pnext = p.get(i,z); // array di processi
var qnext = q.get(z,j); // array di processi
if ( pnext !== undefined && qnext !== undefined ){
// print (pnext.length + ' ' + qnext.length);
var proc_list = get_next(pnext, qnext);
// print("length: " + proc_list.length );
sum = sum.concat( proc_list );
}
}
// aggiungi la somma dei processi alla matrice
if ( sum.length > 0 ){
matrix.set(i,j, sum);
} else {
matrix.set(i,j, undefined);
}
}
}
var new_process = make_process(matrix);
new_process.name = p.name + ";" + q.name;
// print("end seq, created " + new_process.name);
return new_process;
},
// returns the feedback of a process
feed: function(p){
var get_next = function(p, ibin){
var array = new Array();
var i0 = parseInt((ibin+'0').substring(0,ibin.length), 2);
var i1 = parseInt((ibin+'1').substring(0,ibin.length), 2);
array.push( function(){ return p.get( i0 ,j);} );
array.push( function(){ return p.get( i1 ,j);} );
return array;
};
var put_leading_zero = function(text, length){
while ( text.length < length ){
text = '0' + text;
}
return text;
};
var matrix = make_matrix(p.get_input_size()/2, p.get_output_size());
for (var i=0; i<matrix.xlength(); i++){
for ( var j=0; j<matrix.ylength(); j++){
var id = parseInt(i.toString(2) + j.toString(2),2);
var pnext = p.get(id, j);
// TODO i nomi dei processi devono avere ^ finale
matrix.set(i, j, pnext);
}
}
var new_process = make_process(matrix);
new_process.name = p.name + '^';
return new_process;
},
};
},
};
var b = PetriNets.builder();
b.dot( b.xor_merge(), b.id() );
var b1 = b.seq( b.dot( b.source(), b.xor_split() ), b.dot( b.xor_merge(), b.id() ) );
var b2 = b.seq( b.full(), b.and_split() );
var b4 = b.dot( b.dot(b.id(), b.and_merge()), b.seq( b.empty(), b.sink() ));
var b6 = b.seq( b.dot(b1,b2), b4 );
var b7 = b.seq( b6, b.xor_merge() );
var n1 = b.feed( b7 );
var c1 = b.seq( b.dot(b.source(),b.id()), b.seq( b.xor_merge(), b.and_split()) );
var c2 = b.seq( b.feed( b.seq( b.xor_merge(),b.empty() ) ), b.sink());
var c3 = b.seq( c1, b.dot(b.seq(b.and_split(), b.dot(c2, b.id())), b.id()) );
var c4 = b.seq( b.and_split(), b.dot( b.id(), b.empty() ) );
var c5 = b.seq( b.xor_split(), b.dot( c3, c4 ));
var c6 = b.seq( c5, b.dot( b.dot(b.id(),b.and_merge()), b.id() ) );
var c7 = b.seq( c6, b.dot( b.id(),b.xor_merge() ) );
var c8 = b.seq( c7, b.xor_merge() );
var d1 = b.seq( b.feed( b.seq( b.xor_merge(),b.full() ) ), b.sink());
var d2 = b.dot( d1, b.and_split() );
var d3 = b.seq( d2, b.and_merge() );