class Sfp::Operator
A class for Grounded Operator
Attributes
cost[RW]
id[RW]
modifier_id[RW]
name[RW]
params[RW]
ref[R]
Public Class Methods
new(ref, cost=1, id=nil)
click to toggle source
# File lib/sfp/sas_translator.rb, line 1946 def initialize(ref, cost=1, id=nil) @id = (id == nil ? Sfp::SasTranslator.next_operator_id : id) @cost = cost @ref = ref @modifier_id = nil self.update_name end
Public Instance Methods
clone()
click to toggle source
# File lib/sfp/sas_translator.rb, line 1954 def clone op = Operator.new(@ref, @cost) op.params = @params self.each { |key,param| op[key] = param.clone } return op end
conflict?(operator)
click to toggle source
two operators can be parallel if
-
their preconditions are non consistent
-
their effects are not consistent
-
one’s during condition is not consistent with another
# File lib/sfp/sas_translator.rb, line 2034 def conflict?(operator) self.each_value do |param1| next if not operator.has_key?(param1.var.name) param2 = operator[param1.var.name] return true if param1.pre != nil and param2.pre != nil and param1.pre != param2.pre return true if param1.post != nil and param2.post != nil and param1.post != param2.post return true if param1.pre != nil and param2.post != nil and param1.pre != param2.post return true if param1.post != nil and param2.pre != nil and param1.post != param2.pre end return false end
dump(stream, root, variables)
click to toggle source
# File lib/sfp/sas_translator.rb, line 2079 def dump(stream, root, variables) prevails = self.values.select { |p| p.post.nil? } preposts = self.values.select { |p| not p.post.nil? } =begin pre_lines = [] prevails.each do |p| line = p.to_sas(root, variables) return if line[-1] == ' ' pre_lines << line end prepost_lines = [] preposts.each do |p| line = p.to_sas(root, variables, false) return if line [-1] == ' ' prepost_lines << line end =end stream.write "begin_operator\n#{@name}" @params.each do |key,val| stream.write " #{key}=#{val}" if key != '$.this' end if @params.is_a?(Hash) =begin stream.write "\n#{pre_lines.length}" stream.write "\n#{pre_lines.join("\n")}" if pre_lines.length > 0 stream.write "\n#{prepost_lines.length}\n" stream.write "#{prepost_lines.join("\n")}\n" if prepost_lines.length > 0 =end stream.write "\n#{prevails.length}\n" prevails.each { |p| p.dump(stream, root, variables) } stream.write "#{preposts.length}\n" preposts.each { |p| p.dump(stream, root, variables, false) } stream.write "#{@cost}\nend_operator\n" end
get_post_state()
click to toggle source
# File lib/sfp/sas_translator.rb, line 2024 def get_post_state state = {} self.each_value { |p| state[p.var.name] = p.post if p.post != nil } state end
get_pre_state()
click to toggle source
# File lib/sfp/sas_translator.rb, line 2018 def get_pre_state state = {} self.each_value { |p| state[p.var.name] = p.pre if p.pre != nil } state end
merge(operator)
click to toggle source
# File lib/sfp/sas_translator.rb, line 1990 def merge(operator) cost = (@cost > operator.cost ? @cost : operator.cost) names = @name.split('#') name = (names.length > 1 ? names[1] : names[0]) op = Operator.new('#' + name + '|' + operator.name, cost) self.each_value { |p| op[p.var.name] = p.clone } operator.each_value do |p| if not op.has_key?(p.var.name) op[p.var.name] = p.clone elsif p.post != nil op[p.var.name] = p.clone end end return op end
requires?(operator)
click to toggle source
return true if this operator requires an effect of given operator otherwise return false
# File lib/sfp/sas_translator.rb, line 1979 def requires?(operator) self.each_value do |p1| next if p1.pre == nil # can be any value p2 = operator[p1.var.name] if p2 != nil and p2.post != nil and p1.pre == p2.post and p2.pre == nil return true end end return false end
supports?(operator)
click to toggle source
return true if this operator supports given operator’s precondition otherwise return false
# File lib/sfp/sas_translator.rb, line 1967 def supports?(operator) operator.each_value do |p2| # precondition is any value or this operator does not effecting variable 'p2' next if p2.pre == nil or not self.has_key?(p2.var.name) or self[p2.var.name].post == nil return true if self[p2.var.name].post == p2.pre end false end
to_s()
click to toggle source
# File lib/sfp/sas_translator.rb, line 2046 def to_s #return @name + ': ' + self.length.to_s return @name + ": " + (self.map { |k,v| v.to_s }).join("|") end
to_sas(root, variables)
click to toggle source
# File lib/sfp/sas_translator.rb, line 2051 def to_sas(root, variables) prevails = self.values.select { |p| p.post.nil? } preposts = self.values.select { |p| not p.post.nil? } sas = "begin_operator\n#{@name}" @params.each do |key,val| sas << " #{key}=#{val}" if key != '$.this' end if @params.is_a?(Hash) sas << "\n#{prevails.length}\n" prevails.each do |p| line = p.to_sas(root, variables) #raise TranslationException if line[-1] == ' ' return nil if line[-1] == ' ' sas << "#{line}\n" end sas << "#{preposts.length}\n" preposts.each do |p| line = p.to_sas(root, variables, false) #raise TranslationException if line[-1] == ' ' return nil if line[-1] == ' ' sas << "#{line}\n" end sas << "#{@cost}\nend_operator\n" end
to_sfw()
click to toggle source
# File lib/sfp/sas_translator.rb, line 2117 def to_sfw if not (@name =~ /.*\$.*/) id , name = @name.split('-', 2) else id, name = @name.split('$', 2) end sfw = { 'name' => '$' + name, 'parameters' => {}, 'condition' => {}, 'effect' => {} } @params.each { |k,v| sfw['parameters'][k.to_s] = v if k != '$.this' } if @params != nil self.each_value do |param| next if param.var.name == Sfp::SasTranslator::GlobalVariable p = param.to_sfw if not p['pre'].nil? sfw['condition'][p['name']] = (p['pre'].is_a?(Sfp::Null) ? nil : p['pre']) end if not p['post'].nil? sfw['effect'][p['name']] = (p['post'].is_a?(Sfp::Null) ? nil : p['post']) end #sfw['condition'][ p['name'] ] = p['pre'] if p['pre'] != nil #sfw['effect'][ p['name'] ] = p['post'] if p['post'] != nil end return sfw end
total_preposts()
click to toggle source
# File lib/sfp/sas_translator.rb, line 2012 def total_preposts count = 0 self.each_value { |p| count += 1 if not p.post.nil? } count end
total_prevails()
click to toggle source
# File lib/sfp/sas_translator.rb, line 2006 def total_prevails count = 0 self.each_value { |p| count += 1 if p.post.nil? } count end
update_name()
click to toggle source
# File lib/sfp/sas_translator.rb, line 1961 def update_name @name = 'op_' + @id.to_s + @ref end