Help:Creating Graphs with Semantic MediaWiki

From semantic-mediawiki.org
Semantic extension(s): Semantic MediaWiki · Semantic Result Formats
Further extension(s): Graphviz · Flowchartwiki · ImageMap
Keyword(s): Semantic graph · #tag

Description:

In MediaWiki it is possible to render Text in a graph that even has active internal and external links by using Mediawiki extensions. However combining graph rendering with Semantic MediaWiki features can prove tricky, in particular rendering complex graphs as a result of (a)semantic query(s). Hence this collection of some hints.

Semantic Result Format "graph"[edit]

One good way to go is to use the "graph" format based on the graphviz library, see documentation here. This might be a good solution for some purposes. However it does have limitations, such as

  • one graph can contain only results from one query
  • one can only use one node shape in one graph


Flowchartwiki[edit]

In particular for flowcharts this is an interesting solution. It also relies on the graphviz software library (without needing the graphviz extension). This is a MW extension, so it does not explicitely make use of SMW annotations.

It's syntax collides with SMW, thus the code needs to be patched and different markup be used in order to coexist with SMW: http://www.flowchartwiki.org/wiki/index.php/Installation#Parallel_usage_with_Semantic_MediaWiki


GraphViz Extension[edit]

Fortunately as of writing this (September 2015) the efforts and work of the graphviz extension (https://www.mediawiki.org/wiki/Extension:GraphViz) have been resumed. This also is not a semantic but a plain MW extension, relying (among other) on the graphviz library. However it does not collide with SMW but rather blends in nicely.

This is possibly one of the more powerfull ways to combine semantic data and graph drawing. There is one pitfall to circumvent: GraphViz is a tag extension, yet the MW parser would need to render his results into the <graphviz> tag (before the graphviz renderer gets active). This can be achieved using the MagicWord #tag.

One example, in detail:

1. Assuming you had a SMW page called "Name", using a "Who" attribute like this [[Who::World]], then you could on another page have this:

  Hello {{#show: Name| ?Who }}

and it would render "Hello World". If attribute "Who" is of type "Page" (which is default), "World" is a link to the page with that name. Of course you still can force the link even if "Who" is type "Text" by adding square brackets around the #show-statement.

2. Having installed Graphviz, the following should render a simple 2 node graph with 1 oval and 1 box

  <graphviz format="png" >
   digraph nr1 {
   Hello [shape=oval,URL="[[Hello]]"];
   World [shape=box,URL="[[World]]"];
   Hello->World
   }
   </graphviz>

3. Now how to bring these together? The following will *NOT* work:

  <graphviz  format="png" >
   digraph nr2 {
   Hello [shape=oval,URL="[[Hello]]"];
   World [shape=box, URL="{{#show: Name| ?Who}}"];
   Hello -> World
   }
   </graphviz>

(Note: Even if "Who" is of type text und you add the bracket manually it will not work)

But #tag will do the trick for you:

  {{#tag:graphviz|digraph nr3 {
   Hello [shape=oval,URL="[[Hello]]"];
   World [shape=box, URL="{{#show: Name| ?Who}}"];
   Hello -> World
   }|format="png"}}

So what one can do is to construct one or several(!) #show or #ask queries, possibly using the template output format, so to generate the (in this example) 3 inner lines of this code snippet, and all results will render in one single graph. Use conditional statements in the template e.g. to set box vs. oval shape depending on attribute values.