Class: RoadForest::Graph::GraphFocus

Inherits:
Object
  • Object
show all
Includes:
Normalization
Defined in:
lib/roadforest/graph/graph-focus.rb

Direct Known Subclasses

GraphCopier, PostFocus

Constant Summary

Constants included from Normalization

Normalization::Vocabs

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from 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, #uri, #vocabularies_in_graph

Constructor Details

#initialize(access_manager, subject = nil) ⇒ GraphFocus

Returns a new instance of GraphFocus.



54
55
56
57
# File 'lib/roadforest/graph/graph-focus.rb', line 54

def initialize(access_manager, subject = nil)
  @access_manager = access_manager
  self.subject = subject unless subject.nil?
end

Instance Attribute Details

#access_managerObject

Returns the value of attribute access_manager.



50
51
52
# File 'lib/roadforest/graph/graph-focus.rb', line 50

def access_manager
  @access_manager
end

#subjectObject Also known as: rdf

Returns the value of attribute subject.



50
51
52
# File 'lib/roadforest/graph/graph-focus.rb', line 50

def subject
  @subject
end

Instance Method Details

#<<(data) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/roadforest/graph/graph-focus.rb', line 111

def <<(data)
  case data
  when Array
    data = normalize_statement(*data)
  end
  access_manager << data
end

#add(property, value, extra = nil) ⇒ Object



104
105
106
107
108
109
# File 'lib/roadforest/graph/graph-focus.rb', line 104

def add(property, value, extra=nil)
  property, value = normalize_triple(property, value, extra)

  access_manager.insert([subject, property, value])
  return value
end

#add_list(property, extra = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/roadforest/graph/graph-focus.rb', line 163

def add_list(property, extra=nil)
  unless extra.nil?
    property = normalize_property([property, extra])
  end
  if block_given?
    list = add_node(property).as_list
    yield list
  else
    list = FocusList.new
    add(property, list.subject)
  end
  list
end

#add_node(property, url = nil) ⇒ Object



142
143
144
145
146
147
# File 'lib/roadforest/graph/graph-focus.rb', line 142

def add_node(property, url=nil)
  create_node(url) do |node|
    add(property, node.subject)
    yield node if block_given?
  end
end

#all(prefix, property = nil) ⇒ Object



224
225
226
227
228
# File 'lib/roadforest/graph/graph-focus.rb', line 224

def all(prefix, property = nil)
  return  maybe_null( prefix, property,
    forward_query_value( prefix, property )
  )
end

#as_listObject



250
251
252
253
254
# File 'lib/roadforest/graph/graph-focus.rb', line 250

def as_list
  list = FocusList.new(subject, access_manager)
  list.base_node = self
  list
end

#create_node(url = nil) {|node| ... } ⇒ Object

Create a subject node without relationship to the rest of the graph

Yields:

  • (node)


150
151
152
153
154
# File 'lib/roadforest/graph/graph-focus.rb', line 150

def create_node(url=nil)
  node = wrap_node(normalize_resource(url))
  yield node if block_given?
  node
end

#delete(property, extra = nil) ⇒ Object



119
120
121
# File 'lib/roadforest/graph/graph-focus.rb', line 119

def delete(property, extra=nil)
  access_manager.delete(:subject => subject, :predicate => normalize_property(property, extra))
end

#dupObject



80
81
82
83
84
# File 'lib/roadforest/graph/graph-focus.rb', line 80

def dup
  other = self.class.new(access_manager.dup)
  other.subject = subject
  other
end

#empty_list(property, extra = nil) ⇒ Object



156
157
158
159
160
161
# File 'lib/roadforest/graph/graph-focus.rb', line 156

def empty_list(property, extra=nil)
  unless extra.nil?
    property = normalize_property([property, extra])
  end
  list
end

#find_or_add(property, url = nil, &block) ⇒ Object Also known as: first_or_add



123
124
125
126
127
128
129
130
131
# File 'lib/roadforest/graph/graph-focus.rb', line 123

def find_or_add(property, url=nil, &block)
  value = first(property)
  if value.nil?
    value = add_node(property, url, &block)
  else
    yield value if block_given?
  end
  value
end

#first(prefix, property = nil) ⇒ Object



218
219
220
221
222
# File 'lib/roadforest/graph/graph-focus.rb', line 218

def first(prefix, property = nil)
  return maybe_null( prefix, property,
    forward_query_value( prefix, property ).first
  )
end

#forward_propertiesObject



256
257
258
# File 'lib/roadforest/graph/graph-focus.rb', line 256

