Class: SPARQL::Algebra::Operator::Modify

Inherits:
SPARQL::Algebra::Operator show all
Includes:
Update
Defined in:
lib/sparql/algebra/operator/modify.rb

Overview

The SPARQL UPDATE modify operator.

Wraps delete/insert

If options contains any of the Protocol attributes, it is treated as if there is a USING or USING NAMED clause inserted.

  • using-graph-uri

  • using-named-graph-uri

[41] Modify ::= ( ‘WITH’ iri )? ( DeleteClause InsertClause? | InsertClause ) UsingClause* ‘WHERE’ GroupGraphPattern

Examples:

SPARQL Grammar

PREFIX     : <http://example.org/> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
DELETE  { ?a foaf:knows ?b }
INSERT { ?b foaf:knows ?a }
WHERE { ?a foaf:knows ?b }

SSE

(prefix ((: <http://example.org/>)
         (foaf: <http://xmlns.com/foaf/0.1/>))
 (update
  (modify
   (bgp (triple ?a foaf:knows ?b))
   (delete ((triple ?a foaf:knows ?b)))
   (insert ((triple ?b foaf:knows ?a)))) ))

See Also:

  • XXX

Constant Summary collapse

NAME =
[:modify]

Constants inherited from SPARQL::Algebra::Operator

ARITY, IsURI, URI

Constants included from Expression

Expression::PATTERN_PARENTS

Instance Attribute Summary

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Update

#graph_name=, #unshift, #variables

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, #base_uri, base_uri, base_uri=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, to_sparql, #to_sxp, #to_sxp_bin, #validate!, #variable?, #variables, #vars

Methods included from Expression

cast, #constant?, #evaluate, extension, extension?, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Constructor Details

This class inherits a constructor from SPARQL::Algebra::Operator

Instance Method Details

#execute(queryable, **options) ⇒ RDF::Queryable

Executes this upate on the given writable graph or repository.

Execute the first operand to get solutions, and apply those solutions to the subsequent operators.

If options contains any of the Protocol attributes, any using clause is removed and a new using clause is added with entries taken from the using-graph-uri and using-named-graph-uri.

It is an error to supply the using-graph-uri or using-named-graph-uri parameters when using this protocol to convey a SPARQL 1.1 Update request that contains an operation that uses the USING, USING NAMED, or WITH clause.

Parameters:

  • queryable (RDF::Queryable)

    the graph or repository to write

  • options (Hash{Symbol => Object})

    any additional keyword options

Options Hash (**options):

  • debug (Boolean)

    Query execution debugging

Returns:

Raises:

  • (IOError)

    If from does not exist, unless the silent operator is present

See Also:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sparql/algebra/operator/modify.rb', line 58

def execute(queryable, **options)
  debug(options) {"Modify"}
  query = operands.shift

  if %i(using-graph-uri using-named-graph-uri).any? {|k| options.key?(k)}
    raise ArgumentError,
      "query contains USING/WITH clause, which is incompatible with using-graph-uri or using-named-graph-uri query parameters" if
      query.is_a?(Operator::Using) || query.is_a?(Operator::With)

    debug("=> Insert USING clause", options)
    defaults = Array(options.delete(:'using-graph-uri')).map {|uri| RDF::URI(uri)}
    named = Array(options.delete(:'using-named-graph-uri')).map {|uri| [:named, RDF::URI(uri)]}
    
    query = Operator::Using.new((defaults + named), query, **options)
  end

  queryable.query(query, **options.merge(depth: options[:depth].to_i + 1)) do |solution|
    debug(options) {"(solution)=>#{solution.inspect}"}

    # Execute each operand with queryable and solution
    operands.each do |op|
      op.execute(queryable, solutions: solution, **options.merge(depth: options[:depth].to_i + 1))
    end
  end
  queryable
end

#to_sparql(**options) ⇒ String

Returns a partial SPARQL grammar for this operator.

Returns:

  • (String)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sparql/algebra/operator/modify.rb', line 90

def to_sparql(**options)
  if operands.first.is_a?(With)
    operands.first.to_sparql(**options)
  else
    # The content of the WHERE clause, may be USING
    content = operands.first.to_sparql(top_level: false, **options)

    # DELETE | INSERT | DELETE INSERT
    str = operands[1..-1].to_sparql(top_level: false, delimiter: "\n", **options) + "\n"

    # Append the WHERE or USING clause
    str << if operands.first.is_a?(Using)
      content
    else
      Operator.to_sparql(content, project: nil, **options)
    end
  end
end