class Planner::Operator

Attributes

cost[RW]
name[R]
postconditions[RW]
preconditions[RW]
sym[R]
variables[RW]

Public Class Methods

new(name, cost=1) click to toggle source
# File lib/sfpagent/sfplanner.rb, line 150
def initialize(name, cost=1)
        @name = name
        @sym = @name.to_sym
        @cost = cost
        @preconditions = {}
        @postconditions = {}
        @variables = {}
end
read(i, lines, variables) click to toggle source
# 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

Public Instance Methods

applicable(state) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_s() click to toggle source
# File lib/sfpagent/sfplanner.rb, line 205
def to_s
        @name + " pre:" + @preconditions.inspect + " post:" + @postconditions.inspect
end
update_variables_joints_and_dependencies(variables) click to toggle source
# 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