activerdf_sparql 1.3.4 → 1.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +5 -0
 - data/lib/activerdf_sparql/sparql.rb +75 -21
 - data/test/test_sparql_adapter.rb +0 -1
 - metadata +2 -2
 
    
        data/CHANGELOG
    CHANGED
    
    | 
         @@ -1,3 +1,8 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            == activerdf_sparql (1.3.5) Mon, 01 Oct 2007 12:03:40 +0200
         
     | 
| 
      
 2 
     | 
    
         
            +
              * support for POSTing SPARQL queres (Edward Benson)
         
     | 
| 
      
 3 
     | 
    
         
            +
              * added experimental sparql query cache: it never expires (Benjamin Heitmann)
         
     | 
| 
      
 4 
     | 
    
         
            +
              * reverted gem dependency to json (Benjamin Heitmann)
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
       1 
6 
     | 
    
         
             
            == activerdf_sparql (1.3.4) Fri, 21 Sep 2007 10:06:57 +0100
         
     | 
| 
       2 
7 
     | 
    
         
             
              * parse bnodes in SPARQL queries
         
     | 
| 
       3 
8 
     | 
    
         | 
| 
         @@ -9,28 +9,38 @@ require "#{File.dirname(__FILE__)}/sparql_result_parser" 
     | 
|
| 
       9 
9 
     | 
    
         
             
            class SparqlAdapter < ActiveRdfAdapter
         
     | 
| 
       10 
10 
     | 
    
         
             
            	$activerdflog.info "loading SPARQL adapter"
         
     | 
| 
       11 
11 
     | 
    
         
             
            	ConnectionPool.register_adapter(:sparql, self)
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
              attr_reader :engine
         
     | 
| 
      
 14 
     | 
    
         
            +
              attr_reader :caching
         
     | 
| 
      
 15 
     | 
    
         
            +
             
     | 
| 
      
 16 
     | 
    
         
            +
              @@sparql_cache = {}
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
              def SparqlAdapter.get_cache
         
     | 
| 
      
 19 
     | 
    
         
            +
                return @@sparql_cache
         
     | 
| 
      
 20 
     | 
    
         
            +
              end
         
     | 
| 
       12 
21 
     | 
    
         | 
| 
       13 
22 
     | 
    
         
             
            	# Instantiate the connection with the SPARQL Endpoint.
         
     | 
| 
       14 
23 
     | 
    
         
             
            	# available parameters:
         
     | 
| 
       15 
24 
     | 
    
         
             
            	# * :url => url: endpoint location e.g. "http://m3pe.org:8080/repositories/test-people"
         
     | 
| 
       16 
25 
     | 
    
         
             
            	# * :results => one of :xml, :json, :sparql_xml
         
     | 
| 
       17 
     | 
    
         
            -
               
     | 
| 
       18 
     | 
    
         
            -
             
     | 
| 
      
 26 
     | 
    
         
            +
              # * :request_method => :get (default) or :post
         
     | 
| 
      
 27 
     | 
    
         
            +
              # * :timeout => timeout in seconds to wait for endpoint response
         
     | 
| 
       19 
28 
     | 
    
         
             
            	def initialize(params = {})	
         
     | 
| 
       20 
29 
     | 
    
         
             
            		@reads = true
         
     | 
| 
       21 
30 
     | 
    
         
             
            		@writes = false
         
     | 
| 
       22 
31 
     | 
    
         | 
| 
       23 
32 
     | 
    
         
             
            		@url = params[:url] || ''
         
     | 
| 
      
 33 
     | 
    
         
            +
                @caching = params[:caching] || false
         
     | 
| 
      
 34 
     | 
    
         
            +
                @timeout = params[:timeout] || 50
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
       24 
36 
     | 
    
         
             
            		@result_format = params[:results] || :json
         
     | 
| 
      
 37 
     | 
    
         
            +
            		raise ActiveRdfError, "Result format unsupported" unless [:xml, :json, :sparql_xml].include? @result_format
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
       25 
39 
     | 
    
         
             
                @engine = params[:engine]
         
     | 
