Class: RDF::Graph::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/graph/base.rb

Overview

Base class for graph implementations. This class provides basic functionality and common interface for all graphs.

Direct Known Subclasses

Memory

Instance Method Summary collapse

Instance Method Details

#eachObject



95
96
97
98
99
100
# File 'lib/rdf/graph/base.rb', line 95

def each
  result = execute(RDF::Query.new.where(:s, :p, :o))
  result.bindings.each do |b|
    yield Triple.new(b[:s], b[:p], b[:o])
  end
end

#empty?Boolean

Returns true if this graph is empty, false otherwise.

Returns:

  • (Boolean)


103
104
105
# File 'lib/rdf/graph/base.rb', line 103

def empty?
  size == 0
end

#export(format = :ntriples, file = nil) ⇒ Object

Exports the triples for this graph in format to file. If file is nil, then the export is returned as a string.

There is one format provided by default (:ntriples), but the supported formats are depended upon the particular graph implementation, and graph implementations can override this method to use a (presumably) faster export function from the underlying triple store–as long as it preserves the interface expectations.

If a graph implementation overrides this method, and the underlying triple store does not support one of the default formats (see RDF::Format), then it is recommended that the implementation make use of the RDF::Format classes to provide exports for the default formats.

Raises

UnsupportedFormatError

if format is not supported for export



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rdf/graph/base.rb', line 23

def export(format = :ntriples, file = nil)
  file ||= StringIO.new
  
  case format
  when :ntriples
    RDF::Format::NTriples.export(self, file)
  else
    raise UnsupportedFormatError
  end
  
  file.string if file.is_a?(StringIO)
end

#import(file, format = :ntriples) ⇒ Object

Imports the triples from file in format to this graph.

This method will not merge the graphs, meaning the blank nodes from file are not renamed as they are imported. If you want to merge two graphs, then use merge.

There is one format provided by default (:ntriples), but the supported formats are depended upon the particular graph implementation, and graph implementations can override this method to use a (presumably) faster import function from the underlying triple store–as long as it preserves the interface expectations.

If a graph implementation overrides this method, and the underlying triple store does not support one of the default formats (see RDF::Format), then it is recommended that the implementation make use of the RDF::Format classes to provide imports for the default formats.

Raises

ArgumentError

if file is nil

UnsupportedFormatError

if format is not supported for import

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
# File 'lib/rdf/graph/base.rb', line 57

def import(file, format = :ntriples)
  raise ArgumentError, "file cannot be blank" if file.nil?
  
  case format
  when :ntriples
    RDF::Format::NTriples.import(file, self)
  else
    raise UnsupportedFormatError
  end
end

#include?(*triple) ⇒ Boolean

Returns true if the triple (s, p, o) is in this graph, false otherwise.

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/rdf/graph/base.rb', line 89

def include?(*triple)
  triple = Triple.construct(*triple)
  return false if variable?(triple.subject) || variable?(triple.object)
  execute(RDF::Query.new.where(triple.subject, triple.predicate, triple.object)).success?
end

#merge(graph) ⇒ Object

Merges graph into the current graph. In doing so it will rename all of the blank nodes so they don’t collide with any existing blank nodes.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rdf/graph/base.rb', line 70

def merge(graph)
  map = {}
  graph.each do |t|
    s, p, o = t.subject, t.predicate, t.object
    if RDF::BlankNode?(s)
      map[s] = new_blank_node(s.name) unless map.key?(s)
      s = map[s]
    end
    if RDF::BlankNode?(o)
      map[o] = new_blank_node(o.name) unless map.key?(o)
      o = map[o]
    end
    
    add(s, p, o)
  end
end

#new_blank_node(name) ⇒ Object

Creates a new BlankNode associated with this graph.



108
109
110
# File 'lib/rdf/graph/base.rb', line 108

def new_blank_node(name)
  RDF::BlankNode.new(name, self)
end