Class: Planner

Inherits:
Object
  • Object
show all
Defined in:
lib/sfpagent/sfplanner.rb

Defined Under Namespace

Classes: Operator, State, Variable

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p = {}) ⇒ Planner

Returns a new instance of Planner.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

Class Method Details

.dot2image(dot, image_file) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# 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

Instance Method Details

#to_image(p = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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