Module: OpenTox::Parser::Owl
Overview
OWL-DL parser
Defined Under Namespace
Class Method Summary collapse
-
.from_rdf(rdf, type) ⇒ Owl
creates owl object from rdf-data.
Instance Method Summary collapse
-
#initialize(uri) ⇒ OpenTox::Parser::Owl
Create a new OWL-DL parser.
-
#load_metadata(subjectid = nil) ⇒ Hash
Read metadata from opentox service.
Class Method Details
.from_rdf(rdf, type) ⇒ Owl
creates owl object from rdf-data
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/parser.rb', line 78 def self.from_rdf( rdf, type ) # write to file and read convert with rapper into tripples file = Tempfile.new("ot-rdfxml") file.puts rdf file.close #puts "cmd: rapper -i rdfxml -o ntriples #{file} 2>/dev/null" triples = `rapper -i rdfxml -o ntriples #{file.path} 2>/dev/null` # load uri via type uri = nil triples.each_line do |line| triple = line.to_triple if triple[1] == RDF['type'] and triple[2]==type raise "uri already set, two uris found with type: "+type.to_s if uri uri = triple[0] end end File.delete(file.path) # load metadata = {} triples.each_line do |line| triple = line.to_triple [triple[1]] = triple[2].split('^^').first if triple[0] == uri and triple[1] != RDF['type'] end owl = Owl::Generic.new(uri) owl. = owl end |
Instance Method Details
#initialize(uri) ⇒ OpenTox::Parser::Owl
Create a new OWL-DL parser
25 26 27 28 |
# File 'lib/parser.rb', line 25 def initialize(uri) @uri = uri @metadata = {} end |
#load_metadata(subjectid = nil) ⇒ Hash
Read metadata from opentox service
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/parser.rb', line 32 def (subjectid=nil) # avoid using rapper directly because of 2 reasons: # * http errors wont be noticed # * subjectid cannot be sent as header ##uri += "?subjectid=#{CGI.escape(subjectid)}" if subjectid ## `rapper -i rdfxml -o ntriples #{uri} 2>/dev/null`.each_line do |line| if File.exist?(@uri) file = File.new(@uri) else file = Tempfile.new("ot-rdfxml") if @dataset # do not concat /metadata to uri string, this would not work for dataset/R401577?max=3 uri = URI::parse(@uri) uri.path = File.join(uri.path,"metadata") uri = uri.to_s else uri = @uri end file.puts OpenTox::RestClientWrapper.get uri,{:subjectid => subjectid,:accept => "application/rdf+xml"},nil,false file.close to_delete = file.path end statements = [] parameter_ids = [] `rapper -i rdfxml -o ntriples #{file.path} 2>/dev/null`.each_line do |line| triple = line.to_triple @metadata[triple[1]] = triple[2].split('^^').first if triple[0] == @uri and triple[1] != RDF['type'] statements << triple parameter_ids << triple[2] if triple[1] == OT.parameters end File.delete(to_delete) if to_delete unless parameter_ids.empty? @metadata[OT.parameters] = [] parameter_ids.each do |p| parameter = {} statements.each{ |t| parameter[t[1]] = t[2] if t[0] == p and t[1] != RDF['type']} @metadata[OT.parameters] << parameter end end @metadata end |