Class: Sfp::Parameter

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

Overview

A class for operator/axiom parameter (prevail or effect condition)  :pre = nil - it can be anything (translated to -1)  :post = nil - variable’s value doesn’t change

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(var, pre, post = nil) ⇒ Parameter

Returns a new instance of Parameter.



2156
2157
2158
2159
2160
# File 'lib/sfp/sas_translator.rb', line 2156

def initialize(var, pre, post=nil)
  @var = var
  @pre = pre
  @post = post
end

Instance Attribute Details

#postObject

Returns the value of attribute post.



2154
2155
2156
# File 'lib/sfp/sas_translator.rb', line 2154

def post
  @post
end

#preObject

Returns the value of attribute pre.



2154
2155
2156
# File 'lib/sfp/sas_translator.rb', line 2154

def pre
  @pre
end

#varObject

Returns the value of attribute var.



2154
2155
2156
# File 'lib/sfp/sas_translator.rb', line 2154

def var
  @var
end

Instance Method Details

#cloneObject



2170
2171
2172
# File 'lib/sfp/sas_translator.rb', line 2170

def clone
  return Parameter.new(@var, @pre, @post)
end

#dump(stream, root, variables, is_prevail = true) ⇒ Object



2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
# File 'lib/sfp/sas_translator.rb', line 2197

def dump(stream, root, variables, is_prevail=true)
  ### resolve reference
  pre = ((@pre.is_a?(String) and @pre.isref) ? variables[@pre.to_sym].init : @pre)
  ### calculate index
  if pre.is_a?(Hash) and pre.isnull
    pre = 0
  elsif pre.nil?
    pre = -1
  else
    pre = @var.index(pre)
  end

  line = nil
  if is_prevail
    line = "#{@var.id} #{pre}\n"
  else
    ### resolve reference
    post = ((@post.is_a?(String) and @post.isref) ? variables[@post.to_sym].init : @post)
    ### calculate index
    if post.is_a?(Hash) and post.isnull
      line = "0 #{@var.id} #{pre} 0\n"
    else
      line = "0 #{@var.id} #{pre} #{@var.index(post)}\n"
    end
  end

  raise TranslationException if line[-2] == ' '
  stream.write line
end

#effect?Boolean

Returns:

  • (Boolean)


2166
2167
2168
# File 'lib/sfp/sas_translator.rb', line 2166

def effect?
  return (@post != nil)
end

#prevail?Boolean

Returns:

  • (Boolean)


2162
2163
2164
# File 'lib/sfp/sas_translator.rb', line 2162

def prevail?
  return (@post == nil)
end

#to_sObject



2227
2228
2229
2230
2231
# File 'lib/sfp/sas_translator.rb', line 2227

def to_s
  return @var.name + ',' +
    (@pre == nil ? '-' : (@pre.is_a?(Hash) ? @pre.tostring : @pre.to_s)) + ',' +
    (@post == nil ? '-' : (@post.is_a?(Hash) ? @post.tostring : @post.to_s))
end

#to_sas(root, variables, is_prevail = true) ⇒ Object



2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
# File 'lib/sfp/sas_translator.rb', line 2174

def to_sas(root, variables, is_prevail=true)
  ### resolve reference
  pre = ((@pre.is_a?(String) and @pre.isref) ? variables[@pre.to_sym].init : @pre)
  ### calculate index
  if pre.is_a?(Hash) and pre.isnull
    pre = 0
  elsif pre.nil?
    pre = -1
  else
    pre = @var.index(pre)
  end
  return "#{@var.id} #{pre}" if is_prevail

  ### resolve reference
  post = ((@post.is_a?(String) and @post.isref) ? variables[@post.to_sym].init : @post)
  ### calculate index
  if post.is_a?(Hash) and post.isnull
    "0 #{@var.id} #{pre} 0"
  else
    "0 #{@var.id} #{pre} #{@var.index(post)}"
  end
end

#to_sfwObject



2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
# File 'lib/sfp/sas_translator.rb', line 2233

def to_sfw
  pre = @pre
  pre = @pre.ref if @pre.is_a?(Hash) and @pre.isobject
  pre = Sfp::Null.new if @pre.is_a?(Hash) and @pre.isnull

  post = @post
  post = @post.ref if @post.is_a?(Hash) and @post.isobject
  post = Sfp::Null.new if @post.is_a?(Hash) and @post.isnull

  return {
      'name' => @var.name,
      'pre' => pre,
      'post' => post
    }
end