Class: PubliSci::Writers::Base

Inherits:
Object
  • Object
show all
Includes:
Analyzer, Query, RDFParser
Defined in:
lib/publisci/writers/base.rb

Direct Known Subclasses

ARFF, CSV

Instance Method Summary collapse

Methods included from Analyzer

#check_integrity, #dirty?, #recommend_range, #recommend_range_strings

Methods included from RDFParser

#add_node, #bnode_value, #encode_value, #get_ary, #get_hashes, #is_complex?, #is_uri?, #load_string, #observation_hash, #sanitize, #sanitize_hash, #strip_prefixes, #strip_uri, #to_literal, #to_resource, #turtle_indent

Methods included from Query

#execute, #execute_from_file, #property_names, #property_values, #row_names, #vocabulary

Instance Method Details

#codes(input, data_set = nil, select = :label) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/publisci/writers/base.rb', line 64

def codes(input, data_set = nil, select = :label)
  repo = handle_input(input)

  if data_set
    codes = execute_from_file("codes.rq",repo,:graph,{"?dataSet"=>"<#{data_set}>"}).to_h
  else
    codes = execute_from_file("codes.rq",repo,:graph).to_h
  end
  codes.map{|c| c.values.map(&:to_s)}.inject({}){|h,el|
    (h[el.first]||=[]) << el.last; h
  }
end

#dataSet(input, select = :label) ⇒ Object


58
59
60
61
62
# File 'lib/publisci/writers/base.rb', line 58

def dataSet(input, select = :label)
  repo = handle_input(input)

  execute_from_file("dataset.rq",repo,:graph).to_h.first[select].to_s
end

#dimensions(input, data_set = nil, select = :label) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/publisci/writers/base.rb', line 22

def dimensions(input, data_set=nil, select=:label)
  repo = handle_input(input)

  if data_set
    dims = execute_from_file("dimensions.rq",repo,:graph,{"?dataSet"=>"<#{data_set}>"})
  else
    dims = execute_from_file("dimensions.rq",repo,:graph)
  end

  dims.to_h.map{|d| d[select].to_s}
end

#handle_input(input) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/publisci/writers/base.rb', line 8

def handle_input(input)
  if input.is_a? String
    if File.exist? input
      RDF::Repository.load(input)
    else
      raise "UnkownStringInput: #{input}"
    end
  elsif input.is_a? RDF::Repository
    input
  else
    raise "UnkownInput: #{input}, #{input.class}"
  end
end

#measures(input, data_set = nil, select = :label) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/publisci/writers/base.rb', line 34

def measures(input, data_set=nil, select=:label)
  repo = handle_input(input)

  if data_set
    meas = execute_from_file("measures.rq",repo,:graph,{"?dataSet"=>"<#{data_set}>"})
  else
    meas = execute_from_file("measures.rq",repo,:graph)
  end

  meas.to_h.map{|d| d[select].to_s}
end

#observations(input, data_set = nil, shorten_url = true) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/publisci/writers/base.rb', line 46

def observations(input, data_set = nil, shorten_url = true)
  repo = handle_input(input)

  if data_set
    obs = execute_from_file("observations.rq",repo,:graph,{"?dataSet"=>"<#{data_set}>"})
  else
    obs = execute_from_file("observations.rq",repo,:graph)
  end

  observation_hash(obs,shorten_url)
end

#repo_to_ruby(repo, select_dataset = nil, shorten_url = true) ⇒ Object


83
84
85
86
87
88
89
90
# File 'lib/publisci/writers/base.rb', line 83

def repo_to_ruby(repo,select_dataset=nil, shorten_url=true)
  select_dataset = dataSet(repo,:dataset) unless select_dataset
  dims = dimensions(repo,select_dataset)
  meas = measures(repo,select_dataset)
  codes = codes(repo,select_dataset)
  data = observations(repo,select_dataset,shorten_url)
  {measures: meas, dimensions: dims, coded_dimensions: codes, data: data}
end

#turtle_to_ruby(turtle_file, select_dataset = nil, shorten_url = true) ⇒ Object


77
78
79
80
81
# File 'lib/publisci/writers/base.rb', line 77

def turtle_to_ruby(turtle_file, select_dataset=nil, shorten_url=true)
  repo = RDF::Repository.load(turtle_file)

  repo_to_ruby(repo,select_dataset,shorten_url)
end