class Ontopia::Topicmaps::Topicmap

Constants

QUERY
QUERY_PROJECTION_RE
TOPICMAP_READER

Attributes

file[R]
tm[R]

Public Class Methods

extract_query_projection(query) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 58
def extract_query_projection(query)
  projection = query[QUERY_PROJECTION_RE, 1]
  projection.delete('$,').split if projection
end
new(file) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 65
def initialize(file)
  Topicmaps.setup_classpath

  @file = file.include?(File::SEPARATOR) ? file :
    File.join(Topicmaps.base, 'topicmaps', file)

  @tm = read(@file)
end

Public Instance Methods

count(query = nil, declarations = nil) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 76
def count(query = nil, declarations = nil)
  query ||= :count_all_topics
  query_for_list(query, declarations) { |i,| i }.first || 0
end
extract_query_projection(query) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 102
def extract_query_projection(query)
  self.class.extract_query_projection(query)
end
extract_query_variables(query) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 106
def extract_query_variables(query)
  query_wrapper.get_query_processor.parse(query).get_selected_variables.to_a
end
query(query = nil, declarations = nil, str = nil) { |i, str| ... } click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 81
def query(query = nil, declarations = nil, str = nil)
  str = Topicmaps.stringifier(str || :default)

  query_for_list(query, declarations, &block_given? ?
    lambda { |i, *| yield(i, str) } : lambda { |i, *| str[i] }).to_a
end
query_maps(query = nil, declarations = nil) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 88
def query_maps(query = nil, declarations = nil)
  query_wrapper(declarations).query_for_maps(query_string(query)).to_a
end
topics(value_str = nil, key_str = nil) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 92
def topics(value_str = nil, key_str = nil)
  hash, key_str = {}, Topicmaps.stringifier(key_str || :id)

  query(:all_topics, nil, value_str) { |i, str|
    hash[key_str[i]] = str[i]
  }

  hash
end

Private Instance Methods

query_for_list(query, declarations = nil, &block) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 132
def query_for_list(query, declarations = nil, &block)
  args = [query_string(query)]
  args << RowMapper.new(&block) if block

  query_wrapper(declarations).query_for_list(*args)
rescue OntopiaRuntimeException => err
  raise "Query error: #{err}"
end
query_string(query) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 121
def query_string(query)
  QUERY.key?(query ||= :all_topics) ? QUERY[query] :
    query.is_a?(Symbol) ? raise("No such query: #{query}") : query
end
query_wrapper(declarations = nil) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 126
def query_wrapper(declarations = nil)
  qw = QueryWrapper.new(tm)
  qw.set_declarations(declarations) if declarations
  qw
end
read(file, reader = nil) click to toggle source
# File lib/ontopia/topicmaps/topicmap.rb, line 112
def read(file, reader = nil)
  reader ||= TOPICMAP_READER[ext = File.extname(file)]
  raise "Unsupported file format: #{ext}" unless reader

  self.class.const_get(reader).new(Java::JavaIo::File.new(file)).read
rescue Java::JavaIo::IOException => err
  raise "Error reading topic map: #{err}"
end