| 
      
 40 
     | 
    
         
            +
            		raise ActiveRdfError, "SPARQL engine unsupported" unless [:yars2, :sesame2, :joseki, :virtuoso].include? @engine
         
     | 
| 
       26 
41 
     | 
    
         | 
| 
       27 
     | 
    
         
            -
                 
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
            		
         
     | 
| 
       30 
     | 
    
         
            -
            		known_formats = [:xml, :json, :sparql_xml]
         
     | 
| 
       31 
     | 
    
         
            -
            		raise ActiveRdfError, "Result format unsupported" unless known_formats.include?(@result_format)
         
     | 
| 
       32 
     | 
    
         
            -
            		
         
     | 
| 
       33 
     | 
    
         
            -
            		$activerdflog.info "SPARQL adapter initialised #{inspect}"
         
     | 
| 
      
 42 
     | 
    
         
            +
                @request_method = params[:request_method] || :get
         
     | 
| 
      
 43 
     | 
    
         
            +
                raise ActiveRdfError, "Request method unsupported" unless [:get,:post].include? @request_method
         
     | 
| 
       34 
44 
     | 
    
         
             
            	end
         
     | 
| 
       35 
45 
     | 
    
         | 
| 
       36 
46 
     | 
    
         
             
            	def size
         
     | 
| 
         @@ -40,27 +50,49 @@ class SparqlAdapter < ActiveRdfAdapter 
     | 
|
| 
       40 
50 
     | 
    
         
             
            	# query datastore with query string (SPARQL), returns array with query results
         
     | 
| 
       41 
51 
     | 
    
         
             
            	# may be called with a block
         
     | 
| 
       42 
52 
     | 
    
         
             
            	def query(query, &block)
         
     | 
| 
       43 
     | 
    
         
            -
            		time = Time.now
         
     | 
| 
       44 
53 
     | 
    
         
             
                qs = Query2SPARQL.translate(query)
         
     | 
| 
       45 
     | 
    
         
            -
             
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                if @caching
         
     | 
| 
      
 56 
     | 
    
         
            +
                   result = query_cache(qs)
         
     | 
| 
      
 57 
     | 
    
         
            +
                   if result.nil?
         
     | 
| 
      
 58 
     | 
    
         
            +
                     $activerdflog.debug "cache miss for query #{qs}"
         
     | 
| 
      
 59 
     | 
    
         
            +
                   else
         
     | 
| 
      
 60 
     | 
    
         
            +
                     $activerdflog.debug "cache hit for query #{qs}"
         
     | 
| 
      
 61 
     | 
    
         
            +
                     return result
         
     | 
| 
      
 62 
     | 
    
         
            +
                   end
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
                result = execute_sparql_query(qs, header(query), &block)
         
     | 
| 
      
 66 
     | 
    
         
            +
                add_to_cache(qs, result) if @caching
         
     | 
| 
      
 67 
     | 
    
         
            +
            		result = [] if result == "timeout"
         
     | 
| 
      
 68 
     | 
    
         
            +
            		return result
         
     | 
| 
       46 
69 
     | 
    
         
             
            	end
         
     | 
| 
       47 
70 
     | 
    
         | 
| 
       48 
71 
     | 
    
         
             
            	# do the real work of executing the sparql query
         
     | 
| 
       49 
72 
     | 
    
         
             
            	def execute_sparql_query(qs, header=nil, &block)
         
     | 
| 
       50 
73 
     | 
    
         
             
                header = header(nil) if header.nil?
         
     | 
| 
       51 
74 
     | 
    
         | 
| 
       52 
     | 
    
         
            -
            		# encoding query string in URL
         
     | 
| 
       53 
     | 
    
         
            -
            		url = "#@url?query=#{CGI.escape(qs)}"
         
     | 
| 
       54 
     | 
    
         
            -
                #url += "&content-type=#{CGI.escape('application/sparql-results+xml')}" if @yars2
         
     | 
| 
       55 
     | 
    
         
            -
                url = url.gsub("DISTINCT", "") if @yars2
         
     | 
| 
       56 
     | 
    
         
            -
            		$activerdflog.debug "querying #{url}"
         
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
       58 
