Class: RDF::Graph

Inherits:
Object
  • Object
show all
Includes:
Countable, Durable, Enumerable, Mutable, Queryable, Transactable, 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 named graphs otherwise.

Note that in RDF 1.1, graphs are not named, but are associated with a graph name in 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 graph_names.

Examples:

Creating an empty unnamed graph

graph = RDF::Graph.new

Loading graph data from a URL

graph = RDF::Graph.load("http://ruby-rdf.github.io/rdf/etc/doap.nt")

Loading graph data from a URL

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 = graph = RDF::Repository.load("https://raw.githubusercontent.com/ruby-rdf/rdf-trig/develop/etc/doap.trig", format: :trig))
graph = RDF::Graph.new(graph_name: RDF::URI("http://greggkellogg.net/foaf#me"), data: repository)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transactable

#commit_transaction, #rollback_transaction, #transaction

Methods included from Mutable

#<<, #apply_changeset, #clear, #delete, #delete_insert, #delete_statements, #immutable?, #insert, #load, #method_missing, #mutable?, #respond_to_missing?, #snapshot, #update

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Util::Coercions

#coerce_statements

Methods included from Writable

#<<, #insert, #insert_graph, #insert_reader, #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

#canonicalize, #canonicalize!, #dump, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_term, #each_triple, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #quad?, #quads, #respond_to_missing?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #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?, #start_with?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?

Constructor Details

#initialize(graph_name: nil, data: nil, **options) {|graph| ... } ⇒ Graph

Note:

Graph names are only useful when used as a projection on a :data which supports named graphs. 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.

Returns a new instance of Graph.

Parameters:

  • graph_name (RDF::Resource) (defaults to: nil)

    The graph_name from the associated Queryable associated with this graph as provided with the :data option (only for Queryable instances supporting named graphs).

  • data (RDF::Queryable) (defaults to: nil)

    (RDF::Repository.new) Storage behind this graph.

Yields:

  • (graph)

Yield Parameters:

Raises:

  • (ArgumentError)

    if a data does not support named graphs.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rdf/model/graph.rb', line 106

def initialize(graph_name: nil, data: nil, **options, &block)
  @graph_name = case graph_name
    when nil then nil
    when RDF::Resource then graph_name
    else RDF::URI.new(graph_name)
  end

  @options = options.dup
  @data = data || RDF::Repository.new(with_graph_name: false)

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

  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

#dataRDF::Queryable

Queryable backing this graph.

Returns:



61
62
63
# File 'lib/rdf/model/graph.rb', line 61

def data
  @data
end

#graph_nameRDF::Resource Also known as: name

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

Returns:

Since:

  • 1.1.18



52
53
54
# File 'lib/rdf/model/graph.rb', line 52

def graph_name
  @graph_name
end

#optionsHash{Symbol => Object} (readonly)

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

Returns:

  • (Hash{Symbol => Object})


45
46
47
# File 'lib/rdf/model/graph.rb', line 45

def options
  @options
end

Class Method Details

.load(url, graph_name: nil, **options) {|graph| ... } ⇒ Graph

Creates a new Graph instance populated by the RDF data returned by dereferencing the given graph_name Resource.

Parameters:

  • url (String, #to_s)
  • graph_name (RDF::Resource) (defaults to: nil)

    Set set graph name of each loaded statement

  • options (Hash{Symbol => Object})

    Options from Reader.open

Yields:

  • (graph)

Yield Parameters:

Returns:

Since:

  • 0.1.7



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rdf/model/graph.rb', line 76

def self.load(url, graph_name: nil, **options, &block)
  self.new(graph_name: graph_name, **options) do |graph|
    graph.load(url, graph_name: graph_name, **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.

Parameters:

Returns:

  • (Boolean)

See Also:



289
290
291
292
293
# File 'lib/rdf/model/graph.rb', line 289

def ==(other)
  other.is_a?(RDF::Graph) &&
  graph_name == other.graph_name &&
  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 graph, false otherwise.

Returns:

  • (Boolean)


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

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

#countInteger

Returns the number of RDF statements in this graph.

Returns:

  • (Integer)

See Also:

  • Enumerable#count


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

def count
  @data.query({graph_name: graph_name || false}).count
end

#durable?Boolean

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

Returns:

  • (Boolean)

See Also:



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

def durable?
  @data.durable?
end

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

Enumerates each RDF statement in this graph.

Yields:

  • (statement)

Yield Parameters:

Returns:

See Also:



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

def each(&block)
  if @data.respond_to?(:query)
    @data.query({graph_name: graph_name || false}, &block)
  elsif @data.respond_to?(:each)
    @data.each(&block)
  else
    @data.to_a.each(&block)
  end
end

#graph?Boolean #graph?(name) ⇒ Boolean

Overloads:

  • #graph?Boolean

    Returns true to indicate that this is a graph.

    Returns:

    • (Boolean)
  • #graph?(name) ⇒ Boolean

    Returns true if self contains the given RDF graph_name.

    Parameters:

    • graph_name (RDF::Resource, false)

      Use value false to query for the default graph_name

    Returns:

    • (Boolean)


151
152
153
154
155
156
157
# File 'lib/rdf/model/graph.rb', line 151

def graph?(*args)
  case args.length
  when 0 then true
  when 1 then graph_name == args.first
  else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
  end
end

#graph_names(unique: true) ⇒ Enumerator<RDF::Resource>

Returns all unique RDF names for this graph.



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

def graph_names(unique: true)
  (named? ? [graph_name] : []).extend(RDF::Countable)
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 name, if any

See Also:



131
132
133
134
135
136
137
138
# File 'lib/rdf/model/graph.rb', line 131

def load!(*args)
  case
    when args.empty?
      raise ArgumentError, "Can't reload graph without a graph_name" unless graph_name.is_a?(RDF::URI)
      load(graph_name.to_s, base_uri: graph_name)
    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.

Returns:

  • (Boolean)


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

def named?
  !unnamed?
end

#statement?Boolean #statement?(statement) ⇒ Boolean Also known as: has_statement?

Overloads:

  • #statement?Boolean

    Returns false indicating this is not an RDF::Statemenet.

    Returns:

    • (Boolean)

    See Also:

  • #statement?(statement) ⇒ Boolean

    Returns true if this graph contains the given RDF statement.

    A statement is in a graph if the statement if it has the same triples without regard to graph_name.

    Parameters:

    Returns:

    • (Boolean)

    See Also:



241
242
243
244
245
246
247
248
249
250
# File 'lib/rdf/model/graph.rb', line 241

def statement?(*args)
  case args.length
  when 0 then false
  when 1
    statement = args.first.dup
    statement.graph_name = graph_name
    @data.statement?(statement)
  else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
  end
end

#to_sString

Returns a string representation of this graph.

Returns:

  • (String)


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

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

#to_uriRDF::Resource

Returns the Resource representation of this graph.

Returns:



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

def to_uri
  graph_name
end

#unnamed?Boolean

Note:

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

Returns true if this is a unnamed graph.

Returns:

  • (Boolean)


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

def unnamed?
  graph_name.nil?
end