Redland RDF Language Bindings - Ruby RDoc - Class: Redland::Model

### Home Classes Methods

### In Files

- rdf/redland/model.rb

### Parent

Object

### Methods

- ::create_finalizer
- ::new
- #add
- #add_statement
- #add_statements
- #as_stream
- #contexts
- #create_resource
- #delete
- #delete_context
- #delete_statement
- #dump_model
- #find
- #get_resource
- #include?
- #include_statement?
- #load
- #object
- #parse_and_merge
- #predicates
- #query_execute
- #rewrite
- #save
- #size
- #smush
- #statement_array_or_yield
- #statements
- #subject
- #subjects
- #to_string
- #triples
- #triples_with_context
- #yield_node_or_array

### Included Modules

- Redland::Util

### Files

- Makefile
- Makefile
- Makefile
- Makefile

### Class/Module Index

Quicksearch

- Redland
- Redland::BNode
- Redland::ContextParser
- Redland::FileStore
- Redland::HashOpen
- Redland::HashStore
- Redland::Literal
- Redland::MemoryStore
- Redland::MergedModel
- Redland::Model
- Redland::Namespace
- Redland::Node
- Redland::NodeIterator
- Redland::NodeTypeError
- Redland::OWL
- Redland::Parser
- Redland::Query
- Redland::QueryResults
- Redland::RDFS
- Redland::RedlandError
- Redland::Resource
- Redland::Serializer
- Redland::Statement
- Redland::Stream
- Redland::TripleStore
- Redland::Uri
- Redland::Util
- Redland::World
- DC
- FOAF

No matching classes.

# Redland::Model

### Attributes

model[R]

### Public Class Methods

create_finalizer(model) click to toggle source

You shouldn't use this. Used internally for cleanup.

    # File rdf/redland/model.rb, line 27
    def Model.create_finalizer(model)
      proc {|id| # "Finalizer on #{id}"
        #$log_final.info "closing model"
        Redland::librdf_free_model(model)
      }
    end

new(store=nil) click to toggle source

Constructor - takes an optional argument store, which should be one of the storage classes (all derived from TripleStore)

    # File rdf/redland/model.rb, line 17
    def initialize(store=nil)
      @statements = []
      store = MemoryStore.new() if not store
      @model = Redland.librdf_new_model($world.world,store.store,"")
      raise RedlandError.new("Creating new Model failed") if not @model
      @store = store
      ObjectSpace.define_finalizer(self,Model.create_finalizer(@model))
    end

### Public Instance Methods

add(subject,predicate,object,context=nil) click to toggle source

Add the triples (s,p,o) to the model with the optional context

    # File rdf/redland/model.rb, line 71
    def add(subject,predicate,object,context=nil)
      my_statement = Statement.new(subject,predicate,object)
      self.add_statement(my_statement,context)
    end

add_statement(statement,context=nil) click to toggle source

Add the Statement to the Model with optional context Node

    # File rdf/redland/model.rb, line 60
    def add_statement(statement,context=nil)
      if context
        context = Node.ensure(context)
        raise RedlandError.new("Cannot make a Node from an object of #{context.class}") if not context
        return Redland.librdf_model_context_add_statement(self.model,context,statement.statement)
      else
        return Redland.librdf_model_add_statement(self.model,statement.statement)
      end
    end

add_statements(statement_stream,context=nil) click to toggle source

Add the Stream of Statements to the Model with optional context Node

    # File rdf/redland/model.rb, line 78
    def add_statements(statement_stream,context=nil)
      if context
        context = Node.ensure(context)
        raise RedlandError.new("Cannot make a Node from an object of #{context.class}") if not context
        Redland.librdf_model_context_add_statements(self.model,context,statement_stream.stream)
      else
        return Redland.librdf_model_add_statements(self.model,statement_stream.stream)
      end
    end

as_stream(context=nil) click to toggle source

list the model contents as a stream of statements

    # File rdf/redland/model.rb, line 188
    def as_stream(context=nil)
      if context
        context = Node.ensure(context)
        raise RedlandError.new("Cannot make a Node from an object of #{context.class}") if not context
        my_stream = Redland.librdf_model_context_as_stream(self.model,context)
      else
        my_stream = Redland.librdf_model_as_stream(self.model)
      end
      return Stream.new(my_stream,self)
    end

contexts() click to toggle source

