Class: RoadForest::SourceRigor::UpdateManager

Inherits:
Graph::SplitManager show all
Includes:
Rigorous
Defined in:
lib/roadforest/source-rigor/rigorous-access.rb

Constant Summary

Constants included from Rigorous

Rigorous::Af

Constants included from Graph::Normalization

Graph::Normalization::Vocabs

Instance Attribute Summary collapse

Attributes included from Rigorous

#rigor

Attributes inherited from Graph::SplitManager

#target_graph

Attributes inherited from Graph::ReadOnlyManager

#resource, #source_graph

Instance Method Summary collapse

Methods included from Rigorous

#build_query, #query_execute, #query_pattern, #query_resource_pattern, #resource_pattern_from

Methods inherited from Graph::SplitManager

#relevant_prefixes

Methods inherited from Graph::WriteManager

#insert_statement

Methods inherited from Graph::ReadOnlyManager

#build_query, #each, #each_statement, #query_execute, #query_pattern, #relevant_prefixes

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

#initializeUpdateManager

Returns a new instance of UpdateManager.



65
66
67
68
69
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 65

def initialize
  @copied_contexts = {}
  @inserts = Hash.new{|h,k| h[k] = []}
  @deletes = Hash.new{|h,k| h[k] = []}
end

Instance Attribute Details

#copied_contextsObject

Returns the value of attribute copied_contexts.



83
84
85
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 83

def copied_contexts
  @copied_contexts
end

#deletesObject

Returns the value of attribute deletes.



83
84
85
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 83

def deletes
  @deletes
end

#insertsObject

Returns the value of attribute inserts.



83
84
85
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 83

def inserts
  @inserts
end

Instance Method Details

#delete(statement) ⇒ Object



123
124
125
126
127
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 123

def delete(statement)
  statement = statement_from(statement)
  record_delete(statement)
  super
end

#dupObject



85
86
87
88
89
90
91
92
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 85

def dup
  other = super
  other.copied_contexts = self.copied_contexts
  other.inserts = self.inserts
  other.deletes = self.deletes
  other.target_graph = self.target_graph
  return other
end

#each_payloadObject



129
130
131
132
133
134
135
136
137
138
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 129

def each_payload
  update_payload_query = ::RDF::Query.new do
    pattern [ :affordance, Af.target, :resource ]
    pattern [ :affordance, Af.payload, :pattern_root ]
    pattern [ :affordance, RDF.type, Af.Update ]
  end
  query(update_payload_query).each do |solution|
    yield(solution[:resource], solution[:pattern_root], parceller.graph_for(solution[:pattern_root]))
  end
end

#each_targetObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 140

def each_target
  all_subjects = Hash[destination_graph.subjects.map{|s| [s,true]}]
  source_graph.subjects.each do |s|
    all_subjects[s] = true
  end
  check_inserts = inserts.dup
  check_deletes = deletes.dup
  marked_inserts = []
  marked_deletes = []

  each_payload do |root, pattern_root, graph_pattern|
    next unless all_subjects.has_key?(root)

    marked_inserts.clear
    marked_deletes.clear

    matcher = PathMatcher.new
    matcher.pattern = graph_pattern
    matcher.pattern_root = pattern_root

    source_match = matcher.match(root, source_graph)

    dest_match = matcher.match(root, destination_graph)

    if dest_match.successful?
      check_inserts.each_key do |stmt|
        if dest_match.graph.has_statement?(stmt)
          marked_inserts << stmt
        end
      end
    end

    if source_match.successful?
      check_deletes.each_key do |stmt|
        if source_match.graph.has_statement?(stmt)
          marked_deletes << stmt
        end
      end
    end

    if dest_match.successful?
      if !source_match.successful? || (source_match.graph != dest_match.graph)

        yield(root, dest_match.graph)

        marked_inserts.each do |stmt|
          check_inserts.delete(stmt)
        end
        marked_deletes.each do |stmt|
          check_deletes.delete(stmt)
        end
      end
    end
  end

  fallback_needed = {}
  check_inserts.each_value do|resources|
    resources.each {|resource| fallback_needed[resource] = true }
  end
  check_deletes.each_value do|resources|
    resources.each {|resource| fallback_needed[resource] = true }
  end

  fallback_needed.each_key do |key|
    yield(key, parceller.graph_for(key))
  end
end

#execute_search(search, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 94

def execute_search(search, &block)
  destination_enum = search.execute(destination_graph)
  source_enum = search.execute(origin_graph)

  enum = destination_enum.any?{ true } ? destination_enum : source_enum

  enum.each(&block)
  return enum
end

#insert(statement) ⇒ Object



116
117
118
119
120
121
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 116

def insert(statement)
  statement = statement_from(statement)
  record_insert(statement)
  statement.context ||= ::RDF::URI.intern("urn:local-insert")
  super
end

#parcellerObject



208
209
210
211
212
213
214
215
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 208

def parceller
  @parceller ||=
    begin
      parceller = Parcel.new
      parceller.graph = destination_graph
      parceller
    end
end

#record_delete(statement) ⇒ Object



108
109
110
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 108

def record_delete(statement)
  @deletes[statement] << resource
end

#record_insert(statement) ⇒ Object



104
105
106
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 104

def record_insert(statement)
  @inserts[statement] << resource
end

#resetObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 71

def reset
  super

  @copied_contexts.clear
  @inserts.clear
  @deletes.clear

  source_graph.each_statement do |stmt|
    target_graph << stmt
  end
end

#statement_from(statement) ⇒ Object



112
113
114
# File 'lib/roadforest/source-rigor/rigorous-access.rb', line 112

def statement_from(statement)
  ::RDF::Statement.from(statement)
end