Class: Sfp::Operator

Inherits:
Hash
  • Object
show all
Defined in:
lib/sfp/sas_translator.rb

Overview

A class for Grounded Operator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, cost = 1, id = nil) ⇒ Operator

Returns a new instance of Operator.



1921
1922
1923
1924
1925
1926
1927
# File 'lib/sfp/sas_translator.rb', line 1921

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

Instance Attribute Details

#costObject

Returns the value of attribute cost.



1918
1919
1920
# File 'lib/sfp/sas_translator.rb', line 1918

def cost
  @cost
end

#idObject

Returns the value of attribute id.



1918
1919
1920
# File 'lib/sfp/sas_translator.rb', line 1918

def id
  @id
end

#modifier_idObject

Returns the value of attribute modifier_id.



1918
1919
1920
# File 'lib/sfp/sas_translator.rb', line 1918

def modifier_id
  @modifier_id
end

#nameObject

Returns the value of attribute name.



1918
1919
1920
# File 'lib/sfp/sas_translator.rb', line 1918

def name
  @name
end

#paramsObject

Returns the value of attribute params.



1918
1919
1920
# File 'lib/sfp/sas_translator.rb', line 1918

def params
  @params
end

#refObject (readonly)

Returns the value of attribute ref.



1919
1920
1921
# File 'lib/sfp/sas_translator.rb', line 1919

def ref
  @ref
end

Instance Method Details

#cloneObject



1929
1930
1931
1932
1933
1934
# File 'lib/sfp/sas_translator.rb', line 1929

def clone
	op = Operator.new(@ref, @cost)
	op.params = @params
	self.each { |key,param| op[key] = param.clone }
	return op
end

#conflict?(operator) ⇒ Boolean

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

Returns:

  • (Boolean)


2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
# File 'lib/sfp/sas_translator.rb', line 2009

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) ⇒ Object



2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
# File 'lib/sfp/sas_translator.rb', line 2054

def dump(stream, root, variables)
	prevails = self.values.select { |p| p.post.nil? }
	preposts = self.values.select { |p| not p.post.nil? }

	stream.write "begin_operator\n#{@name}"
	@params.each do |key,val|
		stream.write " #{key}=#{val}" if key != '$.this'
	end if @params.is_a?(Hash)

	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_stateObject



1999
2000
2001
2002
2003
# File 'lib/sfp/sas_translator.rb', line 1999

def get_post_state
	state = {}
	self.each_value { |p| state[p.var.name] = p.post if p.post != nil }
	state
end

#get_pre_stateObject



1993
1994
1995
1996
1997
# File 'lib/sfp/sas_translator.rb', line 1993

def get_pre_state
	state = {}
	self.each_value { |p| state[p.var.name] = p.pre if p.pre != nil }
	state
end

#merge(operator) ⇒ Object



1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
# File 'lib/sfp/sas_translator.rb', line 1965

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) ⇒ Boolean

return true if this operator requires an effect of given operator otherwise return false

Returns:

  • (Boolean)


1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
# File 'lib/sfp/sas_translator.rb', line 1954

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) ⇒ Boolean

return true if this operator supports given operator’s precondition otherwise return false

Returns:

  • (Boolean)


1942
1943
1944
1945
1946
1947
1948
1949
1950
# File 'lib/sfp/sas_translator.rb', line 1942

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_sObject



2021
2022
2023
2024
# File 'lib/sfp/sas_translator.rb', line 2021

def to_s
	#return @name + ': ' + self.length.to_s
	return @name + ": " + (self.map { |k,v| v.to_s }).join("|")
end

#to_sas(root, variables) ⇒ Object



2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
# File 'lib/sfp/sas_translator.rb', line 2026

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_sfwObject



2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
# File 'lib/sfp/sas_translator.rb', line 2072

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
	end
	return sfw
end

#total_prepostsObject



1987
1988
1989
1990
1991
# File 'lib/sfp/sas_translator.rb', line 1987

def total_preposts
	count = 0
	self.each_value { |p| count += 1 if not p.post.nil? }
	count
end

#total_prevailsObject



1981
1982
1983
1984
1985
# File 'lib/sfp/sas_translator.rb', line 1981

def total_prevails
	count = 0
	self.each_value { |p| count += 1 if p.post.nil? }
	count
end

#update_nameObject



1936
1937
1938
# File 'lib/sfp/sas_translator.rb', line 1936

def update_name
	@name = 'op_' + @id.to_s + @ref
end