Class: Wongi::Engine::Token
- Inherits:
-
Object
- Object
- Wongi::Engine::Token
- Defined in:
- lib/wongi-engine/token.rb
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#parents ⇒ Object
readonly
Returns the value of attribute parents.
-
#wme ⇒ Object
readonly
Returns the value of attribute wme.
Instance Method Summary collapse
- #[](var) ⇒ Object
- #ancestors ⇒ Object
- #assignment(x) ⇒ Object
- #assignments ⇒ Object
- #child_of?(token) ⇒ Boolean
-
#duplicate?(other) ⇒ Boolean
TODO: ignore assignments?.
- #has_own_var?(x) ⇒ Boolean
- #has_var?(x) ⇒ Boolean
-
#initialize(node, parents, wme, assignments = {}) ⇒ Token
constructor
A new instance of Token.
- #inspect ⇒ Object
- #own_assignments ⇒ Object
- #set(variable, value) ⇒ Object
- #subst(variable, value) ⇒ Object
- #to_s ⇒ Object
- #values_at(*vars) ⇒ Object
Constructor Details
#initialize(node, parents, wme, assignments = {}) ⇒ Token
Returns a new instance of Token.
7 8 9 10 11 12 |
# File 'lib/wongi-engine/token.rb', line 7 def initialize(node, parents, wme, assignments = {}) @node = node @parents = Set.new(Array(parents)) @wme = wme @assignments = assignments end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
5 6 7 |
# File 'lib/wongi-engine/token.rb', line 5 def node @node end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
5 6 7 |
# File 'lib/wongi-engine/token.rb', line 5 def parents @parents end |
#wme ⇒ Object (readonly)
Returns the value of attribute wme.
5 6 7 |
# File 'lib/wongi-engine/token.rb', line 5 def wme @wme end |
Instance Method Details
#[](var) ⇒ Object
40 41 42 43 |
# File 'lib/wongi-engine/token.rb', line 40 def [](var) a = assignment(var) a.respond_to?(:call) ? a.call(self) : a end |
#ancestors ⇒ Object
14 15 16 |
# File 'lib/wongi-engine/token.rb', line 14 def ancestors parents.flat_map(&:ancestors).uniq + parents.to_a end |
#assignment(x) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/wongi-engine/token.rb', line 49 def assignment(x) return @assignments[x] if has_own_var?(x) parents.each do |parent| a = parent.assignment(x) return a if a end nil end |
#assignments ⇒ Object
34 35 36 37 38 |
# File 'lib/wongi-engine/token.rb', line 34 def assignments parents.each_with_object({}) do |parent, acc| acc.merge!(parent.assignments) end.merge(own_assignments) end |
#child_of?(token) ⇒ Boolean
18 19 20 |
# File 'lib/wongi-engine/token.rb', line 18 def child_of?(token) parents.include?(token) end |
#duplicate?(other) ⇒ Boolean
TODO: ignore assignments?
69 70 71 72 73 74 |
# File 'lib/wongi-engine/token.rb', line 69 def duplicate?(other) instance_of?(other.class) && parents == other.parents && wme == other.wme && own_assignments == other.own_assignments end |
#has_own_var?(x) ⇒ Boolean
64 65 66 |
# File 'lib/wongi-engine/token.rb', line 64 def has_own_var?(x) @assignments.key?(x) end |
#has_var?(x) ⇒ Boolean
60 61 62 |
# File 'lib/wongi-engine/token.rb', line 60 def has_var?(x) has_own_var?(x) || parents.any? { _1.has_var?(x) } end |
#inspect ⇒ Object
83 84 85 |
# File 'lib/wongi-engine/token.rb', line 83 def inspect to_s end |
#own_assignments ⇒ Object
30 31 32 |
# File 'lib/wongi-engine/token.rb', line 30 def own_assignments @assignments end |
#set(variable, value) ⇒ Object
26 27 28 |
# File 'lib/wongi-engine/token.rb', line 26 def set(variable, value) @assignments[variable] = value end |
#subst(variable, value) ⇒ Object
22 23 24 |
# File 'lib/wongi-engine/token.rb', line 22 def subst(variable, value) @assignments[variable] = value if @assignments.key? variable end |
#to_s ⇒ Object
76 77 78 79 80 81 |
# File 'lib/wongi-engine/token.rb', line 76 def to_s str = "TOKEN [ #{object_id} ancestors=#{ancestors.map(&:object_id).map(&:to_s).join('.')} " assignments.each_pair { |key, value| str << "#{key}=#{value.is_a?(TokenAssignment) ? "#{value.call} (#{value})" : value} " } str << "]" str end |
#values_at(*vars) ⇒ Object
45 46 47 |
# File 'lib/wongi-engine/token.rb', line 45 def values_at(*vars) vars.map { self[_1] } end |