Class: Export::Graph
- Inherits:
-
Object
- Object
- Export::Graph
- 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', 'Image' => '#CDDC39', }.freeze
- NODE_SHAPES =
{ 'Person' => 'person', 'CollectionObject' => 'circle', 'TaxonName' => 'square', 'CollectingEvent' => 'circle', 'TaxonDetermination' => nil, 'Identifier' => 'triangle', 'Otu' => 'hexagon', 'User' => 'person', 'ControlledVocabularyTerm' => nil, 'BiologicalRelationship' => 'square' , 'Observation' => 'square', 'Repository' => 'circle', 'Georeference' => 'square', 'Source' => 'pentagon', 'Citation' => 'octagon', 'AssertedDistribution' => 'pentagon', 'Image' => 'square', 'GeographicArea' => 'square' }.freeze
- RENDERABLE_ANNOTATIONS =
[]
Instance Attribute Summary collapse
-
#draw_annotations ⇒ Object
Returns the value of attribute draw_annotations.
-
#edges ⇒ Object
Returns the value of attribute edges.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
Instance Method Summary collapse
- #add(object, object_origin = nil, edge_label: nil, edge_link: nil) ⇒ Object
- #add_edge(object, object_origin = nil, edge_label: nil, edge_link: nil) ⇒ Object
-
#add_node(object, citations: true, identifiers: true) ⇒ Object
TODO factor view options out.
- #annotated_nodes(node_type) ⇒ Object
- #count_nodes(node_type) ⇒ Object
- #graph_edge(start_object, end_object, edge_label: nil, edge_link: nil) ⇒ Object
- #graph_node(object, node_link: nil) ⇒ Object
-
#initialize(object: nil, annotations_to_draw: RENDERABLE_ANNOTATIONS) ⇒ Graph
constructor
A new instance of Graph.
-
#merge(graph) ⇒ Object
!param graph [Export::Graph] Merges the edges and nodes from another graph to this one.
- #stats ⇒ Object
- #to_json(include_stats: true) ⇒ Object
Constructor Details
#initialize(object: nil, annotations_to_draw: RENDERABLE_ANNOTATIONS) ⇒ Graph
Returns a new instance of Graph.
53 54 55 56 57 58 59 |
# File 'lib/export/graph.rb', line 53 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_annotations ⇒ Object
Returns the value of attribute draw_annotations.
49 50 51 |
# File 'lib/export/graph.rb', line 49 def draw_annotations @draw_annotations end |
#edges ⇒ Object
Returns the value of attribute edges.
47 48 49 |
# File 'lib/export/graph.rb', line 47 def edges @edges end |
#nodes ⇒ Object
Returns the value of attribute nodes.
46 47 48 |
# File 'lib/export/graph.rb', line 46 def nodes @nodes end |
Instance Method Details
#add(object, object_origin = nil, edge_label: nil, edge_link: nil) ⇒ Object
79 80 81 82 |
# File 'lib/export/graph.rb', line 79 def add(object, object_origin = nil, edge_label: nil, edge_link: nil) add_node(object) add_edge(object_origin, object, edge_label:, edge_link:) if !object_origin.nil? end |
#add_edge(object, object_origin = nil, edge_label: nil, edge_link: nil) ⇒ Object
113 114 115 |
# File 'lib/export/graph.rb', line 113 def add_edge(object, object_origin = nil, edge_label: nil, edge_link: nil) @edges.push graph_edge(object_origin, object, edge_label:, edge_link:) end |
#add_node(object, citations: true, identifiers: true) ⇒ Object
TODO factor view options out
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 110 111 |
# File 'lib/export/graph.rb', line 85 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..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
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/export/graph.rb', line 183 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
179 180 181 |
# File 'lib/export/graph.rb', line 179 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
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/export/graph.rb', line 136 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 if edge_label.present? h[:link] = edge_link if edge_link.present? h end |
#graph_node(object, node_link: nil) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/export/graph.rb', line 117 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
152 153 154 155 156 157 158 |
# File 'lib/export/graph.rb', line 152 def merge(graph) @nodes += graph.nodes @edges += graph.edges @nodes.uniq! @edges.uniq! true end |
#stats ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/export/graph.rb', line 160 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
69 70 71 72 73 74 75 76 77 |
# File 'lib/export/graph.rb', line 69 def to_json(include_stats: true) h = { nodes:, edges: } h[:stats] = stats if include_stats return h end |