Class: Wongi::Engine::NccPartner

Inherits:
BetaNode
  • Object
show all
Defined in:
lib/wongi-engine/beta/ncc_partner.rb

Instance Attribute Summary collapse

Attributes inherited from BetaNode

#children, #parent, #rete

Instance Method Summary collapse

Methods inherited from BetaNode

#assignment_node, #beta_deactivate_children, #depth, #empty?, #initialize, #overlay, #refresh, #root?, #size, #tokens

Methods included from CoreExt

included

Constructor Details

This class inherits a constructor from Wongi::Engine::BetaNode

Instance Attribute Details

#divergentObject

Returns the value of attribute divergent.



4
5
6
# File 'lib/wongi-engine/beta/ncc_partner.rb', line 4

def divergent
  @divergent
end

#nccObject

Returns the value of attribute ncc.



4
5
6
# File 'lib/wongi-engine/beta/ncc_partner.rb', line 4

def ncc
  @ncc
end

Instance Method Details

#beta_activate(token) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wongi-engine/beta/ncc_partner.rb', line 6

def beta_activate(token)
  # p beta_activate: {class: self.class, object_id:, token:}
  return if tokens.find { |t| t.duplicate? token }

  overlay.add_token(token)

  owner = owner_for(token)
  return unless owner

  overlay.add_ncc_token(owner, token)
  owner.node.ncc_deactivate owner
end

#beta_deactivate(token) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/wongi-engine/beta/ncc_partner.rb', line 19

def beta_deactivate(token)
  # p beta_deactivate: {class: self.class, object_id:, token:}

  # fetch the owner before deleting the token
  owner = overlay.ncc_owner(token)

  overlay.remove_token(token)
  return unless owner

  ncc.ncc_activate(owner) if overlay.ncc_tokens_for(owner).empty?
end

#owner_for(token) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/wongi-engine/beta/ncc_partner.rb', line 31

def owner_for(token)
  # find a token in the NCC node that has the same lineage as this token:
  # - the NCC token will be a direct descendant of the divergent, therefore
  # - one of this token's ancestors will be a duplicate of that token
  # TODO: this should be more resilient, but child token creation does not allow for much else at the moment
  ncc.tokens.find { |t| token.ancestors.any? { |ancestor| ancestor.duplicate?(t) } }
end