75 
     | 
    
         
             
                # querying sparql endpoint
         
     | 
| 
      
 76 
     | 
    
         
            +
                require 'timeout'
         
     | 
| 
       59 
77 
     | 
    
         
             
            		response = ''
         
     | 
| 
       60 
78 
     | 
    
         
             
            		begin 
         
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
      
 79 
     | 
    
         
            +
                  case @request_method
         
     | 
| 
      
 80 
     | 
    
         
            +
                  when :get
         
     | 
| 
      
 81 
     | 
    
         
            +
                    # encoding query string in URL
         
     | 
| 
      
 82 
     | 
    
         
            +
                    url = "#@url?query=#{CGI.escape(qs)}"
         
     | 
| 
      
 83 
     | 
    
         
            +
                    $activerdflog.debug "GET #{url}"
         
     | 
| 
      
 84 
     | 
    
         
            +
                    timeout(@timeout) do
         
     | 
| 
      
 85 
     | 
    
         
            +
                      open(url, header) do |f|
         
     | 
| 
      
 86 
     | 
    
         
            +
                        response = f.read
         
     | 
| 
      
 87 
     | 
    
         
            +
                      end
         
     | 
| 
      
 88 
     | 
    
         
            +
                    end
         
     | 
| 
      
 89 
     | 
    
         
            +
                  when :post
         
     | 
| 
      
 90 
     | 
    
         
            +
                    $activerdflog.debug "POST #@url with #{qs}"
         
     | 
| 
      
 91 
     | 
    
         
            +
                    response = Net::HTTP.post_form(URI.parse(@url),{'query'=>qs}).body
         
     | 
| 
      
 92 
     | 
    
         
            +
                  end
         
     | 
| 
      
 93 
     | 
    
         
            +
              	rescue Timeout::Error
         
     | 
| 
      
 94 
     | 
    
         
            +
            			raise ActiveRdfError, "timeout on SPARQL endpoint"
         
     | 
| 
      
 95 
     | 
    
         
            +
              	  return "timeout"
         
     | 
| 
       64 
96 
     | 
    
         
             
            		rescue OpenURI::HTTPError => e
         
     | 
| 
       65 
97 
     | 
    
         
             
            			raise ActiveRdfError, "could not query SPARQL endpoint, server said: #{e}"
         
     | 
| 
       66 
98 
     | 
    
         
             
            			return []
         
     | 
| 
         @@ -68,7 +100,6 @@ class SparqlAdapter < ActiveRdfAdapter 
     | 
|
| 
       68 
100 
     | 
    
         
             
            			raise ActiveRdfError, "connection refused on SPARQL endpoint #@url"
         
     | 
| 
       69 
101 
     | 
    
         
             
            			return []
         
     | 
| 
       70 
102 
     | 
    
         
             
            	 	end
         
     | 
| 
       71 
     | 
    
         
            -
                $activerdflog.debug "response:\n#{response}"
         
     | 
| 
       72 
103 
     | 
    
         | 
| 
       73 
104 
     | 
    
         
             
                # we parse content depending on the result format
         
     | 
| 
       74 
105 
     | 
    
         
             
                results = case @result_format
         
     | 
| 
         @@ -92,6 +123,26 @@ class SparqlAdapter < ActiveRdfAdapter 
     | 
|
| 
       92 
123 
     | 
    
         
             
            	end
         
     | 
| 
       93 
124 
     | 
    
         | 
| 
       94 
125 
     | 
    
         
             
            	private
         
     | 
| 
      
 126 
     | 
    
         
            +
              def add_to_cache(query_string, result)
         
     | 
| 
      
 127 
     | 
    
         
            +
                unless result.nil? or result.empty?
         
     | 
| 
      
 128 
     | 
    
         
            +
                  if result == "timeout"
         
     | 
| 
      
 129 
     | 
    
         
            +
                    @@sparql_cache.store(query_string, [])
         
     | 
| 
      
 130 
     | 
    
         
            +
                  else 
         
     | 
| 
      
 131 
     | 
    
         
            +
                    $activerdflog.debug "adding to sparql cache - query: #{query_string}"
         
     | 
