1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
84
85
86
87
88
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
require 'rubygems'
gem "opentox-ruby", "~> 0"
require 'opentox-ruby'
set :lock, true
=begin
class ModelStore
include DataMapper::Resource
attr_accessor :prediction_dataset, :subjectid
property :id, Serial
property :uri, String, :length => 255
property :yaml, Text, :length => 2**32-1
property :created_at, DateTime
@subjectid = nil
after :save, :check_policy
private
def check_policy
OpenTox::Authorization.check_policy(uri, subjectid)
end
end
=end
class PredictionCache
# cache predictions
include DataMapper::Resource
property :id, Serial
property :compound_uri, String, :length => 255
property :model_uri, String, :length => 255
property :dataset_uri, String, :length => 255
end
DataMapper.auto_upgrade!
before do
@accept = request.env['HTTP_ACCEPT']
@accept = 'application/rdf+xml' if @accept == '*/*' or @accept == '' or @accept.nil?
@id = request.path_info.match(/^\/\d+/)
unless @id.nil?
@id = @id.to_s.sub(/\//,'').to_i
@uri = uri @id
@yaml_file = "public/#{@id}.yaml"
halt 404, "Dataset #{@id} not found." unless File.exists? @yaml_file
end
# make sure subjectid is not included in params, subjectid is set as member variable
params.delete(:subjectid)
end
require 'lazar.rb'
helpers do
def next_id
id = Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.last
id = 0 if id.nil?
id + 1
end
def uri(id)
url_for "/#{id}", :full
end
def uri_available?(urlStr)
url = URI.parse(urlStr)
unless @subjectid
Net::HTTP.start(url.host, url.port) do |http|
return http.head(url.request_uri).code == "200"
end
else
Net::HTTP.start(url.host, url.port) do |http|
return http.post(url.request_uri, "subjectid=#{@subjectid}").code == "202"
end
end
end
def activity(a)
case a.to_s
when "true"
act = "active"
when "false"
act = "inactive"
else
act = "not available"
end
act
end
end
get '/?' do # get index of models
response['Content-Type'] = 'text/uri-list'
Dir["./public/*yaml"].collect{|f| File.basename(f.sub(/.yaml/,'')).to_i}.sort.collect{|n| uri n}.join("\n") + "\n"
end
delete '/:id/?' do
LOGGER.debug "Deleting model with id "+@id.to_s
begin
FileUtils.rm @yaml_file
if @subjectid and !File.exists? @yaml_file and @uri
begin
res = OpenTox::Authorization.delete_policies_from_uri(@uri, @subjectid)
LOGGER.debug "Policy deleted for Dataset URI: #{@uri} with result: #{res}"
rescue
LOGGER.warn "Policy delete error for Dataset URI: #{@uri}"
end
end
response['Content-Type'] = 'text/plain'
"Model #{@id} deleted."
rescue
halt 404, "Model #{@id} does not exist."
end
=begin
begin
uri = ModelStore.get(params[:id]).uri
ModelStore.get(params[:id]).destroy!
"Model #{params[:id]} deleted."
if @subjectid and !ModelStore.get(params[:id]) and uri
begin
res = OpenTox::Authorization.delete_policies_from_uri(uri, @subjectid)
LOGGER.debug "Policy deleted for Model URI: #{uri} with subjectid: #{@subjectid} with result: #{res}"
rescue
LOGGER.warn "Policy delete error for Model URI: #{uri}"
end
end
response['Content-Type'] = 'text/plain'
"Model #{params[:id]} deleted."
rescue
halt 404, "Model #{params[:id]} does not exist."
end
=end
end
delete '/?' do
# TODO delete datasets
FileUtils.rm Dir["public/*.yaml"]
PredictionCache.auto_migrate!
response['Content-Type'] = 'text/plain'
"All models and cached predictions deleted."
end
|