Class: RdfContext::ConjunctiveGraph

Inherits:
Graph show all
Defined in:
lib/rdf_context/conjunctive_graph.rb

Overview

ConjunctiveGraph - The top level container for all named Graphs sharing a Store

A ConjuctiveGraph is a graph that can contain other graphs. Graphs are kept distinct by a context, which is the identifier of the sub-graph. It is the union of all graphs in a Store.

For the sake of persistence, Conjunctive Graphs must be distinguished by identifiers (that may not necessarily be RDF identifiers or may be an RDF identifier normalized - SHA1/MD5 perhaps - for database naming purposes ) which could be referenced to indicate conjunctive queries (queries made across the entire conjunctive graph) or appear as nodes in asserted statements. In this latter case, such statements could be interpreted as being made about the entire ‘known’ universe.

Instance Attribute Summary

Attributes inherited from Graph

#allow_n3, #identifier, #store

Instance Method Summary collapse

Methods inherited from Graph

#<<, #==, #[], #add, #add_seq, #add_triple, #bind, #bnodes, #close, #commit, #contains?, #context_aware?, #destroy, #get_by_type, #graph?, #has_bnode_identifier?, #hash, #inspect, #isomorphic?, #merge!, #n3, #namespace, #nsbinding, #objects, #open, #predicates, #prefix, #properties, #qname, #remove, #rollback, #seq, #serialize, #size, #subjects, #sync_properties, #to_ntriples, #to_rdfxml, #to_s, #type_of, #uri_binding

Methods inherited from Resource

#bnode?, #graph?, #literal?, parse, #resource?, #uri?

Constructor Details

#initialize(options = {}) ⇒ ConjunctiveGraph

Store for ConjunctiveGraph must support contexts.



14
15
16
17
18
19
20
21
# File 'lib/rdf_context/conjunctive_graph.rb', line 14

def initialize(options = {})
  unless options[:store] && options[:store].context_aware?
    raise GraphException.new("ConjunctiveGraph requires store supporting contexts")
  end

  super(:identifier => options[:store].identifier, :store => options[:store])
  @context_aware = true
end

Instance Method Details

#add_quad(subject, predicate, object, context) ⇒ Graph

Adds a quad from the intended subject, predicate, object, and context.

Examples:

g = Graph.new
cg = ConjunctiveGraph.new
cg.add_quad(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new, g)
# => results in the triple being added to g

Parameters:

Returns:

  • (Graph)

    Returns the graph

Raises:

  • (Error)

    Checks parameter types and raises if they are incorrect.



57
58
59
60
61
62
63
# File 'lib/rdf_context/conjunctive_graph.rb', line 57

def add_quad(subject, predicate, object, context)
  graph = context if context.is_a?(Graph)
  graph ||= contexts.detect {|g| g.identifier == context}
  graph ||= Graph.new(:identifier => context, :store => @store)
  graph.add_triple(subject, predicate, object)
  graph
end

#contextsObject

Contexts contained within the store



30
31
32
# File 'lib/rdf_context/conjunctive_graph.rb', line 30

def contexts
  @store.contexts
end

#default_contextObject

The default_context is a Graph having an identifier the same as the identifier of the store.



25
26
27
# File 'lib/rdf_context/conjunctive_graph.rb', line 25

def default_context
  @@default_context = Graph.new(:identifier => @store.identifier, :store => @store)
end

#parse(stream, uri, options = {}, &block) ⇒ Graph

Parse source into a new context.

Create a new context (Graph) and parse into that.

Parameters:

  • stream (IO, String)

    the RDF IO stream, string, Nokogiri::HTML::Document or Nokogiri::XML::Document

  • uri (URIRef, String)

    the URI of the document

  • options:: (Hash)

    Options from

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

    a customizable set of options

Options Hash (options):

  • :debug (Array) — default: nil

    Array to place debug messages

  • :type (:rdfxml, :html, :n3) — default: nil
  • :strict (Boolean) — default: false

    Raise Error if true, continue with lax parsing, otherwise

Returns:

  • (Graph)

    Returns the graph containing parsed triples



76
77
78
79
# File 'lib/rdf_context/conjunctive_graph.rb', line 76

def parse(stream, uri, options = {}, &block) # :yields: triple
  graph = Graph.new(:identifier => uri, :store => self.store)
  Parser.parse(stream, uri, options.merge(:graph => graph), &block)
end

#triples(triple = Triple.new(nil, nil, nil), &block) ⇒ Array

Triples across all contexts in store, optionally matching subject, predicate, or object. Delegates to Store#triples.

Parameters:

  • triple (Triple) (defaults to: Triple.new(nil, nil, nil))

    (nil) Triple to match, may be a pattern triple or nil

Returns:

  • (Array)

    List of matched triples



39
40
41
# File 'lib/rdf_context/conjunctive_graph.rb', line 39

def triples(triple = Triple.new(nil, nil, nil), &block) # :yields: triple, context
  @store.triples(triple, nil, &block) || []
end