Module: RDF::Queryable

Defined in:
lib/sparql/algebra/extensions.rb

Overview

Override RDF::Queryable to execute against SPARQL::Algebra::Query elements as well as RDF::Query and RDF::Pattern

Instance Method Summary collapse

Instance Method Details

#concise_bounded_description(*terms) {|statement| ... } ⇒ RDF::Graph

Concise Bounded Description

Given a particular node (the starting node) in a particular RDF graph (the source graph), a subgraph of that particular graph, taken to comprise a concise bounded description of the resource denoted by the starting node, can be identified as follows:

  1. Include in the subgraph all statements in the source graph where the subject of the statement is the starting node;
  2. Recursively, for all statements identified in the subgraph thus far having a blank node object, include in the subgraph all statements in the source graph where the subject of the statement is the blank node in question and which are not already included in the subgraph.
  3. Recursively, for all statements included in the subgraph thus far, for all reifications of each statement in the source graph, include the concise bounded description beginning from the rdf:Statement node of each reification. (we skip this step)

This results in a subgraph where the object nodes are either URI references, literals, or blank nodes not serving as the subject of any statement in the graph.

Used to implement the SPARQL describe operator.

Parameters:

  • *terms (Array<RDF::Term>)

    List of terms to include in the results.

  • options (Hash{Symbol => Object})

Yields:

  • (statement)

Yield Parameters:

  • statement (RDF::Statement)

Yield Returns:

  • (void)

    ignored

Returns:

  • (RDF::Graph)

See Also:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/sparql/algebra/extensions.rb', line 123

def concise_bounded_description(*terms, &block)
  options = terms.last.is_a?(Hash) ? terms.pop.dup : {}
  options[:non_subjects] = true unless options.has_key?(:non_subjects)

  graph = options[:graph] || RDF::Graph.new

  if options[:non_subjects]
    query_terms = terms.dup

    # Find terms not in self as a subject and recurse with their subjects
    terms.reject {|term| self.first(:subject => term)}.each do |term|
      self.query(:predicate => term) do |statement|
        query_terms << statement.subject
      end

      self.query(:object => term) do |statement|
        query_terms << statement.subject
      end
    end
    
    terms = query_terms.uniq
  end

  # Don't consider term if already in graph
  terms.reject {|term| graph.first(:subject => term)}.each do |term|
    # Find statements from queryiable with term as a subject
    self.query(:subject => term) do |statement|
      yield(statement) if block_given?
      graph << statement
      
      # Include reifications of this statement
      RDF::Query.new({
        :s => {
          RDF.type => RDF["Statement"],
          RDF.subject => statement.subject,
          RDF.predicate => statement.predicate,
          RDF.object => statement.object,
        }
      }).execute(self).each do |solution|
        # Recurse to include this subject
        recurse_opts = options.merge(:non_subjects => false, :graph => graph)
        self.concise_bounded_description(solution[:s], recurse_opts, &block)
      end

      # Recurse if object is a BNode and it is not already in subjects
      if statement.object.node?
        recurse_opts = options.merge(:non_subjects => false, :graph => graph)
        self.concise_bounded_description(statement.object, recurse_opts, &block)
      end
    end
  end
  
  graph
end

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

Queries self for RDF statements matching the given pattern.

Monkey patch to RDF::Queryable#query to execute a SPARQL::Algebra::Operator in addition to an RDF::Query object.

Examples:

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

op = SPARQL::Algebra::Expression.parse(%q((bgp (triple ?a doap:developer ?b))))
queryable.query(op)

Parameters:

Yields:

  • (statement)

    each matching statement

Yield Parameters:

  • statement (RDF::Statement)

Yield Returns:

  • (void)

    ignored

Returns:

  • (Enumerator)

Raises:

  • (TypeError)

See Also:

  • #query_pattern


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sparql/algebra/extensions.rb', line 75

def query(pattern, &block)
  raise TypeError, "#{self} is not readable" if respond_to?(:readable?) && !readable?

  if pattern.is_a?(SPARQL::Algebra::Operator) && pattern.respond_to?(:execute)
    before_query(pattern) if respond_to?(:before_query)
    query_execute(pattern, &block)
    after_query(pattern) if respond_to?(:after_query)
    enum_for(:query_execute, pattern)
  else
    query_without_sparql(pattern, &block)
  end
end

#query_without_sparqlObject



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

alias_method :query_without_sparql, :query