Class: Redlander::Model

Inherits:
Object
  • Object
show all
Includes:
Parsing, Serializing
Defined in:
lib/redlander/model.rb

Overview

The core object incorporating the repository of RDF statements.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Serializing

#to, #to_dot, #to_file, #to_json, #to_ntriples, #to_rdfxml, #to_turtle

Methods included from Parsing

#from, #from_ntriples, #from_rdfxml, #from_turtle, #from_uri

Constructor Details

#initialize(options = {}) ⇒ Model

Create a new RDF model. (For available storage options see librdf.org/docs/api/redland-storage-modules.html)

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :storage (String)
    • “memory” - default, if :storage option is omitted,

    • “hashes”

    • “file” - memory model initialized from RDF/XML file,

    • “uri” - read-only memory model with URI provided in ‘name’ arg,

    • “mysql”

    • “sqlite”

    • “postgresql”

    • “tstore”

    • “virtuoso”

    • … anything else that Redland can handle.

  • :name (String)

    storage identifier (DB file name or database name),

  • :host (String)

    database host name (for store types: :postgres, :mysql, :tstore),

  • :port (String)

    database host port (for store types: :postgres, :mysql, :tstore),

  • :database (String)

    database name (for store types: :postgres, :mysql, :tstore),

  • :user (String)

    database user name (for store types: :postgres, :mysql, :tstore),

  • :password (String)

    database user password (for store types: :postgres, :mysql, :tstore),

  • :hash_type (String)

    hash type (for store types: :bdb), can be either ‘memory’ or ‘bdb’,

  • :new (String)

    force creation of a new store,

  • :dir (String)

    directory path (for store types: :hashes),

  • :contexts (String)

    support contexts (for store types: :hashes, :memory),

  • :write (String)

    allow writing data to the store (for store types: :hashes),

  • ... (...)

    other storage-specific options.

Raises:

  • (RedlandError)

    if it fails to create a storage or a model.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/redlander/model.rb', line 56

def initialize(options = {})
  options = options.dup
  storage_type = options.delete(:storage) || "memory"
  storage_name = options.delete(:name)

  @rdf_storage = Redland.librdf_new_storage(Redlander.rdf_world,
                                            storage_type.to_s,
                                            storage_name.to_s,
                                            Redlander.to_rdf_options(options))
  raise RedlandError, "Failed to initialize '#{storage_name}' storage (type: #{storage_type})" if @rdf_storage.null?
  ObjectSpace.define_finalizer(self, self.class.send(:finalize_storage, @rdf_storage))

  @rdf_model = Redland.librdf_new_model(Redlander.rdf_world, @rdf_storage, "")
  raise RedlandError, "Failed to create a new model" if @rdf_model.null?

  ObjectSpace.define_finalizer(self, self.class.send(:finalize_model, @rdf_model))
end

Instance Attribute Details

#rdf_modelObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/redlander/model.rb', line 13

def rdf_model
  @rdf_model
end

Instance Method Details

#merge(model) ⇒ self

Merge statements from another model (duplicates and invalid statements are skipped)

Parameters:

Returns:

  • (self)

Raises:



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/redlander/model.rb', line 126

def merge(model)
  rdf_stream = Redland.librdf_model_as_stream(model.rdf_model)
  raise RedlandError, "Failed to convert model to a stream" if rdf_stream.null?

  begin
    Redland.librdf_model_add_statements(@rdf_model, rdf_stream)
    self
  ensure
    Redland.librdf_free_stream(rdf_stream)
  end
end

#query(q, options = {}, &block) ⇒ void

Note:

The returned value is determined by the type of the query:

  • Boolean

    for SPARQL ASK queries (ignores block, if given)

  • Redlander::Model

    for SPARQL CONSTRUCT queries

    if given a block, yields the constructed statements to it instead

  • Array<Hash>

    for SPARQL SELECT queries

    where hash values are Redlander::Node instances; if given a block, yields each binding hash to it

  • nil, if query fails