def forward_properties
  query_properties( build_query{|q| q.pattern([ normalize_resource(subject), :property, :value ])} )
end

#get(prefix, property = nil) ⇒ Object Also known as: []



211
212
213
214
215
# File 'lib/roadforest/graph/graph-focus.rb', line 211

def get(prefix, property = nil)
  return maybe_null( prefix, property,
    single_or_enum(forward_query_value( prefix, property))
  )
end

#inspectObject Also known as: to_s



75
76
77
# File 'lib/roadforest/graph/graph-focus.rb', line 75

def inspect
  "#<#{self.class.name}:0x#{"%x" % object_id} s:(#{subject.to_s}) p->o:#{forward_properties.inspect}>" #ok
end

#normalize_triple(property, value, extra = nil) ⇒ Object

Begin old GraphFocus



87
88
89
90
91
92
93
# File 'lib/roadforest/graph/graph-focus.rb', line 87

def normalize_triple(property, value, extra=nil)
  if not extra.nil?
    property = [property, value]
    value = extra
  end
  return normalize_property(property), normalize_term(value)
end

#relevant_prefixesObject

XXX This probably wants to be handled completely in the MediaType handler



207
208
209
# File 'lib/roadforest/graph/graph-focus.rb', line 207

def relevant_prefixes
  access_manager.relevant_prefixes
end

#resetObject



59
60
61
# File 'lib/roadforest/graph/graph-focus.rb', line 59

def reset
  access_manager.reset
end

#rev(prefix, property = nil) ⇒ Object

XXX Maybe rev should return a decorator, so it looks like: focus.rev.get(…) or focus.rev.all(…)



232
233
234
235
236
# File 'lib/roadforest/graph/graph-focus.rb', line 232

def rev(prefix, property = nil)
  return maybe_null( prefix, property,
    single_or_enum(reverse_query_value( prefix, property))
  )
end

#rev_all(prefix, property = nil) ⇒ Object



244
245
246
247
248
# File 'lib/roadforest/graph/graph-focus.rb', line 244

def rev_all(prefix, property = nil)
  return maybe_null( prefix, property,
    reverse_query_value(prefix, property)
  )
end

#rev_first(prefix, property = nil) ⇒ Object



238
239
240
241
242
# File 'lib/roadforest/graph/graph-focus.rb', line 238

def rev_first(prefix, property = nil)
  return maybe_null( prefix, property,
    reverse_query_value(prefix, property).first
  )
end

#reverse_propertiesObject



260
261
262
# File 'lib/roadforest/graph/graph-focus.rb', line 260

def reverse_properties
  query_properties( build_query{|q| q.pattern([ :value, :property, normalize_resource(subject)])} )
end

#root_urlObject



63
64
65
# File 'lib/roadforest/graph/graph-focus.rb', line 63

def root_url
  @access_manager.resource
end

#root_url=(*value) ⇒ Object

XXX curies?



202
203
204
# File 'lib/roadforest/graph/graph-focus.rb', line 202

def root_url=(*value) #XXX curies?
  @root_url = normalize_resource(value)
end

#set(property, value, extra = nil) ⇒ Object Also known as: []=



95
96
97
98
99
100
101
# File 'lib/roadforest/graph/graph-focus.rb', line 95

def set(property, value, extra=nil)
  property, value = normalize_triple(property, value, extra)

  delete(property)
  add(property, value)
  return value
end

#set_node(property, url = nil) ⇒ Object Also known as: node_at



134
135
136
137
138
139
# File 'lib/roadforest/graph/graph-focus.rb', line 134

def set_node(property, url=nil)
  create_node(url) do |node|
    set(property, node.subject)
    yield node if block_given?
  end
end

#to_contextObject



198
199
200
# File 'lib/roadforest/graph/graph-focus.rb', line 198

def to_context
  normalize_context(subject)
end

#unwrap_value(value) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/roadforest/graph/graph-focus.rb', line 189

def unwrap_value(value)
  return nil if value.nil?
  if RDF::Literal === value
    value.object
  else
    wrap_node(value)
  end
end

#wrap_node(value) ⇒ Object

End of old GraphFocus



178
179
180
181
182
183
184
185
186
187
# File 'lib/roadforest/graph/graph-focus.rb', line 178

def wrap_node(value)
  next_step = dup
  if ::RDF::Node === value
    next_step.root_url = self.root_url
  else
    next_step.root_url = normalize_context(value)
  end
  next_step.subject = value
  next_step
end