Engee documentation
Notebook

Working with strings using Engee blocks

In this example, we will create several "custom blocks" that will allow us to perform operations on strings as arrays.

Task description

Sometimes working with strings requires the use of essential mathematical apparatus, which is usually presented in educational materials and articles through flowcharts (for example, in cryptography). In such tasks, it is very useful to be able to quickly model a solution, explore alternatives, and perform experiments. The representation of the algorithm in the form of blocks improves readability and simplifies working with their structure, makes it easy to combine the algorithm with other stages of information processing, visualize interfaces, etc.

The custom blocks we created for this example are designed to demonstrate one of the approaches for working with strings using graphical modeling tools in Engee.

post_pic_string_to_vector_blocks.png

In the properties of each block there is a field through which you can enter a string.

image.png

Further work with this string is carried out in the mask editor, which converts it into a numerical vector using the required method.:

  • translates to a bit representation,
  • or encodes each letter through its position in the Latin alphabet (in lowercase - AbC It will become 123),
  • or does the same thing in the Cyrillic alphabet (бвГ It will become 234).
image.png

The mask performs preprocessing and converts the string entered in the "Text" field to the desired encoding. It is also possible to arrange the encoding selection directly in the mask.

What does the block mask do?

Mask of each custom block:

  1. Converts the text to the desired encoding and sets it as a parameter Value the block Constant Hidden under a mask,
  2. affixes the block Constant special sampling time (SampleTime), so that after passing through the block Unbuffer The vector of output parameters was output one character per second.,
  3. Updates the appearance of the block.

The blocks return an array of some length. To make it easier to work with individual bits or characters, the sampling step of the output value of the block is made equal to the length of the string. During the integration step of the 1c model, a 5-character string will be output by the Constant block only every 5 seconds. After each such block, there is an Unbuffer block that decomposes the input vector into separate elements and returns an element for each step of the simulation.

Launching the model

Launching this model using software control commands:

In [ ]:
# Загрузим модель, если она еще не открыта на холсте
if "string_to_bytes_model"  getfield.(engee.get_all_models(), :name)
    engee.load( "$(@__DIR__)/string_to_bytes_model.engee");
end

model_data = engee.run( "string_to_bytes_model" );

Let's build a graph for the output of the first block Unicode String To Bytes:

In [ ]:
data_frames = collect( model_data["Unicode String To Bytes.1"] );
data_stream = collect( model_data["Unbuffer.1"] );

plot(
    plot( data_frames.time, hcat( data_frames.value... )' .+ (0.01.*collect(1:8))', st=:step, markershape=:circle, title="Кадры (8 бит в каждом + смещение для визуализации)" ),
    plot( data_stream.time, data_stream.value, st=:step, markershape=:circle, title="Поток битов" ),
    layout=(2,1), legend=false, titlefont=font(10)
)
Out[0]:

We get the string "A" (Latin character A, code 0x41 or 65 in the decimal system) in the binary representation: 0100 0001.

You can get a string in a "byte-by-byte representation" using the command codeunits(ваша_строка) but it's worth bearing in mind that the source string is encoded in Unicode, which is why Cyrillic characters will be two-byte.

In [ ]:
codeunits( "Привет" )'
Out[0]:
1×12 adjoint(::Base.CodeUnits{UInt8, String}) with eltype UInt8:
 0xd0  0x9f  0xd1  0x80  0xd0  0xb8  0xd0  0xb2  0xd0  0xb5  0xd1  0x82

Conclusion

Representing a term as an array of numbers is useful in protocol analysis and cryptography tasks.

Using such blocks allows you to hide unnecessary levels of technical work with strings from the relevant specialist and focus on demonstrating the simulated process or algorithm.

Blocks used in example