In a recent article, I introduced the notion of ontology and knowledge graph.

Then I exposed in this post how to parse a triplestore to create a directed graph in memory.

This post will show you how to manipulate the graph. The use-case is a representation of the graph’s information thanks to a template engine.

The engine should be extensible enough to generate any text-based report (HTML, markdown, …)

At the end of the post, we eventually build a basic webserver that similarly presents the information of’s ontology. our implementation (localhost)

Caution: the solution is a proof of concept, its implementation works but is not bulletproof. Some tests would make it safer to use, and a TDD would influence the package’s design. Consider it as a code for educational purposes only.

The template engine

From the documentation of the text/template package of Go’s standard library:

Package template implements data-driven templates for generating textual output.

It works by …

… applying [templates] to a data structure. Annotations in the template refer to elements of the data structure (typically a field of a struct or a key in a map) to control execution and derive values to be displayed. Execution of the template walks the structure and sets the cursor, represented by a period ‘.’ and called “dot”, to the value at the current location in the structure as execution proceeds.

At first, this sounds like a plan for the gnomes of southpark. 1

Let’s rephrase it. In essence, the workflow is:

  • Collect the data (this has been done in the previous post)
  • create a data structure holding the elements you want to represent ;
  • create a skeleton of the representation with placeholders for the values of the structure you expect to see ;
  • apply the template to the data structure ;
  • profit!

The data structure

First, we need a data structure.

The data structure will hold the information we will represent via the application of the template.

We will place ourselves in the context of the node and think like a vertex.

So the structure we are setting starts by referencing the node:

