Class: ChaosDetector::GraphTheory::Edge

Inherits:
Object
  • Object
show all
Defined in:
lib/chaos_detector/graph_theory/edge.rb

Direct Known Subclasses

ChaosGraphs::ChaosEdge

Constant Summary collapse

EDGE_TYPES =
{
  default: 0,
  superclass: 1,
  association: 2,
  class_association: 3
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_node, dep_node, edge_type: :default, reduction: nil) ⇒ Edge

Returns a new instance of Edge.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
# File 'lib/chaos_detector/graph_theory/edge.rb', line 17

def initialize(src_node, dep_node, edge_type: :default, reduction: nil)
  raise ArgumentError, 'src_node is required ' unless src_node
  raise ArgumentError, 'dep_node is required ' unless dep_node

  @src_node = src_node
  @dep_node = dep_node
  @reduction = reduction
  @edge_type = edge_type
  @graph_props = {}
end

Instance Attribute Details

#dep_nodeObject

Returns the value of attribute dep_node.



7
8
9
# File 'lib/chaos_detector/graph_theory/edge.rb', line 7

def dep_node
  @dep_node
end

#edge_typeObject

Returns the value of attribute edge_type.



4
5
6
# File 'lib/chaos_detector/graph_theory/edge.rb', line 4

def edge_type
  @edge_type
end

#graph_propsObject

Default behavior is accessor for @graph_props



45
46
47
# File 'lib/chaos_detector/graph_theory/edge.rb', line 45

def graph_props
  @graph_props
end

#reductionObject

Returns the value of attribute reduction.



8
9
10
# File 'lib/chaos_detector/graph_theory/edge.rb', line 8

def reduction
  @reduction
end

#src_nodeObject

Returns the value of attribute src_node.



6
7
8
# File 'lib/chaos_detector/graph_theory/edge.rb', line 6

def src_node
  @src_node
end

Instance Method Details

#==(other) ⇒ Object



51
52
53
54
# File 'lib/chaos_detector/graph_theory/edge.rb', line 51

def ==(other)
  # puts "Checking src and dep"
  src_node == other.src_node && dep_node == other.dep_node
end

#edge_rankObject



28
29
30
# File 'lib/chaos_detector/graph_theory/edge.rb', line 28

def edge_rank
  EDGE_TYPES.fetch(@edge_type, 0)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/chaos_detector/graph_theory/edge.rb', line 40

def eql?(other)
  self == other
end

#hashObject



36
37
38
# File 'lib/chaos_detector/graph_theory/edge.rb', line 36

def hash
  [@src_node, @dep_node].hash
end

#merge!(other) ⇒ Object

Mutate this Edge; combining attributes from other:

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chaos_detector/graph_theory/edge.rb', line 63

def merge!(other)
  raise ArgumentError, ('Argument other should be Edge object (was %s)' % other.class) unless other.is_a?(Edge)

  if EDGE_TYPES.dig(other.edge_type) > EDGE_TYPES.dig(edge_type)
    @edge_type = other.edge_type
  end

  # puts("EDGE REDUCTION: #{@reduction.class} -- #{other.class}  // #{other.reduction.class}")
  @reduction = ChaosDetector::GraphTheory::Reduction.combine(@reduction, other.reduction)
  self
end

#to_sObject



56
57
58
59
60
# File 'lib/chaos_detector/graph_theory/edge.rb', line 56

def to_s
  s = format('[%s] -> [%s]', src_node.title, dep_node.title)
  s << "(#{reduction.reduction_sum})" if reduction&.reduction_sum.to_i > 1
  s
end

#weightObject



32
33
34
# File 'lib/chaos_detector/graph_theory/edge.rb', line 32

def weight
  @reduction&.reduction_sum || 1
end