class RoadForest::Graph::GraphFocus

Constants

STRIP_TRACE

Attributes

access_manager[RW]
rdf[RW]
subject[RW]

Public Class Methods

new(access_manager, subject = nil) click to toggle source
# 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

Public Instance Methods

<<(data) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 111
def <<(data)
  case data
  when Array
    data = normalize_statement(*data)
  end
  access_manager << data
end
[](prefix, property = nil)
Alias for: get
[]=(property, value, extra=nil)
Alias for: set
add(property, value, extra=nil) click to toggle source
# 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) { |list| ... } click to toggle source
# 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) { |node| ... } click to toggle source
# 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) click to toggle source
# 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_list() click to toggle source
# 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| ... } click to toggle source

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

# 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) click to toggle source
# 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
dup() click to toggle source
# 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) click to toggle source
# 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) { |value| ... } click to toggle source
# 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
Also aliased as: first_or_add
first(prefix, property = nil) click to toggle source
# 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
first_or_add(property, url=nil, &block)
Alias for: find_or_add
forward_properties() click to toggle source
# 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) click to toggle source
# 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
Also aliased as: []
inspect() click to toggle source
# 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
Also aliased as: to_s
node_at(property, url=nil)
Alias for: set_node
normalize_triple(property, value, extra=nil) click to toggle source

Begin old GraphFocus

# 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_prefixes() click to toggle source

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

# File lib/roadforest/graph/graph-focus.rb, line 207
def relevant_prefixes
  access_manager.relevant_prefixes
end
reset() click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 59
def reset
  access_manager.reset
end
rev(prefix, property = nil) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source
# 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_properties() click to toggle source
# 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_url() click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 63
def root_url
  @access_manager.resource
end
root_url=(*value) click to toggle source
# 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) click to toggle source
# 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
Also aliased as: []=
set_node(property, url=nil) { |node| ... } click to toggle source
# 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
Also aliased as: node_at
subject=(*value) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 67
def subject=(*value)
  @subject = normalize_resource(value)
  case @subject
  when ::RDF::URI
    @access_manager.resource = @subject
  end
end
to_context() click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 198
def to_context
  normalize_context(subject)
end
to_s()
Alias for: inspect
unwrap_value(value) click to toggle source
# 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) click to toggle source

End of old GraphFocus

# 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

Protected Instance Methods

build_query(&block) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 301
def build_query(&block)
  access_manager.build_query(&block)
end
execute_query(query) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 305
def execute_query(query)
  query.execute(access_manager)
end
forward_query_value(prefix, property=nil) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 295
def forward_query_value(prefix, property=nil)
  query_value(build_query{|q|
    q.pattern([ normalize_resource(subject), normalize_property(prefix, property), :value])
  })
end
maybe_null(prefix, property, result) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 267
def maybe_null(prefix, property, result)
  if not result.nil?
    if result.respond_to? :empty?
      return result unless result.empty?
    else
      return result
    end
  end
  return NullFocus.new(self, normalize_property(prefix, property), caller(0).drop_while{|line| line =~ STRIP_TRACE})
end
query_properties(query) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 316
def query_properties(query)
  Hash[execute_query(query).map do |solution|
    prop = solution.property
    if qname = prop.qname
      prop = qname
    end
    [prop, solution.value]
  end]
end
query_value(query) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 309
def query_value(query)
  solutions = execute_query(query)
  solutions.map do |solution|
    unwrap_value(solution.value)
  end
end
reverse_query_value(prefix, property=nil) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 289
def reverse_query_value(prefix, property=nil)
  query_value(build_query{|q|
    q.pattern([ :value, normalize_property(prefix, property), normalize_resource(subject)])
  })
end
single_or_enum(values) click to toggle source
# File lib/roadforest/graph/graph-focus.rb, line 278
def single_or_enum(values)
  case values.length
  when 0
    return nil
  when 1
    return values.first
  else
    return values.enum_for(:each)
  end
end