yield the contexts defined in the model. If the store wasn't set up for contexts raises a RedlandError. If no block_given returns an array of the contexts

    # File rdf/redland/model.rb, line 352
    def contexts()
      my_iterator = Redland.librdf_model_get_contexts(@model)
      raise RedlandError.new("Unable to create iterator for contexts") if !my_iterator
      iterator= NodeIterator.new(my_iterator,self)
      if block_given?
        while not iterator.end?
          yield iterator.current
          iterator.next
        end
      else
        context_array = []
        while not iterator.end?
          context_array << iterator.current
          iterator.next
        end
        return context_array
      end
    end

create_resource(str=nil,context=nil) click to toggle source

create a resource for this model. The str can be a String, or a Redland::Uri

    # File rdf/redland/model.rb, line 36
    def create_resource(str=nil,context=nil)
      resource = Resource.new(str,self,context)
      if context
        resource.set_context(Resource.new(context))
      end
      return resource
    end

delete(s,p,o,context=nil) click to toggle source

Remove the statement made of the triples (s,p,o) with the optional context Node

    # File rdf/redland/model.rb, line 156
    def delete(s,p,o,context=nil)
      if [s,p,o].all? { |e| e }
        statement = Statement.new(s,p,o)
        self.delete_statement(statement,context)
      else
        self.find(s,p,o,context) do |ds, dp, d_o|
          st = Statement.new(ds, dp, d_o)
          Kernel.p st
          self.delete_statement(st, context)
        end
      end
    end

delete_context(context) click to toggle source

Remove the Statements from the Model with the given context Node

    # File rdf/redland/model.rb, line 170
    def delete_context(context)
      context = Node.ensure(context)
      raise RedlandError.new("Cannot make a Node from an object of #{context.class}") if not context
      return Redland.librdf_model_context_remove_statements(self.model,context)
    end

delete_statement(statement,context=nil) click to toggle source

Remove the Statement from the Model with the optional context Node

    # File rdf/redland/model.rb, line 145
    def delete_statement(statement,context=nil)
      if context
        context = Node.ensure(context)
        RedlandError.new("Cannot make a Node from an object of #{context.class}") if not context
        return Redland.librdf_model_context_remove_statement(self.model,context,statement.statement)
      else
        return Redland.librdf_model_remove_statement(self.model,statement.statement)
      end
    end

dump_model(context=nil) click to toggle source

a screen dump of the triples, if context is included a screen dump of the triples for the given context

    # File rdf/redland/model.rb, line 380
    def dump_model(context=nil)
      self.triples(context){|s,p,o| puts "#{s}:#{p}:#{o}"}
    end

find(s=nil,p=nil,o=nil,context=nil) click to toggle source

find the statements matching the given triple. The triples can be nil

Find all triples with FOAF of Dominic

    model.find(nil,FOAF['firstName'],'Dominic Sisneros'){|s,p,o ...}

    # File rdf/redland/model.rb, line 238
    def find(s=nil,p=nil,o=nil,context=nil)
      statement = Statement.new(s,p,o,self)
      if context
        context = Node.ensure(context)
        raise RedlandError.new("Cannot make a Node from an object of #{context.class}") if not context
        my_stream = Redland.librdf_model_find_statements_in_context(self.model,statement.statement,context)
      else
        my_stream = Redland.librdf_model_find_statements(self.model,statement.statement)
      end
      return nil if not my_stream
      stream = Stream.new(my_stream,self)
      if block_given?
        while not stream.end?
          my_statement = stream.current
          yield my_statement.subject,my_statement.predicate,my_statement.object
          stream.next
        end
      else #block not given
        return get_statement_array(stream)
      end
    end

get_resource(arg) click to toggle source

get a resource given by str. If arg is a string it will create a Resource from it.

    # File rdf/redland/model.rb, line 46
    def get_resource(arg)
      res = self.find(Resource.new(arg),nil,nil)
      return (res.nil? || res[0].nil?) ? nil : res[0].subject
    end

include?(s,p,o) click to toggle source

Returns true if the given Triple is in the model

    # File rdf/redland/model.rb, line 182
    def include?(s,p,o)
      statement = Statement.new(s,p,o)
      self.include_statement?(statement)
    end

include_statement?(statement) click to toggle source

