Class: SPARQL::Client::Query

Inherits:
RDF::Query show all
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb

Overview

A SPARQL query builder.

Examples:

Iterating over all found solutions

query.each_solution { |solution| puts solution.inspect }

Defined Under Namespace

Classes: Filter

Instance Attribute Summary collapse

Attributes inherited from RDF::Query

#patterns, #variables

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RDF::Query

#+, #<<, #==, Solutions, #apply_graph_name, #default?, #dup, #empty?, execute, #failed?, #graph_name, #graph_name=, #matched?, #named?, #ndvars, #node?, #optimize, #optimize!, #pattern, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #rewrite, #to_sxp_bin, #unnamed?, #valid?, #validate!, #variable?, #vars

Methods included from RDF::Enumerable

add_entailment, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_subject, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_a, #to_h, #to_set, #triples, #valid?, #validate!

Methods included from RDF::Util::Aliasing::LateBound

#alias_method

Methods included from RDF::Isomorphic

#bijection_to, #isomorphic_with?

Methods included from RDF::Countable

#count, #empty?

Constructor Details

#self.construct(*variables, options) ⇒ Query

Returns a new instance of Query

Parameters:

Options Hash (options):

  • :count (Hash{Symbol => Symbol})

    Contents are symbols relating a variable described within the query, to the projected variable.

Parameters:

Yields:

Yield Parameters:



113
114
115
116
117
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 113

