Module: RDF::Queryable

Included in:
MergeGraph
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/extensions.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-reasoner-63fee6d2320c/lib/rdf/reasoner/extensions.rb,
vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/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, &block) ⇒ RDF::Graph #concise_bounded_description(*terms, options, &block) ⇒ 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.

Overloads:

  • #concise_bounded_description(*terms, &block) ⇒ RDF::Graph

    Parameters:

    • terms (Array<RDF::Term>)

      List of terms to include in the results.

  • #concise_bounded_description(*terms, options, &block) ⇒ RDF::Graph

    Parameters:

    Options Hash (options):

    • :non_subjects (Boolean) — default: false

      If term is not a subject within self then add all subjects referencing the term as a predicate or object.

    • graph (RDF::Graph)

      Graph containing statements already considered.

Yields:

  • (statement)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Returns:

See Also:



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
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/extensions.rb', line 47

def concise_bounded_description(*terms, &block)
  options = terms.last.is_a?(Hash) ? terms.pop.dup : {}

  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

#lintHash{Symbol => Hash{Symbol => Array<String>}}

Lint a queryable, presuming that it has already had RDFS entailment expansion.

Returns:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-reasoner-63fee6d2320c/lib/rdf/reasoner/extensions.rb', line 232

def lint
  messages = {}

  # Check for defined classes in known vocabularies
  self.query(predicate: RDF.type) do |stmt|
    vocab = RDF::Vocabulary.find(stmt.object)
    term = (RDF::Vocabulary.find_term(stmt.object) rescue nil) if vocab
    pname = term ? term.pname : stmt.object.pname
    
    # Must be a defined term, not in RDF or RDFS vocabularies
    if term && term.class?
      # Warn against using a deprecated term
      superseded = term.attributes[:'schema:supersededBy']
      superseded = superseded.pname if superseded.respond_to?(:pname)
      (messages[:class] ||= {})[pname] = ["Term is superseded by #{superseded}"] if superseded
    else
      (messages[:class] ||= {})[pname] = ["No class definition found"] unless vocab.nil? || [RDF::RDFV, RDF::RDFS].include?(vocab)
    end
  end

  # Check for defined predicates in known vocabularies and domain/range
  resource_types = {}
  self.each_statement do |stmt|
    vocab = RDF::Vocabulary.find(stmt.predicate)
    term = (RDF::Vocabulary.find_term(stmt.predicate) rescue nil) if vocab
    pname = term ? term.pname : stmt.predicate.pname

    # Must be a valid statement
    begin
      stmt.validate!
    rescue
      ((messages[:statement] ||= {})[pname] ||= []) << "Triple #{stmt.to_ntriples} is invalid"
    end

    # Must be a defined property
    if term && term.property?
      # Warn against using a deprecated term
      superseded = term.attributes[:'schema:supersededBy']
      superseded = superseded.pname if superseded.respond_to?(:pname)
      (messages[:property] ||= {})[pname] = ["Term is superseded by #{superseded}"] if superseded
    else
      ((messages[:property] ||= {})[pname] ||= []) << "No property definition found" unless vocab.nil?
      next
    end

    # See if type of the subject is in the domain of this predicate
    resource_types[stmt.subject] ||= self.query(subject: stmt.subject, predicate: RDF.type).
    map {|s| (t = (RDF::Vocabulary.find_term(s.object) rescue nil)) && t.entail(:subClassOf)}.
      flatten.
      uniq.
      compact

    unless term.domain_compatible?(stmt.subject, self, types: resource_types[stmt.subject])
      ((messages[:property] ||= {})[pname] ||= []) << if !term.domain.empty?
       "Subject #{show_resource(stmt.subject)} not compatible with domain (#{Array(term.domain).map {|d| d.pname|| d}.join(',')})"
      else
        "Subject #{show_resource(stmt.subject)} not compatible with domainIncludes (#{term.domainIncludes.map {|d| d.pname|| d}.join(',')})"
      end
    end

    # Make sure that if ranges are defined, the object has an appropriate type
    resource_types[stmt.object] ||= self.query(subject: stmt.object, predicate: RDF.type).
      map {|s| (t = (RDF::Vocabulary.find_term(s.object) rescue nil)) && t.entail(:subClassOf)}.
      flatten.
      uniq.
      compact if stmt.object.resource?

    unless term.range_compatible?(stmt.object, self, types: resource_types[stmt.object])
      ((messages[:property] ||= {})[pname] ||= []) << if !term.range.empty?
       "Object #{show_resource(stmt.object)} not compatible with range (#{Array(term.range).map {|d| d.pname|| d}.join(',')})"
      else
        "Object #{show_resource(stmt.object)} not compatible with rangeIncludes (#{term.rangeIncludes.map {|d| d.pname|| d}.join(',')})"
      end
    end
  end

  messages[:class].each {|k, v| messages[:class][k] = v.uniq} if messages[:class]
  messages[:property].each {|k, v| messages[:property][k] = v.uniq} if messages[:property]
  messages
end

#query(pattern, options = {}) {|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:

Yield Returns:

  • (void)

    ignored

Returns:

  • (Enumerator)

Raises:

  • (TypeError)

See Also:

  • #query_pattern


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/extensions.rb', line 238

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

  if pattern.is_a?(SPARQL::Algebra::Operator) && pattern.respond_to?(:execute)
    before_query(pattern) if respond_to?(:before_query)
    solutions = if method(:query_execute).arity == 1
      query_execute(pattern, &block)
    else
      query_execute(pattern, options, &block)
    end
    after_query(pattern) if respond_to?(:after_query)

    if !pattern.respond_to?(:query_yeilds_solutions?) || pattern.query_yields_solutions?
      # Just return solutions
      solutions
    else
      # Return an enumerator
      enum_for(:query, pattern, options)
    end
  else
    query_without_sparql(pattern, options, &block)
  end
end

#query_without_sparqlObject



217
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/sparql-7b34b4cf091a/lib/sparql/algebra/extensions.rb', line 217

alias_method :query_without_sparql, :query