File: needuml.rst

package info (click to toggle)
sphinx-needs 5.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,924 kB
  • sloc: python: 21,132; javascript: 187; makefile: 89; sh: 29; xml: 10
file content (494 lines) | stat: -rw-r--r-- 10,946 bytes parent folder | download
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


.. _needuml:

needuml
=======

``needuml`` behaves exactly like the ``uml`` directive from the Sphinx extension
`Sphinxcontrib-PlantUML <https://github.com/sphinx-contrib/plantuml/>`_.
So it allows to define PlantUML diagrams.

But gives you access to need object data by supporting `Jinja <https://jinja.palletsprojects.com/>`_ statements,
which allows you to use loops, if-clauses, and it injects data from need-objects.

.. need-example::

   .. needuml::

      {{uml('FEATURE_NEEDUML1')}}
      {{uml('COMP_NEEDUML2')}}

   .. feature:: NeedUml example need
      :id: FEATURE_NEEDUML1
      :tags: needuml
      :status: draft

      Example Need for NeedUml.

   .. comp:: NeedUml example need 2
      :id: COMP_NEEDUML2
      :tags: needuml
      :status: draft

      Second example Need for NeedUml.

      .. needuml::

         {{flow('COMP_NEEDUML2')}} {
         card implement
         card {{needs['COMP_NEEDUML2'].status}}
         }

.. _needuml_options:

Options
-------


.. _needuml_extra:

extra
~~~~~
Allows to inject additional key-value pairs into the ``needuml`` rendering.
``:extra:`` must be a comma-separated list, containing *key:value* pairs.

.. need-example::

   .. needuml::
      :extra: name:Roberto,work:RocketLab

      card "{{name}}" as a
      card "{{work}}" as b
      a -> b

.. note::

   ``:extra:`` values are only available in the current PlantUML code.
   It is not available in code loaded via :ref:`needuml_jinja_uml`.
   So we suggest to use them only in non-embedded needuml directives.
   In an embedded needuml, you can store the information in the options
   of the need and access them with :ref:`needflow` like in
   :ref:`needuml` introduction.


.. _needuml_config:

config
~~~~~~
Allows to preconfigure PlantUML and set certain layout options.

For details please take a look into needflow :ref:`needflow_config`.


.. _needuml_debug:

debug
~~~~~

If ``:debug:`` is set, a debug-output of the generated PlantUML code gets added after the generated image.

Helpful to identify reasons why a PlantUML build may have thrown errors.

.. need-example::

   .. needuml::
      :debug:

      node "RocketLab" {
         card "Peter"
      }

.. _needuml_key:

key
~~~

Allows to store multiple ``needuml`` inside a need under ``arch`` under the given key, e.g. ``need["arch"]["key_name"]``.
If no option key given, then the first ``needuml`` will be stored in the need under ``arch`` under ``diagram``, ``need["arch"]["diagram"]``.
Option ``:key:`` value can't be empty, and can't be ``diagram``.

.. need-example::

   .. comp:: Component Y
      :id: COMP_002

      .. needuml::
         :key: sequence

         Alice -> Bob: Hi Bob
         Bob --> Alice: Hi Alice

      .. needuml::
         :key: class

         class Foo

      .. needuml::

         B -> C: Hi
         C -> B: Hi there

.. _needuml_save:

save
~~~~

Specifies the file path to store generated Plantuml-code of current ``needuml``. This given file path can be relative path
or file name, e.g. ``needuml_group_A/my_needuml.puml`` or ``my_needuml.puml``.

The file will be created and written during each build by 
using builder :ref:`needumls_builder` or other builder like `html` with configuration option :ref:`needs_build_needumls` configured.

If given file path already exists, it will be overwritten.

.. need-example::

   .. int:: Test needuml save
      :id: INT_001

      .. needuml::
         :save: needuml_group_A/my_needuml.puml

         Alice -> Bob: Hi Bob
         Bob --> Alice: Hi Alice

In this example, if builder :ref:`needumls_builder` is used, the plantuml-code will be exported to file at `outdir` of current builder,
e.g. `_build/needumls/needuml_group_A/my_needuml.puml`.


.. _needuml_jinja:

Jinja context
-------------
When using Jinja statements, the following objects and functions are available.


.. _needuml_jinja_needs:

needs
~~~~~
A Python dictionary containing all Needs. The ``need_id`` is used as key.

.. need-example::

   .. needuml::

      node "{{needs["FEATURE_NEEDUML1"].title}}"


.. _needuml_jinja_flow:

flow(id)
~~~~~~~~
Loads a Sphinx-Need object as PlantUML object.
We use the same layout used for :ref:`needflow`.

This functions represents each Need the same way.

.. versionchanged:: 1.0.3
   In the past the returned plantuml representation string ends with a
   newline. Now it is up to the author of the Jinja template to write
   the newline, which is normally anyway the case. E.g. see the following
   example, where the two `flow()` are separated by a newlone. With this
   approach it is possible to write plantuml code following `flow()`.
   E.g. see even the following example, with text following 
   `{{flow("COMP_001")}}`.

.. need-example::

   .. needuml::

      {{flow("FEATURE_NEEDUML1")}}
      {{flow("COMP_001")}} {
      card manuall_written
      }


.. _needuml_jinja_filter:

filter(filter_string)
~~~~~~~~~~~~~~~~~~~~~
Finds a list of Sphinx-Need objects that pass the given filter string.

.. need-example::

   .. needuml::

      {% for need in filter("type == 'int' and status != 'open'") %}
      node "{{need.title}}"
      {% endfor %}


