The KGraph is the basic data structure used by the Eclipse Layout Kernel (ELK) to describe and work with graphs. While developing layout algorithms, it is often necessary to assemble very specific graphs to see what the algorithm does with them. This is what the KGraph Text language was designed for: to be a simple language to assemble KGraphs for testing purposes.
This short tutorial will first introduce you to the KGraph and then walk you through writing your first KGT file. Grab a cup of tea and a few biscuits, slip into something more comfortable and get ready!
There's a few things to do before we dive into the tutorial itself. For example, to do Eclipse programming, you will have to get your hands on an Eclipse installation first. Read through the following sections to get ready for the tutorial tasks.
Take a look at the meta model on the KGraph Meta Model page. As you can see there, graphs consist of nodes and directed edges that connect the nodes. Nodes can declare special connection points called ports that edges can, but don't have to connect to. Nodes, ports, and edges can have labels that describe them. Since we don't want to pass lists of nodes around, each graph has a top-level node that contains the other nodes. This kind of parent-child relationship also enables us to describe nested graphs: graphs where nodes can contain further nodes themselves. So far, this is not all that surprising.
Our main goal with the KGraph is to have a data structure for layout algorithms. To compute a layout, we need to be able to specify the size and coordinates of each graph element. This is where the KLayoutData Meta Model comes into play. Each graph element can have arbitrarily many KGraphData
elements attached to it. The KLayoutData meta model describes a number of such types that go with different graph elements:
Graph Element | KGraphData Type |
---|---|
KNode | KShapeLayout |
KPort | KShapeLayout |
KLabel | KShapeLayout |
KEdge | KEdgeLayout |
A shape has a size and width as well as coordinates and, possibly, insets. An edge has a starting point, an end point, and a list of bend points. Note that different coordinates have different frames of reference, as shown in the diagram below the KLayoutData meta model.
Layout algorithms will usually provide different options to configure them to your needs. To this end, each KGraphData class has a list of properties associated with it. Layout algorithms will go and evaluate properties as they compute layouts.
Start by adding a first node. Enter the following text into the editor:
knode node1 |
The KLighD view should update itself, but you won't know since your node has no size yet and KLighD doesn't know how to draw it. Yep, that's not quite as helpful as you would have hoped. But alas, help is on its way!
Add two further nodes, node2
and node3
:
knode node2 knode node3 |
Uniform sizes are boring, so let's give more individuality to node3
:
knode node3 { size: width=80 height=40 } |
Let's add connection points to the nodes. Add two ports to node1
:
knode node1 { kport port1_1 kport port1_2 } |
Two black rectangles should now appear at node1
. If we had turned off default values, the ports would not have a proper size and would miss their labels.
port2_1
and port2_2
to node2
and a port port3_1
to node3
.It's now time to connect the nodes. Add two edges to the graph that originate at node1
by adding the following lines under the port definitions of node1
:
kedge (:port1_2 -> node2:port2_1) kedge (:port1_1 -> node3:port3_1) |
Edges can start and end at a node or at a port. The source node does not need to be explicitly specified since it is clear from the context (the edges are defined in the body of the source node, after all). Thus, if an edge connects directly to the source node (that is, not through a port), the part before the arrow (->
) will be empty. The target needs the node to be specified, with an optional target port. Add another edge that starts at port2_2
and ends at port3_1
. By now, the KLighD view should show something like this (with stylish styling enabled, of course):
Let's add a final touch to the graph. Currently, the edges are routed orthogonally. If we want to change that, we need to tell the layout algorithm to use another edge routing algorithm. This can be done by attaching a layout option to our graph. Add a new properties
section to the beginning of the file:
properties: org.eclipse.elk.edgeRouting=POLYLINE |
Your result could look something like this:
Properties can be attached to just about anything in a KGraph: the graph itself, nodes, ports, labels, ... |
Okay, how about a final final touch. The node labels are currently centered inside their nodes. You can change that by adding the following property to, say, node1
:
org.eclipse.elk.nodeLabels.placement="OUTSIDE V_BOTTOM H_CENTER" |
This will center its label below the node.
Right, that concludes our little tutorial. If you want to go further, read more on the KGT syntax and look at a bigger example. Also, solve the following assignment.
Graphs can also be nested. Child nodes are added to a parent node just like you added ports to parent nodes. Try to extend the graph above such that it looks like this: Note that the edges inside |