From Workspace и To Workspace
В данной демонстрации опишем особенности использования блоков From Workspace и To Workspace. Наша цель – показать, как мы можем подать данные в модель или считать их из неё. Для этого реализована простая модель, показанная на рисунке ниже.
Далее мы подключим библиотеку, необходимую для формирования структуры данных WorkspaceArray.
Для начала создадим структуру DataFrame со столбцами временных отметок и данных.
После этого создаем объект WorkspaceArray, первый параметр которого – это внутреннее имя базы данных, а второй – данные из структуры DataFrame.
using DataFrames
# Задаем данные:
t = [1.0, 2.0, 4.0];
d = rand(Float64, size(t));
df = DataFrame(time=t, value=d)
3×2 DataFrame
Row │ time value
│ Float64 Float64
─────┼───────────────────
1 │ 1.0 0.554436
2 │ 2.0 0.564866
3 │ 4.0 0.116877
wa = WorkspaceArray(string(rand()), df)
WorkspaceArray("0.7893540708500778")
Перейдём к запуску нашей модели.
function run_model( name_model, path_to_folder )
Path = path_to_folder * "/" * name_model * ".engee"
if name_model in [m.name for m in engee.get_all_models()] # Проверка условия загрузки модели в ядро
model = engee.open( name_model ) # Открыть модель
model_output = engee.run( model, verbose=true ); # Запустить модель
else
model = engee.load( Path, force=true ) # Загрузить модель
model_output = engee.run( model, verbose=true ); # Запустить модель
engee.close( name_model, force=true ); # Закрыть модель
end
return model_output
end
run_model (generic function with 1 method)
run_model("from_to_workspace", @__DIR__)
Building...
Progress 0%
Progress 0%
Progress 5%
Progress 10%
Progress 15%
Progress 20%
Progress 25%
Progress 30%
Progress 35%
Progress 40%
Progress 45%
Progress 50%
Progress 55%
Progress 60%
Progress 65%
Progress 70%
Progress 75%
Progress 80%
Progress 85%
Progress 90%
Progress 95%
Progress 100%
На выходе мы также получим структуру WorkspaceArray. Выполним просмотр её полей и сравним с входными данными.
dump(out)
WorkspaceArray{Any}
name: String "out"
vecsize: Int64 11
method: Symbol file
chunks: OrderedCollections.OrderedDict{Int64, Array}
slots: Array{Int32}((16,)) Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
keys: Array{Int64}((0,)) Int64[]
vals: Array{Array}((0,))
ndel: Int64 0
maxprobe: Int64 0
dirty: Bool false
type: Symbol pair
range: StepRange{Int64, Int64}
start: Int64 1
step: Int64 1
stop: Int64 11
parent: Nothing nothing
time: WorkspaceArray{Any}
name: String "out"
vecsize: Int64 11
method: Symbol file
chunks: OrderedCollections.OrderedDict{Int64, Array}
slots: Array{Int32}((16,)) Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
keys: Array{Int64}((0,)) Int64[]
vals: Array{Array}((0,))
ndel: Int64 0
maxprobe: Int64 0
dirty: Bool false
type: Symbol time
range: StepRange{Int64, Int64}
start: Int64 1
step: Int64 1
stop: Int64 11
parent: Nothing nothing
time: ErrorException
msg: String "WorkspaceArray типа :time не имеет поля time"
value: ErrorException
msg: String "WorkspaceArray типа :time не имеет поля value"
value: WorkspaceArray{Any}
name: String "out"
vecsize: Int64 11
method: Symbol file
chunks: OrderedCollections.OrderedDict{Int64, Array}
slots: Array{Int32}((16,)) Int32[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
keys: Array{Int64}((0,)) Int64[]
vals: Array{Array}((0,))
ndel: Int64 0
maxprobe: Int64 0
dirty: Bool false
type: Symbol value
range: StepRange{Int64, Int64}
start: Int64 1
step: Int64 1
stop: Int64 11
parent: Nothing nothing
time: ErrorException
msg: String "WorkspaceArray типа :value не имеет поля time"
value: ErrorException
msg: String "WorkspaceArray типа :value не имеет поля value"
data = collect(out)
11×2 DataFrame
Row │ time value
│ Any Any
─────┼────────────────
1 │ 0.0 1.08801
2 │ 1.0 1.10887
3 │ 2.0 1.12973
4 │ 3.0 0.681743
5 │ 4.0 0.233753
6 │ 5.0 0.0
7 │ 6.0 0.0
8 │ 7.0 0.0
9 │ 8.0 0.0
10 │ 9.0 0.0
11 │ 10.0 0.0
using Plots
plot(df.time,df.value) # Входные данные
plot!(data.time,data.value) # Выходные данные
Как мы видим из сравнения, в результате данные, которые мы подали в модель, были увеличены в два раза.
Вывод
Мы продемонстрировали опции передачи данных из рабочей области в модели и обратно. Эти возможности среды существенно упрощают ваше взаимодействие с моделями.