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)


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

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

#executable?Boolean

Returns true as this is executable.

Returns:

  • (Boolean)

    true



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

def executable?; true; end

#mergable?(other) ⇒ Boolean

Two queries can be merged if they share the same graph_name

Parameters:

Returns:

  • (Boolean)


436
437
438
# File 'lib/sparql/algebra/extensions.rb', line 436

def mergable?(other)
  other.is_a?(RDF::Query) && self.graph_name == other.graph_name
end

#merge(other) ⇒ RDF::Query

Two queries are merged by

Parameters:

Returns:

Raises:

  • (ArgumentError)


444
445
446
447
# File 'lib/sparql/algebra/extensions.rb', line 444

def merge(other)
  raise ArgumentError, "Can't merge with #{other.class}" unless mergable?(other)
  self.dup.tap {|q| q.instance_variable_set(:@patterns, q.patterns + other.patterns)}
end

#ndvarsArray<RDF::Query::Variable>

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



514
515
516
# File 'lib/sparql/algebra/extensions.rb', line 514

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

#optimize!(**options) ⇒ self

Optimize the query, removing lexical shortcuts in URIs

Returns:

  • (self)

See Also:



531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/sparql/algebra/extensions.rb', line 531

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



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

alias_method :optimize_without_expression!, :optimize!

#query_yields_boolean?Boolean

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

Returns:

  • (Boolean)


495
496
497
# File 'lib/sparql/algebra/extensions.rb', line 495

def query_yields_boolean?
  false
end

#query_yields_solutions?Boolean

Query results solutions (e.g., SELECT)

Returns:

  • (Boolean)


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

def query_yields_solutions?
  true
end

#query_yields_statements?Boolean

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

Returns:

  • (Boolean)


501
502
503
# File 'lib/sparql/algebra/extensions.rb', line 501

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)


458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/sparql/algebra/extensions.rb', line 458

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



521
522
523
# File 'lib/sparql/algebra/extensions.rb', line 521

def vars
  variables.values
end