Class: Sfp::Variable

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

Overview

SAS Variable is a finite-domain variable It has a finite set of possible values

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, layer = -1,, init = nil, goal = nil, is_final = false) ⇒ Variable

Returns a new instance of Variable.



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

def initialize(name, type, layer=-1, init=nil, goal=nil, is_final=false)
	@name = name
	@type = type
	@layer = layer
	@init = init
	@goal = goal
	@is_final = is_final
	@is_primitive = (type == '$.String' or type == '$.Integer' or type == '$.Boolean')
	@mutable = true
end

Instance Attribute Details

#goalObject

Returns the value of attribute goal.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def goal
  @goal
end

#idObject

Returns the value of attribute id.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def id
  @id
end

#initObject

Returns the value of attribute init.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def init
  @init
end

#is_finalObject

Returns the value of attribute is_final.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def is_final
  @is_final
end

#is_primitiveObject (readonly)

Returns the value of attribute is_primitive.



1886
1887
1888
# File 'lib/sfp/sas_translator.rb', line 1886

def is_primitive
  @is_primitive
end

#issetObject

Returns the value of attribute isset.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def isset
  @isset
end

#layerObject

Returns the value of attribute layer.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def layer
  @layer
end

#mutableObject

Returns the value of attribute mutable.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def mutable
  @mutable
end

#nameObject

Returns the value of attribute name.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def name
  @name
end

#typeObject

Returns the value of attribute type.



1885
1886
1887
# File 'lib/sfp/sas_translator.rb', line 1885

def type
  @type
end

Instance Method Details

#dump(stream, root) ⇒ Object



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

def dump(stream, root)
	stream.write "begin_variable\nvar_#{@id}#{@name}\n#{@layer}\n#{self.length}\n"
	self.each { |v|
		v = root.at?(v) if v.is_a?(String) and v.isref
		v = '"' + v + '"' if v.is_a?(String)
		stream.write (v.is_a?(Hash) ? (v.isnull ? "null\n" : "#{v.ref}\n") : "#{v}\n")
	}
	stream.write "end_variable\n"
end

#not(x) ⇒ Object



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

def not(x)
	self.select { |y| y != x }
end

#to_sObject



1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
# File 'lib/sfp/sas_translator.rb', line 1899

def to_s
	s = @name.to_s + '|' + @type.to_s
	s << '|'
	s << (@init == nil ? '-' : (@init.is_a?(Hash) ? @init.tostring : @init.to_s))
	s << '|'
	s << (@goal == nil ? '-' : (@goal.is_a?(Hash) ? @goal.tostring : @goal.to_s))
	s << '|'
	s << (@is_final ? 'final' : 'notfinal') + "\n"
	s << "\t["
	self.each { |v| s << (v.is_a?(Hash) ? v.tostring : v.to_s) + ',' }
	s = (self.length > 0 ? s.chop : s)
	s << "]"
end

#to_sas(root) ⇒ Object

return variable representation in SAS+ format



1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
# File 'lib/sfp/sas_translator.rb', line 1914

def to_sas(root)
	sas = "begin_variable\nvar_#{@id}#{@name}\n#{@layer}\n#{self.length}\n"
	self.each { |v|
		if v.is_a?(String) and v.isref
			v = root.at?(v)
		end
		v = '"' + v + '"' if v.is_a?(String)
		sas << (v.is_a?(Hash) ? (v.isnull ? "null\n" : "#{v.ref}\n") : "#{v}\n")
	}
	sas << "end_variable\n"
end