Include programmatically generated julia file

Hi!
My question is about a good way to include programmatically generated julia file .
The example is the following: I have a pkg X which generates julia models (functions or DSL-macros) from some external source file (xml-based modeling language) and includes function to simulate a model:

module X

struct MyModel
  f1::Function
  f2::Function
end

function generate_model_jl_file(path_to_xml_file)
  ##
  return path_model_jl_file
end

function build_model(path_to_xml_file)
  path = generate_model_jl_file(path_to_xml_file)
  include(path)
  return Base.invokelatest(__mymodel)
end

function simulate(m::MyModel)
  ##
end

end#module

the generated model.jl file is a function (or macro) that defines a number of functions and wraps it into MyModel struct with __mymodel function:

function __mymodel()
  ## define a number of functions: f1, f2
  return MyModel(f1, f2)
end

The reason I wrap include inside of a function is that I need to hide all technical steps from the enduser (who doesn’t know julia) and allow him to run a single line m=build_model(path_to_xml_file)
The main bad thing about this code is it loads __mymodel function inside of the X module. So all functions defined in __mymodel() are also loaded to X.f1, X.f2. That is bad in general. But also it makes problematic to simulate model on remote workers (@spawnat 2 simulate(m)). Is it possible to rewrite the same idea to include not into X module but into users Main ?

Solved with

function build_model(path_to_xml_file)
  path = generate_model_jl_file(path_to_xml_file)
  Base.include(Main. path)
  return Base.invokelatest(Main.__mymodel)
end

Not sure if it is the “recommended” workflow, but it solves my problem

I cannot help with the details, but writing a file and loading it is not the way modeling packages deal with this. You might take a look at JuMP, for example, to get some inspiration.

1 Like

Thanks for response! I understand that most modeling packages imply using a DSL for writing models directly in Julia. However in my case I have to use the existing modeling software and either parse the the model files and build julia expressions (or generate jl files and import them) to run simulations in Julia

1 Like