Class: Foundries::Similarity::StructureTree

Inherits:
Object
  • Object
show all
Defined in:
lib/foundries/similarity/structure_tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, children: []) ⇒ StructureTree

Returns a new instance of StructureTree.



8
9
10
11
# File 'lib/foundries/similarity/structure_tree.rb', line 8

def initialize(name, children: [])
  @name = name.to_s
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/foundries/similarity/structure_tree.rb', line 6

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/foundries/similarity/structure_tree.rb', line 6

def name
  @name
end

Class Method Details

.root(children:) ⇒ Object



13
14
15
# File 'lib/foundries/similarity/structure_tree.rb', line 13

def self.root(children:)
  new("__root__", children: children)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



39
40
41
42
43
# File 'lib/foundries/similarity/structure_tree.rb', line 39

def ==(other)
  other.is_a?(self.class) &&
    name == other.name &&
    children == other.children
end

#collapse_into(parent_name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/foundries/similarity/structure_tree.rb', line 27

def collapse_into(parent_name)
  if name == parent_name && !children.empty?
    children
  else
    [self]
  end
end

#contains?(other) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/foundries/similarity/structure_tree.rb', line 51

def contains?(other)
  return true if self == other

  embeds?(other) || children.any? { |child| child.contains?(other) }
end

#descendant_countObject



35
36
37
# File 'lib/foundries/similarity/structure_tree.rb', line 35

def descendant_count
  children.sum { |c| 1 + c.descendant_count }
end

#embeds?(other) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/foundries/similarity/structure_tree.rb', line 57

def embeds?(other)
  return false unless name == other.name

  other.children.all? do |other_child|
    children.any? { |child| child.embeds?(other_child) }
  end
end

#hashObject



47
48
49
# File 'lib/foundries/similarity/structure_tree.rb', line 47

def hash
  [name, children].hash
end

#normalizeObject



17
18
19
20
21
22
23
24
25
# File 'lib/foundries/similarity/structure_tree.rb', line 17

def normalize
  normalized_children = children.map(&:normalize)
  collapsed = normalized_children.flat_map { |child| child.collapse_into(name) }
  deduped = collapsed
    .group_by(&:name)
    .map { |_name, group| group.max_by(&:descendant_count) }
    .sort_by(&:name)
  self.class.new(name, children: deduped)
end

#to_sObject



65
66
67
68
69
70
71
# File 'lib/foundries/similarity/structure_tree.rb', line 65

def to_s
  if children.empty?
    name
  else
    "#{name} > [#{children.join(", ")}]"
  end
end