summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Maunz <andreas@maunz.de>2012-10-24 16:35:31 +0200
committerAndreas Maunz <andreas@maunz.de>2012-10-24 16:35:31 +0200
commitad1725a4ee950b15d9d7c5d10a56babd0ae3706a (patch)
treef760ee460f2fd16022970013e4d75e9263977169
parent9b86169dfb87e60c81ae4dd55312cdb5b1b9d2e9 (diff)
Merged shims
-rw-r--r--application.rb2
-rw-r--r--lib/utils/html.rb50
-rw-r--r--lib/utils/shims/feature.rb87
-rw-r--r--lib/utils/shims/opentox.rb51
4 files changed, 2 insertions, 188 deletions
diff --git a/application.rb b/application.rb
index 7c3d766..434555a 100644
--- a/application.rb
+++ b/application.rb
@@ -2,6 +2,8 @@
# Loads libraries and webapps
# Author: Christoph Helma, Andreas Maunz
+require 'sinatra/url_for'
+
# Library code
$logger.debug "Compound booting: #{$compound.collect{|k,v| "#{k}: '#{v}'"} }"
Dir['./lib/utils/shims/*.rb'].each { |f| require f } # Shims for legacy code
diff --git a/lib/utils/html.rb b/lib/utils/html.rb
deleted file mode 100644
index 0392a1d..0000000
--- a/lib/utils/html.rb
+++ /dev/null
@@ -1,50 +0,0 @@
-#OT_LOGO = File.join(CONFIG[:services]["opentox-validation"],"resources/ot-logo.png")
-
-=begin
-* Name: html.rb
-* Description: Tools to provide html output
-* Author: Andreas Maunz <andreas@maunz.de>
-* Date: 10/2012
-=end
-
-# AM: needed since this gem has a nested directory structure
-require 'sinatra/url_for'
-
-class String
- # encloses URI in text with with link tag
- # @return [String] new text with marked links
- def link_urls
- regex = Regexp.new '(https?:\/\/[\S]+)([>"])'
- self.gsub( regex, '<a href="\1">\1</a>\2' )
- end
-end
-
-module OpenTox
-
- # produces a html page for making web services browser friendly
- # format of text (=string params) is preserved (e.g. line breaks)
- # urls are marked as links
- #
- # @param [String] text this is the actual content,
- # @param [optional,String] related_links info on related resources
- # @param [optional,String] description general info
- # @param [optional,Array] post_command, infos for the post operation, object defined below
- # @return [String] html page
- def self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_command=nil )
-
- # TODO add title as parameter
- title = nil #$sinatra.url_for($sinatra.request.env['PATH_INFO'], :full) if $sinatra
- html = "<html>"
- html += "<title>"+title+"</title>" if title
- #html += "<img src=\""+OT_LOGO+"\"><\/img><body>"
-
- html += "<h3>Description</h3><pre><p>"+description.link_urls+"</p></pre>" if description
- html += "<h3>Related links</h3><pre><p>"+related_links.link_urls+"</p></pre>" if related_links
- html += "<h3>Content</h3>" if description || related_links
- html += "<pre><p style=\"padding:15px; border:10px solid \#5D308A\">"
- html += text.link_urls
- html += "</p></pre></body></html>"
- html
- end
-
-end
diff --git a/lib/utils/shims/feature.rb b/lib/utils/shims/feature.rb
deleted file mode 100644
index 297748b..0000000
--- a/lib/utils/shims/feature.rb
+++ /dev/null
@@ -1,87 +0,0 @@
-=begin
-* Name: feature.rb
-* Description: Feature shims
-* Author: Andreas Maunz <andreas@maunz.de>
-* Date: 10/2012
-=end
-
-module OpenTox
-
- # Shims for the feature class
- class Feature
-
- # Load a feature from URI
- # @param [String] Feature URI
- # @return [OpenTox::Feature] Feature object with the full data
- def self.find(uri, subjectid=nil)
- return nil unless uri
- f = OpenTox::Feature.new uri, subjectid
- f.get
- f
- end
-
- # Load or create a feature given its title and metadata
- # Create it if: a) not present, or b) present, but differs in metadata
- # Newly created features are stored at the backend
- # @param[String] title Feature title
- # @param[Hash] metadata Feature metadata
- # @return [OpenTox::Feature] Feature object with the full data, or nil
- def self.find_by_title(title, metadata)
- metadata[RDF.type] = [] unless metadata[RDF.type]
- metadata[RDF.type] << RDF::OT.Feature unless metadata[RDF.type].include?(RDF::OT.Feature)
- metadata[RDF::DC.title] = title unless (metadata[RDF::DC.title])
- feature = feature_new = OpenTox::Feature.new File.join($feature[:uri], SecureRandom.uuid), @subjectid
- feature_new.metadata = metadata
- sparql = "SELECT DISTINCT ?feature WHERE { ?feature <#{RDF.type}> <#{RDF::OT['feature'.capitalize]}>. ?feature <#{RDF::DC.title}> '#{title.to_s}' }"
- feature_uris = OpenTox::Backend::FourStore.query(sparql,"text/uri-list").split("\n")
- features_equal = false # relevant also when no features found
- feature_uris.each_with_index { |feature_uri,idx|
- feature_existing = OpenTox::Feature.find(feature_uri, @subjectid)
- if (feature_new.metadata.size+1 == feature_existing.metadata.size) # +1 due to title
- features_equal = metadata.keys.collect { |predicate|
- unless ( predicate == RDF::DC.title )
- if feature_new[predicate].class == feature_existing[predicate].class
- case feature_new[predicate].class.to_s
- when "Array" then (feature_new[predicate].sort == feature_existing[predicate].sort)
- else (feature_new[predicate] == feature_existing[predicate])
- end
- end
- else
- true
- end
- }.uniq == [true]
- end
- (feature=feature_existing and break) if features_equal
- }
- unless features_equal
- feature_new.put
- end
- feature
- end
-
- # Find out feature type
- # Classification takes precedence
- # @return [String] Feature type
- def feature_type
- bad_request_error "rdf type of feature '#{@uri}' not set" unless self[RDF.type]
- if self[RDF.type].include?(OT.NominalFeature)
- "classification"
- elsif [RDF.type].to_a.flatten.include?(OT.NumericFeature)
- "regression"
- else
- "unknown"
- end
- end
-
- # Get accept values
- # @param[String] Feature URI
- # @return[Array] Accept values
- def accept_values
- accept_values = self[OT.acceptValue]
- accept_values.sort if accept_values
- accept_values
- end
-
- end
-
-end
diff --git a/lib/utils/shims/opentox.rb b/lib/utils/shims/opentox.rb
deleted file mode 100644
index c10d535..0000000
--- a/lib/utils/shims/opentox.rb
+++ /dev/null
@@ -1,51 +0,0 @@
-=begin
-* Name: opentox.rb
-* Description: Architecture shims
-* Author: Andreas Maunz <andreas@maunz.de>
-* Date: 10/2012
-=end
-
-# This avoids having to prefix everything with "RDF::" (e.g. "RDF::DC").
-# So that we can use our old code mostly as is.
-include RDF
-
-module OpenTox
-
- # Help function to provide the metadata= functionality.
- # Downward compatible to opentox-ruby.
- # @param [Hash] Key-Value pairs with the metadata
- # @return self
- def metadata=(hsh)
- hsh.each {|k,v|
- self[k]=v
- }
- end
-
-
- ### Index Structures
-
- # Create parameter positions map
- # @return [Hash] A hash with keys parameter names and values parameter positions
- def build_parameter_positions
- unless @parameter_positions
- @parameters = parameters
- @parameter_positions = @parameters.each_index.inject({}) { |h,idx|
- h[@parameters[idx][DC.title.to_s]] = idx
- h
- }
- end
- end
-
-
- ### Associative Search Operations
-
- # Search a model for a given parameter
- # @param[String] The parameter title
- # @return[Object] The parameter value
- def find_parameter_value(title)
- build_parameter_positions
- res = @parameters[@parameter_positions[title]][OT.paramValue.to_s] if @parameter_positions[title]
- res
- end
-
-end