def initialize(form = :ask, options = {}, &block)
  @subqueries = []
  @form = form.respond_to?(:to_sym) ? form.to_sym : form.to_s.to_sym
  super([], options, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Attribute Details

#form:select, ... (readonly)

The form of the query.

Returns:

  • (:select, :ask, :construct, :describe)

See Also:



14
15
16
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 14

def form
  @form
end

#optionsHash{Symbol => Object} (readonly)

Returns:



18
19
20
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 18

def options
  @options
end

#valuesArray<[key, RDF::Value]> (readonly)

Values returned from previous query.

Returns:



24
25
26
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 24

def values
  @values
end

Class Method Details

.ask(options = {}) ⇒ Query

Creates a boolean ASK query.

Examples:

ASK WHERE { ?s ?p ?o . }

Query.ask.where([:s, :p, :o])

Parameters:

Returns:

See Also:



35
36
37
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 35

def self.ask(options = {})
  self.new(:ask, options)
end

.self.construct(*variables, options) ⇒ Query

Creates a graph CONSTRUCT query.

Examples:

CONSTRUCT { ?s ?p ?o . } WHERE { ?s ?p ?o . }

Query.construct([:s, :p, :o]).where([:s, :p, :o])

Parameters:

Returns:

Parameters:

Returns:

See Also:



97
98
99
100
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 97

def self.construct(*patterns)
  options = patterns.last.is_a?(Hash) ? patterns.pop : {}
  self.new(:construct, options).construct(*patterns) # FIXME
end

.self.describe(*variables, options) ⇒ Query

Creates a DESCRIBE query.

Examples:

DESCRIBE * WHERE { ?s ?p ?o . }

Query.describe.where([:s, :p, :o])

Parameters:

Returns:

Parameters:

Returns:

See Also:



78
79
80
81
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 78

def self.describe(*variables)
  options = variables.last.is_a?(Hash) ? variables.pop : {}
  self.new(:describe, options).describe(*variables)
end

.self.select(*variables, options) ⇒ Query

Creates a tuple SELECT query.

Examples:

SELECT * WHERE { ?s ?p ?o . }

Query.select.where([:s, :p, :o])

SELECT ?s WHERE ?p ?o .

Query.select(:s).where([:s, :p, :o])

SELECT COUNT(?uri as ?c) WHERE a owl:Class

Query.select(count: {uri: :c}).where([:uri, RDF.type, RDF::OWL.Class])

Parameters:

Returns:

Parameters:

Returns:

See Also:



59
60
61
62
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 59

def self.select(*variables)
  options = variables.last.is_a?(Hash) ? variables.pop : {}
  self.new(:select, options).select(*variables)
end

Instance Method Details

#asc(var) ⇒ Query

Examples:

SELECT * WHERE { ?s ?p ?o . } ORDER BY ASC(?o)

query.select.where([:s, :p, :o]).order.asc(:o)
query.select.where([:s, :p, :o]).asc(:o)

Parameters:

Returns:

See Also:



269
270
271
272
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 269

def asc(var)
  (options[:order_by] ||= []) << {var => :asc}
  self
end

#askQuery

Examples:

ASK WHERE { ?s ?p ?o . }

query.ask.where([:s, :p, :o])

Returns:

See Also:



125
126
127
128
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 125

def ask
  @form = :ask
  self
end

#construct(*patterns) ⇒ Query

Examples:

CONSTRUCT { ?s ?p ?o . } WHERE { ?s ?p ?o . }

query.construct([:s, :p, :o]).where([:s, :p, :o])

Parameters:

Returns:

See Also:



169
170
171
172
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 169

def construct(*patterns)
  options[:template] = build_patterns(patterns)
  self
end

#desc(var) ⇒ Query

Examples:

SELECT * WHERE { ?s ?p ?o . } ORDER BY DESC(?o)

query.select.where([:s, :p, :o]).order.desc(:o)
query.select.where([:s, :p, :o]).desc(:o)

Parameters:

Returns:

See Also:



282
283
284
285
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 282

def desc(var)
  (options[:order_by] ||= []) << {var => :desc}
  self
end

#describe(*variables) ⇒ Query

Examples:

DESCRIBE * WHERE { ?s ?p ?o . }

query.describe.where([:s, :p, :o])

Parameters:

Returns:

See Also:



155
156
157
158
159
160
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 155

def describe(*variables)
  @values = variables.map { |var|
    [var, var.is_a?(RDF::URI) ? var : RDF::Query::Variable.new(var)]
  }
  self
end

#distinct(state = true) ⇒ Query

Examples:

SELECT DISTINCT ?s WHERE { ?s ?p ?o . }

query.select(:s).distinct.where([:s, :p, :o])

Returns:

See Also:



307
308
309
310
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 307

def distinct(state = true)
  options[:distinct] = state
  self
end

#each_solution {|solution| ... } ⇒ Enumerator

Enumerates over each matching query solution.

Yields:

  • (solution)

Yield Parameters:

Returns:



588
589
590
591
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 588

def each_solution(&block)
  @solutions = result
  super
end

#each_statement {|statement| ... } ⇒ Enumerator

Yields:

  • (statement)

Yield Parameters:

Returns:



579
580
581
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 579

def each_statement(&block)
  result.each_statement(&block)
end

#executeObject

Returns:

Raises:

  • (NotImplementedError)


601
602
603
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 601

def execute
  raise NotImplementedError
end

#expects_statements?Boolean

Returns expects_statements?

Returns:

  • (Boolean)

    expects_statements?



533
534
535
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 533

def expects_statements?
  [:construct, :describe].include?(form)
end

#false?Boolean

Returns:

  • (Boolean)


565
566
567
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 565

def false?
  !true?
end

#filter(string) ⇒ Query

Examples:

ASK WHERE { ?s ?p ?o . FILTER(regex(?s, 'Abiline, Texas')) }

query.ask.where([:s, :p, :o]).filter("regex(?s, 'Abiline, Texas')")

Returns:



547
548
549
550
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 547

def filter(string)
  ((options[:filters] ||= []) << Filter.new(string)) if string and not string.empty?
  self
end

#from(uri) ⇒ Query

Examples:

query.select.from(RDF::URI.new(a)).where([:s, :p, :o])

Parameters:

Returns:

See Also:



181
182
183
184
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 181

def from(uri)
  options[:from] = uri
  self
end

#graph(graph_uri_or_var) ⇒ Query

Examples:

SELECT * WHERE { GRAPH ?g { ?s ?p ?o . } }

query.select.graph(:g).where([:s, :p, :o])

Parameters:

Returns:

See Also:



329
330
331
332
333
334
335
336
337
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 329

def graph(graph_uri_or_var)
  options[:graph] = case graph_uri_or_var
    when Symbol then RDF::Query::Variable.new(graph_uri_or_var)
    when String then RDF::URI(graph_uri_or_var)
    when RDF::Value then graph_uri_or_var
    else raise ArgumentError
  end
  self
end

#group(*variables) ⇒ Query Also known as: group_by

Examples:

SELECT ?s WHERE { ?s ?p ?o . } GROUP BY ?s

query.select(:s).where([:s, :p, :o]).group_by(:s)

Parameters:

Returns:

See Also:



294
295
296
297
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 294

def group(*variables)
  options[:group_by] = variables
  self
end

#inspectString

Returns a developer-friendly representation of this query.

Returns:



740
741
742
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 740

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s)
end

