在本例中,我们将创建一些 "自定义块",以便对数组字符串执行操作。
在处理字符串时,有时需要使用大量的数学工具,而这些工具在教材和文章中通常是通过流程图来展示的(例如,在密码学中)。在这类问题中,能够快速建立解决方案模型、探索替代方案并进行实验是非常有用的。以块的形式表示算法可以提高可读性,简化处理算法结构的工作,便于将算法与信息处理的其他阶段结合起来,实现界面可视化等。
我们为本示例创建的自定义块旨在展示 Engee 中使用图形建模工具处理字符串的方法之一。
在每个区块的属性中,都有一个可以输入字符串的字段。
对该字符串的进一步处理在掩码编辑器中进行,该编辑器通过所需的方法将其转换为数字矢量:
- 将其转换为位表示、
- 或按每个字母在拉丁字母表中的位置进行编码(小写--
AbC
变为123
)、
- 或在西里尔字母表中做同样的处理 (
бвГ
变成234
)。
掩码会进行预处理,并将 "文本 "字段中输入的字符串转换为所需的编码。也可以直接在掩码中组织编码选择。
每个自定义区块的掩码:
- 将文本转换为所需的编码,并将其设置为
Value
数据块Constant
的参数,隐藏在掩码之下、
- 为块
Constant
设置一个特殊的采样时间 (SampleTime
),以便在通过块Unbuffer
后,每秒输出一个字符的输出参数向量、
- 更新数据块的外观。
块返回一个一定长度的数组。为便于处理单个比特或符号,块输出值的采样步长等于字符串的长度。*如果模型的积分步长为 1 秒,常数块每 5 秒才会输出一个 5 个字符的字符串。在每个常量模块之后都有一个 Unbuffer 模块,用于将输入向量分解为单个元素,并为每个模拟步长返回一个元素。
让我们绘制第一个程序块的输出结果Unicode String To Bytes
:
我们得到二进制表示的字符串 "A"(拉丁字符A
,十进制编码0x41
或65
):0100 0001
。
要获得 "字节表示 "的字符串,可以使用codeunits(ваша_строка)
命令,但应注意源字符串是以Unicode编码的,因此西里尔字符将是双字节。
Out[0]:
1×12 adjoint(::Base.CodeUnits{UInt8, String}) with eltype UInt8:
0xd0 0x9f 0xd1 0x80 0xd0 0xb8 0xd0 0xb2 0xd0 0xb5 0xd1 0x82
将术语表示为数字数组在协议分析和密码学中非常有用。
使用这些数据块,就可以将不必要的字符串技术工作隐藏起来,而不被资料专家发现,并集中精力演示建模过程或算法。
{"id": "db94afce-9c7b-49b7-b8b6-83ab310436d5", "data": [{"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "", "yaxis": "y", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(108, 159, 51, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [0.01, 0.01, 0.01], "type": "scatter", "x": [0, 8, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 3, "currentCount": 3}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "y1", "yaxis": "y", "legendgroup": "y1", "line": {"color": "rgba(0, 154, 250, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0.01, 0.01, 0.01, 0.01, 0.01], "type": "scatter", "x": [0, 8, 8, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 5, "currentCount": 5}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "", "yaxis": "y", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(0, 154, 250, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [1.02, 1.02, 1.02], "type": "scatter", "x": [0, 8, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 3, "currentCount": 3}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "y2", "yaxis": "y", "legendgroup": "y2", "line": {"color": "rgba(227, 111, 71, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [1.02, 1.02, 1.02, 1.02, 1.02], "type": "scatter", "x": [0, 8, 8, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 5, "currentCount": 5}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "", "yaxis": "y", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(227, 111, 71, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [0.03, 0.03, 0.03], "type": "scatter", "x": [0, 8, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 3, "currentCount": 3}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "y3", "yaxis": "y", "legendgroup": "y3", "line": {"color": "rgba(62, 164, 78, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0.03, 0.03, 0.03, 0.03, 0.03], "type": "scatter", "x": [0, 8, 8, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 5, "currentCount": 5}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "", "yaxis": "y", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(62, 164, 78, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [0.04, 0.04, 0.04], "type": "scatter", "x": [0, 8, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 3, "currentCount": 3}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "y4", "yaxis": "y", "legendgroup": "y4", "line": {"color": "rgba(195, 113, 210, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0.04, 0.04, 0.04, 0.04, 0.04], "type": "scatter", "x": [0, 8, 8, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 5, "currentCount": 5}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "", "yaxis": "y", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(195, 113, 210, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [0.05, 0.05, 0.05], "type": "scatter", "x": [0, 8, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 3, "currentCount": 3}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "y5", "yaxis": "y", "legendgroup": "y5", "line": {"color": "rgba(172, 142, 24, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0.05, 0.05, 0.05, 0.05, 0.05], "type": "scatter", "x": [0, 8, 8, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 5, "currentCount": 5}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "", "yaxis": "y", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(172, 142, 24, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [0.06, 0.06, 0.06], "type": "scatter", "x": [0, 8, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 3, "currentCount": 3}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "y6", "yaxis": "y", "legendgroup": "y6", "line": {"color": "rgba(0, 170, 174, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0.06, 0.06, 0.06, 0.06, 0.06], "type": "scatter", "x": [0, 8, 8, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 5, "currentCount": 5}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "", "yaxis": "y", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(0, 170, 174, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [0.07, 0.07, 0.07], "type": "scatter", "x": [0, 8, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 3, "currentCount": 3}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "y7", "yaxis": "y", "legendgroup": "y7", "line": {"color": "rgba(237, 94, 147, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0.07, 0.07, 0.07, 0.07, 0.07], "type": "scatter", "x": [0, 8, 8, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 5, "currentCount": 5}}}, {"showlegend": false, "mode": "markers", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "", "yaxis": "y", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(237, 94, 147, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [1.08, 1.08, 1.08], "type": "scatter", "x": [0, 8, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 3, "currentCount": 3}}}, {"showlegend": true, "mode": "lines", "xaxis": "x", "colorbar": {"y": 0.7395833333333333, "title": {"text": ""}, "len": 0.40392607174103246, "x": 0.9934383202099738}, "name": "y8", "yaxis": "y", "legendgroup": "y8", "line": {"color": "rgba(198, 130, 37, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [1.08, 1.08, 1.08, 1.08, 1.08], "type": "scatter", "x": [0, 8, 8, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 5, "currentCount": 5}}}, {"showlegend": false, "mode": "markers", "xaxis": "x2", "colorbar": {"y": 0.23958333333333323, "title": {"text": ""}, "len": 0.4039260717410324, "x": 0.9934383202099738}, "name": "", "yaxis": "y2", "legendgroup": "", "marker": {"symbol": "circle", "color": "rgba(108, 159, 51, 1.000)", "line": {"color": "rgba(0, 0, 0, 1)", "width": 1}, "size": 8}, "y": [0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0], "type": "scatter", "x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 17, "currentCount": 17}}}, {"showlegend": true, "mode": "lines", "xaxis": "x2", "colorbar": {"y": 0.23958333333333323, "title": {"text": ""}, "len": 0.4039260717410324, "x": 0.9934383202099738}, "name": "y1", "yaxis": "y2", "legendgroup": "y1", "line": {"color": "rgba(0, 154, 250, 1.000)", "shape": "linear", "dash": "solid", "width": 1}, "y": [0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0], "type": "scatter", "x": [0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16], "zaxis": null, "z": null, "metadata": {"shouldEnableSmartZoom": false, "smartZoomParams": {"minCount": 25000, "maxCount": 33, "currentCount": 33}}}], "config": {"showlegend": false, "paper_bgcolor": "rgba(255, 255, 255, 1.000)", "height": 400, "yaxis2": {"showticklabels": true, "gridwidth": 0.5, "tickvals": [0, 0.25, 0.5, 0.75, 1], "range": [-0.030000000000000027, 1.03], "domain": [0.03762029746281702, 0.44154636920384943], "mirror": false, "tickangle": 0, "showline": true, "ticktext": ["0.00", "0.25", "0.50", "0.75", "1.00"], "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "x2", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "xaxis2": {"showticklabels": true, "gridwidth": 0.5, "tickvals": [0, 5, 10, 15], "range": [-0.4800000000000004, 16.48], "domain": [0.0658209390492855, 0.9934383202099738], "mirror": false, "tickangle": 0, "showline": true, "ticktext": ["0", "5", "10", "15"], "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "y2", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "annotations": [{"yanchor": "top", "xanchor": "center", "rotation": 0, "y": 1, "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 20}, "yref": "paper", "showarrow": false, "text": "Кадры (8 бит в каждом + смещение для визуализации)", "xref": "paper", "x": 0.5296296296296296}, {"yanchor": "top", "xanchor": "center", "rotation": 0, "y": 0.49999999999999994, "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 20}, "yref": "paper", "showarrow": false, "text": "Поток битов", "xref": "paper", "x": 0.5296296296296296}], "plot_bgcolor": "rgba(255, 255, 255, 1.000)", "margin": {"l": 0, "b": 20, "r": 0, "t": 20}, "width": 1320.375, "xaxis": {"showticklabels": true, "gridwidth": 0.5, "tickvals": [0, 5, 10, 15], "range": [-0.4800000000000004, 16.48], "domain": [0.0658209390492855, 0.9934383202099738], "mirror": false, "tickangle": 0, "showline": true, "ticktext": ["0", "5", "10", "15"], "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "y", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}, "yaxis": {"showticklabels": true, "gridwidth": 0.5, "tickvals": [0, 0.25, 0.5, 0.75, 1], "range": [-0.02210000000000001, 1.1121], "domain": [0.537620297462817, 0.9415463692038495], "mirror": false, "tickangle": 0, "showline": true, "ticktext": ["0.00", "0.25", "0.50", "0.75", "1.00"], "zeroline": false, "tickfont": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 11}, "zerolinecolor": "rgba(0, 0, 0, 1)", "anchor": "x", "visible": true, "ticks": "inside", "tickmode": "array", "linecolor": "rgba(0, 0, 0, 1)", "showgrid": true, "title": {"text": "", "font": {"color": "rgba(0, 0, 0, 1)", "family": "sans-serif", "size": 15}}, "gridcolor": "rgba(0, 0, 0, 0.1)", "tickcolor": "rgb(0, 0, 0)", "type": "linear"}}}