Class: Redlander::Model
- Inherits:
-
Object
- Object
- Redlander::Model
- Includes:
- Parsing, Serializing
- Defined in:
- lib/redlander/model.rb
Overview
The core object incorporating the repository of RDF statements.
Instance Attribute Summary collapse
- #rdf_model ⇒ Object readonly private
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Model
constructor
Create a new RDF model.
-
#merge(model) ⇒ self
Merge statements from another model (duplicates and invalid statements are skipped).
-
#query(q, options = {}, &block) ⇒ void
Query the model RDF graph using a query language.
-
#size ⇒ Fixnum
Size of the model, in statements.
-
#statements ⇒ ModelProxy
Statements contained in the model.
-
#transaction {|| ... } ⇒ void
Wrap changes to the given model in a transaction.
-
#transaction_commit ⇒ Boolean
Commit a transaction, if it is supported by the backend storage.
-
#transaction_commit! ⇒ true
Commit a transaction.
-
#transaction_rollback ⇒ Boolean
Rollback a transaction, if it is supported by the backend storage.
-
#transaction_rollback! ⇒ true
Rollback a transaction.
-
#transaction_start ⇒ Boolean
Start a transaction, if it is supported by the backend storage.
-
#transaction_start! ⇒ true
Start a transaction.
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)
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( = {}) = .dup storage_type = .delete(:storage) || "memory" storage_name = .delete(:name) @rdf_storage = Redland.librdf_new_storage(Redlander.rdf_world, storage_type.to_s, storage_name.to_s, Redlander.()) 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_model ⇒ Object (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)
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
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
116 117 118 119 |
# File 'lib/redlander/model.rb', line 116 def query(q, = {}, &block) query = Query::Results.new(q, ) query.process(self, &block) end |
#size ⇒ Fixnum
Size of the model, in statements.
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 |
#statements ⇒ ModelProxy
Statements contained in the model.
Similar to Ruby on Rails, a proxy object is actually returned, which delegates methods to Statement class.
80 81 82 |
# File 'lib/redlander/model.rb', line 80 def statements ModelProxy.new(self) end |
#transaction {|| ... } ⇒ void
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.
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_commit ⇒ Boolean
Commit a transaction, if it is supported by the backend storage.
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.
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_rollback ⇒ Boolean
Rollback a transaction, if it is supported by the backend storage.
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.
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_start ⇒ Boolean
Start a transaction, if it is supported by the backend storage.
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.
168 169 170 |
# File 'lib/redlander/model.rb', line 168 def transaction_start! raise RedlandError, "Failed to initialize a transaction" unless transaction_start end |