-
Notifications
You must be signed in to change notification settings - Fork 14
/
query.rb
126 lines (115 loc) · 3.68 KB
/
query.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module SPARQL; module Algebra
##
# A SPARQL algebra query, may be duck-typed as RDF::Query.
#
# Mixin with SPARQL::Algebra::Operator to provide query-like operations on graphs and filters
#
# @abstract
module Query
##
# Prepends an operator.
#
# @param [RDF::Query] query
# a query
# @return [void] self
def unshift(query)
@operands.unshift(query)
self
end
##
# The variables used in this query.
#
# @return [Hash{Symbol => RDF::Query::Variable}]
def variables
operands.inject({}) {|hash, o| o.respond_to?(:variables) ? hash.merge(o.variables) : hash}
end
##
# The solution sequence for this query. This is only set
#
# @return [RDF::Query::Solutions]
attr_reader :solutions
##
# Executes this query on the given `queryable` graph or repository.
#
# @param [RDF::Queryable] queryable
# the graph or repository to query
# @param [Hash{Symbol => Object}] options
# any additional keyword options
# @option options [RDF::Query::Solution] :bindings
# a query solution containing zero or more variable bindings
# @option options [Boolean] :debug
# Query execution debugging
# @option options [RDF::Term, RDF::Query::Variable] :graph_name
# @yield [solution]
# each matching solution, statement or boolean
# @yieldparam [RDF::Statement, RDF::Query::Solution, Boolean] solution
# @yieldreturn [void] ignored
# @return [RDF::Graph, Boolean, RDF::Query::Solutions::Enumerator]
# Note, results may be used with {SPARQL.serialize_results} to obtain appropriate output encoding.
# @raise [NotImplementedError]
# If an attempt is made to perform an unsupported operation
# @see https://www.w3.org/TR/sparql11-query/#sparqlAlgebra
def execute(queryable, **options, &block)
raise NotImplementedError, "#{self.class}#execute(#{queryable})"
end
# Add graph_name to sub-items, unless they already have a graph_name
# @param [RDF::URI, RDF::Query::Variable] value
# @return [RDF::URI, RDF::Query::Variable]
def graph_name=(value)
operands.each do |operand|
operand.graph_name = value if operand.respond_to?(:graph_name) && operand.graph_name != false
end
value
end
##
# Returns `true` if this query did not match when last executed.
#
# When the solution sequence is empty, this method can be used to
# determine whether the query failed to match or not.
#
# @return [Boolean]
# @see #matched?
def failed?
solutions.empty?
end
##
# Returns `true` if this query matched when last executed.
#
# When the solution sequence is empty, this method can be used to
# determine whether the query matched successfully or not.
#
# @return [Boolean]
# @see #failed?
def matched?
!failed?
end
# Determine if this is an empty query, having no operands
def empty?
self.operands.empty?
end
# Query results in a boolean result (e.g., ASK)
# @return [Boolean]
def query_yields_boolean?
false
end
# Query results statements (e.g., CONSTRUCT, DESCRIBE, CREATE)
# @return [Boolean]
def query_yields_statements?
false
end
# Query results solutions (e.g., SELECT)
# @return [Boolean]
def query_yields_solutions?
!(query_yields_boolean? || query_yields_statements?)
end
##
# Enumerates over each matching query solution.
#
# @yield [solution]
# @yieldparam [RDF::Query::Solution] solution
# @return [Enumerator]
def each_solution(&block)
solutions.each(&block)
end
end # Query
end; end # SPARQL::Algebra