Class: MiniKraken::Core::LogVarRef

Inherits:
Term show all
Defined in:
lib/mini_kraken/core/log_var_ref.rb

Overview

Representation of a reference to a MiniKraken logical variable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aName) ⇒ LogVarRef

Create a reference to a logical variable with given name

Parameters:

  • aName (String)

    The name of the variable



18
19
20
21
# File 'lib/mini_kraken/core/log_var_ref.rb', line 18

def initialize(aName)
  super()
  init_name(aName)
end

Instance Attribute Details

#i_nameString

Returns Unique internal name of the variable.

Returns:

  • (String)

    Unique internal name of the variable.



14
15
16
# File 'lib/mini_kraken/core/log_var_ref.rb', line 14

def i_name
  @i_name
end

#nameString (readonly)

Returns User-friendly name of the variable.

Returns:

  • (String)

    User-friendly name of the variable.



11
12
13
# File 'lib/mini_kraken/core/log_var_ref.rb', line 11

def name
  @name
end

Instance Method Details

#dependencies(ctx) ⇒ Set<String>

Return the list of variable (i_names) that this term depends on. For a variable reference, it will return the i_names of its variable

Parameters:

Returns:

  • (Set<String>)

    a set containing the i_name of the variable



80
81
82
83
84
85
# File 'lib/mini_kraken/core/log_var_ref.rb', line 80

def dependencies(ctx)
  @i_name ||= ctx.lookup(name).i_name
  s = Set.new
  s << i_name
  s
end

#dup_cond(substitutions) ⇒ Term

Make a copy of self with all the variable reference being replaced by the corresponding value in the Hash.

Parameters:

  • substitutions (Hash {String => Term})

Returns:



91
92
93
94
95
96
97
98
99
# File 'lib/mini_kraken/core/log_var_ref.rb', line 91

def dup_cond(substitutions)
  key = i_name || name
  if substitutions.include? key
    val = substitutions[key]
    val.kind_of?(Term) ? val.dup_cond(substitutions) : val
  else
    dup
  end
end

#floating?(aContext) ⇒ Boolean

Does the variable have at least one association AND each of these association refer to at least one unbound variable or a floating variable?

Parameters:

Returns:

  • (Boolean)

    true if log var is floating

Raises:

  • (StandardError)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mini_kraken/core/log_var_ref.rb', line 46

def floating?(aContext)
  vr = aContext.lookup(name)
  raise StandardError, "Unknown variable #{name}" unless vr

  assocs = aContext.associations_for(name)
  unless assocs.empty?
    assocs.none? { |as| as.pinned?(aContext) }
  else
    false
  end
end

#pinned?(aContext) ⇒ Boolean

Is the variable pinned? In other words, does the referenced variable have a definite value?

Parameters:

Returns:

  • (Boolean)

    true if log var is pinned

Raises:

  • (StandardError)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mini_kraken/core/log_var_ref.rb', line 62

def pinned?(aContext)
  return true if @pinned

  vr = aContext.lookup(name)
  raise StandardError, "Unknown variable #{name}" unless vr

  assocs = aContext.associations_for(name)
  unless assocs.empty?
    @pinned = assocs.all? { |as| as.pinned?(aContext) }
  else
    false
  end
end

#to_sString

Return a text representation of this logical variable reference.

Returns:

  • (String)


25
26
27
# File 'lib/mini_kraken/core/log_var_ref.rb', line 25

def to_s
  name
end

#unbound?(aContext) ⇒ Boolean

Is the related log variable unbound in the given context? A log var is unbound when there is no association for the variable.

Parameters:

Returns:

  • (Boolean)

    true if log var is unbound

Raises:

  • (StandardError)


33
34
35
36
37
38
39
# File 'lib/mini_kraken/core/log_var_ref.rb', line 33

def unbound?(aContext)
  vr = aContext.lookup(name)
  raise StandardError, "Unknown variable #{name}" unless vr

  bindings = aContext.associations_for(name)
  bindings.empty? || (bindings.size == 1 && bindings[0].kind_of?(Fusion))
end