summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/messagepassing/messagepassing.sty
blob: f807ec06ca147f034c2f101dd6e26998857b4ec3 (plain)
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
%%
%% This is file `messagepassing.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% messagepassing.dtx  (with options: `package')
%% This is a generated file.
%% Copyright (C) 2022 by Martin Vassor
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3 of this license or (at your option) any later
%% version.  The latest version of this license is in:
%% 
%% http://www.latex-project.org/lppl.txt
%% 
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%% 

 \NeedsTeXFormat{LaTeX2e}
 \ProvidesPackage{messagepassing}[2022/02/18 v1.0 A package to draw message passing diagrams]
 \RequirePackage{tikz}
 \usetikzlibrary{quotes, calc, arrows.meta}
 \RequirePackage{xparse}
 \RequirePackage{float}


















\newcounter{processnb}
\setcounter{processnb}{0}
\newcounter{maxtime}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\newfloat{float_messagepassing}{t b h p}{.mp}
\floatname{float_messagepassing}{Execution}

\newif\ifmp@vertical\mp@verticalfalse
\DeclareOption{vertical}{
\mp@verticaltrue
}
\newif\ifmp@annotatevertical\mp@annotateverticalfalse
\DeclareOption{annotatevertical}{
\mp@annotateverticaltrue
}
\ProcessOptions\relax

\ifmp@vertical
\newcommand{\mp@processnameanchor}{south}
\newcommand{\mp@timeticksanchor}{east}
\newcommand{\mp@messagelabelanchor}{south}
\newcommand{\mp@stateintervalanchor}{north west}
\newcommand{\mp@checkpointanchor}{east}
\newcommand{\mp@verticalrotation}{270}
\ifmp@annotatevertical
\newcommand{\mp@annotaterotation}{270}
\else
\newcommand{\mp@annotaterotation}{0}
\fi
\else
\newcommand{\mp@processnameanchor}{east}
\newcommand{\mp@timeticksanchor}{north}
\newcommand{\mp@messagelabelanchor}{west}
\newcommand{\mp@stateintervalanchor}{south west}
\newcommand{\mp@checkpointanchor}{north}
\newcommand{\mp@verticalrotation}{0}
\newcommand{\mp@annotaterotation}{0}
\fi

