Skip to content

Chain definition language

Giovanni Petrantoni edited this page Apr 4, 2021 · 9 revisions

Chainblocks wants to be a meta language or a visual language rather then a pure programming language, but to nicely describe chains and the data they contain we adopt and write a simple language based on EDN and Clojure. Notice that in theory any language can be used, this was just a pragmatic choice. In fact you will also find chains written in C++ digging the Chainblocks codebase.

You can basically think of it as a comprehensible JSON on steroids that can be evaluated or the Ansible of general programming.

The execution of Chainblocks scripts is meant to be fast, the script language is just a description of the computational graph. The runtime itself is completely detached.

Basically load, define the chain/s, attach them to a node and kick off the runtime.

Some examples

Super simple example

Code

; define a node
(defnode main)
; define a simple hello world looped chain
(defloop hello-world
  (Msg "Hello world"))
; schedule the chain for exection
(schedule main hello-world)
; run the node at 1 second intervals
(run main 1.0) ; <-- execution will block here

Output

[info] [2021-04-04 16:43:22.290] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:23.299] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:24.291] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:25.289] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:26.291] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:27.299] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:28.296] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:29.292] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:30.295] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:31.298] [T-148944] [logging.cpp::94] [hello-world] Hello world
[info] [2021-04-04 16:43:32.298] [T-148944] [logging.cpp::94] [hello-world] Hello world

Another example

Code

; define a node
(defnode main)
; define a simple hello world looped chain
(defloop counter
  ; Setup runs only once at chain start
  (Setup
   0 >= .counter)
  ; get counter variable and print
  .counter (Log)
  ; increase the counter
  (Math.Inc .counter)
  ; notice that Math.Inc will passthrough the value
  ; so the value will be before increment
  ; if we wanted to pick up the increment uncomment the next line
  ; .counter
  (When (Is 10) (Stop)))
; schedule the chain for exection
(schedule main counter)
; run the node at 1 second intervals
(run main 1.0) ; <-- execution will block here

Output

[info] [2021-04-04 19:00:43.045] [T-244992] [logging.cpp::53] [counter] 0
[info] [2021-04-04 19:00:44.055] [T-244992] [logging.cpp::53] [counter] 1
[info] [2021-04-04 19:00:45.058] [T-244992] [logging.cpp::53] [counter] 2
[info] [2021-04-04 19:00:46.057] [T-244992] [logging.cpp::53] [counter] 3
[info] [2021-04-04 19:00:47.049] [T-244992] [logging.cpp::53] [counter] 4
[info] [2021-04-04 19:00:48.057] [T-244992] [logging.cpp::53] [counter] 5
[info] [2021-04-04 19:00:49.046] [T-244992] [logging.cpp::53] [counter] 6
[info] [2021-04-04 19:00:50.049] [T-244992] [logging.cpp::53] [counter] 7
[info] [2021-04-04 19:00:51.058] [T-244992] [logging.cpp::53] [counter] 8
[info] [2021-04-04 19:00:52.054] [T-244992] [logging.cpp::53] [counter] 9
[info] [2021-04-04 19:00:53.055] [T-244992] [logging.cpp::53] [counter] 10
Clone this wiki locally