Class: RDF::Graph

Inherits:
Object
  • Object
show all
Includes:
Countable, Durable, Enumerable, Mutable, Queryable, Value
Defined in:
lib/rdf/model/graph.rb

Overview

An RDF graph.

An Graph contains a unique set of Statement. It is based on an underlying data object, which may be specified when the graph is initialized, and will default to a Repository without support for contexts otherwise.

Note that in RDF 1.1, graphs are not named, but are associated with a name in the context of a Dataset, as a pair of . This class allows a name to be associated with a graph when it is a projection of an underlying Repository supporting contexts.

Examples:

Creating an empty unnamed graph

graph = Graph.new

Loading graph data from a URL

graph = Graph.new("http://rubygems.org/")

Loading graph data from a URL (1)

require 'rdf/rdfxml'  # for RDF/XML support

graph = RDF::Graph.new("http://www.bbc.co.uk/programmes/b0081dq5.rdf")
graph.load!

Loading graph data from a URL (2)

require 'rdf/rdfxml'  # for RDF/XML support

graph = RDF::Graph.load("http://www.bbc.co.uk/programmes/b0081dq5.rdf")

Accessing a specific named graph within a Repository

require 'rdf/trig'  # for TriG support

repository = RDF::Repository.load("https://raw.github.com/ruby-rdf/rdf-trig/master/etc/doap.trig")
graph = RDF::Graph.new(:data => repository, :context => RDF::URI("http://greggkellogg.net/foaf#me"))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mutable

#<<, #clear, #delete, #delete_statements, #immutable?, #insert, #load, #method_missing, #mutable?, #update

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Writable

#<<, #insert, #insert_graph, #insert_reader, #insert_statements, #writable?

Methods included from Readable

#readable?

Methods included from Queryable

#first, #first_literal, #first_object, #first_predicate, #first_subject, #first_value, #query, #query_execute

Methods included from Enumerable

#dump, #each_context, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_triple, #enum_context, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_triple, #has_context?, #has_object?, #has_predicate?, #has_quad?, #has_subject?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #quads, #statements, #subjects, #supports?, #to_a, #to_hash, #to_set, #triples, #valid?, #validate!

Methods included from Countable

#empty?

Methods included from Durable

#nondurable?

Methods included from Value

#canonicalize, #canonicalize!, #constant?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?

Constructor Details

#initialize(context, options) ⇒ Graph #initialize(options) ⇒ Graph

Returns a new instance of Graph

Overloads:

  • #initialize(context, options) ⇒ Graph
    Note:

    Contexts are only useful when used as a projection on a :data which supports contexts. Otherwise, there is no such thing as a named graph in RDF 1.1, a repository may have graphs which are named, but the name is not a property of the graph.

    Options Hash (options):

    • :data (RDF::Queryable) — default: RDF::Repository.new

      Storage behind this graph.

    Raises:

    • (ArgumentError)

      if a data does not support contexts.

  • #initialize(options) ⇒ Graph

    Options Hash (options):

    • :data (RDF::Queryable) — default: RDF::Repository.new

      Storage behind this graph.

Yields:

  • (graph)

Yield Parameters:

Raises:

  • (ArgumentError)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rdf/model/graph.rb', line 114

def initialize(*args, &block)
  context = args.shift unless args.first.is_a?(Hash)
  options = args.first || {}
  @context = case context
    when nil then nil
    when RDF::Resource then context
    else RDF::URI.new(context)
  end

  @options = options.dup
  @data    = @options.delete(:data) || RDF::Repository.new(:with_context => false)

  raise ArgumentError, "Can't apply context unless initialized with `data` supporting contexts" if
    @context && !@data.supports?(:context)

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Mutable

Instance Attribute Details

#contextRDF::Resource Also known as: name

Name of this graph, if it is part of an Repository

Since:

  • 1.1.0



57
58
59
# File 'lib/rdf/model/graph.rb', line 57

def context
  @context
end

#dataRDF::Queryable

Queryable backing this graph.



66
67
68
# File 'lib/rdf/model/graph.rb', line 66

def data
  @data
end

#optionsHash{Symbol => Object} (readonly)

Returns the options passed to this graph when it was constructed.



