# Include programmatically generated julia file

**URL:** https://discourse.julialang.org/t/include-programmatically-generated-julia-file/52373
**Category:** General Usage
**Created:** [December 25, 2020, 1:26pm UTC](https://discourse.julialang.org/t/include-programmatically-generated-julia-file/52373 "2020-12-25T13:26:55Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ivborissov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivborissov/32/2841_2.png) [@ivborissov](https://discourse.julialang.org/u/ivborissov)
#### Post date: [December 25, 2020, 1:26pm UTC](https://discourse.julialang.org/t/include-programmatically-generated-julia-file/52373/1 "2020-12-25T13:26:56Z")

</div>

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:

```julia
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:

```julia
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` ?

---

<div class="post-metadata">

### Author: ![ivborissov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivborissov/32/2841_2.png) [@ivborissov](https://discourse.julialang.org/u/ivborissov)
#### Post date: [December 25, 2020, 2:19pm UTC](https://discourse.julialang.org/t/include-programmatically-generated-julia-file/52373/2 "2020-12-25T14:19:02Z")

</div>

Solved with

```julia
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

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [December 25, 2020, 3:29pm UTC](https://discourse.julialang.org/t/include-programmatically-generated-julia-file/52373/3 "2020-12-25T15:29:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ivborissov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ivborissov/32/2841_2.png) [@ivborissov](https://discourse.julialang.org/u/ivborissov)
#### Post date: [December 25, 2020, 3:47pm UTC](https://discourse.julialang.org/t/include-programmatically-generated-julia-file/52373/4 "2020-12-25T15:47:51Z")

</div>

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
