Class: ActiveRDF::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_type = RDFS::Resource) ⇒ Query

Creates a new query. You may pass a different class that is used for “resource” type objects instead of RDFS::Resource



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_rdf/queryengine/query.rb', line 16

def initialize(resource_type = RDFS::Resource)
  @distinct = false
  @select_clauses = []
  @where_clauses = []
  @filter_clauses = {}
  @sort_clauses = []
  @limits = nil
  @offsets = nil
  @keywords = {}
  @reasoning = nil
  @all_types = false
  @nil_clause_idx = -1
  set_resource_class(resource_type)
end

Instance Attribute Details

#filter_clausesObject (readonly)

Returns the value of attribute filter_clauses.



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

def filter_clauses
  @filter_clauses
end

#keywordsObject (readonly)

Returns the value of attribute keywords.



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

def keywords
  @keywords
end

#limitsObject (readonly)

Returns the value of attribute limits.



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

def limits
  @limits
end

#offsetsObject (readonly)

Returns the value of attribute offsets.



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

def offsets
  @offsets
end

#select_clausesObject (readonly)

Returns the value of attribute select_clauses.



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

def select_clauses
  @select_clauses
end

#sort_clausesObject (readonly)

Returns the value of attribute sort_clauses.



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

def sort_clauses
  @sort_clauses
end

#where_clausesObject (readonly)

Returns the value of attribute where_clauses.



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

def where_clauses
  @where_clauses
end

Instance Method Details

#all_types(enabled = true) ⇒ Object

Set query to ignore language & datatypes for objects



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

def all_types(enabled = true)
  @all_types = enabled
  self
end

#askObject

Adds variables to ask clause (see SPARQL specification)



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

def ask
  @ask = true
  self
end

#clear_selectObject

Clears the select clauses



60
61
62
63
64
# File 'lib/active_rdf/queryengine/query.rb', line 60

def clear_select
  ActiveRdfLogger::log_debug "Cleared select clause", self
  @select_clauses = []
  @distinct = false
end

#count(*s) ⇒ Object

Adds variables to count clause



109
110
111
112
# File 'lib/active_rdf/queryengine/query.rb', line 109

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

#datatype(variable, type) ⇒ Object



160
161
162
# File 'lib/active_rdf/queryengine/query.rb', line 160

def datatype(variable, type)
  filter(variable,:datatype,type)
end

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

Adds variables to select distinct clause



102
103
104
105
# File 'lib/active_rdf/queryengine/query.rb', line 102

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



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/active_rdf/queryengine/query.rb', line 226

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

  prepared_query = prepare_query(options)

  if block_given?
    for result in FederationManager.execute(prepared_query, options.merge(:flatten => false))
      yield result
    end
  else
    FederationManager.execute(prepared_query, options)
  end
end

#filter(variable, operator, operand) ⇒ Object

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

Raises:



136
137
138
139
140
# File 'lib/active_rdf/queryengine/query.rb', line 136

def filter(variable, operator, operand)
  raise(ActiveRdfError, "variable must be a Symbol") unless variable.is_a? Symbol
  @filter_clauses[variable] = [operator.to_sym,operand]
  self
end

#initialize_copy(orig) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/active_rdf/queryengine/query.rb', line 48

def initialize_copy(orig)
  # dup the instance variables so we're not messing with the original query's values
  instance_variables.each do |iv|
    orig_val = instance_variable_get(iv)
    case orig_val
    when Array,Hash
      instance_variable_set(iv,orig_val.dup)
    end
  end
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’)



209
210
211
212
213
214
215
216
217
# File 'lib/active_rdf/queryengine/query.rb', line 209

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

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

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



156
157
158
# File 'lib/active_rdf/queryengine/query.rb', line 156

def lang(variable, tag, exact=true)
  filter(variable,:lang,[tag.sub(/^@/,''),exact])
end

#limit(i) ⇒ Object

Adds limit clause (maximum number of results to return)



165
166
167
168
# File 'lib/active_rdf/queryengine/query.rb', line 165

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

#offset(i) ⇒ Object

Add offset clause (ignore first n results)



171
172
173
174
# File 'lib/active_rdf/queryengine/query.rb', line 171

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

