Module: OpenTox::Parser::Owl

Included in:
Dataset, Generic
Defined in:
lib/parser.rb

Overview

OWL-DL parser

Defined Under Namespace

Classes: Dataset, Generic

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_rdf(rdf, type, allow_multiple = false) ⇒ Owl

creates owl object from rdf-data

Parameters:

  • rdf (String)
  • type (String)

    of the info (e.g. OT.Task, OT.ErrorReport) needed to get the subject-uri

Returns:

  • (Owl)

    with uri and metadata set



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
# File 'lib/parser.rb', line 89

def self.from_rdf( rdf, type, allow_multiple = false )

  uris = Array.new
  owls = Array.new

  # 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
       if !allow_multiple
         raise "uri already set, two uris found with type: "+type.to_s if uri
       end
       uri = triple[0]
       uris << uri
    end
  end
  File.delete(file.path)

  # load metadata
  uris.each { |uri|
     = {}
    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. = 
    owls << owl
  }
  allow_multiple ? owls : owls[0]
end

Instance Method Details

#initialize(uri) ⇒ OpenTox::Parser::Owl

Create a new OWL-DL parser

Parameters:

  • uri

    URI of OpenTox object

Returns:



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

Returns:

  • (Hash)

    Object metadata



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
73
74
75
76
77
78
79
80
81
82
83
# 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
      uri = URI::parse(@uri)
      #remove params like dataset/<id>?max=3 from uri, not needed for metadata
      uri.query = nil 
      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
    if triple[0] == @uri
      if triple[1] == RDF.type || triple[1]==OT.predictedVariables || triple[1]==OT.independentVariables # allow multiple types
        @metadata[triple[1]] = [] unless @metadata[triple[1]]
        @metadata[triple[1]] << triple[2].split('^^').first
      else
        @metadata[triple[1]] = triple[2].split('^^').first
      end
    end
    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.each do |k,v|
    #v = v.first if v and v.size == 1
  #end
  @metadata
end