Class: Query

Inherits:
Object
  • Object
show all
Defined in:
lib/active_rdf/queryengine/query.rb

Overview

Represents a query on a datasource, abstract representation of SPARQL features. Query is passed to federation manager or adapter for execution on data source. In all clauses symbols represent variables: Query.new.select(:s).where(:s,:p,:o).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_rdf/queryengine/query.rb', line 13

def initialize
	distinct = false
	limit = nil
	offset = nil
	@select_clauses = []
	@where_clauses = []
	@sort_clauses = []
   @filter_clauses = []
	@keywords = {}
	@reasoning = true
   @reverse_sort_clauses = []
end

Instance Attribute Details

#filter_clausesObject (readonly)

Returns the value of attribute filter_clauses.



9
10
11
# File 'lib/active_rdf/queryengine/query.rb', line 9

def filter_clauses
  @filter_clauses
end

#keywordsObject (readonly)

Returns the value of attribute keywords.



9
10
11
# File 'lib/active_rdf/queryengine/query.rb', line 9

def keywords
  @keywords
end

#limitsObject (readonly)

Returns the value of attribute limits.



9
10
11
# File 'lib/active_rdf/queryengine/query.rb', line 9

def limits
  @limits
end

#offsetsObject (readonly)

Returns the value of attribute offsets.



9
10
11
# File 'lib/active_rdf/queryengine/query.rb', line 9

def offsets
  @offsets
end

#reverse_sort_clausesObject (readonly)

Returns the value of attribute reverse_sort_clauses.



9
10
11
# File 'lib/active_rdf/queryengine/query.rb', line 9

def reverse_sort_clauses
  @reverse_sort_clauses
end

#select_clausesObject (readonly)

Returns the value of attribute select_clauses.



9
10
11
# File 'lib/active_rdf/queryengine/query.rb', line 9

def select_clauses
  @select_clauses
end

#sort_clausesObject (readonly)

Returns the value of attribute sort_clauses.



9
10
11
# File 'lib/active_rdf/queryengine/query.rb', line 9

def sort_clauses
  @sort_clauses
end

#where_clausesObject (readonly)

Returns the value of attribute where_clauses.



9
10
11
# File 'lib/active_rdf/queryengine/query.rb', line 9

def where_clauses
  @where_clauses
end

Instance Method Details

#askObject

Adds variables to ask clause (see SPARQL specification)



45
46
47
48
# File 'lib/active_rdf/queryengine/query.rb', line 45

def ask
	@ask = true
	self
end

#clear_selectObject

Clears the select clauses



27
28
29
30
31
# File 'lib/active_rdf/queryengine/query.rb', line 27

def clear_select
	$activerdflog.debug "cleared select clause"
	@select_clauses = []
	distinct = false
end

#count(*s) ⇒ Object

Adds variables to count clause



58
59
60
61
# File 'lib/active_rdf/queryengine/query.rb', line 58

def count *s
	@count = true
	select(*s)
end

#distinct(*s) ⇒ Object Also known as: select_distinct

Adds variables to select distinct clause



51
52
53
54
# File 'lib/active_rdf/queryengine/query.rb', line 51

def distinct *s
	@distinct = true
	select(*s)
end

#execute(options = {:flatten => false}, &block) ⇒ Object

Executes query on data sources. Either returns result as array (flattened into single value unless specified otherwise) or executes a block (number of block variables should be same as number of select variables)

usage

results = query.execute

usage

query.execute do |s,p,o| … end



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/active_rdf/queryengine/query.rb', line 182

def execute(options={:flatten => false}, &block)
  options = {:flatten => true} if options == :flatten

  if block_given?
    for result in FederationManager.query(self, options)
      yield result
    end
  else
    FederationManager.query(self, options)
  end
end

#filter(*s) ⇒ Object

adds one or more generic filters NOTE: you have to use SPARQL syntax for variables, eg. regex(?s, ‘abc’)



74
75
76
77
78
79
80
# File 'lib/active_rdf/queryengine/query.rb', line 74

def filter *s
  # add filter clauses
  @filter_clauses << s
  @filter_clauses.uniq!

  self
end

#filter_operator(variable, operator, operand) ⇒ Object

adds operator filter one one variable variable is a Ruby symbol that appears in select/where clause, operator is a SPARQL operator (e.g. ‘>’), operand is a SPARQL value (e.g. 15)

Raises:



96
97
98
99
100
# File 'lib/active_rdf/queryengine/query.rb', line 96

def filter_operator(variable, operator, operand)
  raise(ActiveRdfError, "variable must be a Symbol") unless variable.is_a? Symbol

  filter "?#{variable} #{operator} #{operand}"
end

#filter_regexp(variable, regexp) ⇒ Object Also known as: filter_regex

adds regular expression filter on one variable variable is Ruby symbol that appears in select/where clause, regex is Ruby regular expression

