Class: JSONToNetworkGraph

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

Instance Method Summary collapse

Constructor Details

#initialize(input, field1, field2) ⇒ JSONToNetworkGraph

Returns a new instance of JSONToNetworkGraph.



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

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

Instance Method Details

Create or update the appropriate link



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/jsontonetworkgraph.rb', line 78

def addupdateLink(fieldvalue1, fieldvalue2, i)
  identifier = fieldvalue1 + "-" + fieldvalue2
  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



38
39
40
41
42
43
44
45
# File 'lib/jsontonetworkgraph.rb', line 38

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jsontonetworkgraph.rb', line 57

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



27
28
29
30
31
32
33
34
35
# File 'lib/jsontonetworkgraph.rb', line 27

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/jsontonetworkgraph.rb', line 108

def genJSON
  genNodes
  genLinks
  linkCount
  
  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



48
49
50
51
52
53
54
# File 'lib/jsontonetworkgraph.rb', line 48

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



17
18
19
20
21
22
23
24
# File 'lib/jsontonetworkgraph.rb', line 17

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

#linkCountObject

Count the number of links



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/jsontonetworkgraph.rb', line 94

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