Class: QTL2RDF
- Inherits:
-
Object
- Object
- QTL2RDF
- Defined in:
- lib/r2rdf.rb
Instance Attribute Summary collapse
-
#port_4s ⇒ Object
Returns the value of attribute port_4s.
Instance Method Summary collapse
- #dump(var) ⇒ Object
- #dump_dataframe(var) ⇒ Object
-
#initialize(dir = '.') ⇒ QTL2RDF
constructor
A new instance of QTL2RDF.
- #load_history(dir = @dir, file = '.Rhistory') ⇒ Object
- #load_statements(statements) ⇒ Object
- #load_workspace(dir = @dir, file = '.RData') ⇒ Object
-
#n3_for(h) ⇒ Object
get n3 for a dataframe using datacube vocabulary still needs range for measures and dimensions, sdmx-dimension equivalent for rows.
- #statements_for(string, type = :turtle) ⇒ Object
- #to_store(var, parse_type = :turtle) ⇒ Object
- #triples_for(h) ⇒ Object
- #turtletype_for(value) ⇒ Object
- #vars ⇒ Object
Constructor Details
#initialize(dir = '.') ⇒ QTL2RDF
Returns a new instance of QTL2RDF.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/r2rdf.rb', line 9 def initialize(dir='.') @dir = File.absolute_path(dir) @R = Rserve::Connection.new() if File.exist?(dir + "/.RData") # puts "loading workspace #{dir}/.RData" @R.eval("load('#{dir}/.RData')") end if File.exist?(dir + "/.Rhistory") # puts "loading history #{dir}/.Rhistory" @R.eval("loadhistory('#{dir}/.Rhistory')") end @port_4s = 8080 end |
Instance Attribute Details
#port_4s ⇒ Object
Returns the value of attribute port_4s.
7 8 9 |
# File 'lib/r2rdf.rb', line 7 def port_4s @port_4s end |
Instance Method Details
#dump(var) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/r2rdf.rb', line 68 def dump(var) x = @R.eval(var) if x.attr # if x.attr.payload["class"].to_a.include? 'data.frame' dump_dataframe var # end else if x.list? type = x.payload.class.to_s.split("::").last # seems hacky, but probably only temporary value = x.payload.map(&:payload).flatten elsif x.payload.size == 1 type = x.class.to_s.split("::").last value = x.payload.first else type = x.class.to_s.split("::").last value = x.payload end {var => {"attr"=>{"class" => type}, :value => value}} end end |
#dump_dataframe(var) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/r2rdf.rb', line 45 def dump_dataframe(var) h = {} h[var] = {"attr" => {}, "rows"=>{}} x = @R.eval(var) x.attr.payload.keys.map{ |a| h[var]["attr"][a] = x.attr.payload[a].to_ruby } rownames = x.attr.payload["row.names"].to_ruby colnames = x.payload.keys rownames.each_with_index.map{ |row,i| rh = {} colnames.map{ |col| rh[col] = x.payload[col].to_a[i].to_f } h[var]["rows"][row] = rh } h end |
#load_history(dir = @dir, file = '.Rhistory') ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/r2rdf.rb', line 35 def load_history(dir=@dir,file='.Rhistory') path = File.join(File.absolute_path(dir),file) if File.exist?(path) # puts "loading history #{dir}/.Rhistory" @R.eval("loadhistory('#{path}')") else puts "Couldn't find #{path}" end end |
#load_statements(statements) ⇒ Object
208 209 210 211 212 |
# File 'lib/r2rdf.rb', line 208 def load_statements(statements) #maybe a better way than inserting statements one at a time? repo = RDF::FourStore::Repository.new("http://localhost:#{@port_4s}") statements.each{|s| repo.insert(s)} end |
#load_workspace(dir = @dir, file = '.RData') ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/r2rdf.rb', line 25 def load_workspace(dir=@dir,file='.RData') path = File.join(File.absolute_path(dir),file) if File.exist?(path) # puts "loading workspace #{dir}/.RData" @R.eval("load('#{path}')") else puts "Couldn't find #{path}" end end |
#n3_for(h) ⇒ Object
get n3 for a dataframe using datacube vocabulary still needs range for measures and dimensions, sdmx-dimension equivalent for rows
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/r2rdf.rb', line 133 def n3_for(h) str = <<-EOF @prefix : <http://www.rqtl.org/ns/#> . @prefix qb: <http://purl.org/linked-data/cube#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix prop: <http://www.rqtl.org/dc/properties/> . @prefix cs: <http://www.rqtl.org/dc/cs/> . EOF #@prefix sdmx-measure: <http://purl.org/linked-data/sdmx/2009/measure#> var = h.keys.first names = h[var]["attr"]["names"] #generate data structure definition str << ":dsd-#{var} a qb:DataStructureDefinition ;\n" str << "\tqb:component cs:refRow ,\n" names.map{ |n| str << "\t\tcs:#{n} ,\n\n" } str[-3]="." #generate component specifications str << "cs:refRow a qb:ComponentSpecification ;\n\trdfs:label \"Component Specification for Row\" ;\n\tqb:dimension prop:refRow .\n\n" names.map{ |n| str << "cs:#{n} a qb:ComponentSpecification ;\n\trdfs:label \"Component Specification for #{n}\" ;\n\tqb:measure prop:#{n} .\n\n" } #generate dataset definition str << ":dataset-#{var} a qb:DataSet ;\n\trdfs:label \"#{var}\"@en ;\n\tqb:structure :dsd-#{var} .\n\n" #add DimensionProperty for row str << <<-EOF :refRow a rdf:Property, qb:DimensionProperty ; \trdfs:label "Row"@en . EOF #generate MeasureProperties names.map{ |n| str << ":#{n} a rdf:Property, qb:MeasureProperty ;\n\trdfs:label \"#{n}\"@en .\n\n" } #add observations h[var]["rows"].map{|k,v| str << ":obs#{k} a qb:Observation ;\n\tqb:dataSet :dataset-#{var} ;\n\tprop:refRow :#{k} ;\n" v.map{|l,w| str << "\tprop:#{l} #{w} ;\n" } str << "\t.\n\n" } #add row property definitions h[var]["rows"].map{|k,v| str << ":#{k} a prop:refRow ;\n\trdfs:label \"#{k}\" .\n\n" } str end |
#statements_for(string, type = :turtle) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/r2rdf.rb', line 193 def statements_for(string,type=:turtle) statements = [] RDF::Reader.for(:turtle).new(string) do |reader| reader.each_statement do |statement| # puts statement.inspect statements << statement end end statements end |
#to_store(var, parse_type = :turtle) ⇒ Object
214 215 216 217 218 219 220 |
# File 'lib/r2rdf.rb', line 214 def to_store(var, parse_type=:turtle) load_statements(triples_for(dump(var))) if parse_type==:ntriples #TODO: this doesn't work (needs to be converted to statments or # a way of inserting raw strings needs to be figured out. load_statements(n3_for(dump(var))) if parse_type==:turtle end |
#triples_for(h) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/r2rdf.rb', line 89 def triples_for(h) statements = [] base_n = RDF::Node.new attr_n = RDF::Node.new vocab = RDF::Vocabulary.new('http://www.placeholder.com/rqtl#') base_uri = RDF::URI.new('http://www.placeholder.com/') var = h.keys.first statements << RDF::Statement.new(base_n, RDF::DC.title, RDF::Literal.new(var)) if h[var].is_a? Hash if(h[var]["attr"]) statements << RDF::Statement.new(base_n, vocab.attributes, attr_n) h[var]["attr"].map{ |k,v| statements << RDF::Statement.new(attr_n, vocab[k], RDF::Literal.new(v)) } end if h[var]["rows"] h[var]["rows"].map{ |k,v| row_uri = base_uri.join("row#{k}") statements << RDF::Statement.new(row_uri, vocab.row_of, base_n) statements << RDF::Statement.new(row_uri, RDF::DC.title, k) num = 1 # maybe container support exists in RDF.rb? v.map { |j,u| n = RDF::Node.new statements << RDF::Statement.new(n, vocab.entry_of, row_uri) statements << RDF::Statement.new(n, RDF::DC.title, j) statements << RDF::Statement.new(n, RDF::DC.title, j) statements << RDF::Statement.new(n, vocab["_#{num}"], RDF::Literal.new(u)) num += 1 } } end if h[var].has_key? :value statements << RDF::Statement.new(base_n, vocab.has_value, RDF::Literal.new(h[var][:value])) end else statements << RDF::Statement.new(base_n, vocab.has_value, RDF::Literal.new(h[var])) end statements end |
#turtletype_for(value) ⇒ Object
204 205 206 |
# File 'lib/r2rdf.rb', line 204 def turtletype_for(value) #use for providing ranges to better define data (later) end |
#vars ⇒ Object
222 223 224 |
# File 'lib/r2rdf.rb', line 222 def vars @R.eval("ls()").payload end |