Class: Encapsulator::RJSONParser

Inherits:
ProvJSONParser show all
Defined in:
lib/encapsulator/rjson_parser.rb

Instance Attribute Summary collapse

Attributes inherited from ProvJSONParser

#filename

Instance Method Summary collapse

Methods inherited from ProvJSONParser

#parse_json, #prefix, #read_json_file, #read_log_file

Constructor Details

#initialize(r_file, informed = false) ⇒ RJSONParser

Returns a new instance of RJSONParser.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/encapsulator/rjson_parser.rb', line 19

def initialize r_file, informed=false
  @dg = RGL::DirectedAdjacencyGraph.new # initialise the graph structure
  @map = Hash.new(0)
  @files = Hash.new
  @copies = Hash.new
  @instructions = Hash.new
  @libraries = Hash.new
  @packages = Hash.new
  @script = IO.readlines(r_file)
  @informed = informed
end

Instance Attribute Details

#dgObject (readonly)

Returns the value of attribute dg.



10
11
12
# File 'lib/encapsulator/rjson_parser.rb', line 10

def dg
  @dg
end

#fileObject (readonly)

Returns the value of attribute file.



16
17
18
# File 'lib/encapsulator/rjson_parser.rb', line 16

def file
  @file
end

#filesObject (readonly)

Returns the value of attribute files.



12
13
14
# File 'lib/encapsulator/rjson_parser.rb', line 12

def files
  @files
end

#informedObject (readonly)

Returns the value of attribute informed.



17
18
19
# File 'lib/encapsulator/rjson_parser.rb', line 17

def informed
  @informed
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



13
14
15
# File 'lib/encapsulator/rjson_parser.rb', line 13

def instructions
  @instructions
end

#librariesObject (readonly)

Returns the value of attribute libraries.



14
15
16
# File 'lib/encapsulator/rjson_parser.rb', line 14

def libraries
  @libraries
end

#mapObject (readonly)

Returns the value of attribute map.



11
12
13
# File 'lib/encapsulator/rjson_parser.rb', line 11

def map
  @map
end

#packagesObject (readonly)

Returns the value of attribute packages.



15
16
17
# File 'lib/encapsulator/rjson_parser.rb', line 15

def packages
  @packages
end

Instance Method Details

#activity(k, v) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/encapsulator/rjson_parser.rb', line 73

