Compiling
Please choose the appropriate tab for the language that you are working with.
- Java
- Ruby
Causal can generate a set of Java classes that allow you to interact with the system in a type-safe manner. Before generating these classes, you should make sure that your FDL file has a namespace
directive:
namespace "com.example"
The namespace specifies the Java package to which the compiler will write the API classes. If you don't have a namespace declared, Causal uses the default namespace io.causallabs.generated
.
Next, call the compiler with the --java
command line option. The --java
option takes an argument, the directory to write the generated code into:
## install the Causal compiler if it isn't already
$ sudo npm i --global @causal/compiler@latest
## compile the Java api
$ causalc --java build/generated-src/fdl example.fdl
[main] INFO io.causallabs.compiler.Compiler - Writing java code to 'build/generated-src/fdl'
You then need to include the Causal code into your project by marking that directory as a source directory, and including the Causal runtime as a dependency. In gradle:
dependencies
{
implementation 'io.causallabs:runtime:0.48.11'
}
// generate the java source for the Causal API
task generateFdlSource(type: Exec) {
commandLine 'causalc', '--java', 'build/generated-src/fdl', 'example.fdl'
}
compileJava.dependsOn(generateFdlSource)
// Include the Causal generated code in the build
sourceSets {
main {
java {
srcDirs 'build/generated-src/fdl/'
}
}
}
Causal will generate a Ruby file that allows you to interact with the system. Although Ruby is not a type-safe language, Causal generates YARD documentation tags that can be used to type check the code. Most IDEs also use these tags to do excellent autocomplete while working with Causal generated code.
Call the compiler using the --ruby
command line option to generate your ruby API.
## install the Causal compiler if it isn't already
$ sudo npm i --global @causal/compiler@latest
## generate the Ruby API
$ causalc --ruby lib/causal.rb example.fdl
[main] INFO io.causallabs.compiler.Compiler - Writing Ruby code to 'lib/causal.rb'