Skip to main content

Compiling

Please choose the appropriate tab for the language that you are working with.

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.47.0'
}

// 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/'
}
}
}