class Planner

Public Class Methods

dot2image(dot, image_file) click to toggle source
# File lib/sfpagent/sfplanner.rb, line 62
def self.dot2image(dot, image_file)
        dot_file = "/tmp/#{Time.now.to_i}.dot"
        File.open(dot_file, 'w') { |f|
                f.write(dot)
                f.flush
        }
        !!system("dot -Tpng -o #{Shellwords.escape(image_file)} #{Shellwords.escape(dot_file)}")
ensure
        File.delete(dot_file) if File.exist?(dot_file)
end
new(p={}) click to toggle source
# File lib/sfpagent/sfplanner.rb, line 6
def initialize(p={})
        # TODO
        # - build from SAS string
        # - generate image dependencies and joints graph

        @vars = []
        @variables = {}
        @ops = []
        @operators = {}
        @init = @goal = nil

        lines = p[:sas].split("\n")
        i = 0
        while i < lines.length
                if lines[i] == 'begin_variable'
                        i, var = Variable.read(i, lines)
                        @vars << var
                        @variables[var.sym] = var
                elsif lines[i] == 'begin_operator'
                        i, op = Operator.read(i, lines, @vars)
                        @ops << op
                        @operators[op.sym] = op
                elsif lines[i] == 'begin_state'
                        i, @init = State.read(i, lines, @vars)
                elsif lines[i] == 'begin_goal'
                        i, @goal = State.read(i, lines, @vars)
                end
                i += 1
        end

        @ops.each { |op| op.update_variables_joints_and_dependencies(@variables) }

        puts "#{@vars.length} variables"
        puts "#{@ops.length} operators"
        puts "#{@init.length} initial state"
        puts "#{@goal.length} goal state"

        @vars.each { |v|
                puts v.to_s
                if v.dependencies.length > 0
                        print "\tdep|"
                        v.dependencies.each { |k,v| print "#{k}:#{v.length}|" }
                        puts ''
                end
                if v.joints.length > 0
                        print "\tjoint|"
                        v.joints.each { |k,v| print "#{k}:#{v.length}|" }
                        puts ''
                end
        }
        @ops.each { |op| puts op.inspect }
        puts @init.inspect
        puts @goal.inspect
end

Public Instance Methods

to_image(p={}) click to toggle source
# File lib/sfpagent/sfplanner.rb, line 61
        def to_image(p={})
                def self.dot2image(dot, image_file)
                        dot_file = "/tmp/#{Time.now.to_i}.dot"
                        File.open(dot_file, 'w') { |f|
                                f.write(dot)
                                f.flush
                        }
                        !!system("dot -Tpng -o #{Shellwords.escape(image_file)} #{Shellwords.escape(dot_file)}")
                ensure
                        File.delete(dot_file) if File.exist?(dot_file)
                end

                dot = "digraph {\n"
                @variables.each do |sym,var|
                        name = var.name.gsub(/[\s\.\$]/, '_')
                        dot += "#{name} [label=\"#{var.name}\", shape=rect];\n"
                end
                @variables.each do |sym,var|
                        name = var.name.gsub(/[\s\.\$]/, '_')
                        var.dependencies.each { |sym,operators|
                                var2 = @variables[sym]
                                name2 = var2.name.gsub(/[\s\.\$]/, '_')
                                dot += "#{name} -> #{name2} ;\n"
                        }
                end
                dot += "}"
puts dot

                dot2image(dot, p[:file])
        end