#inspect!

This method returns an undefined value.

Outputs a developer-friendly representation of this query to stderr.



731
732
733
734
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 731

def inspect!
  warn(inspect)
  self
end

#limit(length) ⇒ Query

Examples:

SELECT * WHERE { ?s ?p ?o . } LIMIT 10

query.select.where([:s, :p, :o]).limit(10)

Parameters:

Returns:

See Also:



357
358
359
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 357

def limit(length)
  slice(nil, length)
end

#minus(*patterns) {|query| ... } ⇒ Query

The block form can be used for more complicated queries, using the select form (note, use either block or argument forms, not both):

Examples:

SELECT * WHERE { ?book dc:title ?title . MINUS { ?book dc11:title ?title } }

query.select.where([:book, RDF::Vocab::DC.title, :title]).
  minus([:book, RDF::Vocab::DC11.title, :title])

SELECT * WHERE { ?book dc:title ?title MINUS { ?book dc11:title ?title . FILTER(langmatches(lang(?title), 'EN')) } }

query1 = SPARQL::Client::Query.select.
  where([:book, RDF::Vocab::DC11.title, :title]).
  filter("langmatches(?title, 'en')")
query.select.where([:book, RDF::Vocab::DC.title, :title]).minus(query1)

SELECT * WHERE { ?book dc:title ?title MINUS { ?book dc11:title ?title . FILTER(langmatches(lang(?title), 'EN'))} }

query1 = SPARQL::Client::Query.select.where([:book, RDF::Vocab::DC11.title, :title]).filter("langmatches(?title, 'en')")
query.select.where([:book, RDF::Vocab::DC.title, :title]).minus do |q|
  q.select.
    where([:book, RDF::Vocab::DC11.title, :title]).
    filter("langmatches(?title, 'en')")
end

Parameters:

Yields:

  • (query)

    Yield form with or without argument; without an argument, evaluates within the query.

Yield Parameters:

Returns:

See Also:



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 506

def minus(*patterns, &block)
  options[:minuses] ||= []

  if block_given?
    raise ArgumentError, "#minus requires either arguments or a block, not both." unless patterns.empty?
    # Evaluate calls in a new query instance
    query = self.class.select
    case block.arity
      when 1 then block.call(query)
      else query.instance_eval(&block)
    end
    options[:minuses] << query
  elsif patterns.all? {|p| p.is_a?(SPARQL::Client::Query)}
    # With argument form, all must be patterns or queries
    options[:minuses] += patterns
  elsif patterns.all? {|p| p.is_a?(Array)}
    # With argument form, all must be patterns, or queries
    options[:minuses] << self.class.select.where(*patterns)
  else
    raise ArgumentError, "#minus arguments are triple patters or queries, not both."
  end

  self
end

#offset(start) ⇒ Query

Examples:

