emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Process diagrams with dot and some glue using Org-mode


From: Rick Frankel
Subject: Re: [O] Process diagrams with dot and some glue using Org-mode
Date: Wed, 26 Jun 2013 14:25:04 -0400
User-agent: Roundcube Webmail/0.9.0

On 2013-06-26 13:03, Karl Voit wrote:
* Rick Frankel <address@hidden> wrote:

Two things:

        1. You don't need to write table parsing code, as passing in a
           table as an argument to a code block will convert it to an
           array.
                   t=[["a", 1], ["b", 2]]

You're right, I totally forgot about this neat feature.

However, the header information seems to get lost. This requires
hard-coded column content which is a minor drawback of this method.

Just use `:colnames no':

        #+BEGIN_SRC python :var t=ptable :results value :colnames no
          return t
        #+END_SRC

        t=[["head1", "head2"], ["a", 1], ["b", 2]]
        return t

Regardless, here's a hack which does what you want. Note to things:
                - it executes the dot code directly and uses the :file
                  header argument for output, because you need the
                  colnames of the graph table but not of the node table.
                - It requires you to specify the range on the node table

        #+HEADER: :var nodes=foobar-node-table[2:-1]
        #+HEADER: :var graph=foobar-graph-table
        #+BEGIN_SRC emacs-lisp :results file :file "./t.png"
          (org-babel-execute:dot
           (concat
                "  digraph {"
                (mapconcat
                 (lambda (x)
                   (format "%s [label=\"%s\" shape=%s fillcolor=%s]"
                                   (car x) (nth 1 x)
                                   (if (string= "" (nth 2 x)) "box" (nth 2 x))
                                   (if (string= "" (nth 3 x)) "none" (nth 3 x)))) nodes 
"\n")
                "\n"
                (let* ((to-nodes (car graph)) (len (length to-nodes)))
                  (mapconcat
                   (lambda (x)
                         (let ((name (car x)))
                           (mapconcat
                                'identity
                                (loop with result = '()
                                          for i from 1 to len
                                          do
                                          (when (> (length (nth i x)) 0)
                                                (add-to-list 'result
                                                                         (format "%s -> %s 
[label=\"%s\"]\n"
                                                                                
         name
                                                                                
         (nth i to-nodes)
                                                                                
         (substring (nth i x) 0 -1))))
                                          finally
                                          return result) "\n"))) (cdr graph) 
""))
                "}") params)
        #+END_SRC


And here's a simplier version which uses a graph table in the
following format:

        #+name: foobar-graph
        | from       | to         | label |
        |------------+------------+-------|
        | S_start    | S_fill     |       |
        | S_fill     | S_send     |       |
        | S_send     | S_complete |       |
        | S_complete | S_fill     | N     |
        | S_complete | S_do       | Y     |
        | S_do       | S_end      |       |

        #+HEADER: :var nodes=foobar-node-table graph=foobar-graph
        #+BEGIN_SRC emacs-lisp :file ./t2.png :colnames yes
          (org-babel-execute:dot
           (concat
                "digraph {\n"
                (mapconcat
                 (lambda (x)
                   (format "%s [label=\"%s\" shape=%s fillcolor=%s]"
                                   (car x) (nth 1 x)
                                   (if (string= "" (nth 2 x)) "box" (nth 2 x))
                                   (if (string= "" (nth 3 x)) "none" (nth 3 x)))) nodes 
"\n")
                "\n"
                (mapconcat
                 (lambda (x)
                   (format "%s -> %s [taillabel=\"%s\"]"
                                   (car x) (nth 1 x) (nth 2 x))) graph "\n")
                "}\n") params)
        #+END_SRC




reply via email to

[Prev in Thread] Current Thread [Next in Thread]