|
| 1 | +(in-package #:hddl-plan-grapher) |
| 2 | + |
| 3 | +(defmethod graph-plan-tree ((plan-tree-file string) |
| 4 | + &key (attributes nil) |
| 5 | + (graph-object (make-instance 'hddl-plan-tree-graph))) |
| 6 | + (let ((pathname (merge-pathnames (parse-namestring plan-tree-file) |
| 7 | + (make-pathname :type "hddl")))) |
| 8 | + (graph-plan-tree pathname |
| 9 | + :attributes attributes |
| 10 | + :graph-object graph-object))) |
| 11 | + |
| 12 | +(defmethod graph-plan-tree ((plan-tree-file pathname) |
| 13 | + &key (attributes nil) |
| 14 | + (graph-object (make-instance 'hddl-plan-tree-graph))) |
| 15 | + (let ((plan-tree (hddl-io:read-hddl-plan-file plan-tree-file))) |
| 16 | + (unless (eq (first plan-tree) ':hddl-plan) |
| 17 | + (error 'type-error :datum plan-tree :expected-type 'hddl-plan)) |
| 18 | + (graph-plan-tree plan-tree |
| 19 | + :attributes attributes |
| 20 | + :graph-object graph-object))) |
| 21 | + |
| 22 | +(defmethod graph-plan-tree (plan-tree &key (attributes nil) |
| 23 | + (graph-object (make-instance 'hddl-plan-tree-graph))) |
| 24 | + "Takes a SHOP plan forest (PLAN-FOREST) as input, and returns a CL-DOT graph object." |
| 25 | + (let ((roots (getf (rest plan-tree) :roots)) |
| 26 | + (actions (getf (rest plan-tree) :actions)) |
| 27 | + (decompositions (getf (rest plan-tree) :decompositions))) |
| 28 | + (build-lookup-table graph-object actions decompositions) |
| 29 | + (cl-dot:generate-graph-from-roots graph-object roots attributes))) |
| 30 | + |
| 31 | +(defmethod build-lookup-table ((graph-object hddl-plan-tree-graph) actions decompositions) |
| 32 | + (iter (for (index . task) in actions) |
| 33 | + (setf (gethash index (node-lookup-table graph-object)) |
| 34 | + (make-instance 'action :task task))) |
| 35 | + (iter (for (index task method-name . children) in decompositions) |
| 36 | + (setf (gethash index (node-lookup-table graph-object)) |
| 37 | + (make-instance 'decomposition :task task |
| 38 | + :method-name method-name |
| 39 | + :children children)))) |
| 40 | + |
| 41 | +(defmethod lookup ((g hddl-plan-tree-graph) (index integer)) |
| 42 | + (or (gethash index (node-lookup-table g)) |
| 43 | + (error "No graph node with index ~d" index))) |
| 44 | + |
| 45 | +(defmethod cl-dot:graph-object-node ((g hddl-plan-tree-graph) (index integer)) |
| 46 | + (cl-dot:graph-object-node g (lookup g index))) |
| 47 | + |
| 48 | +(defmethod cl-dot:graph-object-node ((g hddl-plan-tree-graph) (obj action)) |
| 49 | + (declare (ignorable g)) |
| 50 | + (make-instance 'cl-dot:node |
| 51 | + :attributes `(:label ,(format nil "~A" (task obj)) |
| 52 | + :shape :box))) |
| 53 | + |
| 54 | +(defmethod cl-dot:graph-object-node ((g hddl-plan-tree-graph) (obj decomposition)) |
| 55 | + (declare (ignorable g)) |
| 56 | + (make-instance 'cl-dot:node |
| 57 | + :attributes `(:label ,(format nil "~A" (task obj)) |
| 58 | + :style :rounded |
| 59 | + :shape :box))) |
| 60 | + |
| 61 | +(defmethod cl-dot:graph-object-points-to ((g hddl-plan-tree-graph) (index integer)) |
| 62 | + (cl-dot:graph-object-points-to g (lookup g index))) |
| 63 | + |
| 64 | +(defmethod cl-dot:graph-object-points-to ((g hddl-plan-tree-graph)(obj action)) |
| 65 | + (declare (ignorable g obj)) |
| 66 | + nil) |
| 67 | + |
| 68 | + |
| 69 | +(defmethod cl-dot:graph-object-points-to ((g hddl-plan-tree-graph)(obj decomposition)) |
| 70 | + (declare (ignorable g)) |
| 71 | + (children obj)) |
0 commit comments