This method returns an undefined value.

Query the model RDF graph using a query language

Parameters:

  • q (String)

    the text of the query

  • options (Hash<Symbol => [String, URI]>) (defaults to: {})

    options for the query

Options Hash (options):

  • :language (String)

    language of the query, one of:

    • “sparql10” SPARQL 1.0 W3C RDF Query Language (default)

    • “sparql” SPARQL 1.1 (DRAFT) Query and Update Languages

    • “sparql11-query” SPARQL 1.1 (DRAFT) Query Language

    • “sparql11-update” SPARQL 1.1 (DRAFT) Update Language

    • “laqrs” LAQRS adds to Querying RDF in SPARQL

    • “rdql” RDF Data Query Language (RDQL)

  • :language_uri (String)

    URI of the query language, if applicable

  • :base_uri (String)

    base URI of the query, if applicable

Raises:



116
117
118
119
# File 'lib/redlander/model.rb', line 116

def query(q, options = {}, &block)
  query = Query::Results.new(q, options)
  query.process(self, &block)
end

#sizeFixnum

Size of the model, in statements.

Returns:

  • (Fixnum)


87
88
89
90
# File 'lib/redlander/model.rb', line 87

def size
  s = Redland.librdf_model_size(@rdf_model)
  s < 0 ? statements.count : s
end

#statementsModelProxy

Statements contained in the model.

Similar to Ruby on Rails, a proxy object is actually returned, which delegates methods to Statement class.

Returns:



80
81
82
# File 'lib/redlander/model.rb', line 80

def statements
  ModelProxy.new(self)
end

#transaction {|| ... } ⇒ void

Note:

Does not work for all storages, in which case the changes are instanteous

This method returns an undefined value.

Wrap changes to the given model in a transaction. If an exception is raised in the block, the transaction is rolled back.

Yield Parameters:

  • (void)


145
146
147
148
149
150
151
152
153
154
155
# File 'lib/redlander/model.rb', line 145

def transaction
  if block_given?
    transaction_start
    result = yield
    transaction_commit
    result
  end
rescue
  transaction_rollback
  raise
end

#transaction_commitBoolean

Commit a transaction, if it is supported by the backend storage.

Returns:

  • (Boolean)


175
176
177
# File 'lib/redlander/model.rb', line 175

def transaction_commit
  Redland.librdf_model_transaction_commit(@rdf_model).zero?
end

#transaction_commit!true

Commit a transaction.

Returns:

  • (true)

Raises:

  • (RedlandError)

    if it is not supported by the backend storage



183
184
185
# File 'lib/redlander/model.rb', line 183

def transaction_commit!
  raise RedlandError, "Failed to commit the transaction" unless transaction_commit
end

#transaction_rollbackBoolean

Rollback a transaction, if it is supported by the backend storage.

Returns:

  • (Boolean)


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

def transaction_rollback
  Redland.librdf_model_transaction_rollback(@rdf_model).zero?
end

#transaction_rollback!true

Rollback a transaction.

Returns:

  • (true)

Raises:

  • (RedlandError)

    if it is not supported by the backend storage



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

def transaction_rollback!
  raise RedlandError, "Failed to rollback the latest transaction" unless transaction_rollback
end

#transaction_startBoolean

Start a transaction, if it is supported by the backend storage.

Returns:

  • (Boolean)


160
161
162
# File 'lib/redlander/model.rb', line 160

def transaction_start
  Redland.librdf_model_transaction_start(@rdf_model).zero?
end

#transaction_start!true

Start a transaction.

Returns:

  • (true)

Raises:

  • (RedlandError)

    if it is not supported by the backend storage



168
169
170
# File 'lib/redlander/model.rb', line 168

def transaction_start!
  raise RedlandError, "Failed to initialize a transaction" unless transaction_start
end