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 should implement a ‘#query_pattern` method that yields RDF statements. Classes may also implement an optimized `#query_execute` method that yields RDF statements.

See Also:

Instance Method Summary collapse

Instance Method Details

#firstRDF::Statement #first(pattern) ⇒ RDF::Statement

Queries ‘self` for an RDF statement matching the given `pattern` and returns that statement if found.

Returns ‘nil` if no statements match `pattern`.

Overloads:

Returns:

Since:

  • 0.1.9



145
146
147
148
149
150
151
152
153
154
# File 'lib/rdf/mixin/queryable.rb', line 145

def first(pattern = nil)
  if pattern
    query(pattern) do |statement|
      return statement
    end
    return nil
  else
    super()
  end
end

#first_literalRDF::Literal #first_literal(pattern) ⇒ RDF::Literal

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.

Overloads:

Returns:

Since:

  • 0.1.9



238
239
240
241
242
243
# File 'lib/rdf/mixin/queryable.rb', line 238

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

#first_objectRDF::Term #first_object(pattern) ⇒ RDF::Term

Queries ‘self` for an RDF statement matching the given `pattern` and returns the statement’s object term.

Returns ‘nil` if no statements match `pattern`.

Overloads:

Returns:

Since:

  • 0.1.9



215
216
217
218
219
220
# File 'lib/rdf/mixin/queryable.rb', line 215

def first_object(pattern = nil)
  __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
    return statement.object
  end
  return nil
end

#first_predicateRDF::URI #first_predicate(pattern) ⇒ RDF::URI

Queries ‘self` for an RDF statement matching the given `pattern` and returns the statement’s predicate term.

Returns ‘nil` if no statements match `pattern`.

Overloads:

Returns:

Since:

  • 0.1.9



193
194
195
196
197
198
# File 'lib/rdf/mixin/queryable.rb', line 193

def first_predicate(pattern = nil)
  __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
    return statement.predicate
  end
  return nil
end

#first_subjectRDF::Resource #first_subject(pattern) ⇒ RDF::Resource

Queries ‘self` for an RDF statement matching the given `pattern` and returns the statement’s subject term.

Returns ‘nil` if no statements match `pattern`.

Overloads:

Returns:

Since:

  • 0.1.9



171
172
173
174
175
176
# File 'lib/rdf/mixin/queryable.rb', line 171

def first_subject(pattern = nil)
  __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
    return statement.subject
  end
  return nil
end

#first_valueObject #first_value(pattern) ⇒ Object

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.

Overloads:

  • #first_valueObject

    Returns:

    • (Object)
  • #first_value(pattern) ⇒ Object

    Parameters:

    Returns:

    • (Object)

Returns:

  • (Object)

Since:

  • 0.1.9



261
262
263
# File 'lib/rdf/mixin/queryable.rb', line 261

def first_value(pattern = nil)
  (literal = first_literal(pattern)) ? literal.value : nil
end

#query(pattern) {|statement| ... } ⇒ Enumerator

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.

Examples:

queryable.query([nil, RDF::DOAP.developer, nil])
queryable.query(:predicate => RDF::DOAP.developer)

Parameters:

Yields:

  • (statement)

    each matching statement

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

Raises:

  • (TypeError)

See Also:

  • #query_pattern


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
# File 'lib/rdf/mixin/queryable.rb', line 31

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