1// Current object to apply to the template
2type Current struct {
3    Node  *graph.Node

Then within the template we can access the exported fields and methods of the graph.Node structure defined in the previous article.

1type Node struct {
2    id              int64
3    Subject         rdf.Term
4    PredicateObject map[rdf.Term][]rdf.Term

For example, this template will display the output to a call to the RawValue() method of the Subject field:

1The subject is {{ .Node.Subject.RawValue }}

Full example

As a proof of concept, we can write a simple Go example that displays a value from a basic ontology:

 1func Example() {
 2	const ontology = `
 3@prefix rdf: <> .
 4@prefix rdfs: <> .
 5@prefix example: <> .
 7example:PostalAddress a rdfs:Class ;
 8    rdfs:label "PostalAddress" ;
 9    rdfs:comment "The mailing address." .
11	const templ = `
12The subject is {{ .Node.Subject.RawValue }}
13	`
14	type Current struct {
15		Node *graph.Node
16	}
17	parser := rdf.NewParser("")
18	gr, _ := parser.Parse(strings.NewReader(ontology))
19	g := graph.NewGraph(gr)
21	postalAddress := g.Dict[""]
22	node := g.Reference[postalAddress]
24	myTemplate, _ := template.New("global").Parse(templ)
25	myTemplate.Execute(os.Stdout, Current{node})
27	// output:
28	// The subject is


  • On line 2, we define a simple ontology as a string constant;
  • on line 11, we define a simple template to display the subject of a node;
  • on lines 21/22, we find the node reference that holds the subject “PostalAddress”;
  • on line 25, we apply the template on the object “Current” created ad-hoc, which contains the targeted node.

On the same principles, we could display the predicates’ content and the objects of the node structure. Those are arrays (slices) and maps. Therefore, we have to use the builtin actions of the template engine to access the data elements (cf the action paragraph in the template documentation for more info)


1The subject is {{ .Node.Subject.RawValue }}
2and the list of preficates are:
3{{ range $predicate, $objects := .Node.PredicateObject -}}
4* {{ $predicate }}
5  - {{ range $objects }}{{ .RawValue }}{{ end }}
6{{ end -}}

which gives:

1The subject is
2and the list of preficates are:
3* <>
4  -
5* <>
6  - PostalAddress
7* <>
8  - The mailing address.

Going further

This system is good enough to display a simple structure. But, we are in the context of ontology and graphs. Therefore it is essential to be able to walk the graph and to display siblings and edges.

To achieve this, we will add a pointer to the graph structure itself in the data provider, that is, the “Current” object:

1// Current object to apply to the template
2type Current struct {
3    Graph *graph.Graph
4    Node  *graph.Node

The we add a some pyntactic sugar to our structure:

1// To the node with edge holding a value from the links array
2func (g Current) To(links ...string) []Current { // ... }
4func (g Current) From(links ...string) []Current { // ... }
6func (g Current) HasPredicate(predicate, object string) bool { // ... }

Note The implementation of the methods and the documentation is isolated into a template package. The reference of this package can be found on

As an example, let’s complete our sample triplestore:

 1example:PostalAddress a rdfs:Class ;
 2    rdfs:label "PostalAddress" ;
 3	rdfs:comment "The mailing address." .
 5example:addressCountry a rdf:Property ;
 6	rdfs:label "addressCountry" ;
 7	rdfs:domain example:PostalAddress ;
 8	rdfs:comment "A comment" .
10example:address a rdf:Property ;
11	rdfs:label "address" ;
12	rdfs:domain example:PostalAddress ;
13	rdfs:comment "Physical address of the item." .

And extend the template with a call to the To function we’ve created.

1{{ range $current := .To }}
2The subject is {{ .Node.Subject.RawValue }}
3and the list of preficates are:
4{{ range $predicate, $objects := .Node.PredicateObject -}}
5- {{ $predicate }}
6  - {{ range $objects}}{{.RawValue}}{{end}}
7{{ end -}}
8{{ end -}}

Then, without modifying the rest of the code we’ve exposed in the previous paragraph, the execution of the example gives the following result:

 1The subject is
 2and the list of preficates are:
 3* <>
 4  -
 5* <>
 6  - address
 7* <>
 8  - Physical address of the item.
 9* <>
10  -
12The subject is
13and the list of preficates are:
14* <>
15  -
16* <>
17  - addressCountry
18* <>
19  - A comment
20* <>
21  -

A simple web service

Now that we have built of the tools we need to render the graph, let’s build a very simple webserver to present the information of the knowledge graph. As explained in introduction, we will use the ontology of as a database (the creation of the knowledge graph is explained in the previous article).

Creating the template

Each representation of a node is a single html page. It is accessed through a call to “http://serviceurl/NodeSubject”.

The skeleton of the page is a template. To make things easier, we split the tamplate into three subtemplates.

  • a main template that will create the HTML structure and the outside table
  • a template to display a class as a tbody structure
  • a property template to display a line of the tbody structure
 1{{ define "main" }}
 2<!DOCTYPE html>
 3<!-- boilerplate of the HTML file -->
 4    <table class="blueTable">
 5        <!-- header of the table -->
 6        {{ template "rdfs:type rdfs:Class" . }}
 7    </table>
 9{{ end }}
11{{ define "rdfs:type rdfs:Property" }}
13{{ calls to display the subjects and predicates }}
15{{ end }}
17{{ define "rdfs:type rdfs:Class" }}
19    <tr>
20    <!-- The rest of the table structure -->
21    {{ range over the "To" nodes for the graph held in `Current` }}
22        {{ for each node, if its type is "property" }}
23            {{ template "rdfs:type rdfs:Property" . -}}
24    {{ range over the "From" nodes for the graph held in `Current` }}
25        {{ for each node, if its type is "class" }}
26                {{ template "rdfs:type rdfs:Class" . -}}
27    </tr>
29{{ end }}

There is not much interest in displaying all the wiring inside this article. The full template is available here

This will display a nice formated page when mixed with a node of the graph.

The web server

To make things easier, let’s encapsulate all of this inside a simple webserver. The goal is to be able to display any node of the graph when accessed through a URL.

First, we create a structure that will implement the http.Handler interface.

For convenience, this structure carries the graph, the template, and a hashmap of the rdf.IRI. The later is used to shorten the URLs (calling http://myservice/blabla instead of http://myservice/

1type handler struct {
2	namespaces map[string]*rdf.IRI
3	g          graph.Graph
4	tmpl       *template.Template
7func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}

The ServeHTTP method is composed of three parts:

  • extract the node from the URL
  • check if the node exists
  • apply the template and write the result on the ResponseWriter

I won’t detail all the code to implement this. You can look at the implementation on GitHub.

We need to glue all the code from the articles together to:

  • parse a triplestore from an io.Reader;
  • create a graph in-memory;
  • read the template file and create the Go template;
  • create the handler to reference the graph and the template;
  • register the handler to serve an HTTP request;

Sounds tricky, but it is reasonably easy and straightforward if you do a little bit of Go. Anyhow, a sample implementation is on GitHub.To launch it, simply do:

1curl -s | go run main.go

Then point your browser to “http://localhost:8080/PostalAddress” and you should get something that looks like this:


This was the last article about ontology. Through the pieces, we’ve discovered a way to describe a knowledge graph to be easy to write for a human and efficient enough to parse for a machine. Then we’ve built a graph in-memory and exploited this graph to represent the knowledge. The representation layer can be seen as a projection layer that exposes the information required for a specific functional domain.

Now let’s have fun with ontology to:

  • document functional areas and act as a shared ubiquitous language
  • provide a map of information to locate and use a specific part of the knowledge of a system

  1. Phase 1: Collect underpants / Phase 2: ? / Phase 3: Profit ↩︎