Class: RDF::Graph::Memory

Inherits:
Base
  • Object
show all
Includes:
Query::Executer
Defined in:
lib/rdf/graph/memory.rb

Overview

An in-memory, pure Ruby implementation of an RDF graph. This graph implementation also serves as an example of the interface that every graph implementation should have.

Instance Method Summary collapse

Methods included from Query::Executer

#execute, #match

Methods inherited from Base

#empty?, #export, #import, #merge, #new_blank_node

Constructor Details

#initializeMemory

Creates a new Graph.



10
11
12
13
14
15
16
# File 'lib/rdf/graph/memory.rb', line 10

def initialize
  super
  @triples = Set.new
  @idx_s = Hash.new{|h, k| h[k] = Set.new}
  @idx_sp = Hash.new{|h, k| h[k] = Set.new}
  @idx_blank = Hash.new(0)
end

Instance Method Details

#add(*triple) ⇒ Object

Adds a triple to the graph. Will accept an instance of Triple, or three parameters for the subject, predicate, and object.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rdf/graph/memory.rb', line 19

def add(*triple)
  triple = Triple.construct(*triple)
  
  # perform some syntax checks
  raise RDF::UnassociatedBlankNodeError, "#{triple.subject} is not associated with this graph" if RDF::BlankNode?(triple.subject) && triple.subject.graph != self
  raise RDF::UnassociatedBlankNodeError, "#{triple.object} is not associated with this graph" if RDF::BlankNode?(triple.object) && triple.object.graph != self
  
  index_subject(triple)
  index_subject_and_predicate(triple)
  index_blank(triple.subject) if RDF::BlankNode?(triple.subject)
  index_blank(triple.object) if RDF::BlankNode?(triple.object)
  @triples << triple
end

#delete(*triple) ⇒ Object

Deletes triple from the graph. Will accept an instance of Triple, or three parameters for the subject, predicate, and object.



34
35
36
37
38
39
40
41
# File 'lib/rdf/graph/memory.rb', line 34

def delete(*triple)
  triple = Triple.construct(*triple)
  unindex_subject(triple)
  unindex_subject_and_predicate(triple)
  unindex_blank(triple.subject) if RDF::BlankNode?(triple.subject)
  unindex_blank(triple.object) if RDF::BlankNode?(triple.object)
  @triples.delete(triple)
end

#each(&b) ⇒ Object

Iterates through all of the triples in this graph.



50
51
52
# File 'lib/rdf/graph/memory.rb', line 50

def each(&b)
  @triples.each(&b)
end

#include?(*triple) ⇒ Boolean

Returns true if graph contains triple. Will accept an instance of Triple, or three parameters for the subject, predicate, and object.

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/rdf/graph/memory.rb', line 44

def include?(*triple)
  triple = Triple.construct(*triple)
  @triples.include?(triple)
end

#sizeObject

Returns the number of triples in this graph.



55
56
57
# File 'lib/rdf/graph/memory.rb', line 55

def size
  @triples.size
end