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.



1843
1844
1845
1846
1847
1848
1849
# File 'lib/sfp/sas_translator.rb', line 1843

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.



1840
1841
1842
# File 'lib/sfp/sas_translator.rb', line 1840

def cost
  @cost
end

#idObject

Returns the value of attribute id.



1840
1841
1842
# File 'lib/sfp/sas_translator.rb', line 1840

def id
  @id
end

#modifier_idObject

Returns the value of attribute modifier_id.



1840
1841
1842
# File 'lib/sfp/sas_translator.rb', line 1840

def modifier_id
  @modifier_id
end

#nameObject

Returns the value of attribute name.



1840
1841
1842
# File 'lib/sfp/sas_translator.rb', line 1840

def name
  @name
end

#paramsObject

Returns the value of attribute params.



1840
1841
1842
# File 'lib/sfp/sas_translator.rb', line 1840

def params
  @params
end

#refObject (readonly)

Returns the value of attribute ref.



1841
1842
1843
# File 'lib/sfp/sas_translator.rb', line 1841

def ref
  @ref
end

Instance Method Details

#cloneObject



1851
1852
1853
1854
1855
1856
# File 'lib/sfp/sas_translator.rb', line 1851

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)


1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
# File 'lib/sfp/sas_translator.rb', line 1931

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

#get_post_stateObject



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

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

#get_pre_stateObject



1915
1916
1917
1918
1919
# File 'lib/sfp/sas_translator.rb', line 1915

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

#merge(operator) ⇒ Object



1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
# File 'lib/sfp/sas_translator.rb', line 1887

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)


1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
# File 'lib/sfp/sas_translator.rb', line 1876

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)


1864
1865
1866
1867
1868
1869
1870
1871
1872
# File 'lib/sfp/sas_translator.rb', line 1864

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



1943
1944
1945
1946
# File 'lib/sfp/sas_translator.rb', line 1943

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



1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
# File 'lib/sfp/sas_translator.rb', line 1948

def to_sas(root, variables)
	prevail = Array.new
	prepost = Array.new
	self.each_value { |p|
		if p.post == nil
			prevail << p
		else
			prepost << p
		end
	}
	sas = "begin_operator\n#{@name}"
	@params.each { |k,v| sas += " #{k}=#{v}" if k != '$.this' } if @params != nil
	sas += "\n#{prevail.length}\n"
	prevail.each { |p|
		line = p.to_sas(root, variables)
		raise TranslationException if line[line.length-1] == ' '
		sas += "#{line}\n"
	}
	sas += "#{prepost.length}\n"
	prepost.each { |p|
		line = p.to_sas(root, variables, false)
		raise TranslationException if line[line.length-1] == ' '
		sas += "#{line}\n"
	}
	sas += "#{@cost}\nend_operator"
	return sas
end

#to_sfwObject



1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
# File 'lib/sfp/sas_translator.rb', line 1976

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_prepostsObject



1909
1910
1911
1912
1913
# File 'lib/sfp/sas_translator.rb', line 1909

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

#total_prevailsObject



1903
1904
1905
1906
1907
# File 'lib/sfp/sas_translator.rb', line 1903

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

#update_nameObject



1858
1859
1860
# File 'lib/sfp/sas_translator.rb', line 1858

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