\newcommand{\mp@oobcolor}{red}
\newcommand{\oobcolor}[1]{
\renewcommand\mp@oobcolor{#1}
}

\newcommand{\mp@colouredboxcolor}{red}
\newcommand{\colouredboxcolor}[1]{
\renewcommand\mp@colouredboxcolor{#1}
}

\newif\iftimeline

\ExplSyntaxOn
%% 1st argument: tikz arguments
%% 2nd argument: Float caption (turns in floating)
%% 3rd argument: Float placement (`p` by default)
%% 4th argument: Float label
\NewDocumentEnvironment{messagepassing} {o o o o}
{
\timelinefalse
\setcounter{processnb}{0}
\IfNoValueTF{#2} {
}{
\IfNoValueTF{#3}{
\begin{float_messagepassing}[p]
} {
\begin{float_messagepassing}[#3]
}
\begin{center}
}
\IfNoValueTF{#1}{
\begin{tikzpicture}[rotate=\mp@verticalrotation]
} {
\begin{tikzpicture}[rotate=\mp@verticalrotation, #1]
}
}{
%% Draw timeline if boolean is true
\iftimeline
\begin{pgfonlayer}{background}
\setcounter{maxtime}{\@maxtime}
\addtocounter{maxtime}{-1}
\coordinate (maxtime) at (\@maxtime, 0);

\addtocounter{processnb}{1}
\coordinate (timeline) at (0, -\value{processnb});
\draw (timeline) node [anchor=\mp@processnameanchor] {{\it time}};
\draw[->] (timeline) -- ($(timeline) + (maxtime)$);
\foreach \i in {0,...,\value{maxtime}} {
\draw ($(timeline) + (\i, 0) + (0, 0.1)$) -- ($(timeline) + (\i, 0) + (0, -0.1)$) node [anchor=\mp@timeticksanchor] {$\i$};
}
\end{pgfonlayer}
\else
\fi
\end{tikzpicture}
\IfNoValueTF{#2} {
\linebreak
} {
\end{center}
\caption{#2}
\IfNoValueTF{#4} {
}{
\label{#4}
}
\end{float_messagepassing}
}
}
\ExplSyntaxOff
%% #1: name
\newcommand{\newprocess}[1]{
\addtocounter{processnb}{1}
\coordinate (#1) at (0, -\value{processnb});
\draw (#1) node[anchor=\mp@processnameanchor] {$#1$};
}
%% #1: name
%% #2: width
\newcommand{\newprocesswithlength}[2]{
\newprocess{#1}
\processlength{#1}{#2}
}
%% #1: name
%% #2: state interval name
\newcommand{\newprocesswithstateinterval}[2]{
\newprocess{#1}
\stateinterval{#1}{0}{#2}
}
%% #1: name
%% #2: width
%% #3: crash name
\newcommand{\newprocesswithcrash}[3]{
\newprocess{#1}{#2}
\crash{#1}{#2}{#3}
}
%% #1: sender's name
%% #2: send date
%% #3: receiver's name
%% #4: receive date
\newcommand{\send}[4]{
\draw[->] (#1) +(#2, 0) -- ($ (#3) +(#4, 0) $);
}
%% #1: sender's name
%% #2: send date
%% #3: receiver's name
%% #4: receive date
%% #5: message name
\newcommand{\sendwithname}[5]{
\draw[->] (#1) +(#2, 0) -- ($ (#3) +(#4, 0) $) node[anchor=\mp@messagelabelanchor, pos=0.3] {#5};
}
%% #1: process name
%% #2: process width
\newcommand{\processlength}[2]{
\draw (#1) -- +(#2, 0);
}
%% #1: sender's name
%% #2: send date
%% #3: receiver's name
%% #4: receive date
%% #5: state interval name
\newcommand{\sendwithstateinterval}[5] {
\send{#1}{#2}{#3}{#4}
\stateinterval{#3}{#4}{#5}
}
%% #1: sender's name
%% #2: send date
%% #3: receiver's name
%% #4: receive date
%% #5: state interval name
%% #6: message name
\newcommand{\sendwithstateintervalandname}[6] {
\sendwithname{#1}{#2}{#3}{#4}{#6}
\stateinterval{#3}{#4}{#5}
}
%% #1: sender's name
%% #2: send date
%% #3: receiver's name
%% #4: receive date
%% #5: OoB message name
\newcommand{\sendoutofband}[5]{
\draw[->, color=\mp@oobcolor] (#1) +(#2, 0) -- ($ (#3) +(#4, 0) $) node[anchor=\mp@messagelabelanchor, pos=0.3] {#5};
}
%% #1: process's name
%% #2: state interval date
%% #3: state interval name
\newcommand{\stateinterval}[3] {
\stateintervalspecial{#1}{#2}{#3}{#3}
}
%% #1: process's name
%% #2: state interval date
%% #3: coordinate name
%% #4: state interval label
\newcommand{\stateintervalspecial}[4] {
\coordinate (#3) at ($ (#1) +(#2, 0) $);
\draw (#3) + (0, 0.1) -- +(0, -0.1) node[anchor=\mp@stateintervalanchor] {$#4$};
}
%% #1: process's name
%% #2: checkpoint date
%% #3: checkpoint name
\newcommand{\checkpoint}[3]{
\coordinate (#3) at ($ (#1) + (#2, 0) $);
\fill (#3) + (-0.05, 0.1) rectangle +(0.05, -0.1);
\draw (#3) + (0, -0.1) node[anchor=\mp@checkpointanchor] {$#3$};
}
%% #1: process's name
%% #2: checkpoint date
%% #3: checkpoint coordinate name
%% #4: checkpoint label
\newcommand{\checkpointspecial}[4]{
\coordinate (#3) at ($ (#1) + (#2, 0) $);
\fill (#3) + (-0.05, 0.1) rectangle +(0.05, -0.1);
\draw (#3) + (0, -0.1) node[anchor=\mp@checkpointanchor] {#4};
}
%% #1: process's name
%% #2: crash date
%% #3: crash name
\newcommand{\crash}[3]{
\coordinate (#3) at ($ (#1) + (#2, 0) $);
\draw (#3) + (-0.1, -0.1) -- +(0.1, 0.1);
\draw (#3) + (0.1, -0.1) -- +(-0.1, 0.1);
}
%% #1: process's name
%% #2: restart date
%% #3: restart length
\newcommand{\restart}[3]{
\draw (#1) + (#2, 0) --  ($ (#1) + (#2, 0) + (#3, 0) $);
}
%% #1: first process's name
%% #2: second process's name
%% #3: begining of the grey box
%% #4: end of the grey box
%% #5: caption
\newcommand{\colouredbox}[5]{
\begin{pgfonlayer}{background}
\fill[color=\mp@colouredboxcolor!20] ($(#1) + (#3, 0)$) rectangle ($(#2) + (#4, 0)$) node[midway, color = \mp@colouredboxcolor, rotate=\mp@annotaterotation]{#5};
\end{pgfonlayer}
}
%% #1: Timeline length
\newcommand{\drawtimeline}[1]{
\timelinetrue
\def\@maxtime{#1}
}
%% #1: process's name
%% #2: annotation date
%% #3: annotation
\newcommand{\annotate}[3]{
\annotatexplicit{#1}{#2}{#3}{#3}
}
%% Same than annotate, but with the coordinate name provided explicitly
%% #1: process's name
%% #2: annotation date
%% #3: annotation
%% #4: coordinate name
\newcommand{\annotatexplicit}[4]{
\coordinate (#4) at ($ (#1) +(#2, 0.1) $);
\draw (#4) node[rotate=\mp@annotaterotation, anchor=south] {#3};
}
\endinput
%%
%% End of file `messagepassing.sty'.