Class: RoadForest::SourceRigor::GraphStore

Inherits:
Object
  • Object
show all
Includes:
RDF::Countable, RDF::Durable, RDF::Enumerable, RDF::Mutable, RDF::Queryable, RDF::Resource, Graph::Normalization
Defined in:
lib/roadforest/source-rigor/graph-store.rb

Constant Summary

Constants included from Graph::Normalization

Graph::Normalization::Vocabs

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Graph::Normalization

#expand_curie, #expand_curie_pair, #interned_uri, #literal, #normalize_context, #normalize_property, #normalize_resource, #normalize_statement, #normalize_term, #normalize_tuple, #normalize_uri, #relevant_prefixes_for_graph, #root_url, #uri, #vocabularies_in_graph

Constructor Details

#initialize(repo = nil) {|_self| ... } ⇒ GraphStore

Returns a new instance of GraphStore.

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
29
30
# File 'lib/roadforest/source-rigor/graph-store.rb', line 24

def initialize(repo = nil)
  @repository = repo || RDF::Repository.new
  @local_context_node = RDF::Node.new(:local)
  @debug_io = nil
  force_impulse
  yield self if block_given?
end

Instance Attribute Details

#current_impulseObject (readonly)

Returns the value of attribute current_impulse.



21
22
23
# File 'lib/roadforest/source-rigor/graph-store.rb', line 21

def current_impulse
  @current_impulse
end

#debug_ioObject

Returns the value of attribute debug_io.



22
23
24
# File 'lib/roadforest/source-rigor/graph-store.rb', line 22

def debug_io
  @debug_io
end

#local_context_nodeObject (readonly)

Returns the value of attribute local_context_node.



21
22
23
# File 'lib/roadforest/source-rigor/graph-store.rb', line 21

def local_context_node
  @local_context_node
end

#repositoryObject (readonly)

Returns the value of attribute repository.



21
22
23
# File 'lib/roadforest/source-rigor/graph-store.rb', line 21

def repository
  @repository
end

Instance Method Details

#add_statement(*args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/roadforest/source-rigor/graph-store.rb', line 104

def add_statement(*args)
  case args.length
  when 1
    subject, predicate, object, context = *args.first
  when 2
    triple, context = *args
    subject, predicate, object = *triple
  when 3
    subject, predicate, object = *args
    context = nil
  when 4
    subject, predicate, object, context = *args
  else
    raise ArgumentError, "insert_statement needs some variation of subject, predicate, object, [context]"
  end
  context ||= local_context_node

  record_statement(normalize_statement(subject, predicate, object, context))
end

#context_variableObject

XXX Needed, maybe, if we need to handle constant patterns def include?(statement) end



177
178
179
# File 'lib/roadforest/source-rigor/graph-store.rb', line 177

def context_variable
  @context_variable ||= RDF::Query::Variable.new(:context)
end

#create_list(values = nil) ⇒ Object



77
78
79
# File 'lib/roadforest/source-rigor/graph-store.rb', line 77

def create_list(values = nil)
  named_list(local_context_node, values)
end

#debug(message) ⇒ Object



54
55
56
57
58
# File 'lib/roadforest/source-rigor/graph-store.rb', line 54

def debug(message)
  io = @debug_io || RoadForest.debug_io
  return if io.nil?
  io.puts(message)
end

#delete_statement(statement) ⇒ Object



132
133
134
135
136
137
# File 'lib/roadforest/source-rigor/graph-store.rb', line 132

def delete_statement(statement)
  repository.query(statement) do |statement|
    next if statement.context.nil?
    repository.delete(statement)
  end
end

#delete_statements(pattern) ⇒ Object



65
66
67
# File 'lib/roadforest/source-rigor/graph-store.rb', line 65

def delete_statements(pattern)
  repository.delete(pattern)
end

#durable?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/roadforest/source-rigor/graph-store.rb', line 158

def durable?
  @repository.durable?
end

#each(&block) ⇒ Object

XXX Credence? Default context?



163
164
165
166
167
168
169
170
171
# File 'lib/roadforest/source-rigor/graph-store.rb', line 163

def each(&block)
  if @repository.respond_to?(:query)
    @repository.query(:context => false, &block)
  elsif @repository.respond_to?(:each)
    @repository.each(&block)
  else
    @repository.to_a.each(&block)
  end
end

#each_statement(context = nil, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/roadforest/source-rigor/graph-store.rb', line 146

def each_statement(context=nil, &block)
  query = {}
  unless context.nil?
    query[:context] = context
  end

  @repository.query(query) do |statement|
    next if statement.context.nil?
    yield statement
  end
end

#force_impulseObject



32
33
34
35
36
# File 'lib/roadforest/source-rigor/graph-store.rb', line 32

