-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFLaAL.sty
540 lines (499 loc) · 19.9 KB
/
FLaAL.sty
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
%%Identification
%%The package identifies itself and the LaTeX version needed
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{FLaAL}[2019/08/27 FLaAL package]
%%Requeiried packages and preliminary definitions, needed by the options
\RequirePackage{xstring}
\RequirePackage{tikz}
\RequirePackage{fp}
\RequirePackage{xkeyval}
\RequirePackage{xparse}
\RequirePackage{ifthen}
\RequirePackage{multido}
\RequirePackage{arrayjobx}
%%This parts handles the options passed to the package.
\ProcessOptions\relax
%%Body of the package, most of the declarations appear here.
\newcommand{\test@empty}{xxx}
\newcommand{\e}{$\varepsilon$}
% Label Processing for
% Finite Automata
\ExplSyntaxOn
\NewDocumentCommand{\fa@label@processing}{O{;}m}
{
\fa_make:nn {#1} {#2}
}
\cs_new_protected:Npn \fa_make:nn #1 #2
{
\seq_set_split:Nnn \l_fa_args_seq { #1 } { #2 }
\seq_pop_left:NN \l_fa_args_seq \l_fa_temp_tl
\StrLen{\l_fa_temp_tl}[\falength]
\seq_clear:N \l_fa_used_labels
\ifthenelse{\equal{\falength}{0}}{
$\varepsilon$
}{
\seq_put_right:NV \l_fa_used_labels \l_fa_temp_tl
\l_fa_temp_tl
}
\seq_map_inline:Nn \l_fa_args_seq {
\tl_set:Nn \tl_fa_ele_in_list {false}
\seq_map_inline:Nn \l_fa_used_labels {
\ifthenelse{\equal{##1}{####1}}{
\tl_set:Nn \tl_fa_ele_in_list {true}
}{}
}
\ifthenelse{\equal{false}{\tl_fa_ele_in_list}}{
,\space ##1
\seq_put_right:Nx \l_fa_used_labels {##1}
}{}
}
}
\tl_new:N \tl_fa_ele_in_list
\seq_new:N \l_fa_used_labels
\seq_new:N \l_fa_args_seq
\tl_new:N \l_fa_temp_tl
\ExplSyntaxOff
% % Label Processing for
% % DKA
% \ExplSyntaxOn
% \NewDocumentCommand{\dka@label@processing}{O{;}m}
% {
% \dka_make:nn {#1} {#2}
% }
% \cs_new_protected:Npn \dka_make:nn #1 #2
% {
% % split by semicolon
% \seq_set_split:Nnn \l_dka_args_seq { #1 } { #2 }
% % pop first element
% \seq_pop_left:NN \l_dka_args_seq \l_dka_temp_tl
% % split first element by comma
% \seq_set_split:NnV \l_dka_ele_args_seq {,} {\l_dka_temp_tl}
% % pop first element
% \seq_pop_left:NN \l_dka_ele_args_seq \l_dka_ele_first_tl
% % pop second element
% \seq_pop_left:NN \l_dka_ele_args_seq \l_dka_ele_second_tl
% % pop third element
% \seq_pop_left:NN \l_dka_ele_args_seq \l_dka_ele_third_tl
% \StrLen{\l_dka_ele_third_tl}[\dkathirdlength]
% % result is (first,second):third
% (\l_dka_ele_first_tl,\l_dka_ele_second_tl):\int_compare:nTF {\dkathirdlength=0}{$\varepsilon$}{\l_dka_ele_third_tl}
% % clearing the processed list, to be ready to be filled again
% \seq_clear:N \l_dka_proc_args
% % going trough all remaining labels
% \seq_map_inline:Nn \l_dka_args_seq {
% % split first element by comma
% \seq_set_split:Nnn \l_dka_ele_args_seq {,} {##1}
% % pop first element
% \seq_pop_left:NN \l_dka_ele_args_seq \l_dka_ele_first_tl
% % pop second element
% \seq_pop_left:NN \l_dka_ele_args_seq \l_dka_ele_second_tl
% % pop third element
% \seq_pop_left:NN \l_dka_ele_args_seq \l_dka_ele_third_tl
% \StrLen{\l_dka_ele_third_tl}[\dkathirdlength]
% % adding labels to the proccessed labels
% \seq_put_right:Nx \l_dka_proc_args {(\l_dka_ele_first_tl,\l_dka_ele_second_tl):\int_compare:nTF {\dkathirdlength=0}{$\varepsilon$}{\l_dka_ele_third_tl}}
% }
% % print it below each other
% \seq_map_inline:Nn \l_dka_proc_args {\\##1}
% }
% \seq_new:N \l_dka_ele_args_seq
% \seq_new:N \l_dka_args_seq
% \seq_new:N \l_dka_proc_args
% \tl_new:N \l_dka_temp_tl
% \tl_new:N \l_dka_ele_first_tl
% \tl_new:N \l_dka_ele_second_tl
% \tl_new:N \l_dka_ele_third_tl
% \ExplSyntaxOff
% Label Processing for
% Pushdown Automata
\ExplSyntaxOn
\NewDocumentCommand{\pa@label@processing}{O{;}m}
{
\pa_make:nn {#1} {#2}
}
\cs_new_protected:Npn \pa_make:nn #1 #2
{
% clearing the used labels for new use
\seq_clear:N \l_pa_used_labels
% split by semicolon
\seq_set_split:Nnn \l_pa_args_seq { #1 } { #2 }
% pop first element
\seq_pop_left:NN \l_pa_args_seq \l_pa_temp_tl
% put the label in the used labels
\seq_put_right:NV \l_pa_used_labels \l_pa_temp_tl
% split first element by comma
\seq_set_split:NnV \l_pa_ele_args_seq {,} {\l_pa_temp_tl}
% pop first element
\seq_pop_left:NN \l_pa_ele_args_seq \l_pa_ele_first_tl
% pop second element
\seq_pop_left:NN \l_pa_ele_args_seq \l_pa_ele_second_tl
\StrLen{\l_pa_ele_second_tl}[\paseclength]
% pop third element
\seq_pop_left:NN \l_pa_ele_args_seq \l_pa_ele_third_tl
\StrLen{\l_pa_ele_third_tl}[\pathirdlength]
% result is (first,second):third
(\l_pa_ele_first_tl,\int_compare:nTF {\paseclength=0}{$\varepsilon$}{\l_pa_ele_second_tl}):\int_compare:nTF {\pathirdlength=0}{$\varepsilon$}{\l_pa_ele_third_tl}
% clearing the processed list, to be ready to be filled again
\seq_clear:N \l_pa_proc_args
% going trough all remaining labels
\seq_map_inline:Nn \l_pa_args_seq {
% set the pa element in list flag to false
\tl_set:Nn \tl_pa_ele_in_list {false}
% search for matching, already used label
\seq_map_inline:Nn \l_pa_used_labels {
\ifthenelse{\equal{##1}{####1}}{
\tl_set:Nn \tl_pa_ele_in_list {true}
}{}
}
% if element is not in List add it and continue with default procedure
\ifthenelse{\equal{false}{\tl_pa_ele_in_list}}{
% isn't in use already so add to the used label list
\seq_put_right:Nx \l_pa_used_labels {##1}
% continue with standard procedure
% split first element by comma
\seq_set_split:Nnn \l_pa_ele_args_seq {,} {##1}
% pop first element
\seq_pop_left:NN \l_pa_ele_args_seq \l_pa_ele_first_tl
% pop second element
\seq_pop_left:NN \l_pa_ele_args_seq \l_pa_ele_second_tl
\StrLen{\l_pa_ele_second_tl}[\paseclength]
% pop third element
\seq_pop_left:NN \l_pa_ele_args_seq \l_pa_ele_third_tl
\StrLen{\l_pa_ele_third_tl}[\pathirdlength]
% adding labels to the proccessed labels
\seq_put_right:Nx \l_pa_proc_args {(\l_pa_ele_first_tl,\int_compare:nTF {\paseclength=0}{$\varepsilon$}{\l_pa_ele_second_tl} ):\int_compare:nTF {\pathirdlength=0}{$\varepsilon$}{\l_pa_ele_third_tl}}
}{}
}
% print it below each other
\seq_map_inline:Nn \l_pa_proc_args {\\##1}
}
\tl_new:N \tl_pa_ele_in_list
\seq_new:N \l_pa_used_labels
\seq_new:N \l_pa_ele_args_seq
\seq_new:N \l_pa_args_seq
\seq_new:N \l_pa_proc_args
\tl_new:N \l_pa_temp_tl
\tl_new:N \l_pa_ele_first_tl
\tl_new:N \l_pa_ele_second_tl
\tl_new:N \l_pa_ele_third_tl
\ExplSyntaxOff
% Label Processing for
% TM
\ExplSyntaxOn
\NewDocumentCommand{\tm@label@processing}{O{;}m}
{
\tm_make:nn {#1} {#2}
}
\cs_new_protected:Npn \tm_make:nn #1 #2
{
% clearing the used labels for new use
\seq_clear:N \l_tm_used_labels
% split by semicolon
\seq_set_split:Nnn \l_tm_args_seq { #1 } { #2 }
% pop first element
\seq_pop_left:NN \l_tm_args_seq \l_tm_temp_tl
% put the label in the used labels
\seq_put_right:NV \l_tm_used_labels \l_tm_temp_tl
% split first element by comma
\seq_set_split:NnV \l_tm_ele_args_seq {,} {\l_tm_temp_tl}
% pop first element
\seq_pop_left:NN \l_tm_ele_args_seq \l_tm_ele_first_tl
% pop second element
\seq_pop_left:NN \l_tm_ele_args_seq \l_tm_ele_second_tl
% pop third element
\seq_pop_left:NN \l_tm_ele_args_seq \l_tm_ele_third_tl
% result is (first,second):third
\l_tm_ele_first_tl :\l_tm_ele_second_tl,\l_tm_ele_third_tl
% clearing the processed list, to be ready to be filled again
\seq_clear:N \l_tm_proc_args
% going trough all remaining labels
\seq_map_inline:Nn \l_tm_args_seq {
% set the pa element in list flag to false
\tl_set:Nn \tl_tm_ele_in_list {false}
% search for matching, already used label
\seq_map_inline:Nn \l_tm_used_labels {
\ifthenelse{\equal{##1}{####1}}{
\tl_set:Nn \tl_tm_ele_in_list {true}
}{}
}
% if element is not in List add it and continue with default procedure
\ifthenelse{\equal{false}{\tl_tm_ele_in_list}}{
% isn't in use already so add to the used label list
\seq_put_right:Nx \l_tm_used_labels {##1}
% continue with standard procedure
% split first element by comma
\seq_set_split:Nnn \l_tm_ele_args_seq {,} {##1}
% pop first element
\seq_pop_left:NN \l_tm_ele_args_seq \l_tm_ele_first_tl
% pop second element
\seq_pop_left:NN \l_tm_ele_args_seq \l_tm_ele_second_tl
% pop third element
\seq_pop_left:NN \l_tm_ele_args_seq \l_tm_ele_third_tl
% adding labels to the proccessed labels
% Result is first:second,third
\seq_put_right:Nx \l_tm_proc_args {\l_tm_ele_first_tl :\l_tm_ele_second_tl,\l_tm_ele_third_tl}
}{}
}
% print it below each other
\seq_map_inline:Nn \l_tm_proc_args {\\##1}
}
\tl_new:N \tl_tm_ele_in_list
\seq_new:N \l_tm_used_labels
\seq_new:N \l_tm_ele_args_seq
\seq_new:N \l_tm_args_seq
\seq_new:N \l_tm_proc_args
\tl_new:N \l_tm_temp_tl
\tl_new:N \l_tm_ele_first_tl
\tl_new:N \l_tm_ele_second_tl
\tl_new:N \l_tm_ele_third_tl
\ExplSyntaxOff
\newcommand{\startState}[3]{
\FPset\x{0}
\FPadd{\x}{-10}{#2}
\FPset\y{0}
\FPadd{\y}{}{#3}
\node (#1) [state] at (#2mm, #3mm) {#1};
\draw [arrow] (\x mm, \y mm) -- node[anchor=east,xshift=-0.3cm] {Start} (#1);
}
\newcommand{\normalState}[3]{
\node (#1) [state] at (#2mm, #3mm) {#1};
}
\newcommand{\finalState}[3]{
\node (#1) [state] at (#2mm, #3mm) {#1};
\node (#1finalstate)[smallerstate, right of=#1, xshift=-1cm, yshift=0] {#1};
}
\newcommand{\stafiState}[3]{
\FPset\x{0}
\FPadd{\x}{-10}{#2}
\FPset\y{0}
\FPadd{\y}{}{#3}
\node (#1) [state] at (#2mm, #3mm) {#1};
\node (#1finalstate)[smallerstate, right of=#1, xshift=-1cm, yshift=0] {#1};
\draw [arrow] (\x mm, \y mm) -- node[anchor=east,xshift=-0.3cm] {Start} (#1);
}
\newcommand{\t@line}{straight}
\newcommand{\t@label}{south}
\define@key{trans}{line}{
\IfStrEqCase{#1}{
{straight}{
\renewcommand{\t@line}{straight}
}%
{left}{
\renewcommand{\t@line}{bend left}
}%
{right}{
\renewcommand{\t@line}{bend right}
}%
{top}{
\renewcommand{\t@line}{loop above}
}%
{bot}{
\renewcommand{\t@line}{loop below}
}%
{start}{
\renewcommand{\t@line}{start}
}%
}[\PackageWarning{FLaAL}{#1 is no valid option for line. Please use left, right, straight, top or bot}{}\renewcommand{\t@line}{start}]
}
\define@key{trans}{label}{
\IfStrEqCase{#1}{
{top}{
\renewcommand{\t@label}{south}
}%
{bot}{
\renewcommand{\t@label}{north}
}%
{left}{
\renewcommand{\t@label}{east}
}%
{right}{
\renewcommand{\t@label}{west}
}%
{midway}{
\renewcommand{\t@label}{midway}
}%
}[\PackageWarning{FLaAL}{#1 is no valid option for label. Please choose one of top, bot, left or right}{}\renewcommand{\t@label}{south}]
}
% Manages all the state occurances
\ExplSyntaxOn
\NewDocumentCommand{\State@Manager}{mmmmmm}
{
\state_manager:nnnnnn {#1} {#2} {#3} {#4} {#5} {#6}
}
% 1 - source of calling (c - clear; s - state; t - transition)
% 2 - state name (or start of transition)
% 3 - state type (or style of transition)
% 4 - x (or labels of transition)
% 5 - y (or destination state of transition)
% 6 - automaton type
\cs_new_protected:Npn \state_manager:nnnnnn #1 #2 #3 #4 #5 #6
{
\ifthenelse{\equal{c}{#1}}{
% clear the states sequence
\seq_clear:N \l_states
}{
% check if given state is in states sequence
\tl_set:Nn \tl_state_in_states {false}
\seq_map_inline:Nn \l_states {
% if given state #2 equals state in list ##1
\ifthenelse{\equal{#2}{##1}}{
\tl_set:Nn \tl_state_in_states {true}
}{}
}
\ifthenelse{\equal{s}{#1}}{
% this function got called by state command
\ifthenelse{\equal{false}{\tl_state_in_states}}{
% state is not in states
\seq_put_right:Nx \l_states {#2}
\IfStrEqCase{#3}{
{s}{
\startState{#2}{#4}{#5}
}%
{n}{
\normalState{#2}{#4}{#5}
}%
{f}{
\finalState{#2}{#4}{#5}
}%
{sf}{
\stafiState{#2}{#4}{#5}
}
}[\PackageWarning{FLaAL}{#3\space is\space no\space valid\space option.\space Defaults\space to\space normal}{}\normalState{#2}{#4}{#5}]
}{
% state is in states
\errmessage{#2\space is\space already\space defined.\space Choose\space another\space unique\space name\space for\space this\space state}
}
}{
\ifthenelse{\equal{t}{#1}}{
\ifthenelse{\equal{true}{\tl_state_in_states}}{
% src state is in states
% this function got called by transition command
\tl_set:Nn \tl_state_dest_in_states {false}
\seq_map_inline:Nn \l_states {
% if given dest state #5 equals state in list ##1
\ifthenelse{\equal{#5}{##1}}{
\tl_set:Nn \tl_state_dest_in_states {true}
}{}
}
\ifthenelse{\equal{true}{\tl_state_dest_in_states}}{
% both states are in l_States
\IfSubStr{#3}{line}{
\IfSubStr{#3}{label}{
% line und label sind drin
\setkeys{trans}{#3}
}{
% line aber kein label
\setkeys{trans}{#3,label=midway}
}
}{
\IfSubStr{#3}{label}{
% label aber kein line
\setkeys{trans}{line=start,#3}
}{
% kein line und kein label
\setkeys{trans}{line=start,label=midway}
}
}
% If start and destination are equal
\ifthenelse{\equal{#2}{#5}}
{
% selftransition
\IfStrEqCase{\t@line}{
{bend\space left}{
\renewcommand{\t@line}{loop\space left}
}%
{bend\space right}{
\renewcommand{\t@line}{loop\space right}
}%
{straight}{
\PackageWarning{FLaAL}{straight\space is\space not\space a\space valid\space option\space for\space a\space self\space transition}{}
\renewcommand{\t@line}{loop\space above}
}%
{start}{
\renewcommand{\t@line}{loop\space above}
}%
}
}
{
% normal transitions
\IfStrEqCase{\t@line}{
{loop above}{
\PackageWarning{FLaAL}{top\space is\space not\space a\space valid\space option\space for\space a\space normal\space transition}{}
\renewcommand{\t@line}{}
}%
{loop below}{
\PackageWarning{FLaAL}{bot\space is\space not\space a\space valid\space option\space for\space a\space normal\space transition}{}
\renewcommand{\t@line}{}
}%
{straight}{
\renewcommand{\t@line}{}
}%
{start}{
\renewcommand{\t@line}{}
}%
}
}
\ifthenelse{\equal{\t@label}{midway}}{
\path (#2) edge [\t@line] node [\t@label,align=center] %
{%
\IfStrEqCase{#6}{
{fa}{\fa@label@processing{#4}}
{pa}{\pa@label@processing{#4}}
{tm}{\tm@label@processing{#4}}
} %
} (#5);
}{
\path (#2) edge [\t@line] node [anchor=\t@label,align=center] %
{%
\IfStrEqCase{#6}{
{fa}{\fa@label@processing{#4}}
{pa}{\pa@label@processing{#4}}
{tm}{\tm@label@processing{#4}}
} %
} (#5);
}
}{
% dest is not in l_States
\PackageWarning{FLaAL}{Destination\space state\space #5\space is\space not\space in\space States.\space Please\space make\space sure\space that\space the\space state\space was\space defined.\space Ignoring\space this\space transition}
}
}{% src state is not in states
\PackageWarning{FLaAL}{Source\space state\space #2\space is\space not\space in\space States.\space Please\space make\space sure\space that\space the\space state\space was\space defined.\space Ignoring\space this\space transition}
}
}{
% start is not in States
\PackageWarning{FLaAL}{Start\space state\space #2\space is\space not\space in\space States.\space Please\space make\space sure\space that\space the\space state\space was\space defined.\space Ignoring\space this\space transition}
}
}
}
}
\tl_new:N \tl_state_in_states
\tl_new:N \tl_state_dest_in_states
\seq_new:N \l_states
\ExplSyntaxOff
\newenvironment{transitiongraph}[1][fa]
{
\IfStrEqCase{#1}{
{fa}{}%
{pa}{}%
{tm}{}%
}[\PackageError{FLaAL}{#1 is no valid option for tg environment. Use one of fa (Finite Automata), pa (Pushdown Automata) or tm (Turing Machine)};]
\tikzstyle{state} = [circle, minimum width=1cm, text centered, draw=black, fill=white,inner sep=4]
\tikzstyle{smallerstate} = [circle, minimum width=0.8cm, text centered, draw=black, fill=white,inner sep=2]
\tikzstyle{arrow} = [thick,->,>=stealth]
\State@Manager{c}{c}{c}{c}{c}{#1}
\newcommand{\state}[4][n]{
\State@Manager{s}{##2}{##1}{##3}{##4}{#1}
}
% 1 = {anchor for text; default= south (means above)}
% 2 = start
% 3 = destination
% 4 = labels
\newcommand{\transition}[4][line=start,label=midway]
{
\State@Manager{t}{##2}{##1}{##4}{##3}{#1}
}
\begin{tikzpicture}[->,>=stealth,auto]
}
{
\end{tikzpicture}
}