Class: RDF::Graph

Inherits:
Object
  • Object
show all
Includes:
Countable, 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, #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?, #objects, #predicates, #quads, #statements, #subjects, #to_a, #to_hash, #to_set, #triples

Methods included from Resource

new, #resource?

Methods included from Term

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

Methods included from Value

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

Constructor Details

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

A new instance of Graph

Parameters:

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

Yields:

  • (graph)

Yield Parameters:



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

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

- (RDF::Resource) context

Returns:



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

def context
  @context
end

- (RDF::Queryable) data

Returns:



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

def data
  @data
end

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

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

Returns:

  • (Hash{Symbol => Object})


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

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:

  • (String, #to_s) url
  • (Hash{Symbol => Object}) options (defaults to: {})

Yields:

  • (graph)

Yield Parameters:

Returns:

Since:

  • 0.1.7



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

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) anonymous?

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

Returns:

  • (Boolean)


161
162
163
# File 'lib/rdf/model/graph.rb', line 161

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

- (Enumerator<RDF::Resource>) contexts

Returns all unique RDF contexts for this graph.



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

def contexts
  (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


170
171
172
# File 'lib/rdf/model/graph.rb', line 170

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

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

Enumerates each RDF statement in this graph.

Yields:

  • (statement)

Yield Parameters:

Returns:

See Also:



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

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

- (Boolean) empty?

Returns true if this graph contains no RDF statements.

Returns:

  • (Boolean)

See Also:

  • Enumerable#empty?


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

def empty?
  @data.empty?
end

- (Boolean) graph?

Returns true to indicate that this is a graph.

Returns:

  • (Boolean)


104
105
106
# File 'lib/rdf/model/graph.rb', line 104

def graph?
  true
end

- (Boolean) has_statement?(statement)

Returns true if this graph contains the given RDF statement.

Parameters:

Returns:

  • (Boolean)

See Also:



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

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

- load!(*args)

This method returns an undefined value.



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

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

- (Boolean) named?

Returns true if this is a named graph.

Returns:

  • (Boolean)


112
113
114
# File 'lib/rdf/model/graph.rb', line 112

def named?
  !unnamed?
end

- (String) to_s

Returns a string representation of this graph.

Returns:

  • (String)


144
145
146
# File 'lib/rdf/model/graph.rb', line 144

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

- (RDF::URI) to_uri

Returns the URI representation of this graph.

Returns:



136
137
138
# File 'lib/rdf/model/graph.rb', line 136

def to_uri
  context
end

- (Boolean) unnamed?

Returns true if this is a unnamed graph.

Returns:

  • (Boolean)


120
121
122
# File 'lib/rdf/model/graph.rb', line 120

def unnamed?
  context.nil?
end