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, #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 Durable

#nondurable?

Methods included from Resource

new, #resource?

Methods included from Term

#<=>, #eql?, #escape, #term?, #to_base

Methods included from Value

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

Constructor Details

- (Graph) initialize(context, options) - (Graph) initialize(options)

Note:

Currently, context makes this a named garph;

in the next release it will not

Overloads:

  • - (Graph) initialize(context, options)
    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.

    Parameters:

    • context (RDF::Resource)

      The context only provides a context for loading relative documents

    • options (Hash{Symbol => Object})
  • - (Graph) initialize(options)

    Parameters:

    • options (Hash{Symbol => Object})

Yields:

  • (graph)

Yield Parameters:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rdf/model/graph.rb', line 94

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

  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

- (RDF::Resource) context Also known as: name

Note:

In the next release, only projections from an

Name of this graph, if it is part of an Repository Enumerable supporting contexts will have a context.

Returns:



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

def context
  @context
end

- (RDF::Queryable) data

Queryable backing this graph.

Returns:



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

def data
  @data
end

- (Hash{Symbol => Object}) options (readonly)

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

Returns:

  • (Hash{Symbol => Object})


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

def options
  @options
end

Class Method Details

+ (Graph) load(url, options = {}) {|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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rdf/model/graph.rb', line 66

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

- (Boolean) ==(other)

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:



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

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

- (Boolean) anonymous?

Note:

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

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

Returns:

  • (Boolean)


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

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

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

Returns all unique RDF contexts for this graph.



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

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

- (Integer) count

Returns the number of RDF statements in this graph.

Returns:

  • (Integer)

See Also:

  • Enumerable#count


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

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

- (Boolean) durable?

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

Returns:

  • (Boolean)

See Also:



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

def durable?
  @data.durable?
end

- (Enumerator) each {|statement| ... }

Enumerates each RDF statement in this graph.

Yields:

  • (statement)

Yield Parameters:

Returns:

See Also:



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

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

- (Boolean) empty?

Returns true if this graph contains no RDF statements.

Returns:

  • (Boolean)

See Also:

  • Enumerable#empty?


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

def empty?
  !@data.has_context?(context || false)
end

- (Boolean) graph?

Returns true to indicate that this is a graph.

Returns:

  • (Boolean)


129
130
131
# File 'lib/rdf/model/graph.rb', line 129

def graph?
  true
end

- (Boolean) has_statement?(statement)

Returns true if this graph contains the given RDF statement.

Parameters:

Returns:

  • (Boolean)

See Also:



217
218
219
220
221
# File 'lib/rdf/model/graph.rb', line 217

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

- load!(*args)

Note:

The next release, graphs will not be named

This method returns an undefined value.



117
118
119
120
121
122
123
# File 'lib/rdf/model/graph.rb', line 117

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

- (Boolean) named?

Note:

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

Returns true if this is a named graph.

Returns:

  • (Boolean)


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

def named?
  !unnamed?
end

- (String) to_s

Returns a string representation of this graph.

Returns:

  • (String)


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

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

- (RDF::Resource) to_uri

Returns the Resource representation of this graph.

Returns:



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

def to_uri
  context
end

- (Boolean) unnamed?

Note:

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

Returns true if this is a unnamed graph.

Returns:

  • (Boolean)


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

def unnamed?
  context.nil?
end