def activity k, v
  self.add v['rdt:type']
  if v['rdt:type'] == 'Operation'
    @instructions[k]=get_operation(v['rdt:startLine'].to_i, v['rdt:endLine'].to_i)
  end
  if /(library|require)\(('|")[a-zA-Z]+('|")/.match v['rdt:name']
    @libraries[k]=v['rdt:name']
  end
  if k == 'environment'
    v['rdt:installedPackages'].each do |l|
      packages[l['package']]=l['version']
    end
  end
end

#add(key) ⇒ Object



41
42
43
# File 'lib/encapsulator/rjson_parser.rb', line 41

def add key
  @map[key]=@map[key]+1
end

#agent(k, v) ⇒ Object



88
89
90
# File 'lib/encapsulator/rjson_parser.rb', line 88

def agent k, v
  self.add v['rdt:type']
end

#entity(k, v) ⇒ Object



66
67
68
69
70
71
# File 'lib/encapsulator/rjson_parser.rb', line 66

def entity k, v
  self.add v['rdt:type']
  if v['rdt:type'] == 'File'
    @files[k]=v['rdt:name']
  end
end

#file_showObject



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/encapsulator/rjson_parser.rb', line 190

def file_show
  puts 'Files'
			puts '-----'
  @files.each do |key, value|
    if is_input? value
	puts "Input #{value}"
else
	puts "Output #{value}"
end
  end
end

#get_list(file) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/encapsulator/rjson_parser.rb', line 132

def get_list file
	id = ''
	@files.each do |k, v|
		if v == file
			id = k
			break
		end
	end
	if id==''
		return Array.new
	end
    tree = @dg.bfs_search_tree_from(id)
    g = @dg.vertices_filtered_by {|v| tree.has_vertex? v}
    list = g.vertices
end

#get_operation(start_line, end_line) ⇒ Object



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

def get_operation start_line, end_line
  start_line-=1
  end_line-=1
  operation = ''
  for i in start_line..end_line
    operation += @script[i] unless @script[i].nil?
  end
  return operation
end

#informationObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/encapsulator/rjson_parser.rb', line 92

def information
  str = "\n------\n"
  str += "Graph:\n"
  str += "------\n"
  if @dg.directed?
    str += "directed\n"
  else
    str += "not directed\n"
  end

  if @dg.acyclic?
    str += "acyclic\n"
  else
    str += "not acyclic\n"
  end

  str += @dg.num_edges.to_s() +" edges.\n"
  str += @dg.num_vertices.to_s() +" vertices.\n"
  str += (@dg.num_edges.to_f/@dg.num_vertices).to_s() + " edges/vertices ratio.\n"
  puts str
end

#installObject



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/encapsulator/rjson_parser.rb', line 227

def install
  instructions = Array.new
  @packages.each do |key, value|
    if key == 'base'
      instructions << "  sudo dnf -y -v install R-#{value}"
     instructions << "  sudo dnf -y -v install R"
      instructions << "  sudo su - -c \"R -e \\\\\\\"install.packages(\'devtools\', repos=\'http://cran.rstudio.com/\', dependencies = TRUE)\\\\\\\"\""
    elsif !['datasets', 'utils', 'graphics', 'grDevices', 'methods', 'stats', 'provR', 'devtools'].include?(key)
      instructions << "  sudo su - -c \"R -e \\\\\\\"require('devtools');install_version(\'#{key}\', version=\'#{value}\', repos=\'http://cran.rstudio.com/\')\\\\\\\"\""
    end
  end
  return instructions
end

#is_input?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/encapsulator/rjson_parser.rb', line 212

def is_input? file_name
	source_code(file_name).empty?
end

#is_input_with_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
219
220
221
222
223
224
225
# File 'lib/encapsulator/rjson_parser.rb', line 216

def is_input_with_id? id
	@files.each do |key, value|
      if id == key
			if is_input? value
				return true
			end
		end
    end
	return false
end

#jpgObject



120
121
122
123
124
# File 'lib/encapsulator/rjson_parser.rb', line 120

def jpg
  Dir.chdir('..') do
    @dg.write_to_graphic_file('jpg')
  end
end

#packages_showObject



202
203
204
205
206
207
208
209
210
# File 'lib/encapsulator/rjson_parser.rb', line 202

def packages_show
    puts 'Packages'
	puts '--------'
	@packages.each do |key, value|
		if !['datasets', 'utils', 'graphics', 'grDevices', 'methods', 'stats', 'provR', 'devtools'].include?(key)
      	puts "#{key} v#{value}"
      end
	end
end

#pngObject



126
127
128
129
130
# File 'lib/encapsulator/rjson_parser.rb', line 126

def png
  Dir.chdir('..') do
    @dg.write_to_graphic_file('png')
  end
end

#script(output) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/encapsulator/rjson_parser.rb', line 175

def script output
  script = Array.new
  @libraries.each do |k, v|
    script << v
  end
  statements = source_code output
  script << statements unless statements.empty?
end

#script_inputs(file) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/encapsulator/rjson_parser.rb', line 160

def script_inputs file
	inputs = Hash.new
	list = get_list file
	list.delete_if { |v| !is_input_with_id?(v) }
	list.each do |v|
		next unless !@files[v].nil?
		Find.find '..' do |path|
			if path.include? '/'+@files[v]
				inputs[path.gsub @files[v], '']= path
			end
		end
    end
	return inputs
end

#showObject



184
185
186
187
188
# File 'lib/encapsulator/rjson_parser.rb', line 184

def show
  file_show
			puts "\n\n"
  packages_show
end

#source_code(file) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/encapsulator/rjson_parser.rb', line 148

def source_code file
			statements = Array.new
  list = get_list file
  list.delete_if { |v| !v.include?('p') }
  list = list.sort_by{ |m| m.tr('p', '').to_i }
  list.each do |v|
next unless !@instructions[v].nil?
    statements << @instructions[v]
  end
  return statements
end

#svgObject



114
115
116
117
118
# File 'lib/encapsulator/rjson_parser.rb', line 114

def svg
  Dir.chdir('..') do
    @dg.write_to_graphic_file('svg')
  end
end

#used(k, v) ⇒ Object



45
46
47
# File 'lib/encapsulator/rjson_parser.rb', line 45

def used k, v
  @dg.add_edge v['prov:activity'], v['prov:entity']
end

#wasAssociatedWith(k, v) ⇒ Object



61
62
63
64
# File 'lib/encapsulator/rjson_parser.rb', line 61

def wasAssociatedWith k, v
  @dg.add_edge v['prov:activity'], v['prov:agent']
  @dg.add_edge v['prov:agent'], v['prov:plan'] unless !v.key? 'prov:plan'
end

#wasDerivedFrom(k, v) ⇒ Object



53
54
55
# File 'lib/encapsulator/rjson_parser.rb', line 53

def wasDerivedFrom k, v
  @dg.add_edge v['prov:generatedEntity'], v['prov:usedEntity']
end

#wasGeneratedBy(k, v) ⇒ Object



49
50
51
# File 'lib/encapsulator/rjson_parser.rb', line 49

def wasGeneratedBy k, v
  @dg.add_edge v['prov:entity'], v['prov:activity']
end

#wasInformedBy(k, v) ⇒ Object



57
58
59
# File 'lib/encapsulator/rjson_parser.rb', line 57

def wasInformedBy k, v
  @dg.add_edge v['prov:informed'], v['prov:informant'] unless !informed
end