Returns true if the Statement is in the Model

    # File rdf/redland/model.rb, line 177
    def include_statement?(statement)
      return (Redland.librdf_model_contains_statement(self.model,statement.statement) != 0)
    end

load(uri, name="", mime_type="", type_uri=nil) click to toggle source

    # File rdf/redland/model.rb, line 433
    def load(uri, name="", mime_type="", type_uri=nil)
      ruri = uri.uri
      rtype_uri = type_uri.nil? ? nil : type_uri.uri
      return Redland.librdf_model_load(@model, ruri, name, mime_type, rtype_uri)
    end

object(source,predicate) click to toggle source

Return one Node in the Model matching (source, predicate,?) The source and predicate can be a Node or Uri

    # File rdf/redland/model.rb, line 266
    def object(source,predicate)
      if source.class == Uri then source = Node.new(source) end
      if predicate.class == Uri then predicate = Node.new(predicate) end
      my_node = Redland.librdf_model_get_target(self.model,source.node,predicate.node)
      return nil if not my_node
      return Literal.from_node(my_node) if is_literal?(my_node)
      return Resource.new(my_node,self,false)
    end

parse_and_merge(parser,uri,predlist,context=uri,st=nil) click to toggle source

parser the file with the given parser and uri and smushes the file with the model on the statements with the given predlist

    # File rdf/redland/model.rb, line 387
    def parse_and_merge(parser,uri,predlist,context=uri,st=nil)
      context = Uri.new(context) if context == uri
      temp_model = MergedModel.new(self)
      if st
        temp_model.add_statement(st)
      else
        parser.parse_as_stream(uri){|s|temp_model.add_statement(s)}
      end
      temp_model.find_canonical(predlist)
      temp_model.rewrite(context)
      if temp_model.global_rewrites.size > 0
        print "***** GLOBAL REWRITES UNDERWAY ***"
        #self.rewrite(temp_model.global_rewrites)
      end
    end

predicates(source,target) click to toggle source

Return an array of Predicate Nodes in the Model matching (subject,?,target) The subject and target can be a Node or Uri If block_given? yields the Subject Node

    # File rdf/redland/model.rb, line 317
    def predicates(source,target)
      if source.class == Uri
        source = Node.new(source)
      end
      if target.class == Uri or target.class == String
        target =Node.new(target)
      end
      my_iterator = Redland.librdf_model_get_arcs(self.model,source.node,target.node)
      raise RedlandError.new("unable to create iterator") if !my_iterator
      iterator = NodeIterator.new(my_iterator,self,source,target)
      if block_given?
        while not iterator.end?
          yield iterator.current
          iterator.next
        end
      else
        return get_node_array(iterator)
      end
    end

query_execute(query) click to toggle source

execute a query

    # File rdf/redland/model.rb, line 416
    def query_execute(query)
      results=Redland.librdf_model_query_execute(@model, query.query)
      if not results
        return nil
      else
        return QueryResults.new(results)
      end
    end

rewrite(change_hash) click to toggle source

clears the transactions (?? confirm)

    # File rdf/redland/model.rb, line 404
    def rewrite(change_hash)
      transactions = []
    end

save(filename) click to toggle source

saves this model to the specified filename

    # File rdf/redland/model.rb, line 373
    def save(filename)
      serializer = Serializer.new()
      serializer.to_file(filename,self)
    end

size() click to toggle source

Return the size of the Storage Model. Raises a redland error if the model has non countable storage

    # File rdf/redland/model.rb, line 53
    def size()
      s = get_size()
      raise RedlandError.new("Attempt to get size when using non-countable storage") if s < 0
      return s
    end

smush(pred) click to toggle source

Effectively removes a Predicate from the Model by replacing all occurrences with a blank node (?? confirm)

    # File rdf/redland/model.rb, line 90
    def smush(pred)
      identifying_hash = {}
      canonical = {}
      mapme = []
      transactions = []
      self.find(nil,pred,nil){|sub,pred,obj|
        if not identifying_hash.has_key?(obj.to_s)
          identifying_hash[obj.to_s] = sub
        elsif identifying_hash[obj.to_s] != sub
          subject_string = sub.to_s
          if not canonical.has_key?(subject_string)
            # print "amodule Rdf dding mapping: ",subject_string,"--->",identifying_hash[obj.to_s]
            canonical[subject_string] = identifying_hash[obj.to_s]
            mapme << Node.new(sub)
          end
        end
      }

      self.triples(){ |sub,pred,obj|
        ss = sub.to_s
        os = obj.to_s
        new_statement = nil
        replace = false

        if canonical.has_key?(ss)
          if not replace
            new_statement = Statement.new(sub,pred,obj)
            replace = 1
          end
          new_statement.subject = canonical[ss]
        end
        if canonical.has_key?(os)
          if not replace
            new_statement = Statement.new(sub,pred,obj)
            replace = 1
            new_statement.object = canonical[os]
          end
        end

        if replace

          transactions << [Statement.new(sub,pred,obj),new_statement]
        end
      }

      transactions.each{ |pair|
        self.delete_statement(pair[0])
        self.add_statement(pair[1])
      }
    end

