Page 27 - Krész, Miklós, and Andrej Brodnik (eds.). MATCOS-13. Proceedings of the 2013 Mini-Conference on Applied Theoretical Computer Science. Koper: University of Primorska Press, 2016.
P. 27
Code examples into the global data-flow graph. Nodes that output con-
stants have an additional argument in their constructor. In-
All constructs in our language are functional expressions and dividual nodes can be connected using the function
can be combined in the same way as arithmetic expressions. connect(node1, "outputName", node2, "inputName"),
The language is not whitespace sensitive. which connects output port outputName on node1 to input
port inputName on node2.
Let’s look at how we might calculate the n-th Fibonacci
number in our language: To get a better idea of how this works, let’s take a look at a
real-world example:
func int fib(int n) if n < 3 then 1 else fib(n-1) + fib(n-2)
func int main()
An iterative version using real numbers would look like this: with node s1, node s2, node s3, node s4,
node s5, node c1, node r1, node r2,
func real fibr(real n) node li1, node li2, node ck1,
with real a = 1.0, real b = 1.0, real c do node cb1, node si1
(for real i = 3.0, i < n do do (
c = a + b; # Create constant generators
a = b; s1 = String("bg.png");
b = c); s2 = String("fg.png");
b # this is the value that the function returns c1 = Color(color(0.0, 1.0, 0.0, 1.0));
r1 = Real(0.05);
Variables are declared with the keyword with and are valid r2 = Real(0.10);
in the expression following the keyword do. The for loop is s3 = String("over");
enclosed in parentheses due to the priority of the ; operator. s4 = String("f");
This operator behaves similarly to , (comma) in C. s5 = String("out.png");

4. DATA-FLOW GRAPHS # Create operations
li1 = LoadImage();
Data-flow graphs consist of nodes, which represent opera- li2 = LoadImage();
tions, and edges, which facilitate the flow of data between ck1 = ChromaKey();
nodes. In our implementation, the graphs are directed and cb1 = Combine();
acyclic, as this is powerful enough to represent all current si1 = SaveImage();
use cases and simplifies parallelisation.
# Connect ports
in2 connect(s1, "const", li1, "fileName");
in1 in3 connect(s2, "const", li2, "fileName");
connect(c1, "const", ck1, "keyColor");
Operation connect(r1, "const", ck1, "tolNear");
connect(r2, "const", ck1, "tolFar");
out1 out2 connect(li2, "imageOut", ck1, "imageIn");
connect(s3, "const", cb1, "mode");
Figure 2: Node structure. connect(s4, "const", cb1, "clipTo");
connect(ck1, "imageOut", cb1, "fgImage");
Each node can have input and output ports (see Figure 2). connect(li1, "imageOut", cb1, "bgImage");
An input port is merely a pointer to an output port of an- connect(cb1, "imageOut", si1, "imageIn");
other node. All input ports must be connected to something. connect(s5, "const", si1, "fileName");
Output ports also contain the data or results of the opera- );
tion of its node. Output ports can remain unconnected. 0

Nodes without any input ports are usually generators of con- This script generates the graph shown in Figure 3.
stants, while nodes without output ports usually save or dis-
play the results. String: String: Color: Real: Real:
bg.png fg.png rgb(0,1,0) 0.05 0.10
Each port has a data type associated with it. If the types
of an input and an output port don’t match, no connection const const const const const
between them can be made.
fileName fileName String: String:
5. GRAPH CREATION over f
LoadImage LoadImage
Graphs can be created using our domain-specific language. const const
Every operation gets its own constructor, which returns a imageOut imageOut
node handle and automatically adds the newly created node
imageIn keyColor tolNear tolFar

ChromaKey

imageOut

fgImage mode clipTo

Combine String:
out.png
bgImage
imageOut const

imageIn fileName

SaveImage

Figure 3: Graph example (image compositing).

The generated graph loads two images (bg.png and fg.png),
performs chroma keying on the foreground image, overlays
the result over the background image, and saves the final
composite into out.png.

m a t c o s -1 3 Proceedings of the 2013 Mini-Conference on Applied Theoretical Computer Science 27
Koper, Slovenia, 10-11 October
   22   23   24   25   26   27   28   29   30   31   32