Search

More poor man's Pipeline - adding Graphviz

I expanded the pipe function by allowing simple operators as steps. The first to be added was "dot" to transform an input dot format file through Graphviz to SVG.

To demonstrate this I wrote a small XQuery script which generates an XHTML with SVG generated by the pipes embedded in-line. Both views of the emp-dept database used in the SQL teaching are generated by a small pipeline with two steps each:

  • The first is an XQuery script to access the database and generate a dot format file (as XML) for input into ...
  • The second, denoted by "dot" in the pipe, passes the dot structure to Graphviz which returns SVG

Each pipe can be run on its own for testing, and the last parameter allows a shorter pipe to be run for testing.

A scaling transform is used to reduce the size of the image to fit the page



declare option exist:serialize "method=xhtml media-type=application/xhtml+xml omit-xml-declaration=no indent=yes 
        doctype-public=-//W3C//DTD XHTML 1.0 Strict//EN
        doctype-system=http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
        
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:svg="http://www.w3.org/2000/svg"
      xml:lang="en">
   <head>
       <title>Views of a Company</title>
    </head>
    <body>
        <h1>Views of a Company</h1>
         <h2>Reporting hierarchy</h2>
         <svg:svg>
            <svg:g transform="scale(0.4)">
               {doc("http://www.cems.uwe.ac.uk/xmlwiki/graph2svg/pipe.xq?pipe=empdept/hierarchydotx2.xq;dot")}
            </svg:g>
        </svg:svg>
        <h2>Departmental Structure</h2>
         <svg:svg>
             <svg:g transform="scale(0.4)">
               {doc("http://www.cems.uwe.ac.uk/xmlwiki/graph2svg/pipe.xq?pipe=empdept/company2dotx.xq;dot")}
             </svg:g>
         </svg:svg>
     </body>
</html>


Show the Page

The script to generate the dot file is quite simple. For example, to generate the reporting hierarchy:



<graph>
  digraph {{
  rankdir=BT;
 { for $emp in //Emp
   let $mgr := //Emp[EmpNo = $emp/MgrNo]
   where exists($mgr)
   return
     <link>{$emp/Ename}  -> {$mgr/Ename} ; </link>
 }
  }}
</graph>


The dot step translates in an HTTP call:



 doc( concat("http://www.cems.uwe.ac.uk/~cjwallac/apps/services/dot2media.php?output=svg&amp;dot=",encode-for-uri(normalize-space($doc))))


Performance is hardly sparkling because Graphviz is being run on a different server and interfaced with PHP. I simply must find time to integrate Graphviz with eXist as a module. 

Script development is hampered by caching so I need to add some cache-busting to the pipe. However I think this is a useful approach for simple sequences of transformations.