Module: GraphHelper

Defined in:
app/helpers/graph_helper.rb

Instance Method Summary collapse

Instance Method Details

#citation_graph(citation, graph: nil, target: nil, source: false, citation_object: false, topics: true) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/graph_helper.rb', line 60

def citation_graph(citation, graph: nil, target: nil, source: false, citation_object: false, topics: true)
  c = citation
  return nil if c.nil?

  g = initialize_graph(graph, c, target)

  if source
    source_graph(c.source, graph: g, target: c, authors: true, editors: true)
  end

  if citation_object
    g.add_node(c.citation_object, citations: false, identifiers: true)
    g.add_edge(c, c.citation_object)
  end

  if topics
    c.topics.each do |t|
      g.add(t, c)
    end
  end
  g
end

#collecting_event_graph(collecting_event, graph: nil, target: nil, collection_objects: false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/graph_helper.rb', line 106

def collecting_event_graph(collecting_event, graph: nil, target: nil, collection_objects: false)
  c = collecting_event
  return nil if c.nil?

  g = initialize_graph(graph, c, target)

  if collection_objects
    c.collection_objects.each do |o|
      collection_object_graph(o, graph: g, target: c, taxon_determinations: true)
    end
  end

  c.collectors.each do |p|
    g.add(p,c)
  end

  c.georeferences.each do |r|
    g.add(r,c)
    r.georeferencers.each do |p|
      g.add(p,r)
    end
  end

  g
end

#collection_object_graph(collection_object, graph: nil, target: nil, collecting_event: false, taxon_determinations: false, biological_associations: false) ⇒ Object

Returns Export::Graph.

Returns:

  • Export::Graph



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'app/helpers/graph_helper.rb', line 186

def collection_object_graph(collection_object, graph: nil, target: nil, collecting_event: false, taxon_determinations: false, biological_associations: false)
  c = collection_object
  return nil if c.nil?

  g = initialize_graph(graph, c, target)

  collecting_event_graph(c.collecting_event, graph: g, target: c)

  if r = c.repository
    g.add(r, c)
  end

  c.biocuration_classes.each do |b|
    g.add(b, c)
  end

  c.observations.each do |o|
    g.add(o, c)
  end

  if taxon_determinations
  c.taxon_determinations.each do |d|
      taxon_determination_graph(d, graph: g, target: c, taxon_names: true)
    end
  end

  if biological_associations
  c.all_biological_associations.each do |b|
    g.add_node(b.biological_association_subject)
    g.add_node(b.biological_association_object)
    g.add_node(b.biological_relationship)

    g.add_edge(b.biological_relationship, b.biological_association_subject)
    g.add_edge(b.biological_relationship, b.biological_association_object)
  end
  end

  g
end

#initialize_graph(graph, object, target) ⇒ Object (private)



263
264
265
266
267
268
269
270
271
# File 'app/helpers/graph_helper.rb', line 263

def initialize_graph(graph, object, target)
  g = graph
  if g.nil?
    g = Export::Graph.new(object: object)
  else
    g.add(object, target)
  end
  g
end

#object_graph(object) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/graph_helper.rb', line 3

def object_graph(object)
  return nil if object.nil?

  case object.class.base_class.name
  when 'CollectionObject'
    collection_object_graph(object, collecting_event: true, taxon_determinations: true, biological_associations: true).to_json
  when 'CollectingEvent'
    collecting_event_graph(object, collection_objects: true).to_json
  when 'TaxonName'
    taxon_name_graph(object, children: true, parents: true, otus: true, synonymy: true).to_json
  when 'Otu'
    otu_graph(object, collection_objects: true, taxon_name: true, synonymy: true, biological_associations: true).to_json
  when 'TaxonDetermination'
    taxon_determination_graph(object, collection_object: true, taxon_names: true).to_json
  when 'Person'
    person_graph(object).to_json
  when 'Citation'
    citation_graph(object, source: true, citation_object: true, topics: true).to_json
  when 'Source'
    source_graph(object, citations: true, authors: true, editors: true).to_json
  else
    g = Export::Graph.new( object: object )
    g.to_json
  end
end

