Module: RDF::Queryable
- Includes:
- Enumerable
- Included in:
- Graph, Repository
- Defined in:
- lib/rdf/mixin/queryable.rb
Overview
An RDF query mixin.
Classes that include this module must implement an #each method that
yields RDF statements.
Instance Method Summary (collapse)
-
- (RDF::Statement) first(pattern = nil)
Queries
selffor an RDF statement matching the givenpatternand returns that statement if found. -
- (RDF::Literal) first_literal(pattern = nil)
Queries
selffor RDF statements matching the givenpatternand returns the first found object literal. -
- (RDF::Term) first_object(pattern = nil)
Queries
selffor an RDF statement matching the givenpatternand returns the statement's object term. -
- (RDF::URI) first_predicate(pattern = nil)
Queries
selffor an RDF statement matching the givenpatternand returns the statement's predicate term. -
- (RDF::Resource) first_subject(pattern = nil)
Queries
selffor an RDF statement matching the givenpatternand returns the statement's subject term. -
- (Object) first_value(pattern = nil)
Queries
selffor RDF statements matching the givenpatternand returns the value of the first found object literal. -
- (Enumerator) query(pattern) {|statement| ... }
Queries
selffor RDF statements matching the givenpattern. -
- query_execute(query) {|solution| ... }
protected
Queries
selfusing the given basic graph pattern (BGP) query, yielding each matched solution to the given block. -
- query_pattern(pattern) {|statement| ... }
protected
Queries
selffor RDF statements matching the givenpattern, yielding each matched statement to the given block.
Instance Method Details
- (RDF::Statement) first - (RDF::Statement) first(pattern)
Queries self for an RDF statement matching the given pattern and
returns that statement if found.
Returns nil if no statements match pattern.
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rdf/mixin/queryable.rb', line 144 def first(pattern = nil) if pattern query(pattern) do |statement| return statement end return nil else super() end end |
- (RDF::Literal) first_literal - (RDF::Literal) first_literal(pattern)
Queries self for RDF statements matching the given pattern and
returns the first found object literal.
Returns nil if no statements match pattern or if none of the found
statements have a literal as their object term.
237 238 239 240 241 242 |
# File 'lib/rdf/mixin/queryable.rb', line 237 def first_literal(pattern = nil) __send__(*(pattern ? [:query, pattern] : [:each])) do |statement| return statement.object if statement.object.is_a?(RDF::Literal) end return nil end |
- (RDF::Term) first_object - (RDF::Term) first_object(pattern)
Queries self for an RDF statement matching the given pattern and
returns the statement's object term.
Returns nil if no statements match pattern.
214 215 216 217 218 219 |
# File 'lib/rdf/mixin/queryable.rb', line 214 def first_object(pattern = nil) __send__(*(pattern ? [:query, pattern] : [:each])) do |statement| return statement.object end return nil end |
- (RDF::URI) first_predicate - (RDF::URI) first_predicate(pattern)
Queries self for an RDF statement matching the given pattern and
returns the statement's predicate term.
Returns nil if no statements match pattern.
192 193 194 195 196 197 |
# File 'lib/rdf/mixin/queryable.rb', line 192 def first_predicate(pattern = nil) __send__(*(pattern ? [:query, pattern] : [:each])) do |statement| return statement.predicate end return nil end |
- (RDF::Resource) first_subject - (RDF::Resource) first_subject(pattern)
Queries self for an RDF statement matching the given pattern and
returns the statement's subject term.
Returns nil if no statements match pattern.
170 171 172 173 174 175 |
# File 'lib/rdf/mixin/queryable.rb', line 170 def first_subject(pattern = nil) __send__(*(pattern ? [:query, pattern] : [:each])) do |statement| return statement.subject end return nil end |
- (Object) first_value - (Object) first_value(pattern)
Queries self for RDF statements matching the given pattern and
returns the value of the first found object literal.
Returns nil if no statements match pattern or if none of the found
statements have a literal as their object term.
260 261 262 |
# File 'lib/rdf/mixin/queryable.rb', line 260 def first_value(pattern = nil) (literal = first_literal(pattern)) ? literal.value : nil end |
- (Enumerator) query(pattern) {|statement| ... }
Queries self for RDF statements matching the given pattern.
This method delegates to the protected #query_pattern method for the actual lower-level query pattern matching implementation.
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 |
# File 'lib/rdf/mixin/queryable.rb', line 30 def query(pattern, &block) raise TypeError, "#{self} is not readable" if respond_to?(:readable?) && !readable? case pattern # A basic graph pattern (BGP) query: when Query if block_given? before_query(pattern) if respond_to?(:before_query) query_execute(pattern, &block) after_query(pattern) if respond_to?(:after_query) end enum_for(:query_execute, pattern) # A simple triple/quad pattern query: else pattern = Query::Pattern.from(pattern) before_query(pattern) if block_given? && respond_to?(:before_query) enum = case # Blank triple/quad patterns are equivalent to iterating over # every statement, so as a minor optimization we'll just do that # directly instead of bothering with `#query_pattern`: when pattern.blank? each(&block) if block_given? enum_for(:each) # Constant triple/quad patterns are equivalent to looking up a # particular statement, so as a minor optimization we'll just do # that directly instead of bothering with `#query_pattern`: when pattern.constant? statement = Statement.from(pattern) block.call(statement) if block_given? && include?(statement) enum_for(:query, pattern) # Otherwise, we delegate to `#query_pattern`: else # pattern.variable? query_pattern(pattern, &block) if block_given? enum_for(:query_pattern, pattern) end after_query(pattern) if block_given? && respond_to?(:after_query) enum.extend(RDF::Queryable, RDF::Enumerable, RDF::Countable) def enum.to_a super.extend(RDF::Queryable, RDF::Enumerable, RDF::Countable) end enum end end |
- query_execute(query) {|solution| ... } (protected)
This method returns an undefined value.
Queries self using the given basic graph pattern (BGP) query,
yielding each matched solution to the given block.
Since RDF.rb 0.3.0, repository implementations can override this method in order to provide for storage-specific optimized graph pattern query execution.
94 95 96 97 98 99 100 |
# File 'lib/rdf/mixin/queryable.rb', line 94 def query_execute(query, &block) # By default, we let RDF.rb's built-in `RDF::Query#execute` handle BGP # query execution by breaking down the query into its constituent # triple patterns and invoking `RDF::Query::Pattern#execute` on each # pattern. query.execute(self).each(&block) end |
- query_pattern(pattern) {|statement| ... } (protected)
This method returns an undefined value.
Queries self for RDF statements matching the given pattern,
yielding each matched statement to the given block.
Since RDF.rb 0.2.0, repository implementations should override this method in order to provide for storage-specific optimized triple pattern matching.
120 121 122 123 124 125 126 |
# File 'lib/rdf/mixin/queryable.rb', line 120 def query_pattern(pattern, &block) # By default, we let Ruby's built-in `Enumerable#grep` handle the # matching of statements by iterating over all statements and calling # `RDF::Query::Pattern#===` on each statement. # @see http://ruby-doc.org/core/classes/Enumerable.html#M003121 grep(pattern, &block) end |