SELECT * WHERE { ?s ?p ?o . } OFFSET 100

query.select.where([:s, :p, :o]).offset(100)

Parameters:

Returns:

See Also:



346
347
348
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 346

def offset(start)
  slice(start, nil)
end

#optional(*patterns) {|query| ... } ⇒ Query

The block form can be used for adding filters:

Examples:

SELECT * WHERE { ?s ?p ?o . OPTIONAL { ?s a ?o . ?s <http://purl.org/dc/terms/abstract\> ?o . } }

query.select.where([:s, :p, :o]).
  optional([:s, RDF.type, :o], [:s, RDF::Vocab::DC.abstract, :o])

ASK WHERE { ?s ?p ?o . OPTIONAL { ?s ?p ?o . FILTER(regex(?s, 'Abiline, Texas'))} }

query.ask.where([:s, :p, :o]).optional([:s, :p, :o]) do
  filter("regex(?s, 'Abiline, Texas')")
end

Parameters:

Yields:

  • (query)

    Yield form with or without argument; without an argument, evaluates within the query.

Yield Parameters:

Returns:

See Also:



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 407

def optional(*patterns, &block)
  (options[:optionals] ||= []) << build_patterns(patterns)

  if block_given?
    # Steal options[:filters]
    query_filters = options[:filters]
    options[:filters] = []
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
    options[:optionals].last.concat(options[:filters])
    options[:filters] = query_filters
  end

  self
end

#order(*variables) ⇒ Query Also known as: order_by

Examples:

SELECT * WHERE { ?s ?p ?o . } ORDER BY ?o

query.select.where([:s, :p, :o]).order(:o)
query.select.where([:s, :p, :o]).order_by(:o)

SELECT * WHERE { ?s ?p ?o . } ORDER BY ?o ?p

query.select.where([:s, :p, :o]).order_by(:o, :p)

SELECT * WHERE { ?s ?p ?o . } ORDER BY ASC(?o) DESC(?p)

query.select.where([:s, :p, :o]).order_by(:o => :asc, :p => :desc)

Parameters:

Returns:

See Also:



254
255
256
257
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 254

def order(*variables)
  options[:order_by] = variables
  self
end

#prefix(string) ⇒ Query

Examples:

PREFIX dc: http://purl.org/dc/elements/1.1/ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT * WHERE { ?s ?p ?o . }

query.select.
  prefix(dc: RDF::URI("http://purl.org/dc/elements/1.1/")).
  prefix(foaf: RDF::URI("http://xmlns.com/foaf/0.1/")).
  where([:s, :p, :o])

Returns:

See Also:



383
384
385
386
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 383

def prefix(string)
  (options[:prefixes] ||= []) << string
  self
end

#reduced(state = true) ⇒ Query

Examples:

SELECT REDUCED ?s WHERE { ?s ?p ?o . }

query.select(:s).reduced.where([:s, :p, :o])

Returns:

See Also:



318
319
320
321
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 318

def reduced(state = true)
  options[:reduced] = state
  self
end

#resultObject

Returns:



595
596
597
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 595

def result
  @result ||= execute
end

#select(*variables) ⇒ Query

Examples:

SELECT * WHERE { ?s ?p ?o . }

query.select.where([:s, :p, :o])

SELECT ?s WHERE ?p ?o .

query.select(:s).where([:s, :p, :o])

SELECT COUNT(?uri as ?c) WHERE a owl:Class

query.select(count: {uri: :c}).where([:uri, RDF.type, RDF::OWL.Class])

Parameters:

Returns:

See Also:



143
144
145
146
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 143

def select(*variables)
  @values = variables.map { |var| [var, RDF::Query::Variable.new(var)] }
  self
end

#slice(start, length) ⇒ Query

Examples:

SELECT * WHERE { ?s ?p ?o . } OFFSET 100 LIMIT 10

query.select.where([:s, :p, :o]).slice(100, 10)

Parameters:

Returns:



368
369
370
371
372
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 368

def slice(start, length)
  options[:offset] = start.to_i if start
  options[:limit] = length.to_i if length
  self
end

#solutionsEnumerable<RDF::Query::Solution>

Returns:



571
572
573
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 571

def solutions
  result
end

#to_sString

Returns the string representation of this query.

Returns:



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 609

def to_s
  buffer = [form.to_s.upcase]

  case form
    when :select, :describe
      only_count = values.empty? && options[:count]
      buffer << 'DISTINCT' if options[:distinct] and not only_count
      buffer << 'REDUCED'  if options[:reduced]
      buffer << ((values.empty? and not options[:count]) ? '*' : values.map { |v| SPARQL::Client.serialize_value(v[1]) }.join(' '))
      if options[:count]
        options[:count].each do |var, count|
          buffer << '( COUNT(' + (options[:distinct] ? 'DISTINCT ' : '') +
            (var.is_a?(String) ? var : "?#{var}") + ') AS ' + (count.is_a?(String) ? count : "?#{count}") + ' )'
        end
      end
    when :construct
      buffer << '{'
      buffer += SPARQL::Client.serialize_patterns(options[:template])
      buffer << '}'
  end

  buffer << "FROM #{SPARQL::Client.serialize_value(options[:from])}" if options[:from]

  unless patterns.empty? && form == :describe
    buffer += self.to_s_ggp.unshift('WHERE')
  end

  options.fetch(:unions, []).each do |query|
    buffer += query.to_s_ggp.unshift('UNION')
  end

  if options[:group_by]
    buffer << 'GROUP BY'
    buffer += options[:group_by].map { |var| var.is_a?(String) ? var : "?#{var}" }
  end

  if options[:order_by]
    buffer << 'ORDER BY'
    options[:order_by].map { |elem|
      case elem
        # .order_by({ :var1 => :asc, :var2 => :desc})
        when Hash
          elem.each { |key, val|
            # check provided values
            if !key.is_a?(Symbol)
              raise ArgumentError, 'keys of hash argument must be a Symbol'
            elsif !val.is_a?(Symbol) || (val != :asc && val != :desc)
              raise ArgumentError, 'values of hash argument must either be `:asc` or `:desc`'
            end
            buffer << "#{val == :asc ? 'ASC' : 'DESC'}(?#{key})"
          }
        # .order_by([:var1, :asc], [:var2, :desc])
        when Array
          # check provided values
          if elem.length != 2
            raise ArgumentError, 'array argument must specify two elements'
          elsif !elem[0].is_a?(Symbol)
            raise ArgumentError, '1st element of array argument must contain a Symbol'
          elsif !elem[1].is_a?(Symbol) || (elem[1] != :asc && elem[1] != :desc)
            raise ArgumentError, '2nd element of array argument must either be `:asc` or `:desc`'
          end
          buffer << "#{elem[1] == :asc ? 'ASC' : 'DESC'}(?#{elem[0]})"
        # .order_by(:var1, :var2)
        when Symbol
          buffer << "?#{elem}"
        # .order_by('ASC(?var1) DESC(?var2)')
        when String
          buffer << elem
        else
          raise ArgumentError, 'argument provided to `order()` must either be an Array, Symbol or String'
      end
    }
  end

  buffer << "OFFSET #{options[:offset]}" if options[:offset]
  buffer << "LIMIT #{options[:limit]}"   if options[:limit]
  options[:prefixes].reverse.each { |e| buffer.unshift("PREFIX #{e}") } if options[:prefixes]

  buffer.join(' ')
end

#true?Boolean

Returns:

  • (Boolean)


554
555
556
557
558
559
560
561
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 554

def true?
  case result
    when TrueClass, FalseClass then result
    when RDF::Literal::Boolean then result.true?
    when Enumerable then !result.empty?
    else false
  end
end

#union(*patterns) {|query| ... } ⇒ Query

The block form can be used for more complicated queries, using the select form (note, use either block or argument forms, not both):

Examples:

SELECT * WHERE { ?book dc:title ?title } UNION { ?book dc11:title ?title }

query.select.where([:book, RDF::Vocab::DC.title, :title]).
  union([:book, RDF::Vocab::DC11.title, :title])

SELECT * WHERE { ?book dc:title ?title } UNION { ?book dc11:title ?title . FILTER(langmatches(lang(?title), 'EN'))}

query1 = SPARQL::Client::Query.select.
  where([:book, RDF::Vocab::DC11.title, :title]).
  filter("langmatches(?title, 'en')")
query.select.where([:book, RDF::Vocab::DC.title, :title]).union(query1)

SELECT * WHERE { ?book dc:title ?title } UNION { ?book dc11:title ?title . FILTER(langmatches(lang(?title), 'EN'))}

query1 = SPARQL::Client::Query.select.where([:book, RDF::Vocab::DC11.title, :title]).filter("langmatches(?title, 'en')")
query.select.where([:book, RDF::Vocab::DC.title, :title]).union do |q|
  q.select.
    where([:book, RDF::Vocab::DC11.title, :title]).
    filter("langmatches(?title, 'en')")
end

Parameters:

Yields:

  • (query)

    Yield form with or without argument; without an argument, evaluates within the query.

Yield Parameters:

Returns:

See Also:



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 453

def union(*patterns, &block)
  options[:unions] ||= []

  if block_given?
    raise ArgumentError, "#union requires either arguments or a block, not both." unless patterns.empty?
    # Evaluate calls in a new query instance
    query = self.class.select
    case block.arity
      when 1 then block.call(query)
      else query.instance_eval(&block)
    end
    options[:unions] << query
  elsif patterns.all? {|p| p.is_a?(SPARQL::Client::Query)}
    # With argument form, all must be patterns or queries
    options[:unions] += patterns
  elsif patterns.all? {|p| p.is_a?(Array)}
    # With argument form, all must be patterns, or queries
    options[:unions] << self.class.select.where(*patterns)
  else
    raise ArgumentError, "#union arguments are triple patters or queries, not both."
  end

  self
end

#where(*patterns_queries) {|query| ... } ⇒ Query Also known as: whether

Block form can be used for chaining calls in addition to creating sub-select queries.

Examples:

SELECT * WHERE { ?s ?p ?o . }

query.select.where([:s, :p, :o])
query.select.whether([:s, :p, :o])

SELECT * WHERE { { SELECT * WHERE { ?s ?p ?o . } } . ?s ?p ?o . }

subquery = query.select.where([:s, :p, :o])
query.select.where([:s, :p, :o], subquery)

SELECT * WHERE { { SELECT * WHERE { ?s ?p ?o . } } . ?s ?p ?o . }

query.select.where([:s, :p, :o]) do |q|
  q.select.where([:s, :p, :o])
end

SELECT * WHERE { ?s ?p ?o . } ORDER BY ?o

query.select.where([:s, :p, :o]) do
  order(:o)
end

Parameters:

Yields:

  • (query)

    Yield form with or without argument; without an argument, evaluates within the query.

Yield Parameters:

  • query (SPARQL::Client::Query)

    Actually a delegator to query. Methods other than #select are evaluated against self. For #select, a new Query is created, and the result added as a subquery.

Returns:

See Also:



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/sparql-client-f33da4e3600f/lib/sparql/client/query.rb', line 214

def where(*patterns_queries, &block)
  subqueries, patterns = patterns_queries.partition {|pq| pq.is_a? SPARQL::Client::Query}
  @patterns += build_patterns(patterns)
  @subqueries += subqueries

  if block_given?
    decorated_query = WhereDecorator.new(self)
    case block.arity
      when 1 then block.call(decorated_query)
      else decorated_query.instance_eval(&block)
    end
  end
  self
end