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.
In the properties of each block there is a field through which you can enter a string.
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 -
AbCIt will become123), - or does the same thing in the Cyrillic alphabet (
бвГIt will become234).
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:
- Converts the text to the desired encoding and sets it as a parameter
Valuethe blockConstantHidden under a mask, - affixes the block
Constantspecial sampling time (SampleTime), so that after passing through the blockUnbufferThe vector of output parameters was output one character per second., - 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:
# Загрузим модель, если она еще не открыта на холсте
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:
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)
)
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.
codeunits( "Привет" )'
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.