50
51
52
# File 'lib/rdf/model/graph.rb', line 50

def options
  @options
end

Class Method Details

.load(url, options = {}) {|graph| ... } ⇒ Graph

Creates a new Graph instance populated by the RDF data returned by dereferencing the given context URL.

Yields:

  • (graph)

Yield Parameters:

Since:

  • 0.1.7



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rdf/model/graph.rb', line 79

def self.load(url, options = {}, &block)
  self.new(options) do |graph|
    graph.load(url, options)

    if block_given?
      case block.arity
        when 1 then block.call(graph)
        else graph.instance_eval(&block)
      end
    end
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Graph equivalence based on the contents of each graph being exactly the same. To determine if the have the same meaning, consider rdf-isomorphic.



264
265
266
267
268
# File 'lib/rdf/model/graph.rb', line 264

def ==(other)
  other.is_a?(RDF::Graph) &&
  context == other.context &&
  statements.to_a == other.statements.to_a
end

#anonymous?Boolean

Note:

The next release, graphs will not be named, this will return true

Returns true if this graph has an anonymous context, false otherwise.



214
215
216
# File 'lib/rdf/model/graph.rb', line 214

def anonymous?
  context.nil? ? false : context.anonymous?
end

#contexts(options = {}) ⇒ Enumerator<RDF::Resource>

Returns all unique RDF contexts for this graph.



189
190
191
# File 'lib/rdf/model/graph.rb', line 189

def contexts(options = {})
  (named? ? [context] : []).to_enum.extend(RDF::Countable)
end

#countInteger

Returns the number of RDF statements in this graph.

See Also:

  • Enumerable#count


223
224
225
# File 'lib/rdf/model/graph.rb', line 223

def count
  @data.query(:context => context || false).count
end

#durable?Boolean

A graph is durable if it's underlying data model is durable

See Also:



181
182
183
# File 'lib/rdf/model/graph.rb', line 181

def durable?
  @data.durable?
end

#each {|statement| ... } ⇒ Enumerator

Enumerates each RDF statement in this graph.

Yields:

  • (statement)

Yield Parameters:

See Also:



246
247
248
249
250
251
252
253
254
# File 'lib/rdf/model/graph.rb', line 246

def each(&block)
  if @data.respond_to?(:query)
    @data.query(:context => context || false, &block)
  elsif @data.respond_to?(:each)
    @data.each(&block)
  else
    @data.to_a.each(&block)
  end
end

#graph?Boolean

Returns true to indicate that this is a graph.



154
155
156
# File 'lib/rdf/model/graph.rb', line 154

def graph?
  true
end

#has_statement?(statement) ⇒ Boolean

Returns true if this graph contains the given RDF statement.



233
234
235
236
237
# File 'lib/rdf/model/graph.rb', line 233

def has_statement?(statement)
  statement = statement.dup
  statement.context = context
  @data.has_statement?(statement)
end

#load!(*args)

This method returns an undefined value.

(re)loads the graph from the specified location, or from the location associated with the graph context, if any

See Also:



141
142
143
144
145
146
147
148
# File 'lib/rdf/model/graph.rb', line 141

def load!(*args)
  case
    when args.empty?
      raise ArgumentError, "Can't reload graph with no context" unless context.is_a?(RDF::URI)
      load(context.to_s, {:base_uri => context}.merge(@options))
    else super
  end
end

#named?Boolean

Note:

The next release, graphs will not be named, this will return false

Returns true if this is a named graph.



163
164
165
# File 'lib/rdf/model/graph.rb', line 163

def named?
  !unnamed?
end

#to_sString

Returns a string representation of this graph.



205
206
207
# File 'lib/rdf/model/graph.rb', line 205

def to_s
  named? ? context.to_s : "default"
end

#to_uriRDF::Resource

Returns the Resource representation of this graph.



197
198
199
# File 'lib/rdf/model/graph.rb', line 197

def to_uri
  context
end

#unnamed?Boolean

Note:

The next release, graphs will not be named, this will return true

Returns true if this is a unnamed graph.



172
173
174
# File 'lib/rdf/model/graph.rb', line 172

def unnamed?
  context.nil?
end