summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2012-05-02 20:06:53 +0000
committerChristoph Helma <helma@in-silico.ch>2012-05-02 20:06:53 +0000
commitd794b927754e8f5015eb5283731114abd6388d23 (patch)
treec99b11719412c9aaa06aad5efa7b70aee2a0fc64
parent7823d94600673060d3194cbd70c766486dbd592f (diff)
initial task service
-rw-r--r--lib/4store.rb50
1 files changed, 39 insertions, 11 deletions
diff --git a/lib/4store.rb b/lib/4store.rb
index b20af00..d564892 100644
--- a/lib/4store.rb
+++ b/lib/4store.rb
@@ -14,7 +14,7 @@ module OpenTox
#"text/x-yaml" => :yaml,
#"text/yaml" => :yaml,
"text/html" => :html,
- # TODO: compression, forms
+ # TODO: forms
#/sparql/ => :sparql #removed to prevent sparql injections
}
@@ -36,7 +36,7 @@ module OpenTox
mime_type = "text/html" if mime_type.match(%r{\*/\*})
bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.collect{|f| @@format_mime[f]}.join(", ")} in the Accept Header." unless @@accept_formats.include? @@mime_format[mime_type]
if mime_type =~ /json|yaml|uri-list/
- sparql = "SELECT ?s WHERE {?s <#{RDF.type}> <#{@@class}>. }"
+ sparql = "SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o} }"
elsif mime_type =~ /turtle|html|rdf|plain/
sparql = "CONSTRUCT {?s ?p ?o.} WHERE {?s <#{RDF.type}> <#{@@class}>; ?p ?o. }"
end
@@ -46,11 +46,13 @@ module OpenTox
def self.get uri, mime_type
mime_type = "text/html" if mime_type.match(%r{\*/\*})
bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.collect{|f| @@format_mime[f]}.join(", ")} in the Accept Header." unless @@accept_formats.include? @@mime_format[mime_type]
- not_found_error "#{uri} not found." unless list("text/uri-list").split("\n").include?(uri)
+ not_found_error "#{uri} not found." unless available? uri
sparql = "CONSTRUCT {?s ?p ?o.} FROM <#{uri}> WHERE { ?s ?p ?o. }"
query sparql, mime_type
end
+ # TODO: add created at, modified at statements, submitter?
+
def self.post uri, rdf, mime_type
bad_request_error "'#{mime_type}' is not a supported content type. Please use one of #{@@content_type_formats.collect{|f| @@format_mime[f]}.join(", ")}." unless @@content_type_formats.include? @@mime_format[mime_type]
rdf = convert rdf, @@mime_format[mime_type], :ntriples#, uri unless mime_type == 'text/plain'
@@ -66,19 +68,30 @@ module OpenTox
elsif mime_type != "text/plain" # ntriples are not converted
rdf = convert rdf, @@mime_format[mime_type], :ntriples
end
- rdf = "<#{uri}> <#{RDF.type}> <#{@@class}>." unless rdf # create empty resource
+ unless rdf # create empty resource
+ rdf = "<#{uri}> <#{RDF.type}> <#{@@class}>."
+ rdf += "\n<#{uri}> <#{RDF::DC.date}> \"#{DateTime.now}\"."
+ end
RestClient.put File.join(four_store_uri,"data",uri), rdf, :content_type => "application/x-turtle" # content-type not very consistent in 4store
end
def self.delete uri
- RestClientWrapper.delete data_uri uri
+ pute data_uri(uri)
+ RestClientWrapper.delete data_uri(uri)
+ end
+
+ def self.update sparql
+ RestClient.post(update_uri, :update => sparql )
+ #RestClient.get(update_uri, :params => { :update => sparql })
end
def self.query sparql, mime_type
if sparql =~ /SELECT/i
- xml = RestClient.post File.join(four_store_uri,"sparql")+"/", :query => sparql
- #TODO request tab delimited format to speed up parsing
- list = parse_sparql_xml_results(xml).collect{|hash| hash["s"]}
+ #puts sparql_uri
+ #puts sparql
+ list = RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body.gsub(/<|>/,'').split("\n")
+ list.shift
+ return list unless mime_type
case mime_type
when /json/
return list.to_json
@@ -98,7 +111,7 @@ module OpenTox
when /html/
# TODO: fix and improve
html = "<html><body>"
- html += convert(nt,:ntriples, :turtle).gsub(%r{<(.*)>},'&lt;<a href="\1">\1</a>&gt;').gsub(/\n/,'<br/>')#.gsub(/ /,'&nbsp;')
+ html += convert(nt,:ntriples, :turtle).gsub(%r{<(.*)>},'&lt;<a href="\1">\1</a>&gt;').gsub(/\n/,'<br/>')
html += "</body></html>"
return html
when "application/rdf+xml"
@@ -112,6 +125,14 @@ module OpenTox
private
+ def self.available? uri
+ sparql = "SELECT DISTINCT ?s WHERE {GRAPH <#{uri}> {?s <#{RDF.type}> <#{@@class}>} }"
+ r = query(sparql, nil)
+ #puts "RESULT"
+ #puts r.inspect
+ r.size == 1 and r.first == uri
+ end
+
def self.convert rdf_string, input_format, output_format, rewrite_uri=nil
rewrite_uri ? serialize(parse_and_rewrite_uri(rdf_string,input_format, rewrite_uri), output_format) : serialize(parse(rdf_string,input_format), output_format)
end
@@ -144,8 +165,9 @@ module OpenTox
def self.serialize rdf, format
if format == :turtle # prefixes seen to need RDF::N3
- # TODO add prefixes
- string = RDF::N3::Writer.for(format).buffer(:prefixes => {:ot => "http://www.opentox.org/api/1.2#"}) do |writer|
+ prefixes = {:rdf => "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}
+ ['OT', 'DC', 'XSD'].each{|p| prefixes[p.downcase.to_sym] = eval("RDF::#{p}.to_s") }
+ string = RDF::N3::Writer.for(format).buffer(:prefixes => prefixes) do |writer|
rdf.each{|statement| writer << statement}
end
else
@@ -164,10 +186,15 @@ module OpenTox
File.join(four_store_uri, "sparql") + '/'
end
+ def self.update_uri
+ File.join(four_store_uri, "update") + '/'
+ end
+
def self.data_uri uri
File.join(four_store_uri, "data","?graph=#{uri}")
end
+=begin
def self.parse_sparql_xml_results(xml)
results = []
doc = REXML::Document.new(REXML::Source.new(xml))
@@ -183,6 +210,7 @@ module OpenTox
end
results
end
+=end
end
end