.. _needuml_jinja_ref:

ref(id, option, text)
~~~~~~~~~~~~~~~~~~~~~

Allows to create an hyperlink to a Sphinx-Need object in a PlantUML schema. The
text associated to the hyperlink is either defined by `option` (in this case,
Sphinx-Need picks the text of the field specified by `option`), or by the free text `text`.


.. need-example::

   .. needuml::

      Alice -> Bob: {{ref("FEATURE_NEEDUML1", option="title")}}
      Bob -> Alice: {{ref("COMP_NEEDUML2", text="A completely free text")}}

.. _needuml_jinja_uml:

uml(id)
~~~~~~~
Loads a Sphinx-Need object as PlantUML object or reuses the stored PlantUML code inside the Sphinx-Need object.

If diagram code is available in the need data under ``arch``, the stored PlantUML diagram gets imported.

Please read :ref:`need_diagram` for details.


.. need-example::

   .. needuml::

      allowmixing

      {{uml("COMP_001")}}
      {{uml("FEATURE_NEEDUML1")}}


.. _needuml_jinja_uml_key:

Key argument
++++++++++++

:ref:`uml() <needuml_jinja_uml>` supports ``key`` argument to define which PlantUML code to load from the Sphinx-Need object.
``key`` value by default is ``diagram``. If no key argument given, then the PlantUML code is loaded from ``diagram`` under ``arch``
inside the need object.

.. need-example::

   .. comp:: Z
      :id: COMP_Z

      .. needuml::

         {{uml('COMP_002', 'sequence')}}


.. _needuml_jinja_uml_args:

Additional keyword arguments
++++++++++++++++++++++++++++

:ref:`uml() <needuml_jinja_uml>` supports additional keyword parameters which are then available in the loaded PlantUML code.

.. need-example::

   .. comp:: Variant A or B
      :id: COMP_A_B

      .. needuml::

         {% if variant == "A" %}
           class "A" as cl
         {% elif variant == "B" %}
           class "B" as cl {
               attribute_x
               function_x()
           }
         {% else %}
           class "Unknown" as cl
         {% endif %}

      By default **Unknown** is shown, as no variant was set.


Passing ``variant="A"`` parameter to the :ref:`uml() <needuml_jinja_uml>` function, we get the following:

.. need-example::

   .. needuml::
      :debug:

      {{uml("COMP_A_B", variant="A")}}

Passing ``variant="B"`` parameter to the :ref:`uml() <needuml_jinja_uml>` function, we get the following:

.. need-example::

   .. needuml::
      :debug:

      {{uml("COMP_A_B", variant="B")}}


.. _needuml_jinja_uml_chain:

Chaining diagrams
+++++++++++++++++
PlantUML Need objects uses the ``needuml`` directive internally to define their diagrams.
All features are available and ``uml()`` can be used multiple time on different levels of a planned architecture.


.. tab-set::

    .. tab-item:: Needs

        .. int:: Interface A
           :id: INT_A

           .. needuml::

              circle "Int A" as int

        .. comp:: Component X
           :id: COMP_X

           .. needuml::

               allowmixing

               {{uml("INT_A")}}

               class "Class A" as cl_a
               class "Class B" as cl_b

               cl_a o-- cl_b
               cl_a --> int

        .. sys:: System RocketScience
           :id: SYS_ROCKET

           .. needuml::

               allowmixing

               node "RocketScience" as rocket {
                   {{uml("COMP_X")}}
                   card "Service Y" as service

                   int --> service
               }

        And finally a ``needuml`` to make use of the Sphinx-Need system object:

        .. needuml::

            allowmixing

            {{uml("SYS_ROCKET")}}

            actor "A friend" as me #ff5555

            me --> rocket: doing


    .. tab-item:: Code

        .. code-block:: rst

            .. int:: Interface A
               :id: INT_A

               .. needuml::

                  circle "Int A" as int

            .. comp:: Component X
               :id: COMP_X

               .. needuml::

                  allowmixing

                  {{uml("INT_A")}}

                  class "Class A" as cl_a
                  class "Class B" as cl_b

                  cl_a o-- cl_b
                  cl_a --> int

            .. sys:: System RocketScience
               :id: SYS_ROCKET

               .. needuml::

                  allowmixing

                  node "RocketScience" {
                      {{uml("COMP_X")}}
                      card "Service Y" as service

                      int --> service
                  }

            And finally a ``needuml`` to make use of the Sphinx-Need system object:

            .. needuml::

               allowmixing

               {{uml("SYS_ROCKET")}}

               actor "A friend" as me #ff5555

               me --> rocket: doing


.. _needuml_example:

NeedUml Examples
----------------

.. need-example::

   .. needuml::
      :scale: 50%
      :align: right

      allowmixing

      class "Sphinx-Needs" as sn {
      requirements
      specifications
      test_cases
      customize()
      automate()
      export()
      }

      {% set ids = ["FEATURE_NEEDUML1", "COMP_NEEDUML2"]%}
      {% for need in needs.values() %}
         {% if need.id in ids %}
            card "{{need['title']}}" as need_{{loop.index}} #ffcc00
            need_{{loop.index}} --> sn
         {% endif %}
      {% endfor %}

      card "and much more..." as much #ffcc00
      much -> sn

.. need-example::

    .. comp:: Component X
       :id: COMP_001

       .. needuml::

          class "Class X" as class_x {
            attribute_1
            attribute_2
            function_1()
            function_2()
            function_3()
          }

           class "Class Y" as class_y {
                attribute_1
                function_1()
           }

           class_x o-- class_y