Class: Export::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/export/graph.rb

Constant Summary collapse

NODE_COLORS =
{
  'Person' => '#009688',
  'CollectionObject' => '#2196F3',
  'TaxonName' => '#E91E63',
  'CollectingEvent' => '#7E57C2',
  'TaxonDetermination' => '#FF9800',
  'Identifier' => '#EF6C00',
  'Otu' =>'#4CAF50',
  'User' => '#F44336',
  'ControlledVocabularyTerm' => '#CDDC39',
  'BiologicalRelationship' => '#9C27B0',
  'Repository' => '#009688',
  'Observation' => '#CDDC39',
  'Citation' => '#CCCCCC',
  'Georeference' => '#CDDC39',
  'Source' => '#2196F3',
  'AssertedDistribution' => '#FF9800'
}
NODE_SHAPES =
{
  'Person' => 'person',
  'CollectionObject' => 'circle',
  'TaxonName' => 'square',
  'CollectingEvent' => 'circle',
  'TaxonDetermination' => nil,
  'Identifier' => 'triangle',
  'Otu' => 'hexagon',
  'User' => 'person',
  'ControlledVocabularyTerm' => nil,
  'BiologicalRelationship' => 'square' ,
  'Observation' => 'square',
  'Citation' => 'square',
  'Repository' => 'circle',
  'Georeference' => 'square',
  'Source' => 'pentagon',
  'Citation' => 'octagon',
  'AssertedDistribution' => 'pentagon'
}
RENDERABLE_ANNOTATIONS =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object: nil, annotations_to_draw: RENDERABLE_ANNOTATIONS) ⇒ Graph

Returns a new instance of Graph.



51
52
53
54
55
56
57
# File 'lib/export/graph.rb', line 51

def initialize(object: nil, annotations_to_draw: RENDERABLE_ANNOTATIONS )
  @draw_annotations = annotations_to_draw
  @nodes = []
  @edges = []

  add(object) if !object.nil?
end

Instance Attribute Details

#draw_annotationsObject

Returns the value of attribute draw_annotations.



47
48
49
# File 'lib/export/graph.rb', line 47

def draw_annotations
  @draw_annotations
end

#edgesObject

Returns the value of attribute edges.



45
46
47
# File 'lib/export/graph.rb', line 45

def edges
  @edges
end

#nodesObject

Returns the value of attribute nodes.



44
45
46
# File 'lib/export/graph.rb', line 44

def nodes
  @nodes
end

Instance Method Details

#add(object, object_origin = nil, edge_label: nil, edge_link: nil) ⇒ Object



77
78
79
80
# File 'lib/export/graph.rb', line 77

def add(object, object_origin = nil, edge_label: nil, edge_link: nil)
  add_node(object)
  add_edge(object_origin, object, edge_label: edge_label, edge_link: edge_link) if !object_origin.nil?
end

#add_edge(object, object_origin = nil, edge_label: nil, edge_link: nil) ⇒ Object



111
112
113
# File 'lib/export/graph.rb', line 111

def add_edge(object, object_origin = nil, edge_label: nil, edge_link: nil)
  @edges.push graph_edge(object_origin, object, edge_label: edge_label, edge_link: edge_link)
end

#add_node(object, citations: true, identifiers: true) ⇒ Object

TODO factor view options out



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/export/graph.rb', line 83

def add_node(object, citations: true, identifiers: true)
  @nodes.push graph_node(object)

  if citations && object.respond_to?(:citations)
    object.citations.each do |c|
      add_node(c, citations: false, identifiers: false)
      add_node(c.source, citations: false, identifiers: true)

      add_edge(c, object)
      add_edge(c.source, c)

      if c.source.is_bibtex?
        c.source.authors.each do |p|
          add_node(p, citations: false, identifiers: true)
          add_edge(p, c.source)
        end
      end
    end
  end

  if identifiers && object.respond_to?(:identifiers)
    object.identifiers.each do |i|
      add_node(i, citations: false, identifiers: false)
      add_edge(i, object)
    end
  end
end

#annotated_nodes(node_type) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/export/graph.rb', line 182

def annotated_nodes(node_type)
  n = []
  edges.each do |e|
    if e[:start_id] =~ /#{node_type}/
      n.push e[:end_id]
    elsif e[:end_id] =~ /#{node_type}/
      n.push e[:start_id]
    end
  end
  n.uniq
end

#count_nodes(node_type) ⇒ Object



178
179
180
# File 'lib/export/graph.rb', line 178

def count_nodes(node_type)
  nodes.select{|n| n[:id] =~ /#{node_type}/}.count
end

#graph_edge(start_object, end_object, edge_label: nil, edge_link: nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/export/graph.rb', line 134

def graph_edge(start_object, end_object, edge_label: nil, edge_link: nil)
  return nil if start_object.nil? || end_object.nil?
  h = {
    start_id: start_object.to_global_id.to_s,
    end_id: end_object.to_global_id.to_s
  }

  # TODO: change label to name
  h[:label] = edge_label unless edge_label.blank?
  h[:link] = edge_link unless edge_link.blank?
  h
end

#graph_node(object, node_link: nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/export/graph.rb', line 115

def graph_node(object, node_link: nil)
  return nil if object.nil?
  b = object.class.base_class.name
  h = {
    id: object.to_global_id.to_s,
    name:  ApplicationController.helpers.label_for(object) || object.class.base_class.name,
  }

  if b == 'ControlledVocabularyTerm'
    object.css_color  || '#000000'
  else
    h[:color] = NODE_COLORS[b] || '#000000'
  end

  h[:shape] = NODE_SHAPES[b] if !NODE_SHAPES[b].nil?
  h[:link] = node_link || Rails.application.routes.url_helpers.object_graph_task_path(global_id: object.to_global_id.to_s)
  h
end

#merge(graph) ⇒ Object

!param graph [Export::Graph] Merges the edges and nodes from another graph to this one

Returns:

  • True



150
151
152
153
154
155
156
# File 'lib/export/graph.rb', line 150

def merge(graph)
  @nodes += graph.nodes
  @edges += graph.edges
  @nodes.uniq!
  @edges.uniq!
  true
end

#statsObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/export/graph.rb', line 158

def stats
  count_nodes = nodes.count
  count_peopled_nodes = annotated_nodes('Person').count
  count_identified_nodes = annotated_nodes('Identifier').count
  count_identifiers = count_nodes('Identifier')

  return {
    nodes: count_nodes,
    edges: edges.count,
    people: count_nodes('Person'),
    identifiers: count_identifiers,
    peopled_nodes: count_peopled_nodes,
    identified_nodes: count_identified_nodes,
    node_personability:  ( count_peopled_nodes.to_f / count_nodes.to_f * 100.0 ).round(2).to_s + '%',
    node_identifiability:  ( count_identified_nodes.to_f / count_nodes.to_f * 100.0).round(2).to_s + '%',
    identifier_saturation:  (count_identifiers.to_f / count_identified_nodes.to_f * 100.0).round(2).to_s + '% (total identifiers / identified nodes * 100)',

  }
end

#to_json(include_stats: true) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/export/graph.rb', line 67

def to_json(include_stats: true)
  h =  {
    nodes: nodes,
    edges: edges
  }

  h[:stats] = stats if include_stats
  return h
end