Class: JSONToNetworkGraph

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

Instance Method Summary collapse

Constructor Details

#initialize(input, field1, group1, field2, group2) ⇒ JSONToNetworkGraph

Returns a new instance of JSONToNetworkGraph.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/jsontonetworkgraph.rb', line 7

def initialize(input, field1, group1, field2, group2)
  @input = JSON.parse(input)
  @field1 = field1
  @field2 = field2
  @group1 = group1
  @group2 = group2
  @nodehash = Hash.new
  @linkhash = Hash.new
  @grouphash = Hash.new
  @groupnum = 0
  @nodeindex = 0
end

Instance Method Details

Create or update the appropriate link



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jsontonetworkgraph.rb', line 82

def addupdateLink(fieldvalue1, fieldvalue2, i)
  identifier = fieldvalue1.to_s + "-" + fieldvalue2.to_s
  if !@linkhash.include? identifier
    source = @nodehash[fieldvalue1].getID
    target = @nodehash[fieldvalue2].getID

    @nodehash[fieldvalue1].addLink
    @nodehash[fieldvalue2].addLink

    @linkhash[identifier] = Link.new(source, target, fieldvalue1, fieldvalue2, i)
  else
    @linkhash[identifier].update(i)
  end
end

#addupdateNode(fieldname, fieldvalue, i) ⇒ Object

Create or update the appropriate node



42
43
44
45
46
47
48
49
# File 'lib/jsontonetworkgraph.rb', line 42

def addupdateNode(fieldname, fieldvalue, i)
  if !(@nodehash.include? fieldvalue)
    @nodehash[fieldvalue] = Node.new(@nodeindex, fieldvalue, fieldname, i)
    @nodeindex += 1
  else
    @nodehash[fieldvalue].update(i)
  end
end

Handle fields that are arrays



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jsontonetworkgraph.rb', line 61

def arrayCheckLink(fieldvalue1, fieldvalue2, i)
  if (fieldvalue1.is_a? Array) && !(fieldvalue2.is_a? Array)
    fieldvalue1.each do |a|
      addupdateLink(a, fieldvalue2, i)
    end
  elsif (fieldvalue2.is_a? Array) && !(fieldvalue1.is_a? Array)
    fieldvalue2.each do |a|
      addupdateLink(fieldvalue1, a, i)
    end
  elsif (fieldvalue1.is_a? Array) && (fieldvalue2.is_a? Array)
    fieldvalue1.each do |a|
      fieldvalue2.each do |b|
        addupateLink(a, b, i)
      end
    end
  else
    addupdateLink(fieldvalue1, fieldvalue2, i)
  end
end

#arrayCheckNode(fieldname, fieldvalue, i) ⇒ Object

Handle fields that are arrays



31
32
33
34
35
36
37
38
39
# File 'lib/jsontonetworkgraph.rb', line 31

def arrayCheckNode(fieldname, fieldvalue, i)
  if fieldvalue.is_a? Array
    fieldvalue.each do |a|
      addupdateNode(fieldname, a, i)
    end
  else
    addupdateNode(fieldname, fieldvalue, i)
  end
end

#genJSONObject

Generate JSON with nodes and links



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/jsontonetworkgraph.rb', line 150

def genJSON
  genNodes
  genLinks
  linkCount
  groupNodes(@group1, @field1)
  groupNodes(@group2, @field2)
  
  nodearray = Array.new
  @nodehash.each_value do |n|
    nodearray.push(n.nodeData)
  end

  linkarray = Array.new
  @linkhash.each_value do |l|
    linkarray.push(l.linkData)
  end

  jsonhash = {:nodes => nodearray, :links => linkarray}
  JSON.pretty_generate(jsonhash)
end

Generate all the links



52
53
54
55
56
57
58
# File 'lib/jsontonetworkgraph.rb', line 52

def genLinks
  @input.each do |i|
    if !(i[@field1].nil? || i[@field2].nil? || i[@field1].empty? || i[@field2].empty?)
      arrayCheckLink(i[@field1], i[@field2], i)
    end
  end
end

#genNodesObject

Generate all the nodes



21
22
23
24
25
26
27
28
# File 'lib/jsontonetworkgraph.rb', line 21

def genNodes
  @input.each do |i|
    if !(i[@field1].nil? || i[@field2].nil? || i[@field1].empty? || i[@field2].empty?)
      arrayCheckNode(@field1, i[@field1], i)
      arrayCheckNode(@field2, i[@field2], i)
    end
  end
end

#groupNodes(groupfield, nodetype) ⇒ Object

Groups nodes based on another field



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jsontonetworkgraph.rb', line 112

def groupNodes(groupfield, nodetype)
  @input.each do |i|
    if !(i[@field1].nil? || i[@field2].nil? || i[@field1].empty? || i[@field2].empty?)
      
      # Handles fields that are arrays for groups
      if i[groupfield].is_a? Array
        i[groupfield].each do |a|
          if !@grouphash.include? a
            @groupnum += 1
            @grouphash[a] = @groupnum
          end
        end

      # Adds non-array values to grouphash
      else
        if !@grouphash.include? i[groupfield]
          @groupnum += 1
          @grouphash[i[groupfield]] = @groupnum
        end
      end
    end
  end
  
  # Assigns the correct value to each node from the grouphash
  @nodehash.each_value do |n|
    if n.getType == nodetype
      n.getValue.each do |v|
        if v[groupfield].is_a? Array
          n.setGroup(@grouphash[n.getName])
        else
          n.setGroup(@grouphash[v[groupfield]])
        end
      end
    end
  end
end

#linkCountObject

Count the number of links



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jsontonetworkgraph.rb', line 98

def linkCount
  @linkhash.each_value do |l|
    count1 = @nodehash[l.instance_variable_get(:@field1)].instance_variable_get(:@linkcount)
    count2 = @nodehash[l.instance_variable_get(:@field2)].instance_variable_get(:@linkcount)

    if count1 > count2
      l.linkCount(count2)
    else
      l.linkCount(count1)
    end
  end
end