summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorch <ch@ch.in-silico.ch>2015-07-12 12:14:13 +0200
committerch <ch@ch.in-silico.ch>2015-07-12 12:14:13 +0200
commitf850712765a67bf31b1327401e3eddf59e3e6f50 (patch)
tree18933386be18e87758486745369a978a75ddc1a3
parent723d731bc08d90a82f22474c9fafcc504eb424d3 (diff)
initial dataset tests
-rw-r--r--lib/compound.rb19
-rw-r--r--lib/dataset.rb146
-rw-r--r--lib/opentox.rb76
-rw-r--r--lib/task.rb10
4 files changed, 134 insertions, 117 deletions
diff --git a/lib/compound.rb b/lib/compound.rb
index f63e127..82ea94e 100644
--- a/lib/compound.rb
+++ b/lib/compound.rb
@@ -5,8 +5,13 @@ module OpenTox
# Ruby wrapper for OpenTox Compound Webservices (http://opentox.org/dev/apis/api-1.2/structure).
class Compound
+ def initialize uri
+ @data = {}
+ @data["uri"] = uri
+ end
+
def ==(c)
- @uri == c.uri
+ @data["uri"] == c.uri
end
# Create a compound from smiles string
@@ -45,25 +50,25 @@ module OpenTox
# Get InChI
# @return [String] InChI string
def inchi
- @inchi ||= RestClientWrapper.get(@uri,{},{:accept => 'chemical/x-inchi'}).chomp
+ @inchi ||= RestClientWrapper.get(@data["uri"],{},{:accept => 'chemical/x-inchi'}).chomp
end
# Get InChIKey
# @return [String] InChI string
def inchikey
- @inchikey ||= RestClientWrapper.get(@uri,{},{:accept => 'chemical/x-inchikey'}).chomp
+ @inchikey ||= RestClientWrapper.get(@data["uri"],{},{:accept => 'chemical/x-inchikey'}).chomp
end
# Get (canonical) smiles
# @return [String] Smiles string
def smiles
- @smiles ||= RestClientWrapper.get(@uri,{},{:accept => 'chemical/x-daylight-smiles'}).chomp
+ @smiles ||= RestClientWrapper.get(@data["uri"],{},{:accept => 'chemical/x-daylight-smiles'}).chomp
end
# Get sdf
# @return [String] SDF string
def sdf
- RestClientWrapper.get(@uri,{},{:accept => 'chemical/x-mdl-sdfile'}).chomp
+ RestClientWrapper.get(@data["uri"],{},{:accept => 'chemical/x-mdl-sdfile'}).chomp
end
# Get gif image
@@ -77,13 +82,13 @@ module OpenTox
# image = compound.png
# @return [image/png] Image data
def png
- RestClientWrapper.get(File.join @uri, "image")
+ RestClientWrapper.get(File.join @data["uri"], "image")
end
# Get URI of compound image
# @return [String] Compound image URI
def image_uri
- File.join @uri, "image"
+ File.join @data["uri"], "image"
end
# Get all known compound names. Relies on an external service for name lookups.
diff --git a/lib/dataset.rb b/lib/dataset.rb
index d7c4076..28133b2 100644
--- a/lib/dataset.rb
+++ b/lib/dataset.rb
@@ -5,63 +5,75 @@ module OpenTox
# Ruby wrapper for OpenTox Dataset Webservices (http://opentox.org/dev/apis/api-1.2/dataset).
class Dataset
- attr_writer :features, :compounds, :data_entries
-
def initialize uri=nil
super uri
- @features = []
- @compounds = []
- @data_entries = []
+ @data["features"] ||= []
+ @data["compounds"] ||= []
+ @data["data_entries"] ||= []
+ end
+
+ [:features, :compounds, :data_entries].each do |method|
+ send :define_method, method do
+ @data[method.to_s]
+ end
+ send :define_method, "#{method}=" do |value|
+ @data[method.to_s] = value.collect{|v| v.uri}
+ end
+ send :define_method, "#{method}<<" do |value|
+ @data[method.to_s] << value.uri
+ end
end
# Get data (lazy loading from dataset service)
# overrides {OpenTox#metadata} to only load the metadata instead of the whole dataset
# @return [Hash] the metadata
def metadata force_update=false
- if @metadata.empty? or force_update
- uri = File.join(@uri,"metadata")
- begin
- parse_ntriples RestClientWrapper.get(uri,{},{:accept => "text/plain"})
- rescue # fall back to rdfxml
- parse_rdfxml RestClientWrapper.get(uri,{},{:accept => "application/rdf+xml"})
- end
- @metadata = @rdf.to_hash[RDF::URI.new(@uri)].inject({}) { |h, (predicate, values)| h[predicate] = values.collect{|v| v.to_s}; h }
+ if @data.empty? or force_update
+ uri = File.join(@data["uri"],"metadata")
+ #begin
+ RestClientWrapper.get(uri,{},{:accept => "application/json"})
+ @data = JSON.parse RestClientWrapper.get(uri,{},{:accept => "application/json"})
+ #parse_ntriples RestClientWrapper.get(uri,{},{:accept => "text/plain"})
+ #rescue # fall back to rdfxml
+ #parse_rdfxml RestClientWrapper.get(uri,{},{:accept => "application/rdf+xml"})
+ #end
+ #@data = @rdf.to_hash[RDF::URI.new(@data["uri"])].inject({}) { |h, (predicate, values)| h[predicate] = values.collect{|v| v.to_s}; h }
end
- @metadata
+ @data
end
# @return [Array] feature objects (NOT uris)
def features force_update=false
- if @features.empty? or force_update
- uri = File.join(@uri,"features")
+ if @data["features"].empty? or force_update
+ uri = File.join(@data["uri"],"features")
begin
uris = RestClientWrapper.get(uri,{},{:accept => "text/uri-list"}).split("\n") # ordered datasets return ordered features
rescue
uris = []
end
- @features = uris.collect{|uri| Feature.new(uri)}
+ @data["features"] = uris.collect{|uri| Feature.new(uri)}
end
- @features
+ @data["features"]
end
# @return [Array] compound objects (NOT uris)
def compounds force_update=false
- if @compounds.empty? or force_update
- uri = File.join(@uri,"compounds")
+ if @data["compounds"].empty? or force_update
+ uri = File.join(@data["uri"],"compounds")
begin
uris = RestClientWrapper.get(uri,{},{:accept => "text/uri-list"}).split("\n") # ordered datasets return ordered compounds
rescue
uris = []
end
- @compounds = uris.collect{|uri| Compound.new(uri)}
+ @data["compounds"] = uris.collect{|uri| Compound.new(uri)}
end
- @compounds
+ @data["compounds"]
end
# @return [Array] with two dimensions,
# first index: compounds, second index: features, values: compound feature values
def data_entries force_update=false
- if @data_entries.empty? or force_update
+ if @data["data_entries"].empty? or force_update
sparql = "SELECT ?cidx ?fidx ?value FROM <#{uri}> WHERE {
?data_entry <#{RDF::OLO.index}> ?cidx ;
<#{RDF::OT.values}> ?v .
@@ -71,16 +83,16 @@ module OpenTox
} ORDER BY ?fidx ?cidx"
RestClientWrapper.get(service_uri,{:query => sparql},{:accept => "text/uri-list"}).split("\n").each do |row|
r,c,v = row.split("\t")
- @data_entries[r.to_i] ||= []
+ @data["data_entries"][r.to_i] ||= []
# adjust value class depending on feature type, StringFeature takes precedence over NumericFeature
if features[c.to_i][RDF.type].include? RDF::OT.NumericFeature and ! features[c.to_i][RDF.type].include? RDF::OT.StringFeature
v = v.to_f if v
end
- @data_entries[r.to_i][c.to_i] = v if v
+ @data["data_entries"][r.to_i][c.to_i] = v if v
end
# TODO: fallbacks for external and unordered datasets
end
- @data_entries
+ @data["data_entries"]
end
# Find data entry values for a given compound and feature
@@ -137,18 +149,18 @@ module OpenTox
end
# Adding data methods
- # (Alternatively, you can directly change @features and @compounds)
+ # (Alternatively, you can directly change @data["features"] and @data["compounds"])
# Create a dataset from file (csv,sdf,...)
# @param filename [String]
# @return [String] dataset uri
def upload filename, wait=true
- uri = RestClientWrapper.put(@uri, {:file => File.new(filename)})
+ uri = RestClientWrapper.put(@data["uri"], {:file => File.new(filename)})
wait_for_task uri if URI.task?(uri) and wait
compounds true
features true
metadata true
- @uri
+ @data["uri"]
end
# @param compound [OpenTox::Compound]
@@ -156,17 +168,17 @@ module OpenTox
# @param value [Object] (will be converted to String)
# @return [Array] data_entries
def add_data_entry compound, feature, value
- @compounds << compound unless @compounds.collect{|c| c.uri}.include?(compound.uri)
- row = @compounds.collect{|c| c.uri}.index(compound.uri)
- @features << feature unless @features.collect{|f| f.uri}.include?(feature.uri)
- col = @features.collect{|f| f.uri}.index(feature.uri)
- if @data_entries[row] and @data_entries[row][col] # duplicated values
- @compounds << compound
- row = @compounds.collect{|c| c.uri}.rindex(compound.uri)
+ @data["compounds"] << compound unless @data["compounds"].collect{|c| c.uri}.include?(compound.uri)
+ row = @data["compounds"].collect{|c| c.uri}.index(compound.uri)
+ @data["features"] << feature unless @data["features"].collect{|f| f.uri}.include?(feature.uri)
+ col = @data["features"].collect{|f| f.uri}.index(feature.uri)
+ if @data["data_entries"][row] and @data["data_entries"][row][col] # duplicated values
+ @data["compounds"] << compound
+ row = @data["compounds"].collect{|c| c.uri}.rindex(compound.uri)
end
if value
- @data_entries[row] ||= []
- @data_entries[row][col] = value
+ @data["data_entries"][row] ||= []
+ @data["data_entries"][row][col] = value
end
end
@@ -181,15 +193,15 @@ module OpenTox
# d << [ Compound.new("c1ccccc1"), feature-value-a, feature-value-b ]
def << row
compound = row.shift # removes the compound from the array
- bad_request_error "Dataset features are empty." unless @features
- bad_request_error "Row size '#{row.size}' does not match features size '#{@features.size}'." unless row.size == @features.size
+ bad_request_error "Dataset features are empty." unless @data["features"]
+ bad_request_error "Row size '#{row.size}' does not match features size '#{@data["features"].size}'." unless row.size == @data["features"].size
bad_request_error "First column is not a OpenTox::Compound" unless compound.class == OpenTox::Compound
- @compounds << compound
- @data_entries << row
+ @data["compounds"] << compound.uri
+ @data["data_entries"] << row
end
# Serialisation
-
+
# converts dataset to csv format including compound smiles as first column, other column headers are feature titles
# @return [String]
def to_csv(inchi=false)
@@ -213,11 +225,11 @@ module OpenTox
reader.each_statement{ |statement| @rdf << statement }
end
query = RDF::Query.new({ :uri => { RDF.type => RDF::OT.Compound } })
- @compounds = query.execute(@rdf).collect { |solution| OpenTox::Compound.new solution.uri }
+ @data["compounds"] = query.execute(@rdf).collect { |solution| OpenTox::Compound.new solution.uri }
query = RDF::Query.new({ :uri => { RDF.type => RDF::OT.Feature } })
- @features = query.execute(@rdf).collect { |solution| OpenTox::Feature.new solution.uri }
- @compounds.each_with_index do |c,i|
- @features.each_with_index do |f,j|
+ @data["features"] = query.execute(@rdf).collect { |solution| OpenTox::Feature.new solution.uri }
+ @data["compounds"].each_with_index do |c,i|
+ @data["features"].each_with_index do |f,j|
end
end
end
@@ -225,13 +237,13 @@ module OpenTox
# redefine rdf serialization methods
send :define_method, "to_#{format}".to_sym do
- @metadata[RDF.type] = [RDF::OT.Dataset, RDF::OT.OrderedDataset]
+ @data[RDF.type] = [RDF::OT.Dataset, RDF::OT.OrderedDataset]
create_rdf
- @features.each_with_index do |feature,i|
+ @data["features"].each_with_index do |feature,i|
@rdf << [RDF::URI.new(feature.uri), RDF::URI.new(RDF.type), RDF::URI.new(RDF::OT.Feature)]
@rdf << [RDF::URI.new(feature.uri), RDF::URI.new(RDF::OLO.index), RDF::Literal.new(i)]
end
- @compounds.each_with_index do |compound,i|
+ @data["compounds"].each_with_index do |compound,i|
@rdf << [RDF::URI.new(compound.uri), RDF::URI.new(RDF.type), RDF::URI.new(RDF::OT.Compound)]
if defined? @neighbors and neighbors.include? compound
@rdf << [RDF::URI.new(compound.uri), RDF::URI.new(RDF.type), RDF::URI.new(RDF::OT.Neighbor)]
@@ -239,14 +251,14 @@ module OpenTox
@rdf << [RDF::URI.new(compound.uri), RDF::URI.new(RDF::OLO.index), RDF::Literal.new(i)]
data_entry_node = RDF::Node.new
- @rdf << [RDF::URI.new(@uri), RDF::URI.new(RDF::OT.dataEntry), data_entry_node]
+ @rdf << [RDF::URI.new(@data["uri"]), RDF::URI.new(RDF::OT.dataEntry), data_entry_node]
@rdf << [data_entry_node, RDF::URI.new(RDF.type), RDF::URI.new(RDF::OT.DataEntry)]
@rdf << [data_entry_node, RDF::URI.new(RDF::OLO.index), RDF::Literal.new(i)]
@rdf << [data_entry_node, RDF::URI.new(RDF::OT.compound), RDF::URI.new(compound.uri)]
- @data_entries[i].each_with_index do |value,j|
+ @data["data_entries"][i].each_with_index do |value,j|
value_node = RDF::Node.new
@rdf << [data_entry_node, RDF::URI.new(RDF::OT.values), value_node]
- @rdf << [value_node, RDF::URI.new(RDF::OT.feature), RDF::URI.new(@features[j].uri)]
+ @rdf << [value_node, RDF::URI.new(RDF::OT.feature), RDF::URI.new(@data["features"][j].uri)]
@rdf << [value_node, RDF::URI.new(RDF::OT.value), RDF::Literal.new(value)]
end
end
@@ -260,24 +272,24 @@ module OpenTox
# TODO: fix bug that affects data_entry positions # DG: who wrotes this comment ?
def to_ntriples # redefined string version for better performance
ntriples = ""
- @metadata[RDF.type] = [ RDF::OT.Dataset, RDF::OT.OrderedDataset ]
- @metadata.each do |predicate,values|
+ @data[RDF.type] = [ RDF::OT.Dataset, RDF::OT.OrderedDataset ]
+ @data.each do |predicate,values|
[values].flatten.each do |value|
URI.valid?(value) ? value = "<#{value}>" : value = "\"#{value}\""
- ntriples << "<#{@uri}> <#{predicate}> #{value} .\n" #\n"
+ ntriples << "<#{@data["uri"]}> <#{predicate}> #{value} .\n" #\n"
end
end
@parameters.each_with_index do |parameter,i|
p_node = "_:parameter"+ i.to_s
- ntriples << "<#{@uri}> <#{RDF::OT.parameters}> #{p_node} .\n"
+ ntriples << "<#{@data["uri"]}> <#{RDF::OT.parameters}> #{p_node} .\n"
ntriples << "#{p_node} <#{RDF.type}> <#{RDF::OT.Parameter}> .\n"
parameter.each { |k,v| ntriples << "#{p_node} <#{k}> \"#{v.to_s.tr('"', '\'')}\" .\n" }
end
- @features.each_with_index do |feature,i|
+ @data["features"].each_with_index do |feature,i|
ntriples << "<#{feature.uri}> <#{RDF.type}> <#{RDF::OT.Feature}> .\n"
ntriples << "<#{feature.uri}> <#{RDF::OLO.index}> \"#{i}\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n" # sorting at dataset service does not work without type information
end
- @compounds.each_with_index do |compound,i|
+ @data["compounds"].each_with_index do |compound,i|
ntriples << "<#{compound.uri}> <#{RDF.type}> <#{RDF::OT.Compound}> .\n"
if defined? @neighbors and neighbors.include? compound
ntriples << "<#{compound.uri}> <#{RDF.type}> <#{RDF::OT.Neighbor}> .\n"
@@ -285,16 +297,16 @@ module OpenTox
ntriples << "<#{compound.uri}> <#{RDF::OLO.index}> \"#{i}\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n" # sorting at dataset service does not work without type information
data_entry_node = "_:dataentry"+ i.to_s
- ntriples << "<#{@uri}> <#{RDF::OT.dataEntry}> #{data_entry_node} .\n"
+ ntriples << "<#{@data["uri"]}> <#{RDF::OT.dataEntry}> #{data_entry_node} .\n"
ntriples << "#{data_entry_node} <#{RDF.type}> <#{RDF::OT.DataEntry}> .\n"
ntriples << "#{data_entry_node} <#{RDF::OLO.index}> \"#{i}\"^^<http://www.w3.org/2001/XMLSchema#integer> .\n" # sorting at dataset service does not work without type information
ntriples << "#{data_entry_node} <#{RDF::OT.compound}> <#{compound.uri}> .\n"
- @data_entries[i].each_with_index do |value,j|
+ @data["data_entries"][i].each_with_index do |value,j|
value_node = data_entry_node+ "_value"+ j.to_s
ntriples << "#{data_entry_node} <#{RDF::OT.values}> #{value_node} .\n"
- ntriples << "#{value_node} <#{RDF::OT.feature}> <#{@features[j].uri}> .\n"
+ ntriples << "#{value_node} <#{RDF::OT.feature}> <#{@data["features"][j].uri}> .\n"
ntriples << "#{value_node} <#{RDF::OT.value}> \"#{value}\" .\n"
- end unless @data_entries[i].nil?
+ end unless @data["data_entries"][i].nil?
end
ntriples
@@ -361,7 +373,7 @@ module OpenTox
unless defined?(@cmp_indices) and @cmp_indices.has_key?(compound_uri)
@cmp_indices = {}
compounds().size.times do |i|
- c = @compounds[i].uri
+ c = @data["compounds"][i].uri
if @cmp_indices[c]==nil
@cmp_indices[c] = [i]
else
@@ -374,9 +386,9 @@ module OpenTox
# returns compound feature value using the compound-index and the feature_uri
def data_entry_value(compound_index, feature_uri)
- data_entries(true) if @data_entries.empty?
- col = @features.collect{|f| f.uri}.index feature_uri
- @data_entries[compound_index] ? @data_entries[compound_index][col] : nil
+ data_entries(true) if @data["data_entries"].empty?
+ col = @data["features"].collect{|f| f.uri}.index feature_uri
+ @data["data_entries"][compound_index] ? @data["data_entries"][compound_index][col] : nil
end
end
diff --git a/lib/opentox.rb b/lib/opentox.rb
index 850ef4b..fe49ece 100644
--- a/lib/opentox.rb
+++ b/lib/opentox.rb
@@ -4,26 +4,29 @@ $logger.level = Logger::DEBUG
module OpenTox
- attr_reader :uri
- attr_writer :metadata, :parameters
-
# Ruby interface
# Create a new OpenTox object
# @param uri [optional,String] URI
# @return [OpenTox] OpenTox object
def initialize uri=nil
- @metadata = {}
- @metadata[:type] = self.class.to_s.split(/::/).last
- #@parameters = []
- uri ? @uri = uri.to_s.chomp : @uri = File.join(service_uri, SecureRandom.uuid)
+ @data = {}
+ if uri
+ @data["uri"] = uri.to_s.chomp
+ get
+ else
+ @data["uuid"] = SecureRandom.uuid
+ @data["uri"] = File.join(service_uri, @data["uuid"])
+ @data["created_at"] = DateTime.now.to_s
+ @data["type"] = self.class.to_s.split('::').last
+ end
end
# Object metadata (lazy loading)
# @return [Hash] Object metadata
def metadata force_update=false
- get #if (@metadata.nil? or @metadata.empty? or force_update) and URI.accessible? @uri
- @metadata
+ get if force_update and URI.accessible? @data["uri"]
+ @data
end
# Metadata values
@@ -31,8 +34,8 @@ module OpenTox
# @return [Array, String] Predicate value(s)
def [](predicate)
predicate = predicate.to_s
- return nil if metadata[predicate].nil?
- metadata[predicate].size == 1 ? metadata[predicate].first : metadata[predicate]
+ return nil if @data[predicate].nil?
+ @data[predicate].size == 1 ? @data[predicate].first : @data[predicate]
end
# Set a metadata entry
@@ -40,7 +43,7 @@ module OpenTox
# @param values [Array, String] Predicate value(s)
def []=(predicate,values)
predicate = predicate.to_s
- @metadata[predicate] = [values].flatten
+ @data[predicate] = [values].flatten
end
=begin
@@ -78,14 +81,14 @@ module OpenTox
# @param [String,optional] mime_type
def get mime_type="application/json"
bad_request_error "Mime type #{mime_type} is not supported. Please use 'application/json' (default), 'text/plain' (ntriples) or mime_type == 'application/rdf+xml'." unless mime_type == "application/json" or mime_type == "text/plain" or mime_type == "application/rdf+xml"
- response = RestClientWrapper.get(@uri,{},{:accept => mime_type})
+ response = RestClientWrapper.get(@data["uri"],{},{:accept => mime_type})
if URI.task?(response)
uri = wait_for_task response
response = RestClientWrapper.get(uri,{},{:accept => mime_type})
end
case mime_type
when 'application/json'
- @metadata = JSON.parse(response)
+ @data = JSON.parse(response) if response
when "text/plain"
parse_ntriples response
when "application/rdf+xml"
@@ -112,25 +115,20 @@ module OpenTox
# Save object at webservice (replace or create object)
def put wait=true, mime_type="application/json"
- bad_request_error "Mime type #{mime_type} is not supported. Please use 'application/json' (default)." unless mime_type == "application/json" or mime_type == "text/plain" or mime_type == "application/rdf+xml"
- @metadata[:created_at] = DateTime.now unless URI.accessible? @uri
- #@metadata[RDF::DC.modified] = DateTime.now
case mime_type
- when 'text/plain'
- body = self.to_ntriples
- when 'application/rdf+xml'
- body = self.to_rdfxml
when 'application/json'
- body = self.to_json
+ body = @data.to_json
+ else
+ bad_request_error "Mime type #{mime_type} is not supported. Please use 'application/json' (default)."
end
- uri = RestClientWrapper.put @uri, body, { :content_type => mime_type}
+ uri = RestClientWrapper.put @data["uri"], body, { :content_type => mime_type}
wait ? wait_for_task(uri) : uri
end
# Delete object at webservice
def delete
- RestClientWrapper.delete(@uri)
- #Authorization.delete_policies_from_uri(@uri) if $aa[:uri]
+ RestClientWrapper.delete(@data["uri"])
+ #Authorization.delete_policies_from_uri(@data["uri"]) if $aa[:uri]
end
def service_uri
@@ -147,11 +145,11 @@ module OpenTox
# DG: uri in object should be in brackets, otherwise query for uri-list ignores the object.
# see: http://www.w3.org/TR/rdf-testcases/#sec-uri-encoding
@metadata.each do |predicate,values|
- [values].flatten.each{ |value| @rdf << [RDF::URI.new(@uri), predicate, (URI.valid?(value) ? RDF::URI.new(value) : value)] unless value.nil? }
+ [values].flatten.each{ |value| @rdf << [RDF::URI.new(@data["uri"]), predicate, (URI.valid?(value) ? RDF::URI.new(value) : value)] unless value.nil? }
end
@parameters.each do |parameter|
p_node = RDF::Node.new
- @rdf << [RDF::URI.new(@uri), RDF::OT.parameters, p_node]
+ @rdf << [RDF::URI.new(@data["uri"]), RDF::OT.parameters, p_node]
@rdf << [p_node, RDF.type, RDF::OT.Parameter]
parameter.each { |k,v| @rdf << [p_node, k, v] unless v.nil?}
end
@@ -167,7 +165,7 @@ module OpenTox
reader.each_statement{ |statement| @rdf << statement }
end
# return values as plain strings instead of RDF objects
- @metadata = @rdf.to_hash[RDF::URI.new(@uri)].inject({}) { |h, (predicate, values)| h[predicate] = values.collect{|v| v.to_s}; h }
+ @metadata = @rdf.to_hash[RDF::URI.new(@data["uri"])].inject({}) { |h, (predicate, values)| h[predicate] = values.collect{|v| v.to_s}; h }
end
=begin
@@ -196,27 +194,23 @@ module OpenTox
end
def to_json
- @metadata[:uri] = @uri
- @metadata.to_json
+ @data.to_json
end
# @return [String] converts OpenTox object into html document (by first converting it to a string)
def to_html
- to_turtle.to_html
+ to_json.to_html
end
-=begin
# short access for metadata keys title, description and type
- #{ :title => RDF::DC.title, :description => RDF::DC.description, :type => RDF.type }.each do |method,predicate|
- [ :title , :description, :type ].each do |method|
+ [ :title , :description , :type , :uri, :uuid ].each do |method|
send :define_method, method do
- self.[](method)
+ self[method]
end
send :define_method, "#{method}=" do |value|
- self.[]=(method,value)
+ self[method] = value
end
end
-=end
# define class methods within module
def self.included(base)
@@ -257,13 +251,17 @@ module OpenTox
def self.create metadata
object = self.new
- object.metadata = metadata
+ metadata.each{|k,v| object[k] = v}
object.put
object
end
def self.find_or_create metadata
- uris = RestClientWrapper.get(service_uri,{:query => metadata},{:accept => "text/uri-list"}).split("\n")
+ search = metadata
+ search.delete("_id")
+ search.delete("uri")
+ search.delete("uuid")
+ uris = RestClientWrapper.get(service_uri,{:query => search},{:accept => "text/uri-list"}).split("\n")
uris.empty? ? self.create(metadata) : self.new(uris.first)
end
end
diff --git a/lib/task.rb b/lib/task.rb
index 7f4d39a..9d03aed 100644
--- a/lib/task.rb
+++ b/lib/task.rb
@@ -17,7 +17,7 @@ module OpenTox
def self.run(description, creator=nil, uri=nil)
task = Task.new uri
- task[:created_at] = DateTime.now
+ task[:created_at] = DateTime.now.to_s
task[:hasStatus] = "Running"
task[:description] = description.to_s
task[:creator] = creator.to_s
@@ -89,14 +89,14 @@ module OpenTox
while running?
sleep dur
dur = [[(Time.new - start_time)/20.0,0.3].max,300.0].min
- request_timeout_error "max wait time exceeded ("+DEFAULT_TASK_MAX_DURATION.to_s+"sec), task: '"+@uri.to_s+"'" if (Time.new > due_to_time)
+ request_timeout_error "max wait time exceeded ("+DEFAULT_TASK_MAX_DURATION.to_s+"sec), task: '"+uri.to_s+"'" if (Time.new > due_to_time)
end
end
end
def code
- RestClientWrapper.get(@uri).code.to_i
+ RestClientWrapper.get(uri).code.to_i
end
# get only header for status requests
@@ -118,6 +118,7 @@ module OpenTox
[:hasStatus, :resultURI, :created_at, :finished_at, :percentageCompleted].each do |method|
define_method method do
+ get
self.[](method)
end
end
@@ -125,11 +126,12 @@ module OpenTox
# Check status of a task
# @return [String] Status
def status
+ get
self[:hasStatus]
end
def error_report
- #get
+ get
self[:errorReport]
end