#otu_graph(otu, graph: nil, target: nil, collection_objects: false, taxon_name: true, synonymy: false, biological_associations: true) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/helpers/graph_helper.rb', line 132

def otu_graph(otu, graph: nil, target: nil, collection_objects: false, taxon_name: true, synonymy: false, biological_associations: true )
  o = otu
  return nil if o.nil?
  g = initialize_graph(graph, o, target)

  if taxon_name
    taxon_name_graph(o.taxon_name, graph: g, target: o, synonymy: synonymy, parents: true)
  end

  if collection_objects
    o.collection_objects.each do |co|
      collection_object_graph(co, graph: g, target: o, collecting_event: true)
    end
  end

  if biological_associations
    o.all_biological_associations.each do |b|
      g.add_node(b.biological_association_subject)
      g.add_node(b.biological_association_object)
      g.add_node(b.biological_relationship)

      g.add_edge(b.biological_relationship, b.biological_association_subject)
      g.add_edge(b.biological_relationship, b.biological_association_object)
    end
  end

  g
end

#person_graph(person, graph: nil, target: nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/graph_helper.rb', line 83

def person_graph(person, graph: nil, target: nil)
  p = person
  return nil if p.nil?

  g = initialize_graph(graph, p, target)

  # TODO: Loop has_many
  [:authored_sources, :edited_sources, :human_sources].each do |m|
    p.send(m).each do |o|
      g.add(o, p)
    end
  end

  [ :collecting_events, :taxon_determinations, :authored_taxon_names, :georeferences, :collection_objects, :dwc_occurrences].each do |m|
    p.send(m).where(project_id: sessions_current_project_id).each do |o|
      g.add(o,p)
    end
  end

  g
end

#source_graph(source, graph: nil, target: nil, citations: false, authors: false, editors: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/helpers/graph_helper.rb', line 29

def source_graph(source, graph: nil, target: nil, citations: false, authors: false, editors: false)
  s = source
  return nil if s.nil?

  g = initialize_graph(graph, nil, nil)

  # We can't auto-cite, so .add can not be used
  g.add_node(s, citations: false, identifiers: true)
  g.add_edge(target, s)

  if authors && s.respond_to?(:authors)
    s.authors.each do |p|
      g.add(p, s)
    end
  end

  if editors && s.respond_to?(:editors)
    s.editors.each do |p|
      g.add(p, s)
    end
  end

  if citations
    s.citations.where(project_id: sessions_current_project_id).each do |c|
      citation_graph(c, graph: g, target: s, citation_object: true, topics: true)
    end
  end

  g
end

#taxon_determination_graph(taxon_determination, graph: nil, target: nil, collection_object: false, taxon_names: false) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/helpers/graph_helper.rb', line 161

def taxon_determination_graph(taxon_determination, graph: nil, target: nil, collection_object: false, taxon_names: false)
  td = taxon_determination
  return nil if td.nil?
  g = initialize_graph(graph, td, target)

  g.add(td.otu, td)

  if collection_object
    g.add(td.biological_collection_object, td)
  end

  if taxon_names
    if td.otu.taxon_name
      taxon_name_graph(td.otu.taxon_name, graph: g, target: td.otu, parents: true)
    end
  end

  td.determiners.each do |p|
    g.add(p, td)
  end

  g
end

#taxon_name_graph(taxon_name, graph: nil, target: nil, synonymy: false, children: false, otus: false, parents: false) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/helpers/graph_helper.rb', line 226

def taxon_name_graph(taxon_name, graph: nil, target: nil, synonymy: false, children: false, otus: false, parents: false)
  t = taxon_name
  return nil if t.nil?

  g = initialize_graph(graph, t, target)

  t.taxon_name_authors.each do |p|
    g.add(p, t)
  end

  if children
    t.children.each do |n|
      taxon_name_graph(n, target: t, graph: g)
    end
  end

  if synonymy
    t.synonyms.each do |n|
      taxon_name_graph(n, target: t, graph: g)
    end
  end

  if otus
    t.otus.each do |o|
      otu_graph(o, target: t, graph: g)
    end
  end

  if parents
    taxon_name_graph(t.parent, graph: g, target: t, parents: parents)
  end

  g
end