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.



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

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.



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

def cost
  @cost
end

#nameObject (readonly)

Returns the value of attribute name.



124
125
126
# File 'lib/sfpagent/sfplanner.rb', line 124

def name
  @name
end

#postconditionsObject

Returns the value of attribute postconditions.



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

def postconditions
  @postconditions
end

#preconditionsObject

Returns the value of attribute preconditions.



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

def preconditions
  @preconditions
end

#symObject (readonly)

Returns the value of attribute sym.



124
125
126
# File 'lib/sfpagent/sfplanner.rb', line 124

def sym
  @sym
end

#variablesObject

Returns the value of attribute variables.



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

def variables
  @variables
end

Class Method Details

.read(i, lines, variables) ⇒ Object



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

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



170
171
172
173
# File 'lib/sfpagent/sfplanner.rb', line 170

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

#apply(state) ⇒ Object



175
176
177
# File 'lib/sfpagent/sfplanner.rb', line 175

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

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



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

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



203
204
205
# File 'lib/sfpagent/sfplanner.rb', line 203

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

#update_variables_joints_and_dependencies(variables) ⇒ Object



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

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