Class: RDF::Query

Inherits:
Object show all
Defined in:
lib/sparql/algebra/extensions.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean

Equivalence for Queries: Same Patterns Same Context

Returns:

  • (Boolean)


404
405
406
407
# File 'lib/sparql/algebra/extensions.rb', line 404

def ==(other)
  # FIXME: this should be graph_name == other.graph_name
  other.is_a?(RDF::Query) && patterns == other.patterns && graph_name == graph_name
end

#bind(solution) ⇒ self

Binds the pattern to a solution, making it no longer variable if all variables are resolved to bound variables

Parameters:

Returns:

  • (self)


471
472
473
474
# File 'lib/sparql/algebra/extensions.rb', line 471

def bind(solution)
  patterns.each {|p| p.bind(solution)}
  self
end

#executable?Boolean

Returns true as this is executable.

Returns:

  • (Boolean)

    true



532
# File 'lib/sparql/algebra/extensions.rb', line 532

def executable?; true; end

#ndvarsArray<RDF::Query::Variable>

Return the non-destinguished variables contained within patterns and graph name



497
498
499
# File 'lib/sparql/algebra/extensions.rb', line 497

def ndvars
  vars.reject(&:distinguished?)
end

#optimize!(**options) ⇒ self

Optimize the query, removing lexical shortcuts in URIs

Returns:

  • (self)

See Also:



514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/sparql/algebra/extensions.rb', line 514

def optimize!(**options)
  @patterns = @patterns.map do |pattern|
    components = pattern.to_quad.map do |term|
      #if term.respond_to?(:lexical=)
      #  term.dup.instance_eval {@lexical = nil; self}
      #else
        term
      #end
    end
    RDF::Query::Pattern.from(components, **pattern.options)
  end
  self.optimize_without_expression!(**options)
end

#optimize_without_expression!Object



508
# File 'lib/sparql/algebra/extensions.rb', line 508

alias_method :optimize_without_expression!, :optimize!

#query_yields_boolean?Boolean

Query results in a boolean result (e.g., ASK)

Returns:

  • (Boolean)


478
479
480
# File 'lib/sparql/algebra/extensions.rb', line 478

def query_yields_boolean?
  false
end

#query_yields_solutions?Boolean

Query results solutions (e.g., SELECT)

Returns:

  • (Boolean)


490
491
492
# File 'lib/sparql/algebra/extensions.rb', line 490

def query_yields_solutions?
  true
end

#query_yields_statements?Boolean

Query results statements (e.g., CONSTRUCT, DESCRIBE, CREATE)

Returns:

  • (Boolean)


484
485
486
# File 'lib/sparql/algebra/extensions.rb', line 484

def query_yields_statements?
  false
end

#rewrite(&block) ⇒ SPARQL::Algebra::Expression

Don't do any more rewriting

Returns:



412
413
414
# File 'lib/sparql/algebra/extensions.rb', line 412

def rewrite(&block)
  self
end

#to_sparql(top_level: true, filter_ops: [], **options) ⇒ String

Returns a partial SPARQL grammar for this query.

Parameters:

  • top_level (Boolean) (defaults to: true)

    (true) Treat this as a top-level, generating SELECT ... WHERE {}

  • filter_ops (Array<Operator>) (defaults to: [])

    ([]) Filter Operations

Returns:

  • (String)


441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/sparql/algebra/extensions.rb', line 441

def to_sparql(top_level: true, filter_ops: [], **options)
  str = @patterns.map do |e|
    e.to_sparql(top_level: false, **options) + " . \n"
  end.join("")
  str = "GRAPH #{graph_name.to_sparql(**options)} {\n#{str}\n}\n" if graph_name
  if top_level
    SPARQL::Algebra::Operator.to_sparql(str, filter_ops: filter_ops, **options)
  else
    # Filters
    filter_ops.each do |op|
      str << "\nFILTER (#{op.to_sparql(**options)}) ."
    end

    # Extensons
    extensions = options.fetch(:extensions, [])
    extensions.each do |as, expression|
      v = expression.to_sparql(**options)
      pp = RDF::Query::Variable.new(as).to_sparql(**options)
      str << "\nBIND (" << v << " AS " << pp << ") ."
    end
    str = "{#{str}}" unless filter_ops.empty? && extensions.empty?
    str
  end
end

#to_sxp_binArray

Transform Query into an Array form of an SSE

If Query has the as_container option set, serialize as Quads Otherwise, If Query is named, serialize as a GroupGraphPattern. Otherise, serialize as a BGP

Returns:



423
424
425
426
427
428
429
430
# File 'lib/sparql/algebra/extensions.rb', line 423

def to_sxp_bin
  if options[:as_container]
    [:graph, graph_name] + [patterns.map(&:to_sxp_bin)]
  else
    res = [:bgp] + patterns.map(&:to_sxp_bin)
    (graph_name ? [:graph, graph_name, res] : res)
  end
end

#varsArray<RDF::Query::Variable>

Return the variables contained within patterns and graph name



504
505
506
# File 'lib/sparql/algebra/extensions.rb', line 504

def vars
  variables.values
end