queries.txt
20.9 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
The plugin 'queries' introduces two new classes:
- Query allows to describe and execute various kind of queries, that
can be serialized to PNML in order to be exchanged with another
program. SNAKES uses an extension of PNML, for a complete list of
SNAKES' PNML tags, see 'snakes-pnml.txt'.
- UDPServer is a sample server over UDP that handles a limited number
of simple queries. However, these queries can be nested, in the
client program as well as in the communication with the server, so
the range of possibilities is unlimited and very complex queries
may be constructed from those simple ones.
A server program is provides in 'utils/query/snkd.py' and is general
an robust enough to be used in real cases (but considering it only
implements the limited set of queries exposed above). As it works in a
disconnected mode (UDP), this server does not try to make any
difference between possibly several clients. This may result in data
sharing or conflicts, which makes this server more suitable to a local
use only. Start the server with option '-h' to have details about its
command line.
A sample client is provided also, in order to allow to experiment with
the server. (Notice that since the client is programmed in Python, it
could use SNAKES directly, which explains why it will never be made
more sophisticated.) It takes the form a shell-like program that
accepts commands of the form "? command(param, ...)" where "? " is the
prompt.
First come local commands, these are commands that do not send any
data to the server but are handled locally:
? help()
list available commands
? help(command)
print help about the given command
? quit()
exits the client, end-of-file or ^C may be used equivalently
? load(path)
loads a PNML file and return the object it represents. This is
useful for instance to load a Petri net on the server as in:
"? set('net', load('mynet.pnml'))"
? show(obj)
prints the PNML representation of 'obj', this may be useful for
instance to see how a query is translated, as in:
"? show(set('x', [1, 2, 'hello']))"
? verbose(mode)
turns on (mode=True), off (mode=False) or toggle (no mode given)
the printing of queries before they are sent to the server
Then come commands that actually generate queries, there is only 4 of
them:
? set('name', value)
assign value to 'name', which is equivalent to the Python statement
"name=value". 'value' may be any Python expression. It is important
to notice that 'name' is assigned on the server side, in an
environment that initially contains Python's builtins, the content
of 'operator' and of 'snakes.net' modules (the later being extended
by all the plugins loaded before 'query').
? get('name')
returns the value previously assigned to 'name'.
? delete('name')
Equivalent to the Python statement "del name".
? call(obj, ...)
equivalent to the Python statement "obj(...)". 'obj' may be a name
or an access to an object like 'x' or 'x.method', or even the
result from a nested query (see examples below).
##
## Answers
##
In case of a success with no return value, the answer is:
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok"/>
</pnml>
If there is a return value, it is given as a PNML sub-tree of tag
<answer>. For instance:
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<object type="str">hello world!</object>
</answer>
</pnml>
If an errors occurs during the handling of the query, an answer with
status "error" is returned. The data in tag <answer> is the error
message and the tag has an attribute 'error' that is the name of the
caught exception. For instance:
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer error="ExceptionName" status="error">Exception message</answer>
</pnml>
##
## Queries
##
Query arguments are passed as tags <argument> nested in <query>, the
value of each argument is encoded in PNML using the tags presented in
'snakes-pnml.txt'. The following is a copy/paste from a snkc session,
interleaved with comments.
First, we turn on the verbose mode.
? verbose()
dump of queries enabled
The first query assigns to 'x' a list composed of an integer, a float
and a string. This allows to illustrate how these types are encoded.
? set('x', [1, 3.14, 'hello'])
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="set">
<argument>
<object type="str">x</object>
</argument>
<argument>
<object type="list">
<object type="int">1</object>
<object type="float">3.14</object>
<object type="str">hello</object>
</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok"/>
</pnml>
If we get 'x' back, the same encoding is used again but in the other
direction.
? get('x')
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="get">
<argument>
<object type="str">
x
</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<object type="list">
<object type="int">1</object>
<object type="float">3.14</object>
<object type="str">hello</object>
</object>
</answer>
</pnml>
'x' can be removed, after which trying to get it again results in an
error.
? delete('x')
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="del">
<argument>
<object type="str">x</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok"/>
</pnml>
? get('x')
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="get">
<argument>
<object type="str">x</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer error="AttributeError" status="error">
'module' object has no attribute 'x'
</answer>
</pnml>
We set a new name 's' that is a string in order to illustrate method
calls.
? set('s', 'hello world!')
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="set">
<argument>
<object type="str">s</object>
</argument>
<argument>
<object type="str">hello world!</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok"/>
</pnml>
The following calls the method 'replace' of 's' passing it two string
arguments.
? call('s.replace', 'o', '_')
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<object type="str">s.replace</object>
</argument>
<argument>
<object type="str">o</object>
</argument>
<argument>
<object type="str">_</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<object type="str">hell_ w_rld!</object>
</answer>
</pnml>
In order to cascade calls, that is call a method of the object
returned by the first call, we can use the function 'getattr' that
returns a named attribute of an object. Here, we get and call the
'split' method from the string returned by 's.replace'. This is
exactly the same as "s.replace('o', '_').split()" in Python.
? call(call('getattr', call('s.replace', 'o', '_'), 'split'))
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<query name="call">
<argument>
<object type="str">getattr</object>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">s.replace</object>
</argument>
<argument>
<object type="str">o</object>
</argument>
<argument>
<object type="str">_</object>
</argument>
</query>
</argument>
<argument>
<object type="str">split</object>
</argument>
</query>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<object type="list">
<object type="str">hell_</object>
<object type="str">w_rld!</object>
</object>
</answer>
</pnml>
Let's now apply these techniques to work with Petri nets. First we
load a net from PNML file. In practical cases, the command 'load' from
snkc is not available. But it is enough to read the PNML file, extract
its '<net>...</net>' part and paste it in the middle of the 'set'
request.
? set('n', load('simple-coloured.pnml'))
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="set">
<argument>
<object type="str">n</object>
</argument>
<argument>
<net id="mynet">
<place id="p2">
<type domain="universal"/>
<initialMarking>
<multiset/>
</initialMarking>
</place>
<place id="p1">
<type domain="universal"/>
<initialMarking>
<multiset>
<item>
<value>
<object type="int">1</object>
</value>
<multiplicity>1</multiplicity>
</item>
<item>
<value>
<object type="int">2</object>
</value>
<multiplicity>1</multiplicity>
</item>
</multiset>
</initialMarking>
</place>
<transition id="t"/>
<arc id="p1:t" source="p1" target="t">
<inscription>
<variable>x</variable>
</inscription>
</arc>
<arc id="t:p2" source="t" target="p2">
<inscription>
<expression>x+1</expression>
</inscription>
</arc>
</net>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok"/>
</pnml>
The marking of the net can then be retrieved. Only place 'p1' is
marked by the two integer-valued tokens 1 and 2.
? call('n.get_marking')
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<object type="str">n.get_marking</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<marking>
<place id="p1">
<tokens>
<multiset>
<item>
<value>
<object type="int">1</object>
</value>
<multiplicity>1</multiplicity>
</item>
<item>
<value>
<object type="int">2</object>
</value>
<multiplicity>1</multiplicity>
</item>
</multiset>
</tokens>
</place>
</marking>
</answer>
</pnml>
Using the same techniques as for emulating "s.replace().split()"
above, we can query the modes of transition 't' in net 'n'. We get in
return a list of two substitutions that allow to fire 't'.
? call(call('getattr', call('n.transition', 't'), 'modes'))
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<query name="call">
<argument>
<object type="str">getattr</object>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">n.transition</object>
</argument>
<argument>
<object type="str">t</object>
</argument>
</query>
</argument>
<argument>
<object type="str">modes</object>
</argument>
</query>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<object type="list">
<substitution>
<item>
<name>x</name>
<value>
<object type="int">1</object>
</value>
</item>
</substitution>
<substitution>
<item>
<name>x</name>
<value>
<object type="int">2</object>
</value>
</item>
</substitution>
</object>
</answer>
</pnml>
Instead of getting this list of modes, we could have saved it to a
name 's'. We just need to nest the above query in a 'set' query.
? set('s', call(call('getattr', call('n.transition', 't'), 'modes')))
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="set">
<argument>
<object type="str">s</object>
</argument>
<argument>
<query name="call">
<argument>
<query name="call">
<argument>
<object type="str">getattr</object>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">n.transition</object>
</argument>
<argument>
<object type="str">t</object>
</argument>
</query>
</argument>
<argument>
<object type="str">modes</object>
</argument>
</query>
</argument>
</query>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok"/>
</pnml>
In order to fire 't', we will call its method 'fire' and pass it one
of the modes stored in 's'. Here, we use function 'getitem' to
retrieve the first item in 's' (numbered 0). In a realistic example,
it could be simpler to parse and store on the client the list of
substitutions, instead of storing it on the server.
? call(call('getattr', call('n.transition', 't'), 'fire'), call('getitem', get('s'), 0))
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<query name="call">
<argument>
<object type="str">getattr</object>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">n.transition</object>
</argument>
<argument>
<object type="str">t</object>
</argument>
</query>
</argument>
<argument>
<object type="str">fire</object>
</argument>
</query>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">getitem</object>
</argument>
<argument>
<query name="get">
<argument>
<object type="str">s</object>
</argument>
</query>
</argument>
<argument>
<object type="int">0</object>
</argument>
</query>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok"/>
</pnml>
As we can see now, both places are marked.
? call('n.get_marking')
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<object type="str">n.get_marking</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<marking>
<place id="p2">
<tokens>
<multiset>
<item>
<value>
<object type="int">2</object>
</value>
<multiplicity>1</multiplicity>
</item>
</multiset>
</tokens>
</place>
<place id="p1">
<tokens>
<multiset>
<item>
<value>
<object type="int">2</object>
</value>
<multiplicity>1</multiplicity>
</item>
</multiset>
</tokens>
</place>
</marking>
</answer>
</pnml>
Then we can call again 'fire', with the second available mode.
? call(call('getattr', call('n.transition', 't'), 'fire'), call('getitem', get('s'), 1))
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<query name="call">
<argument>
<object type="str">getattr</object>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">n.transition</object>
</argument>
<argument>
<object type="str">t</object>
</argument>
</query>
</argument>
<argument>
<object type="str">fire</object>
</argument>
</query>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">getitem</object>
</argument>
<argument>
<query name="get">
<argument>
<object type="str">s</object>
</argument>
</query>
</argument>
<argument>
<object type="int">1</object>
</argument>
</query>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok"/>
</pnml>
And now only 'p2' is marked.
? call('n.get_marking')
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<object type="str">n.get_marking</object>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<marking>
<place id="p2">
<tokens>
<multiset>
<item>
<value>
<object type="int">2</object>
</value>
<multiplicity>1</multiplicity>
</item>
<item>
<value>
<object type="int">3</object>
</value>
<multiplicity>1</multiplicity>
</item>
</multiset>
</tokens>
</place>
</marking>
</answer>
</pnml>
Querying the modes of 't' now results in an empty list because there
is no more tokens in the input place of 't'.
? call(call('getattr', call('n.transition', 't'), 'modes'))
# query to localhost:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<query name="call">
<argument>
<object type="str">getattr</object>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">n.transition</object>
</argument>
<argument>
<object type="str">t</object>
</argument>
</query>
</argument>
<argument>
<object type="str">modes</object>
</argument>
</query>
</argument>
</query>
</pnml>
# answer from 127.0.0.1:1234
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<answer status="ok">
<object type="list"/>
</answer>
</pnml>
These examples are intended to illustrate what _can_ be made, but not
necessarily what _should_ be made. Here, we use snkc at the client
side, so we have no way to store locally information, nor to parse the
PNML we get from the server. So, when we need to record some data, we
put it at the server side using a 'set' query. Doing so, we will need
to use quite complicated queries in order to extract the bits of data
that we will want to use. See for instance how complicated was the
firing of a transition.
In a realistic client, it is possible to store an manage locally some
information and so avoid complex queries. It is even possible to
completely parse and interpret PNML data received from the server.
Both extremities have pros and cons:
- Storing everything on the server simplifies processing for the
client; but it increases (a lot) the complexity of queries.
- Storing everything on the client requires to parse and interpret
PNML data, and to manage stored information; but it simplifies
queries and may give more control on the amount of exchanged.
Any intermediary position may be adopted: it is possible for a client
to partially interpret PNML and to store fragments of uninterpreted
PNML text as symbolic values. These fragments can then be inserted
into queries where they are required.
For instance, a client could parse the list of modes of a transition
up to the tag <substitution>, which is quite a simple task. Each mode
could then be saved locally as a fragment of PNML text
"<substitution>...</substitution>". Then, firing a transition would
simply require to insert such a fragment at the right position in a
template query. For instance, let's run:
? show(call(call('getattr', call('n.transition', 't'), 'fire'), 'SUBST'))
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="call">
<argument>
<query name="call">
<argument>
<object type="str">getattr</object>
</argument>
<argument>
<query name="call">
<argument>
<object type="str">n.transition</object>
</argument>
<argument>
<object type="str">t</object>
</argument>
</query>
</argument>
<argument>
<object type="str">fire</object>
</argument>
</query>
</argument>
<argument>
<object type="str">SUBST</object>
</argument>
</query>
</pnml>
Then, we just need to replace '<object type="str">SUBST</object>' with
the saved fragment '<substitution>...</substitution>' in order to fire
the transition with the chosen mode. A similar techniques can be
applied to many situations. A lazy (but clever) approach would be to
prepare a series of template queries where placeholders could be
substituted with fragments of PNML text retrieved from the server.
##
## Keyword arguments
##
A <query> may also accepts keyword arguments like functions in Python.
But currently no query expects that. The syntax to add keyword
arguments is to use a tag <keyword> for each such argument, with an
attribute 'name' that store the keyword name and with a child tag that
stores the keyword value. For instance, a Python call "example('x', 1,
foo=5, bar='hello')" would translate to a query:
<?xml version="1.0" encoding="utf-8"?>
<pnml>
<query name="example">
<argument>
<object type="str">
x
</object>
</argument>
<argument>
<object type="int">
1
</object>
</argument>
<keyword name="foo">
<object type="int">
5
</object>
</keyword>
<keyword name="bar">
<object type="str">
hello
</object>
</keyword>
</query>
</pnml>