Class: RDF::Graph

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

Overview

An RDF graph.

Examples:

Creating an empty unnamed graph

graph = Graph.new

Creating an empty named graph

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

Loading graph data from a URL (1)

require 'rdf/raptor'  # 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/raptor'  # for RDF/XML support

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mutable

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

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Writable

#<<, #insert, #writable?

Methods included from Readable

#readable?

Methods included from Queryable

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

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?, #objects, #predicates, #quads, #statements, #subjects, #supports?, #to_a, #to_hash, #to_set, #triples

Methods included from Countable

#enum_for

Methods included from Durable

#nondurable?

Methods included from Resource

new, #resource?

Methods included from Term

#<=>, #==, #constant?, #eql?, #variable?

Methods included from Value

#inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?, #variable?

Constructor Details

#initialize(context = nil, options = {}) {|graph| ... } ⇒ Graph

Returns a new instance of Graph.

Parameters:

  • context (RDF::Resource) (defaults to: nil)
  • options (Hash{Symbol => Object}) (defaults to: {})

Yields:

  • (graph)

Yield Parameters:



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

def initialize(context = nil, options = {}, &block)
  @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

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

Instance Attribute Details

#contextRDF::Resource

Returns:



39
40
41
# File 'lib/rdf/model/graph.rb', line 39

def context
  @context
end

#dataRDF::Queryable

Returns:



43
44
45
# File 'lib/rdf/model/graph.rb', line 43

def data
  @data
end

#optionsHash{Symbol => Object} (readonly)

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

Returns:

  • (Hash{Symbol => Object})


35
36
37
# File 'lib/rdf/model/graph.rb', line 35

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.

Parameters:

Yields:

  • (graph)

Yield Parameters:

Returns:

Since:

  • 0.1.7



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rdf/model/graph.rb', line 56

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

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

Instance Method Details

#anonymous?Boolean

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

Returns:

  • (Boolean)


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

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

#contextsEnumerator<RDF::Resource>

Returns all unique RDF contexts for this graph.



139
140
141
# File 'lib/rdf/model/graph.rb', line 139

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

#countInteger

Returns the number of RDF statements in this graph.

Returns:

  • (Integer)

See Also:

  • Enumerable#count


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

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

#durable?Boolean

Returns ‘false` to indicate that this graph is not durable

Returns:

  • (Boolean)

See Also:



131
132
133
# File 'lib/rdf/model/graph.rb', line 131

def durable?
  false
end

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

Enumerates each RDF statement in this graph.

Yields:

  • (statement)

Yield Parameters:

Returns:

See Also:



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

def each(&block)
  @data.query(:context => context).each(&block)
end

#each_graph(&block) ⇒ Object

See Also:

Since:

  • 0.2.0



259
260
261
# File 'lib/rdf/model/graph.rb', line 259

def each_graph(&block)
  block_given? ? block.call(self) : enum_graph
end

#empty?Boolean

Returns ‘true` if this graph contains no RDF statements.

Returns:

  • (Boolean)

See Also:

  • Enumerable#empty?


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

def empty?
  @data.empty?
end

#graph?Boolean

Returns ‘true` to indicate that this is a graph.

Returns:

  • (Boolean)


106
107
108
# File 'lib/rdf/model/graph.rb', line 106

def graph?
  true
end

#graphsObject

See Also:

  • Enumerable#graphs

Since:

  • 0.2.0



251
252
253
# File 'lib/rdf/model/graph.rb', line 251

def graphs
  enum_graph
end

#has_statement?(statement) ⇒ Boolean

Returns ‘true` if this graph contains the given RDF statement.

Parameters:

Returns:

  • (Boolean)

See Also:



191
192
193
194
195
# File 'lib/rdf/model/graph.rb', line 191

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

#load!(*args) ⇒ void

This method returns an undefined value.



94
95
96
97
98
99
100
# File 'lib/rdf/model/graph.rb', line 94

def load!(*args)
  case
    when args.empty?
      load(context.to_s, context ? {:base_uri => context}.merge(@options) : @options)
    else super
  end
end

#named?Boolean

Returns ‘true` if this is a named graph.

Returns:

  • (Boolean)


114
115
116
# File 'lib/rdf/model/graph.rb', line 114

def named?
  !unnamed?
end

#to_sString

Returns a string representation of this graph.

Returns:

  • (String)


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

def to_s
  named? ? context.to_s : "<>"
end

#to_uriRDF::URI

Returns the URI representation of this graph.

Returns:



147
148
149
# File 'lib/rdf/model/graph.rb', line 147

def to_uri
  context
end

#unnamed?Boolean

Returns ‘true` if this is a unnamed graph.

Returns:

  • (Boolean)


122
123
124
# File 'lib/rdf/model/graph.rb', line 122

def unnamed?
  context.nil?
end