You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/c_tutorial/c_tutorial.rst
+9-10Lines changed: 9 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
Integrating C with DDlog
3
3
************************
4
4
5
+
**Thank you, @smadaminov, for kindly contributing this tutorial!**
6
+
5
7
.. contents::
6
8
7
9
Prerequisites
@@ -15,28 +17,27 @@ There is also an expectation that you are familiar with DDlog syntax and command
15
17
Introduction
16
18
============
17
19
18
-
In this set of tutorials we are going to explore various features of DDlog programming language and learn how to integrate DDlog program into C program.
20
+
In this set of tutorials we are going to explore various features of DDlog programming language and learn how to integrate a DDlog program into a C program.
19
21
To do that we are going to build a simple yet functional application as it is more fun to build real programs.
20
22
We will start by writing a simple program with a limited functionality to get familiar with the basics of DDlog.
21
23
Afterwards, over the course of this set of tutorials, we will gradually improve our application by applying more advanced techniques offered by DDlog.
22
24
23
25
Tutorial #1 - Reachability Monitor
24
26
==================================
25
27
26
-
In our first tutorial we are going to build a program that will calculate the reachability information in the network.
28
+
In our first tutorial we are going to build a program that will calculate the reachability information in a network composed of nodes connected by links.
27
29
User will update link status between nodes through CLI and our program will calculate the rest.
28
30
The purpose of this program is to provide a simple interface to a network operator and return information regarding which node can reach which.
29
31
Thus, the only concern of network operator is to update link status without having to worry about doing those calculations themself.
30
32
We are going to test our program with the network consisting of four nodes: Menlo Park, Santa Barbara, Los Angeles, and Salt Lake City.
31
33
However, nothing prevents us from using the network comprised of hundreds of nodes.
32
34
33
35
Let's start with defining our program in the DDlog language.
34
-
In our program we need to supply source node, destination node, and a link status between them.
35
36
Now we are ready to define our input relation :code:`Links`.
36
37
Let's create a file called :code:`t1_reachability_monitor.dl` and add there a following line representing input relation for our reachability monitor:
However, for the reachability monitor to be useful it also should provide some output and not just ingest the data.
@@ -58,7 +59,6 @@ First, we say that any two directly connected nodes such that the :code:`link_st
58
59
Furthermore, any two nodes are considered connected if there is path from the source node to the destination node such that all directly connected nodes along the path also have the :code:`link_status` between them to be :code:`true`.
59
60
Take a minute here to realize how easy it is to write such rule in DDlog.
60
61
While these rules may not require much effort in other programming languages, the distinct feature of DDlog is that the computing is done incrementally!
61
-
Isn't is amazing?
62
62
63
63
With that done, let's compile and start our program.
64
64
We will feed our DDlog code to the DDlog compiler and it will generate a new folder.
@@ -88,7 +88,7 @@ To make our life more interesting (and just slightly more complicated) the links
88
88
89
89
.. image:: ./images/topology.jpg
90
90
91
-
Note that DDlog is a `transaction-based`_ programming language.
91
+
Note that DDlog exposes a `transaction-based`_ API.
92
92
Each transaction begins with a :code:`start` command and ends with a :code:`commit` command.
93
93
Let's start DDlog shell again and insert records to recreate the network topology above:
94
94
@@ -122,8 +122,8 @@ Let's start DDlog shell again and insert records to recreate the network topolog
122
122
In the output we can see all cities with direct links between them are connected.
123
123
Furthermore, as we specified in our DDlog code, if there is a path between two cities then they are also connected, e.g., Menlo Park is connected to Los Angeles.
124
124
However, some nodes are connected to themselves.
125
-
But why this happened?
126
-
If we take a closer look at our rules we can notice that this phenomenon actually make sense.
125
+
How did this happen?
126
+
If we take a closer look at our rules we can notice that this phenomenon actually makes sense.
127
127
For example, Santa Barbara is reachable from Santa Barbara through Los Angeles.
128
128
While it is not necessarily horrible or wrong we may want to avoid it as it clutters the relation and the output.
129
129
More notably, we definitely don't want the network traffic go to Santa Barbara from Santa Barbara through Los Angeles (in the real world this actually may happen but this is a completely different topic).
@@ -395,8 +395,7 @@ We need to provide this function with the table ID of a target relation and a re
395
395
Once we have command ready we can apply it to our DDlog program using :code:`ddlog_apply_updates()` function.
396
396
We need to supply program handle, array of commands to be applied, and the length of this array.
397
397
In our case, we only have a single command to apply.
398
-
However, it is possible to pass multiple commands to a single call to the :code:`ddlog_apply_update()` function.
399
-
It helps avoiding cluttering code if you want to apply multiple commands.
398
+
However, it is possible (and is more efficient) to pass multiple commands to a single call to the :code:`ddlog_apply_update()` function.
400
399
Note, that in the case of multiple commands if some of them fail then some subset of commands may still be applied.
401
400
Please refer to the API description in the :code:`ddlog.h` header file for more details.
0 commit comments