| 
      
 132 
     | 
    
         
            +
                    @@sparql_cache.store(query_string, result) 
         
     | 
| 
      
 133 
     | 
    
         
            +
                  end
         
     | 
| 
      
 134 
     | 
    
         
            +
                end
         
     | 
| 
      
 135 
     | 
    
         
            +
              end
         
     | 
| 
      
 136 
     | 
    
         
            +
              
         
     | 
| 
      
 137 
     | 
    
         
            +
              
         
     | 
| 
      
 138 
     | 
    
         
            +
              def query_cache(query_string)
         
     | 
| 
      
 139 
     | 
    
         
            +
                if @@sparql_cache.include?(query_string)
         
     | 
| 
      
 140 
     | 
    
         
            +
                  return @@sparql_cache.fetch(query_string)
         
     | 
| 
      
 141 
     | 
    
         
            +
                else
         
     | 
| 
      
 142 
     | 
    
         
            +
                  return nil
         
     | 
| 
      
 143 
     | 
    
         
            +
                end
         
     | 
| 
      
 144 
     | 
    
         
            +
              end
         
     | 
| 
      
 145 
     | 
    
         
            +
             
     | 
| 
       95 
146 
     | 
    
         
             
            	# constructs correct HTTP header for selected query-result format
         
     | 
| 
       96 
147 
     | 
    
         
             
            	def header(query)
         
     | 
| 
       97 
148 
     | 
    
         
             
            		case @result_format
         
     | 
| 
         @@ -146,4 +197,7 @@ class SparqlAdapter < ActiveRdfAdapter 
     | 
|
| 
       146 
197 
     | 
    
         
             
                  value.to_s
         
     | 
| 
       147 
198 
     | 
    
         
             
                end
         
     | 
| 
       148 
199 
     | 
    
         
             
              end
         
     | 
| 
      
 200 
     | 
    
         
            +
              
         
     | 
| 
      
 201 
     | 
    
         
            +
              
         
     | 
| 
      
 202 
     | 
    
         
            +
              
         
     | 
| 
       149 
203 
     | 
    
         
             
            end
         
     | 
    
        data/test/test_sparql_adapter.rb
    CHANGED
    
    | 
         @@ -56,7 +56,6 @@ class TestSparqlAdapter < Test::Unit::TestCase 
     | 
|
| 
       56 
56 
     | 
    
         
             
                begin
         
     | 
| 
       57 
57 
     | 
    
         
             
                  movies = Query.new.
         
     | 
| 
       58 
58 
     | 
    
         
             
                    select(:title).
         
     | 
| 
       59 
     | 
    
         
            -
                    where(:film, RDF.type, RDFS::Resource.new('http://dbpedia.org/class/yago/film')).
         
     | 
| 
       60 
59 
     | 
    
         
             
                    where(:film, RDFS.label, :title).
         
     | 
| 
       61 
60 
     | 
    
         
             
                    where(:title, RDFS::Resource.new('bif:contains'), 'kill').
         
     | 
| 
       62 
61 
     | 
    
         
             
                    filter_regex(:title, /Kill$/).execute
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -3,8 +3,8 @@ rubygems_version: 0.9.4 
     | 
|
| 
       3 
3 
     | 
    
         
             
            specification_version: 1
         
     | 
| 
       4 
4 
     | 
    
         
             
            name: activerdf_sparql
         
     | 
| 
       5 
5 
     | 
    
         
             
            version: !ruby/object:Gem::Version 
         
     | 
| 
       6 
     | 
    
         
            -
              version: 1.3. 
     | 
| 
       7 
     | 
    
         
            -
            date: 2007- 
     | 
| 
      
 6 
     | 
    
         
            +
              version: 1.3.5
         
     | 
| 
      
 7 
     | 
    
         
            +
            date: 2007-10-01 00:00:00 +02:00
         
     | 
| 
       8 
8 
     | 
    
         
             
            summary: ActiveRDF adapter to SPARQL endpoint
         
     | 
| 
       9 
9 
     | 
    
         
             
            require_paths: 
         
     | 
| 
       10 
10 
     | 
    
         
             
            - lib
         
     |