Raises:



85
86
87
88
89
90
# File 'lib/active_rdf/queryengine/query.rb', line 85

def filter_regexp(variable, regexp)
  raise(ActiveRdfError, "variable must be a symbol") unless variable.is_a? Symbol
  raise(ActiveRdfError, "regexp must be a ruby regexp") unless regexp.is_a? Regexp

  filter "regex(str(?#{variable}), #{regexp.inspect.gsub('/','"')})"
end

#keyword_where(s, o) ⇒ Object

Adds keyword constraint to the query. You can use all Ferret query syntax in the constraint (e.g. keyword_where(:s,‘eyal|benjamin’)



164
165
166
167
168
169
170
171
172
173
# File 'lib/active_rdf/queryengine/query.rb', line 164

def keyword_where s,o
	@keyword = true
	s = parametrise(s)
	if @keywords.include?(s)
		@keywords[s] = @keywords[s] + ' ' + o
	else
		@keywords[s] = o
	end
	self
end

#lang(variable, tag, exact = false) ⇒ Object

filter variable on specified language tag, e.g. lang(:o, ‘en’) optionally matches exactly on language dialect, otherwise only language-specifier is considered



105
106
107
108
109
110
111
# File 'lib/active_rdf/queryengine/query.rb', line 105

def lang variable, tag, exact=false
  if exact
    filter "lang(?#{variable} = '#{tag}'"
  else
    filter "regex(lang(?#{variable}), '^#{tag.gsub(/_.*/,'')}$')"
  end
end

#limit(i) ⇒ Object

Adds limit clause (maximum number of results to return)



123
124
125
126
# File 'lib/active_rdf/queryengine/query.rb', line 123

def limit(i)
	@limits = i.to_i
	self
end

#offset(i) ⇒ Object

Add offset clause (ignore first n results)



129
130
131
132
# File 'lib/active_rdf/queryengine/query.rb', line 129

def offset(i)
	@offsets = i.to_i
	self
end

#reverse_sort(*s) ⇒ Object

adds reverse sorting predicates



114
115
116
117
118
119
120
# File 'lib/active_rdf/queryengine/query.rb', line 114

def reverse_sort *s
   # add sort clauses without duplicates
	s.each { |clause| @reverse_sort_clauses << parametrise(clause) }
	@reverse_sort_clauses.uniq!

	self
end

#select(*s) ⇒ Object

Adds variables to select clause



34
35
36
37
38
39
40
41
42
# File 'lib/active_rdf/queryengine/query.rb', line 34

def select *s
	@select = true
	s.each do |e|
		@select_clauses << parametrise(e) 
	end
	# removing duplicate select clauses
	@select_clauses.uniq!
	self
end

#sort(*s) ⇒ Object

Adds sort predicates



64
65
66
67
68
69
70
# File 'lib/active_rdf/queryengine/query.rb', line 64

def sort *s
   # add sort clauses without duplicates
	s.each { |clause| @sort_clauses << parametrise(clause) }
	@sort_clauses.uniq!

	self
end

#to_sObject

Returns query string depending on adapter (e.g. SPARQL, N3QL, etc.)



195
196
197
198
199
200
201
# File 'lib/active_rdf/queryengine/query.rb', line 195

def to_s
if ConnectionPool.read_adapters.empty?
	inspect 
else
	ConnectionPool.read_adapters.first.translate(self)
end
end

#to_spObject

Returns SPARQL serialisation of query



204
205
206
207
# File 'lib/active_rdf/queryengine/query.rb', line 204

def to_sp
require 'queryengine/query2sparql'
Query2SPARQL.translate(self)
end

#where(s, p, o, c = nil) ⇒ Object

Adds where clauses (s,p,o) where each constituent is either variable (:s) or an RDFS::Resource. Keyword queries are specified with the special :keyword symbol: Query.new.select(:s).where(:s, :keyword, ‘eyal’)



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/active_rdf/queryengine/query.rb', line 137

def where s,p,o,c=nil
case p
when :keyword
	# treat keywords in where-clauses specially
	keyword_where(s,o)
else
	# remove duplicate variable bindings, e.g.
	# where(:s,type,:o).where(:s,type,:oo) we should remove the second clause, 
	# since it doesn't add anything to the query and confuses the query 
	# generator. 
	# if you construct this query manually, you shouldn't! if your select 
	# variable happens to be in one of the removed clauses: tough luck.

	unless s.respond_to?(:uri) or s.is_a?(Symbol)
		raise(ActiveRdfError, "cannot add a where clause with s #{s}: s must be a resource or a variable")
	end
	unless p.respond_to?(:uri) or p.is_a?(Symbol)
		raise(ActiveRdfError, "cannot add a where clause with p #{p}: p must be a resource or a variable")
	end

	@where_clauses << [s,p,o,c].collect{|arg| parametrise(arg)}
end
  self
end