statement_array_or_yield(stream) click to toggle source

    # File rdf/redland/model.rb, line 260
    def statement_array_or_yield(stream)

    end

statements(context = nil) click to toggle source

Yield the statements if a block is given.

    # File rdf/redland/model.rb, line 216
    def statements(context = nil)
      stream = self.as_stream(context)
      while not stream.end?
        yield stream.current
        stream.next
      end
    end

subject(predicate,target) click to toggle source

Return one Node in the Model matching (?,predicate,target) The source and predicate can be a Node or Uri

    # File rdf/redland/model.rb, line 301
    def subject(predicate,target)
      if predicate.class == Uri
        predicate = Node.new(predicate)
      end
      if target.class == Uri or target.class == String
        target = Node.new(target)
      end
      my_node = Redland.librdf_model_get_source(self.model,predicate.node,target.node)
      return nil if !my_node
      return Literal.from_node(my_node) if is_literal?(my_node)
      return Resource.new(my_node,self,false)
    end

subjects(predicate,target) click to toggle source

Return an array of Subject Nodes in the Model matching (?,predicate,target) The predicate and target can be a Node or Uri If block_given? yields the Subject Node

    # File rdf/redland/model.rb, line 278
    def subjects(predicate,target)
      if predicate.class == Uri
        predicate = Node.new(predicate)
      end
      if target.class == Uri or target.class == String
        target = Node.new(target)
      end
      my_iterator = Redland.librdf_model_get_sources(self.model,predicate.node,target.node)
      raise RedlandError.new("Unable to create iterator") if !my_iterator
      iterator = NodeIterator.new(my_iterator,self,predicate,target)
      if block_given?
        while not iterator.end?
          yield iterator.current
          iterator.next
        end
      else
        return get_node_array(iterator)
      end

    end

to_string(name="", base_uri=nil, mime_type="", type_uri=nil) click to toggle source

Serialize the Model to a syntax

    # File rdf/redland/model.rb, line 426
    def to_string(name="", base_uri=nil, mime_type="", type_uri=nil)
      rbase_uri = base_uri.nil? ? nil : base_uri.uri
      rtype_uri = type_uri.nil? ? nil : type_uri.uri

      return Redland.librdf_model_to_string(@model, rbase_uri, name, mime_type, rtype_uri)
    end

triples(context=nil) click to toggle source

Yield the triples subject, predicate, and object if a block is given. Otherwise return an array of the statements

    # File rdf/redland/model.rb, line 202
    def triples(context=nil)
      stream = self.as_stream(context)
      return nil if not stream
      if block_given?
        while not stream.end?
          yield stream.current.subject ,stream.current.predicate,stream.current.object
          stream.next
        end
      else
        return get_statement_array(stream)
      end
    end

triples_with_context(context=nil) click to toggle source

Yield the triples as well as the context

    # File rdf/redland/model.rb, line 225
    def triples_with_context(context=nil)
      stream = self.as_stream(context)
      while not stream.end?
        statement = stream.current
        yield statement.subject, statement.predicate, statement.object, stream.context
        stream.next
      end
    end

yield_node_or_array(iterator) click to toggle source

    # File rdf/redland/model.rb, line 337
    def yield_node_or_array(iterator)
      iterator = NodeIterator.new(my_iterator,self,source,target)
      if block_given?
        while not iterator.end?
          yield iterator.current
          iterator.next
        end
      else
        return get_node_array(iterator)
      end
    end

Go to Redland Home - Language Bindings Home - Ruby API Home

(C) Copyright 2004-2013 Dave Beckett, (C) Copyright 2004-2005 University of Bristol