|
| 1 | +:github_url: https://github.com/UniversalRobots/Universal_Robots_Client_Library/blob/master/doc/architecture/script_reader.rst |
| 2 | + |
| 3 | +.. _script_reader: |
| 4 | + |
| 5 | +ScriptReader |
| 6 | +============ |
| 7 | + |
| 8 | +Script code used by the :ref:`script_sender` is read from a file. That script code might have to be |
| 9 | +dynamically modified based on some configuration input. For example, if the script code contains |
| 10 | +connections to a remote PC, that PC's IP address might be configured by the user. Another example |
| 11 | +would be to include certain parts of the script code only if the robot's software version supports |
| 12 | +that. |
| 13 | + |
| 14 | +For that purpose the ``ScriptReader`` class is provided. It reads the script code from a file and |
| 15 | +performs the following substitutions: |
| 16 | + |
| 17 | +- Replaces variables in the form of ``{{variable_name}}`` with the value of the variable |
| 18 | + from a provided dictionary. |
| 19 | +- Includes other script files using the directive ``{% include file_name %}``. The included file is |
| 20 | + read from the same directory as the main script file. Nested includes are also possible. |
| 21 | +- Use conditionals in order to add certain parts of the script code only if a condition matches. |
| 22 | + |
| 23 | +The supported substitutions use a basic implementation of the `Jinja2 templating engine` syntax. |
| 24 | + |
| 25 | +.. note:: |
| 26 | + One special literal is defined for **version information**. Use a software version prefixed with a |
| 27 | + ``v`` character, e.g. ``v10.8.0`` to encode a software version. Version information entries can be |
| 28 | + compared with each other. |
| 29 | + |
| 30 | + **Do not** wrap version information into quotes, as this will be interpreted as a string. |
| 31 | + |
| 32 | +Example |
| 33 | +------- |
| 34 | + |
| 35 | +Given two script files: |
| 36 | + |
| 37 | +.. literalinclude:: ../../tests/resources/example_urscript_main.urscript |
| 38 | + :caption: tests/resources/example_urscript_main.urscript |
| 39 | + :linenos: |
| 40 | + :lineno-match: |
| 41 | + |
| 42 | +.. literalinclude:: ../../tests/resources/example_urscript_feature.urscript |
| 43 | + :language: python |
| 44 | + :caption: tests/resources/example_urscript_feature.urscript |
| 45 | + :linenos: |
| 46 | + :lineno-match: |
| 47 | + |
| 48 | +The dictionary entry for ``feature_name`` is "torque control". |
| 49 | + |
| 50 | +Depending on the ``SOFTWARE_VERSION`` entry in the dictionary passed to the |
| 51 | +``ScriptReader``, the script code will be read as follows: |
| 52 | + |
| 53 | +Given ``SOFTWARE_VERSION = v5.21.0``, the script code will be: |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | + popup("The cool new feature is not supported on Software version 5.23.0") |
| 58 | +
|
| 59 | +
|
| 60 | +Given ``SOFTWARE_VERSION = v5.23.0``, the script code will be: |
| 61 | + |
| 62 | +.. code-block:: python |
| 63 | +
|
| 64 | + textmsg("torque control is a very cool feature!") |
| 65 | +
|
| 66 | +Supported Data |
| 67 | +-------------- |
| 68 | + |
| 69 | +Data dictionary (C++ side) |
| 70 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 71 | + |
| 72 | +The data dictionary supports the following types |
| 73 | + |
| 74 | +- ``str``: A string value, e.g. "Hello World" |
| 75 | +- ``int``: An integer value, e.g. 42 |
| 76 | +- ``double``: A floating point value, e.g. 3.14 |
| 77 | +- ``bool``: A boolean value, e.g. ``true`` or ``false`` |
| 78 | +- ``VersionInformation``: A version information value, e.g. ``VersionInformation::fromString("10.8.0")`` |
| 79 | + |
| 80 | +Script code side |
| 81 | +~~~~~~~~~~~~~~~~ |
| 82 | + |
| 83 | +Variable replacements |
| 84 | +^^^^^^^^^^^^^^^^^^^^^ |
| 85 | + |
| 86 | +Variable replacements in the script code are done using the syntax ``{{ variable_name }}``. For |
| 87 | +this to work, the variable ``variable_name`` has to be defined in the data dictionary passed to the |
| 88 | +``ScriptReader``. |
| 89 | + |
| 90 | +The expression ``{{ variable_name }}`` will be replaced with the string representation of the |
| 91 | +variable's content. |
| 92 | + |
| 93 | +- If the variable is a string, it has to be wrapped into quotes in the script |
| 94 | + code. e.g. |
| 95 | + |
| 96 | + .. code-block:: python |
| 97 | +
|
| 98 | + textmsg("{{ log_message }}") |
| 99 | +
|
| 100 | +
|
| 101 | +- Boolean variables will be replaced with the string ``True`` or ``False``. |
| 102 | +- Numeric variables (integer and floating point) will be replaced with the string representation generated by |
| 103 | + `std::to_string() <https://en.cppreference.com/w/cpp/string/basic_string/to_string>`_. |
| 104 | +- Version information variables will be replaced with the string representation similar to |
| 105 | + ``10.7.0.0`` |
| 106 | + |
| 107 | +Boolean expressions |
| 108 | +^^^^^^^^^^^^^^^^^^^ |
| 109 | + |
| 110 | +Boolean expressions have to follow one of two possible syntax variations |
| 111 | + |
| 112 | +- Direct evaluation of a boolean variable from the data dictionary: |
| 113 | + |
| 114 | + .. code-block:: |
| 115 | +
|
| 116 | + boolean_variable_name |
| 117 | +
|
| 118 | +- Comparison of a variable with a value using an operator. |
| 119 | + |
| 120 | + .. code-block:: |
| 121 | +
|
| 122 | + variable_name operator value |
| 123 | +
|
| 124 | + The operator has to be one of ``==``, ``!=``, ``<``, ``<=``, ``>``, ``>=``. On the lefthand side |
| 125 | + of the operator there has to be a variable from the data dictionary. The right hand side can be |
| 126 | + either a variable name or a value. If the right hand side is a variable name, it has to be |
| 127 | + defined in the data dictionary as well. Values will be parsed as follows: |
| 128 | + |
| 129 | + - Strings: Wrapped in quotes, e.g. ``"Hello World"`` or ``'Universal Robots'``. |
| 130 | + - Numerical values such as ``42``, ``3.14``, ``-1``, ``1e-12``. |
| 131 | + - Boolean values: See below. |
| 132 | + - Version information: Prefixed with a ``v`` character, e.g. ``v10.8.0``, ``v5.23.0``. |
| 133 | + |
| 134 | +Boolean values parsing |
| 135 | +^^^^^^^^^^^^^^^^^^^^^^ |
| 136 | + |
| 137 | +Boolean values can be parsed from the following strings: |
| 138 | + |
| 139 | +- ``true``, ``True``, ``TRUE`` |
| 140 | +- ``on``, ``On``, ``ON`` |
| 141 | +- ``yes``, ``Yes``, ``YES`` |
| 142 | +- ``1`` |
| 143 | +- ``false``, ``False``, ``FALSE`` |
| 144 | +- ``off``, ``Off``, ``OFF`` |
| 145 | +- ``no``, ``No``, ``NO`` |
| 146 | +- ``0`` |
| 147 | + |
| 148 | +Conditional blocks |
| 149 | +^^^^^^^^^^^^^^^^^^ |
| 150 | + |
| 151 | +Conditional blocks have to be started with a ``{% if condition %}`` directive and closed with a |
| 152 | +``{% endif %}`` directive. The condition can be any boolean expression as described above. |
| 153 | + |
| 154 | +The ``{% elif condition %}`` and ``{% else %}`` directives can be used to add alternative paths. |
| 155 | + |
| 156 | +Conditional blocks can be nested. |
0 commit comments