From de25ae35ccb0613290ba20bfcf0ea15476c26ed5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 28 Feb 2007 02:48:54 +0000 Subject: Added python script to convert RDF files to dot graphs. git-svn-id: http://svn.drobilla.net/lad/machina@340 a436a847-0d15-0410-975c-d299462d15a1 --- util/Makefile.am | 1 + util/machina2dot.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 util/Makefile.am create mode 100755 util/machina2dot.py (limited to 'util') diff --git a/util/Makefile.am b/util/Makefile.am new file mode 100644 index 0000000..7186db5 --- /dev/null +++ b/util/Makefile.am @@ -0,0 +1 @@ +EXTRA_DIST = machina2dot.py diff --git a/util/machina2dot.py b/util/machina2dot.py new file mode 100755 index 0000000..bbf9725 --- /dev/null +++ b/util/machina2dot.py @@ -0,0 +1,71 @@ +#!/usr/bin/python + +import sys +import RDF + +model = RDF.Model() +parser = RDF.Parser(name="guess") + +if len(sys.argv) != 2: + print "Usage: machina2dot FILE" + sys.exit(-1) + +parser.parse_into_model(model, "file:" + sys.argv[1]) + + +print """ +digraph finite_state_machine { + rankdir=LR; + size="8,5" + node [shape = doublecircle]; +""", + + +initial_nodes = RDF.SPARQLQuery(""" +PREFIX machina: +SELECT ?n ?dur WHERE { + ?m machina:initialNode ?n . + ?n a machina:Node ; + machina:duration ?dur . +} +""").execute(model) + +for result in initial_nodes: + print '\t', result['n'].blank_identifier, "[ label = \"", + print float(result['dur'].literal_value['string']), + print "\"]; " + +print "\tnode [shape = circle];" + + +nodes = RDF.SPARQLQuery(""" +PREFIX machina: +SELECT ?n ?dur WHERE { + ?m machina:node ?n . + ?n a machina:Node ; + machina:duration ?dur . +} +""").execute(model) + +for result in nodes: + print '\t', result['n'].blank_identifier, "[ label = \"", + print float(result['dur'].literal_value['string']), + print "\"]; " + + +edges = RDF.SPARQLQuery(""" +PREFIX machina: +SELECT ?tail ?head ?prob WHERE { + ?e a machina:Edge ; + machina:tail ?tail ; + machina:head ?head ; + machina:probability ?prob . +} +""").execute(model); + +for result in edges: + print '\t', result['tail'].blank_identifier, ' -> ', + print result['head'].blank_identifier, ' ', + print "[ label = \"", result['prob'].literal_value['string'], "\" ];" + +print "\n}" -- cgit v1.2.1