#!/usr/bin/lua --require 'strict' local Class = require 'class' Edge = Class { init = function (self, s_node, e_node, label) self.s = s_node self.e = e_node self.label = label self.directed = false end, } Node = Class { init = function (self, label) self.label = label self.edges = {} end, } Graph = Class { init = function (self, name) self.name = name self.start_node = {} self.end_node = {} self.nodes = {} self.edges = {} end, writeDotFile = function (self, filename) print ("writing file: " .. filename .. ".png") local outfile = io.open (filename .. ".dot", "w+") outfile:write("digraph dungeon {\n") for i,e in ipairs (self.edges) do outfile:write (e.s.label .. " -> " .. e.e.label) local options = {} if e.directed == false then table.insert (options, "dir=none") end if e.label ~= "" then table.insert (options, "label=" .. e.label) end local have_options = #options > 0 if have_options then outfile:write (" [") end for i,v in ipairs(options) do if i > 1 then outfile:write (", ") end outfile:write (v) end if have_options then outfile:write(" ]") end outfile:write ("\n") end outfile:write("}\n") outfile:close() os.execute ("dot -Tpng " .. filename .. ".dot -o " .. filename .. ".png") os.execute ("eog " .. filename .. ".png") end, insertSubGraph = function (self, edge_index, subgraph) end } function create_simple() local graph = Graph("simple") local start_node = Node("SimpleStart") local goal_node = Node("SimpleGoal") local se_edge = Edge(start_node, goal_node, "se") se_edge.directed = true local es_edge = Edge(goal_node, start_node, "es") graph.start_node = start_node graph.goal_node = goal_node table.insert (graph.nodes, start_node) table.insert (graph.nodes, goal_node) table.insert (graph.edges, se_edge) table.insert (graph.edges, es_edge) return graph end function create_lock() local graph = Graph("lock") local start_node = Node("LockStart") local goal_node = Node("LockGoal") local lock_node = Node("Lock") local sl_edge = Edge(start_node, lock_node, "sl") local ls_edge = Edge(lock_node, start_node, "ls_key") ls_edge.directed = true local lg_edge = Edge(lock_node, goal_node, "lg") graph.start_node = start_node graph.goal_node = goal_node table.insert (graph.nodes, start_node) table.insert (graph.nodes, lock_node) table.insert (graph.nodes, goal_node) table.insert (graph.edges, sl_edge) table.insert (graph.edges, ls_edge) table.insert (graph.edges, lg_edge) return graph end local simple = create_simple() simple:writeDotFile("simple") local lock = create_lock() lock:writeDotFile("lock_graph")