#reasoning(bool) ⇒ Object

Request reasoning be performed on query



82
83
84
85
# File 'lib/active_rdf/queryengine/query.rb', line 82

def reasoning(bool)
  @reasoning = truefalse(bool)
  self
end

#reasoning=(bool) ⇒ Object



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

def reasoning=(bool)
  self.reasoning(bool)
end

#reasoning?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/active_rdf/queryengine/query.rb', line 91

def reasoning?
  @reasoning
end

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

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

Raises:



145
146
147
148
149
150
# File 'lib/active_rdf/queryengine/query.rb', line 145

def regexp(variable, regexp)
  raise(ActiveRdfError, "variable must be a symbol") unless variable.is_a? Symbol
  regexp = regexp.source if(regexp.is_a?(Regexp))

  filter(variable, :regexp, regexp)
end

#resource_classObject

This returns the class that is be used for resources, by default this is RDFS::Resource



33
34
35
# File 'lib/active_rdf/queryengine/query.rb', line 33

def resource_class
  @resource_class ||= RDFS::Resource
end

#reverse_sort(*s) ⇒ Object

adds reverse sorting predicates



125
126
127
128
129
130
131
# File 'lib/active_rdf/queryengine/query.rb', line 125

def reverse_sort *s
  s.each do |var| 
    raise(ActiveRdfError, "variable must be a Symbol") unless var.is_a? Symbol
    @sort_clauses << [var,:desc]
  end
  self
end

#select(*s) ⇒ Object

Adds variables to select clause

Raises:



67
68
69
70
71
72
73
# File 'lib/active_rdf/queryengine/query.rb', line 67

def select *s
  raise(ActiveRdfError, "variable must be a Symbol") unless s.all?{|var| var.is_a?(Symbol)}
  @select = true
  # removing duplicate select clauses
  @select_clauses.concat(s).uniq!
  self
end

#set_resource_class(resource_class) ⇒ Object

Sets the resource_class. Any class may be used, however it is required that it can be created using the uri of the resource as it’s only parameter and that it has an ‘uri’ property

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
# File 'lib/active_rdf/queryengine/query.rb', line 40

def set_resource_class(resource_class)
  raise(ArgumentError, "resource_class must be a class") unless(resource_class.class == Class)

  test = resource_class.new("http://uri")
  raise(ArgumentError, "Must have an uri property") unless(test.respond_to?(:uri))
  @resource_class = resource_class
end

#sort(*s) ⇒ Object

Adds sort predicates



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

def sort *s
  s.each do |var| 

    @sort_clauses << [var,:asc]
  end
  self
end

#to_sObject

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



241
242
243
244
245
246
247
# File 'lib/active_rdf/queryengine/query.rb', line 241

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

#to_spObject

Returns SPARQL serialisation of query



250
251
252
253
# File 'lib/active_rdf/queryengine/query.rb', line 250

def to_sp
  require 'queryengine/query2sparql' unless(defined?(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 (or equivalent class). Keyword queries are specified with the special :keyword symbol: Query.new.select(:s).where(:s, :keyword, ‘eyal’)



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/active_rdf/queryengine/query.rb', line 179

def where s,p,o,c=nil
  case p
  when :keyword
    # treat keywords in where-clauses specially
    keyword_where(s,o)
  else
    # give nil clauses a unique variable
    s,p,o = [s,p,o].collect{|clause| clause.nil? ? "nil#{@nil_clause_idx += 1}".to_sym : clause}

    # 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)) and (s.class != RDFS::BNode)
      raise(ActiveRdfError, "Cannot add a where clause with s #{s}: s must be a resource or a variable, is a #{s.class.name}")
    end
    unless (p.respond_to?(:uri) or p.is_a?(Symbol)) and (s.class != RDFS::BNode)
      raise(ActiveRdfError, "Cannot add a where clause with p #{p}: p must be a resource or a variable, is a #{p.class.name}")
    end
    raise(ActiveRdfErrror, "Cannot add a where clause where o is a blank node") if(o.class == RDFS::BNode)

    @where_clauses << [s,p,o,c]
  end
  self
end