def force_impulse
  @current_impulse = RDF::Node.new
  repository.insert(normalize_statement(@current_impulse, [:rdf, 'type'], [:rf, 'Impulse'], nil))
  repository.insert(normalize_statement(@current_impulse, [:rf, 'begunAt'], Time.now, nil))
end

#insert_document(document) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/roadforest/source-rigor/graph-store.rb', line 81

def insert_document(document)
  #puts; puts "#{__FILE__}:#{__LINE__} => #{(document).inspect}"
  #puts document.body_string
  reader = RDF::Reader.for(:content_type => document.content_type) do
    sample = document.body.read(1000)
    document.body.rewind
    sample
  end.new(document.body, :base_uri => document.root_url) #consider :processor_graph
  insert_reader(document.source, reader)
end

#insert_reader(context, reader) ⇒ Object Also known as: insert_graph



92
93
94
95
96
97
98
99
100
101
# File 'lib/roadforest/source-rigor/graph-store.rb', line 92

def insert_reader(context, reader)
  #puts; puts "#{__FILE__}:#{__LINE__} => #{(context).inspect}"
  context = normalize_context(context)
  delete_statements(:context => context)
  reader.each_statement do |statement|
    statement.context = context
    record_statement(statement)
  end
  #puts; puts "#{__FILE__}:#{__LINE__} => \n#{(graph_dump(:nquads))}"
end

#insert_statement(statement) ⇒ Object Also known as: record_statement



124
125
126
127
128
129
# File 'lib/roadforest/source-rigor/graph-store.rb', line 124

def insert_statement(statement)
  repository.insert(statement)

  repository.delete([statement.context, expand_curie([:rf, "impulse"]), nil])
  repository.insert(normalize_statement(statement.context, [:rf, "impulse"], current_impulse, nil))
end

#named_graph(context) ⇒ Object



69
70
71
# File 'lib/roadforest/source-rigor/graph-store.rb', line 69

def named_graph(context)
  ::RDF::Graph.new(context, :data => repository)
end

#named_list(context, values = nil) ⇒ Object



73
74
75
# File 'lib/roadforest/source-rigor/graph-store.rb', line 73

def named_list(context, values = nil)
  ::RDF::List.new(nil, named_graph(context), values)
end

#next_impulseObject



38
39
40
41
42
43
# File 'lib/roadforest/source-rigor/graph-store.rb', line 38

def next_impulse
  return if quiet_impulse?
  force_impulse
  #mark ended?
  #chain impulses?
end

#query_execute(query, &block) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/roadforest/source-rigor/graph-store.rb', line 181

def query_execute(query, &block)
  #XXX Weird edge case of GM getting queried with a vanilla RDF::Query...
  #needs tests, thought
  query = RoadForest::SourceRigor::ResourceQuery.from(query)
  query.execute(self).filter do |solution|
    solution.respond_to?(:context) and not solution.context.nil?
  end.each(&block)
end

#query_pattern(pattern, &block) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/roadforest/source-rigor/graph-store.rb', line 190

def query_pattern(pattern, &block)
  case pattern
  when RoadForest::SourceRigor::ResourcePattern
    pattern.execute(@repository, {}, :context_roles => {:local => local_context_node}) do |statement|
      next if statement.context.nil?
      yield statement if block_given?
    end
  else
    pattern.execute(@repository, {}) do |statement|
      next if statement.context.nil?
      yield statement if block_given?
    end
  end
end

#quiet_impulse?Boolean Also known as: raw_quiet_impulse?

Returns:

  • (Boolean)


45
46
47
# File 'lib/roadforest/source-rigor/graph-store.rb', line 45

def quiet_impulse?
  repository.query([nil, nil, @current_impulse, false]).to_a.empty?
end

#reader_for(content_type, repository) ⇒ Object



50
51
52
# File 'lib/roadforest/source-rigor/graph-store.rb', line 50

def reader_for(content_type, repository)
  RDF::Reader.for(content_type)
end

#replace(original, statement) ⇒ Object



139
140
141
142
143
144
# File 'lib/roadforest/source-rigor/graph-store.rb', line 139

def replace(original, statement)
  unless original == statement
    repository.delete(original)
    repository.insert(statement)
  end
end

#repository_dump(format = :turtle) ⇒ Object Also known as: graph_dump



60
61
62
# File 'lib/roadforest/source-rigor/graph-store.rb', line 60

def repository_dump(format = :turtle)
  repository.dump(format)
end

#unnamed_graphObject



205
206
207
# File 'lib/roadforest/source-rigor/graph-store.rb', line 205

def unnamed_graph
  ::RDF::Graph.new(nil, :data => @repository)
end