Class: RDF::Repository

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

Overview

An RDF repository.

Examples:

Creating a transient in-memory repository

repository = RDF::Repository.new

Checking whether a repository is readable/writable

repository.readable?
repository.writable?

Checking whether a repository is persistent or transient

repository.persistent?
repository.transient?

Checking whether a repository is empty

repository.empty?

Checking how many statements a repository contains

repository.count

Checking whether a repository contains a specific statement

repository.has_statement?(statement)

Enumerating statements in a repository

repository.each_statement { |statement| statement.inspect! }

Inserting statements into a repository

repository.insert(*statements)
repository.insert(statement)
repository.insert([subject, predicate, object])
repository << statement
repository << [subject, predicate, object]

Deleting statements from a repository

repository.delete(*statements)
repository.delete(statement)
repository.delete([subject, predicate, object])

Deleting all statements from a repository

repository.clear!

Defined Under Namespace

Modules: Implementation

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Durable

#durable?, #nondurable?

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Mutable

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

Methods included from Writable

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

Methods included from Enumerable

#contexts, #dump, #each_context, #each_graph, #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_statement?, #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

#count, #empty?

Constructor Details

- (Repository) initialize(options = {}) {|repository| ... }

Initializes this repository instance.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :uri (URI, #to_s) — default: nil
  • :title (String, #to_s) — default: nil
  • :with_context (Boolean) — default: true

    Indicates that the repository supports named graphs, otherwise, only the default graph is supported.

Yields:

  • (repository)

Yield Parameters:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rdf/repository.rb', line 104

def initialize(options = {}, &block)
  @options = {:with_context => true}.merge(options)
  @uri     = @options.delete(:uri)
  @title   = @options.delete(:title)

  # Provide a default in-memory implementation:
  send(:extend, Implementation) if self.class.equal?(RDF::Repository)

  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

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

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

Returns:

  • (Hash{Symbol => Object})


54
55
56
# File 'lib/rdf/repository.rb', line 54

def options
  @options
end

- (String) title (readonly)

Returns the title of this repository.

Returns:

  • (String)


67
68
69
# File 'lib/rdf/repository.rb', line 67

def title
  @title
end

- (URI) uri (readonly) Also known as: url

Returns the URI of this repository.

Returns:



60
61
62
# File 'lib/rdf/repository.rb', line 60

def uri
  @uri
end

Class Method Details

+ load(filenames, options = {}) {|repository| ... }

This method returns an undefined value.

Loads one or more RDF files into a new transient in-memory repository.

Parameters:

  • filenames (String, Array<String>)
  • options (Hash{Symbol => Object}) (defaults to: {})

    Options from #initialize and Mutable#load

Yields:

  • (repository)

Yield Parameters:



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

def self.load(filenames, options = {}, &block)
  self.new(options) do |repository|
    Array(filenames).each do |filename|
      repository.load(filename, options)
    end

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

Instance Method Details

- (RDF::Transaction) begin_transaction(context) (protected)

Begins a new transaction.

Subclasses implementing transaction-capable storage adapters may wish to override this method in order to begin a transaction against the underlying storage.

Parameters:

Returns:

Since:

  • 0.3.0



183
184
185
# File 'lib/rdf/repository.rb', line 183

def begin_transaction(context)
  RDF::Transaction.new(:graph => context)
end

- commit_transaction(tx) (protected)

This method returns an undefined value.

Commits the given transaction.

Subclasses implementing transaction-capable storage adapters may wish to override this method in order to commit the given transaction to the underlying storage.

Parameters:

Since:

  • 0.3.0



211
212
213
# File 'lib/rdf/repository.rb', line 211

def commit_transaction(tx)
  tx.execute(self)
end

- (String) inspect

Returns a developer-friendly representation of this object.

Returns:

  • (String)


124
125
126
# File 'lib/rdf/repository.rb', line 124

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, uri.to_s)
end

- inspect!

This method returns an undefined value.

Outputs a developer-friendly representation of this object to stderr.



133
134
135
136
# File 'lib/rdf/repository.rb', line 133

def inspect!
  each_statement { |statement| statement.inspect! }
  nil
end

- rollback_transaction(tx) (protected)

This method returns an undefined value.

Rolls back the given transaction.

Subclasses implementing transaction-capable storage adapters may wish to override this method in order to roll back the given transaction in the underlying storage.

Parameters:

Since:

  • 0.3.0



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

def rollback_transaction(tx)
  # nothing to do
end

- transaction(context = nil) {|tx| ... } Also known as: transact

This method returns an undefined value.

Executes the given block in a transaction.

Examples:

repository.transaction do |tx|
  tx.insert [RDF::URI("http://rdf.rubyforge.org/"), RDF::DC.title, "RDF.rb"]
end

Parameters:

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

    Context on which to run the transaction, use false for the default context and nil the entire Repository

Yields:

  • (tx)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

See Also:

Since:

  • 0.3.0



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rdf/repository.rb', line 155

def transaction(context = nil, &block)
  tx = begin_transaction(context)
  begin
    case block.arity
      when 1 then block.call(tx)
      else tx.instance_eval(&block)
    end
  rescue => error
    rollback_transaction(tx)
    raise error
  end
  commit_transaction(tx)
  self
end