Class: RDF::N3::Repository

Inherits:
Repository
  • Object
show all
Defined in:
lib/rdf/n3/repository.rb

Overview

Sub-class of RDF::Repository which allows for native lists in different positions.

Constant Summary collapse

DEFAULT_GRAPH =
false

Instance Method Summary collapse

Constructor Details

#initialize(uri: nil, title: nil, **options) {|repository| ... } ⇒ Repository

Initializes this repository instance.

Parameters:

  • uri (URI, #to_s) (defaults to: nil)

    (nil)

  • title (String, #to_s) (defaults to: nil)

    (nil)

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :with_graph_name (Boolean) — default: true

    Indicates that the repository supports named graphs, otherwise, only the default graph is supported.

  • :with_validity (Boolean) — default: true

    Indicates that the repository supports named validation.

  • :transaction_class (Boolean) — default: DEFAULT_TX_CLASS

    Specifies the RDF::Transaction implementation to use in this Repository.

Yields:

  • (repository)

Yield Parameters:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rdf/n3/repository.rb', line 22

def initialize(uri: nil, title: nil, **options, &block)
  @data = options.delete(:data) || {}
  super do
    if block_given?
      case block.arity
        when 1 then block.call(self)
        else instance_eval(&block)
      end
    end
  end
end

Instance Method Details

#apply_changeset(changeset) ⇒ Object

See Also:

  • Mutable#apply_changeset


155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rdf/n3/repository.rb', line 155

def apply_changeset(changeset)
  data = @data
  changeset.deletes.each do |del|
    if del.constant?
      data = delete_from(data, del)
    else
      # we need this condition to handle wildcard statements
      query_pattern(del) { |stmt| data = delete_from(data, stmt) }
    end
  end
  changeset.inserts.each { |ins| data = insert_to(data, ins) }
  @data = data
end

#countObject

See Also:

  • Countable#count


62
63
64
65
66
67
68
69
70
# File 'lib/rdf/n3/repository.rb', line 62

def count
  count = 0
  @data.each do |_, ss|
    ss.each do |_, ps|
      ps.each { |_, os| count += os.size }
    end
  end
  count
end

#each_expanded_statement {|RDF::Statement| ... } ⇒ Object

Projects statements with lists expanded to first/rest chains

Yields:



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rdf/n3/repository.rb', line 128

def each_expanded_statement(&block)
  if block_given?
    each_statement do |st|
      if st.subject.list?
        st.subject.each_statement(&block)
        st.subject = st.subject.subject
      end
      if st.object.list?
        st.object.each_statement(&block)
        st.object = st.object.subject
      end
      block.call(st)
    end
  end
  enum_for(:each_expanded_statement) unless block_given?
end

#each_graph(&block) ⇒ Object

See Also:

  • Enumerable#each_graph


89
90
91
92
93
94
95
96
# File 'lib/rdf/n3/repository.rb', line 89

def each_graph(&block)
  if block_given?
    @data.each_key do |gn|
      yield RDF::Graph.new(graph_name: (gn == DEFAULT_GRAPH ? nil : gn), data: self)
    end
  end
  enum_graph
end

#each_statement(&block) ⇒ Object Also known as: each

See Also:

  • Enumerable#each_statement


108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rdf/n3/repository.rb', line 108

def each_statement(&block)
  if block_given?
    @data.each do |g, ss|
      ss.each do |s, ps|
        ps.each do |p, os|
          os.each do |o, object_options|
            yield RDF::Statement.new(s, p, o, object_options.merge(graph_name: g.equal?(DEFAULT_GRAPH) ? nil : g))
          end
        end
      end
    end
  end
  enum_statement
end

#expanded_statementsArray<RDF::Statement>

Returns the expanded statements for this repository

Returns:



149
150
151
# File 'lib/rdf/n3/repository.rb', line 149

def expanded_statements
  each_expanded_statement.to_a
end

#graph_names(options = nil, &block) ⇒ Object

See Also:

  • Enumerable#each_graph


82
83
84
# File 'lib/rdf/n3/repository.rb', line 82

def graph_names(options = nil, &block)        
  @data.keys.reject { |g| g == DEFAULT_GRAPH }.to_a
end

#has_graph?(graph) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#has_graph?


75
76
77
# File 'lib/rdf/n3/repository.rb', line 75

def has_graph?(graph)
  @data.has_key?(graph)
end

#has_statement?(statement) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • Enumerable#has_statement?


101
102
103
# File 'lib/rdf/n3/repository.rb', line 101

def has_statement?(statement)
  has_statement_in?(@data, statement)
end

#isolation_levelObject

See Also:

  • Dataset#isolation_level


171
172
173
# File 'lib/rdf/n3/repository.rb', line 171

def isolation_level
  :serializable
end

#supports?(feature) ⇒ Boolean

Returns ‘true` if this respository supports the given `feature`.

This repository supports list_terms.

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/rdf/n3/repository.rb', line 38

def supports?(feature)
  case feature.to_sym
  when :list_terms       then true
  when :rdfstar          then true
  when :snapshots        then false
  else super
  end
end

#to_queryRDF::Query

Creates a query from the statements in this repository, turning blank nodes into non-distinguished variables. This can be used to determine if this repository is logically a subset of another repository.

Returns:



51
52
53
54
55
56
57
# File 'lib/rdf/n3/repository.rb', line 51

def to_query
  RDF::Query.new do |query|
    each do |statement|
      query.pattern RDF::Query::Pattern.from(statement, ndvars: true)
    end
  end
end