class BELParser::Resource::JenaTDBReader

Constants

BELV
DC
RESOLVE_CONCEPT
RESOLVE_CONCEPTS
SKOS
VALUE_PREDICATE_ORDER

Public Class Methods

new(tdb_directory_path) click to toggle source
# File lib/bel_parser/resource/jena_tdb_reader.rb, line 27
def initialize(tdb_directory_path)
  @rdf = RDF::Jena::Repository.new(tdb_directory_path)
end

Public Instance Methods

retrieve_resource(identifier) click to toggle source
# File lib/bel_parser/resource/jena_tdb_reader.rb, line 31
def retrieve_resource(identifier)
  uri   = RDF::URI(identifier)
  domain, prefix, pref_label = nil
  types                      = []
  @rdf.query([:subject => uri]).each do |solution|
    case solution.predicate
    when RDF.type
      types << solution.object
    when BELV.domain
      domain = solution.object.to_s
    when BELV.prefix
      prefix = solution.object.to_s
    when SKOS.prefLabel
      pref_label = solution.object.to_s
    end
  end
  return nil unless types.any?(&method(:scheme_class?))
  ConceptScheme.new(uri.to_s, domain, prefix, pref_label, types.map(&:to_s))
end
retrieve_value_from_resource(identifier, value) click to toggle source
# File lib/bel_parser/resource/jena_tdb_reader.rb, line 51
def retrieve_value_from_resource(identifier, value)
  concept_scheme = retrieve_resource(identifier)
  return nil unless concept_scheme

  template_binding = binding
  sparql_query     = RESOLVE_CONCEPT.result(template_binding)
  to_concept       = method(:hash_to_concept).to_proc.curry[concept_scheme]

  @rdf.query_execute(sparql_query).map(&to_concept)
end
retrieve_values_from_resource(identifier) click to toggle source
# File lib/bel_parser/resource/jena_tdb_reader.rb, line 62
def retrieve_values_from_resource(identifier)
  concept_scheme = retrieve_resource(identifier)
  return nil unless concept_scheme

  template_binding = binding
  sparql_query     = RESOLVE_CONCEPTS.result(template_binding)
  to_concept       = method(:hash_to_concept).to_proc.curry[concept_scheme]

  @rdf.query_execute(sparql_query).map(&to_concept)
end

Private Instance Methods

find_value_uris(resource_uri, value) click to toggle source
# File lib/bel_parser/resource/jena_tdb_reader.rb, line 79
def find_value_uris(resource_uri, value)
  VALUE_PREDICATE_ORDER.each do |predicate|
    subjects =
      @rdf.query([:predicate => predicate, :object => value])
      .select do |stmt|
        @rdf.has_statement?(
          RDF::Statement(stmt.subject, SKOS.inScheme, resource_uri))
      end.map(&:subject)

    if !subjects.empty?
      return subjects
    end
  end

  []
end
hash_to_concept(concept_scheme, hash) click to toggle source
# File lib/bel_parser/resource/jena_tdb_reader.rb, line 96
def hash_to_concept(concept_scheme, hash)
  Concept.new(concept_scheme, 
    *hash.values_at('concept', 'prefLabel', 'identifier', 'title',
      'altLabels', 'types'))
end
scheme_class?(uri) click to toggle source
# File lib/bel_parser/resource/jena_tdb_reader.rb, line 75
def scheme_class?(uri)
  uri == BELV.AnnotationConceptScheme || uri == BELV.NamespaceConceptScheme
end