diff options
author | Martin Gütlein <martin.guetlein@gmail.com> | 2010-04-23 18:47:37 +0200 |
---|---|---|
committer | Martin Gütlein <martin.guetlein@gmail.com> | 2010-04-23 18:47:37 +0200 |
commit | afa31803afd167c19c9ba95e73d311c9e2b8b43a (patch) | |
tree | 1b3d7b16c3d13ed45b02ba5b27d55e0168b1ffb2 | |
parent | 08232976c35511086fe6787c6c8c098c63ef0818 (diff) |
changing owl once again
-rwxr-xr-x | bin/yaml2owl.rb | 2 | ||||
-rw-r--r-- | lib/dataset.rb | 19 | ||||
-rw-r--r-- | lib/model.rb | 9 | ||||
-rw-r--r-- | lib/owl.rb | 94 | ||||
-rw-r--r-- | lib/rest_client_wrapper.rb | 24 | ||||
-rw-r--r-- | lib/task.rb | 38 |
6 files changed, 101 insertions, 85 deletions
diff --git a/bin/yaml2owl.rb b/bin/yaml2owl.rb index b259ad1..1002912 100755 --- a/bin/yaml2owl.rb +++ b/bin/yaml2owl.rb @@ -5,7 +5,7 @@ require 'opentox-ruby-api-wrapper' input = YAML.load_file(ARGV[0]) dataset = OpenTox::Dataset.new dataset.title = input[:title] -dataset.source = input[:source] +dataset.creator = input[:source] input[:data].each do |c,f| f.each do |k,v| v.each do |value| diff --git a/lib/dataset.rb b/lib/dataset.rb index 4c3f103..c513463 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -4,7 +4,7 @@ module OpenTox class Dataset - attr_accessor :uri, :title, :source, :identifier, :data, :features, :compounds + attr_accessor :uri, :title, :creator, :data, :features, :compounds def initialize @data = {} @@ -15,15 +15,15 @@ module OpenTox def self.find(uri) if uri.match(/webservices.in-silico.ch|localhost|ot.dataset.de|opentox.informatik.uni-freiburg.de/) # try to get YAML first - d = YAML.load RestClientWrapper.get(uri, :accept => 'application/x-yaml').to_s + d = YAML.load RestClientWrapper.get(uri.to_s.strip, :accept => 'application/x-yaml').to_s + d.uri = uri unless d.uri else # get default rdf+xml - owl = OpenTox::Owl.from_uri(uri) + owl = OpenTox::Owl.from_uri(uri.to_s.strip, "Dataset") d = Dataset.new - d.title = owl.title - d.source = owl.source - d.identifier = owl.identifier.sub(/^\[/,'').sub(/\]$/,'') - d.uri = d.identifier + d.title = owl.get("title") + d.creator = owl.get("creator") + d.uri = owl.uri # when loading a dataset from owl, only compound- and feature-uris are loaded owl.load_dataset(d.compounds, d.features) @@ -33,13 +33,12 @@ module OpenTox d.compounds.uniq! d.features.uniq! end - d.uri = uri unless d.uri return d end # creates a new dataset, using only those compounsd specified in new_compounds # returns uri of new dataset - def create_new_dataset( new_compounds, new_features, new_title, new_source ) + def create_new_dataset( new_compounds, new_features, new_title, new_creator ) # load require features if ((defined? @dirty_features) && (@dirty_features - new_features).size > 0) @@ -48,7 +47,7 @@ module OpenTox dataset = OpenTox::Dataset.new dataset.title = new_title - dataset.source = new_source + dataset.creator = new_creator dataset.features = new_features dataset.compounds = new_compounds diff --git a/lib/model.rb b/lib/model.rb index 8b275ed..236de84 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -3,19 +3,20 @@ module OpenTox class Generic - attr_accessor :uri, :title, :creator, :date, :format, :identifier, :predictedVariables, :independentVariables, :dependentVariables, :trainingDataset, :feature_dataset_uri, :effects, :activities, :p_values, :fingerprints, :features, :algorithm + attr_accessor :uri, :title, :creator, :date, :format, :predictedVariables, :independentVariables, :dependentVariables, :trainingDataset, :feature_dataset_uri, :effects, :activities, :p_values, :fingerprints, :features, :algorithm def self.find(uri) - owl = OpenTox::Owl.from_uri(uri) + owl = OpenTox::Owl.from_uri(uri, "Model") return self.new(owl) end protected def initialize(owl) - [:uri, :date, :creator, :title, :format, :identifier, :algorithm, - :dependentVariables, :independentVariables, :predictedVariables, :trainingDataset].each do |a| + [:date, :creator, :title, :format, :algorithm, :dependentVariables, + :independentVariables, :predictedVariables, :trainingDataset].each do |a| self.send("#{a.to_s}=".to_sym, owl.get(a.to_s)) end + @uri = owl.uri RestClientWrapper.raise_uri_error "invalid model:\n"+ self.to_yaml+"\n",@uri.to_s unless (Utils.is_uri?(@uri) and @dependentVariables and @independentVariables and @predictedVariables) if ENV['RACK_ENV'] =~ /test|debug/ @@ -19,24 +19,24 @@ module OpenTox # ot_class is the class of the object, e.g. "Model","Dataset", ... # root_node is the root-object node in the rdf - # identifier is the uri of the object - attr_accessor :ot_class, :root_node, :identifier, :model + # uri the uri of the object + attr_accessor :ot_class, :root_node, :uri, :model def initialize @model = Redland::Model.new Redland::MemoryStore.new end def self.create( ot_class, uri ) + owl = OpenTox::Owl.new owl.ot_class = ot_class - owl.root_node = Redland::Resource.new(uri.chomp+"XYZ") - owl.identifier = uri.chomp - owl.set("type",owl.ot_class) - owl.set('identifier', owl.identifier, "xsd:anyURI") + owl.root_node = Redland::Resource.new(uri.to_s.strip) + owl.set("type",owl.node(owl.ot_class)) owl end - def self.from_data(data,base_uri) + # loads owl from data + def self.from_data(data, base_uri, ot_class, no_wrong_class_exception=false ) owl = OpenTox::Owl.new parser = Redland::Parser.new @@ -44,50 +44,56 @@ module OpenTox begin parser.parse_string_into_model(owl.model, data, base_uri) - # getting object-(root)-node and identifier via identifier property - # PENDING easier solution? - owl.identifier = nil - owl.model.find(nil, owl.node('identifier'), nil) do |s,p,o| - #LOGGER.debug "ID Statements "+s.to_s+" . "+p.to_s+" -> "+o.to_s - root_id = true + # now loading root_node and uri + owl.model.find(nil, owl.node("type"), owl.node(ot_class)) do |s,p,o| + #LOGGER.debug "about statements "+s.to_s+" . "+p.to_s+" -> "+o.to_s + is_root = true owl.model.find(nil, nil, s) do |ss,pp,oo| - root_id = false + is_root = false break end - if root_id - if o.is_a?(Redland::Literal) - raise "cannot derieve object uri from rdf, more than one identifier" if owl.identifier - owl.root_node = s - owl.identifier = o.value - else - raise "illegal identifier, not a literal: "+o.to_s - end + if is_root + raise "cannot derieve root object from rdf, more than one object specified" if owl.uri + raise "illegal root node type, no uri specified\n"+data.to_s if s.blank? + owl.uri = s.uri.to_s + owl.root_node = s + end + end + + # handle error if no root node was found + unless owl.root_node + types = [] + owl.model.find(nil, owl.node("type"), nil){ |s,p,o| types << o.to_s } + msg = "root node for class '"+ot_class+"' not found (available type nodes: "+types.inspect+")" + if no_wrong_class_exception + LOGGER.debug "suppressing error: "+msg + return nil + else + raise msg end end - raise "Illegal RDF: root identifier missing" unless owl.identifier - owl.ot_class = owl.get("type") - LOGGER.debug "RDF loaded, uri/identifier:"+owl.identifier+", ot-class: "+owl.ot_class.to_s+", root-node:"+owl.root_node.to_s + + raise "no uri in rdf: '"+owl.uri+"'" unless owl.uri and Utils.is_uri?(owl.uri) + owl.ot_class = ot_class owl rescue => e RestClientWrapper.raise_uri_error(e.message, base_uri) end end - def self.from_uri(uri) - return from_data(RestClient.get(uri,:accept => "application/rdf+xml").to_s, uri) + def self.from_uri(uri, ot_class) + return from_data(RestClient.get(uri,:accept => "application/rdf+xml").to_s, uri, ot_class) end def rdf @model.to_string end - def uri - @identifier - end - def get(name) + #PENDING remove debug checks + raise "get identifier deprecated, use uri instead" if name=="identifier" + raise "uri is no prop, use owl.uri instead" if name=="uri" property_node = node(name.to_s) - raise "Method '#{name.to_s}' not found." unless property_node val = @model.object(@root_node, property_node) return nil unless val if val.is_a?(Redland::Literal) @@ -98,20 +104,22 @@ module OpenTox end def set(name, value, datatype=nil) + #PENDING remove debug checks + raise "set identifier deprecated, use uri instead" if name=="identifier" + raise "uri is no prop, cannot set uri" if name=="uri" property_node = node(name.to_s) - raise "Method '#{name.to_s}' not found." unless property_node - begin # delete existing entry - t = @model.object(@root_node, property_node) - @model.delete @root_node, property_node, t - rescue + begin # delete existing entry + t = @model.object(@root_node, property_node) + @model.delete @root_node, property_node, t + rescue end - if value.first.is_a?(Redland::Node) + if value.is_a?(Redland::Node) raise "not nil datatype not allowed when setting redland node as value" if datatype @model.add @root_node, property_node, value elsif datatype - @model.add @root_node, property_node, Redland::Literal.create(value, datatype) + @model.add @root_node, property_node, Redland::Literal.create(value.to_s, datatype) else - @model.add @root_node, property_node, value + @model.add @root_node, property_node, value.to_s end end @@ -122,7 +130,8 @@ module OpenTox @model.add parameter, node('title'), name @model.add parameter, node('paramScope'), settings[:scope] @model.add parameter, node('paramValue'), settings[:value] - end + @model.add @root_node, node('parameters'), parameter + end end def add_data_entries(compound_uri,features) @@ -246,7 +255,7 @@ module OpenTox # search for all feature_value_node with property 'ot_feature' # feature_node is either nil, i.e. a wildcard or specified - @model.find(nil, ot_feature, feature_node) do |feature_value_node,p,o| + @model.find(nil, node('feature'), feature_node) do |feature_value_node,p,o| # get compound_uri by "backtracking" to values node (property is 'values'), then get compound_node via 'compound' value_nodes = @model.subjects(node('values'),feature_value_node) @@ -294,6 +303,7 @@ module OpenTox end @@property_nodes = { "type" => RDF["type"], + "about" => RDF["about"], "title" => DC["title"], "creator" => DC["creator"], "uri" => DC["identifier"], diff --git a/lib/rest_client_wrapper.rb b/lib/rest_client_wrapper.rb index 86ee890..0219f51 100644 --- a/lib/rest_client_wrapper.rb +++ b/lib/rest_client_wrapper.rb @@ -111,7 +111,7 @@ module OpenTox task = nil case res.content_type when /application\/rdf\+xml|text\/x-yaml/ - task = OpenTox::Task.from_data(res, res.content_type, uri) + task = OpenTox::Task.from_data(res, res.content_type, uri, true) when /text\// return res if res.content_type=~/text\/uri-list/ and res.split("\n").size > 1 #if uri list contains more then one uri, its not a task @@ -121,14 +121,14 @@ module OpenTox res.content_type = "text/uri-list" return res end - task = OpenTox::Task.find(res) if Utils.task_uri?(res) + task = OpenTox::Task.find(res.to_s) if Utils.task_uri?(res) else raise "unknown content-type when checking for task: "+res.content_type+" content: "+res[0..200] end # task could be loaded, wait for task to finish if task - LOGGER.debug "result is a task "+task.uri.to_s+", wait for completion" + LOGGER.debug "result is a task '"+task.uri.to_s+"', wait for completion" task.wait_for_completion raise task.description if task.error? res = WrapperResult.new(task.resultURI) @@ -165,15 +165,15 @@ module OpenTox error = [Error.new(code, body, uri, payload, headers)] end - ##debug utility: write error to file - #error_dir = "/tmp/ot_errors" - #FileUtils.mkdir(error_dir) unless File.exist?(error_dir) - #raise "could not create error dir" unless File.exist?(error_dir) and File.directory?(error_dir) - #file_name = "error" - #time=Time.now.strftime("%m.%d.%Y-%H:%M:%S") - #count = 1 - #count+=1 while File.exist?(File.join(error_dir,file_name+"_"+time+"_"+count.to_s)) - #File.new(File.join(error_dir,file_name+"_"+time+"_"+count.to_s),"w").puts(body) +# #debug utility: write error to file +# error_dir = "/tmp/ot_errors" +# FileUtils.mkdir(error_dir) unless File.exist?(error_dir) +# raise "could not create error dir" unless File.exist?(error_dir) and File.directory?(error_dir) +# file_name = "error" +# time=Time.now.strftime("%m.%d.%Y-%H:%M:%S") +# count = 1 +# count+=1 while File.exist?(File.join(error_dir,file_name+"_"+time+"_"+count.to_s)) +# File.new(File.join(error_dir,file_name+"_"+time+"_"+count.to_s),"w").puts(body) # return error (by halting, halts should be logged) # PENDING always return yaml for now diff --git a/lib/task.rb b/lib/task.rb index 99d8f13..4b147d7 100644 --- a/lib/task.rb +++ b/lib/task.rb @@ -9,7 +9,7 @@ module OpenTox private def initialize(uri) - @uri = uri + @uri = uri.to_s.strip end public @@ -24,38 +24,44 @@ module OpenTox return task end - def self.from_data(data, content_type, base_uri) - begin - task = Task.new(nil) - task.reload_from_data(data, content_type, base_uri) - return task - rescue + # test_if_task = true -> error suppressed if data is no task, nil is returned + def self.from_data(data, content_type, base_uri, test_if_task) + task = Task.new(nil) + task.reload_from_data(data, content_type, base_uri, test_if_task) + if test_if_task and (!task.uri or task.uri.strip.size==0) return nil + else + return task end end def reload result = RestClientWrapper.get(uri) - reload_from_data(result, result.content_type) + reload_from_data(result, result.content_type, uri, false) end - def reload_from_data( data=nil, content_type=nil, base_uri=nil ) + # test_if_task = true -> error suppressed if data is no task, empty task is returned + def reload_from_data( data, content_type, base_uri, test_if_task ) case content_type when /text\/x-yaml/ task = YAML.load data - raise "yaml data is no task" if task.is_a?(Task) - TASK_ATTRIBS.each{ |a| send("#{a.to_s}=".to_sym,task[a]) } + if task.is_a?(Task) + TASK_ATTRIBS.each{ |a| send("#{a.to_s}=".to_sym,task[a]) } + else + raise "yaml data is no task: "+task.class.to_s unless test_if_task + end when /application\/rdf\+xml/ - base_uri = uri unless base_uri - owl = OpenTox::Owl.from_data(data,base_uri) - raise "not a task" if owl.ot_class=="Task" - TASK_ATTRIBS.each{|a| self.send("#{a.to_s}=".to_sym, owl.get(a.to_s))} + owl = OpenTox::Owl.from_data(data,base_uri,"Task",test_if_task) + if owl + self.uri = owl.uri + (TASK_ATTRIBS-[:uri]).each{|a| self.send("#{a.to_s}=".to_sym, owl.get(a.to_s))} + end else raise "content type for tasks not supported: "+content_type.to_s end + raise "uri is null after loading" unless @uri and @uri.to_s.strip.size>0 unless test_if_task end - # invalid: getters in task.rb should work for non-internal tasks as well # #def self.base_uri |