Class: Planner::Operator

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cost = 1) ⇒ Operator

Returns a new instance of Operator.



150
151
152
153
154
155
156
157
# File 'lib/sfpagent/sfplanner.rb', line 150

def initialize(name, cost=1)
	@name = name
	@sym = @name.to_sym
	@cost = cost
	@preconditions = {}
	@postconditions = {}
	@variables = {}
end

Instance Attribute Details

#costObject

Returns the value of attribute cost.



127
128
129
# File 'lib/sfpagent/sfplanner.rb', line 127

def cost
  @cost
end

#nameObject (readonly)

Returns the value of attribute name.



126
127
128
# File 'lib/sfpagent/sfplanner.rb', line 126

def name
  @name
end

#postconditionsObject

Returns the value of attribute postconditions.



127
128
129
# File 'lib/sfpagent/sfplanner.rb', line 127

def postconditions
  @postconditions
end

#preconditionsObject

Returns the value of attribute preconditions.



127
128
129
# File 'lib/sfpagent/sfplanner.rb', line 127

def preconditions
  @preconditions
end

#symObject (readonly)

Returns the value of attribute sym.



126
127
128
# File 'lib/sfpagent/sfplanner.rb', line 126

def sym
  @sym
end

#variablesObject

Returns the value of attribute variables.



127
128
129
# File 'lib/sfpagent/sfplanner.rb', line 127

def variables
  @variables
end

Class Method Details

.read(i, lines, variables) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sfpagent/sfplanner.rb', line 129

def self.read(i, lines, variables)
	op = Operator.new(lines[i+1])
	i += 2
	last = nil
	i.upto(lines.length) do |j|
		i = j
		break if lines[j] == 'end_operator'
		parts = lines[j].split(' ')
		if parts.length > 1
			var = variables[parts[1].to_i]
			pre = (parts[2] == '-1' ? nil : var[parts[2].to_i])
			post = (parts[3].nil? ? nil : var[parts[3].to_i])
			op.param var, pre, post
		end
		last = lines[j]
	end
	op.cost = last.to_i
	fail "Cannot find end_operator" if lines[i] != 'end_operator'
	[i, op]
end

Instance Method Details

#applicable(state) ⇒ Object



172
173
174
175
# File 'lib/sfpagent/sfplanner.rb', line 172

def applicable(state)
	@preconditions.each { |var,pre| return false if state[var.sym] != pre }
	true
end

#apply(state) ⇒ Object



177
178
179
# File 'lib/sfpagent/sfplanner.rb', line 177

def apply(state)
	@postconditions.each { |var,post| state[var.sym] = post }
end

#param(variable, pre = nil, post = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/sfpagent/sfplanner.rb', line 159

def param(variable, pre=nil, post=nil)
	return if variable.nil? or (pre.nil? and post.nil?)
	if !pre.nil?
		fail "Invalid precondition #{variable.name}:#{pre}" if !variable.index(pre)
		@preconditions[variable.sym] = pre
	end
	if !post.nil?
		fail "Invalid postcondition #{variable.name}:#{post}" if !variable.index(post)
		@postconditions[variable.sym] = post
	end
	@variables[variable.sym] = variable
end

#to_sObject



205
206
207
# File 'lib/sfpagent/sfplanner.rb', line 205

def to_s
	@name + " pre:" + @preconditions.inspect + " post:" + @postconditions.inspect
end

#update_variables_joints_and_dependencies(variables) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sfpagent/sfplanner.rb', line 181

def update_variables_joints_and_dependencies(variables)
	@postconditions.each_key { |post|
		var_post = variables[post]
		@preconditions.each_key { |pre|
			next if post == pre
			var_pre = variables[pre]
			if !var_post.dependencies.has_key?(pre)
				var_post.dependencies[pre] = [self]
			else
				var_post.dependencies[pre] << self
			end
		}
		@postconditions.each_key { |post2|
			next if post == post2
			var_post2 = variables[post2]
			if !var_post.joints.has_key?(post2)
				var_post.joints[post2] = [self]
			else
				var_post.joints[post2] << self
			end
		}
	}
end