module RoadForest::Graph::Normalization

Constants

Vocabs

Public Instance Methods

expand_curie(from) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 129
def expand_curie(from)
  case from
  when Array
    case from.length
    when 2
      prefix, property = *from
      return interned_uri(expand_curie_pair(prefix.to_s, property.to_s))
    when 1
      return expand_curie(from.first)
    else
      return from
    end
  else
    return from
  end
end
expand_curie_pair(prefix, property) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 108
def expand_curie_pair(prefix, property)
  vocab = Vocabs.fetch(prefix) do
    vocab = RDF::Vocabulary.detect do |vocab|
      unless vocab.__prefix__.is_a? RDF::URI
        Vocabs[vocab.__prefix__.to_s] = vocab
      end
      vocab.__prefix__.to_s == prefix
    end
    #p k => vocab #ok
    if vocab.nil?
      raise "Don't know a vocabulary for prefix #{prefix.inspect} in CURIE #{prefix}:#{property}"
    end
    vocab
  end
  begin
    vocab[property]
  rescue KeyError
    raise KeyError, "No property #{property} in #{vocab.inspect} - (try: #{vocab.methods(false).sort.inspect})"
  end
end
interned_uri(value) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 167
def interned_uri(value)
  RDF::URI.intern(uri(value))
end
literal(object) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 75
def literal(object)
  RDF::Literal.new(object)
end
normalize_context(from) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 43
def normalize_context(from)
  case from
  when Array
    from = uri(expand_curie(from))
  when RDF::URI, Addressable::URI, String
    from = uri(from)
  else
    return nil
  end
  from.fragment = nil
  return RDF::URI.intern(from.to_s)
end
normalize_property(prefix, property = nil) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 146
def normalize_property(prefix, property = nil)
  if property.nil?
    property = prefix

    case property
    when Array
      normalize_property(*property)
    when String
      RDF::URI.intern(property)
    else
      property
    end
  else
    expand_curie([prefix, property])
  end
end
normalize_resource(from) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 25
def normalize_resource(from)
  from = expand_curie(from)
  case from
  when nil
    from = RDF::Node.new
  when RDF::Resource
  when /^_:/
    from = RDF::Resource.new(from)
  when String, RDF::URI, Addressable::URI
    from = interned_uri(from)
  when Symbol
    from = RDF::Node.new(from)
  else
    from = RDF::Resource.new(from)
  end
  return from
end
normalize_statement(subject, predicate, object, context=nil) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 9
def normalize_statement(subject, predicate, object, context=nil)
  subject = normalize_resource(subject) || RDF::Node.new
  predicate = normalize_uri(predicate)
  object = normalize_term(object) || RDF::Node.new
  unless context.nil?
    context = normalize_resource(context)
  end

  RDF::Statement.new(subject, predicate, object, :context => context)
end
normalize_term(object) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 67
def normalize_term(object)
  if Array === object
    RDF::Resource.new(expand_curie(object))
  else
    object
  end
end
normalize_tuple(tuple) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 20
def normalize_tuple(tuple)
  subject, predicate, object, context = *tuple
  [ normalize_resource(subject) || RDF::Node.new, normalize_uri(predicate), normalize_term(object) || RDF::Node.new, normalize_resource(context) ]
end
normalize_uri(from) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 56
def normalize_uri(from)
  from = expand_curie(from)
  case from
  when nil
  when RDF::URI
  else
    from = interned_uri(from)
  end
  return from
end
relevant_prefixes_for_graph(graph) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 79
def relevant_prefixes_for_graph(graph)
  Hash[ vocabularies_in_graph(graph).map do |prefix|
    vocab = Vocabs[prefix]
    [prefix, vocab.to_uri]
  end]
end
root_url() click to toggle source
# File lib/roadforest/graph/normalization.rb, line 163
def root_url
  nil
end
uri(value) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 171
def uri(value)
  if root_url
    value = root_url.join(value)
  else
    value = RDF::URI.new(value)
  end

  if !value.query.nil? and value.query.empty?
    value.query = nil
  end
  value.canonicalize!
  value.validate!

  value
end
vocabularies_in_graph(graph) click to toggle source
# File lib/roadforest/graph/normalization.rb, line 86
def vocabularies_in_graph(graph)
  patterns = Vocabs.map do |prefix, vocab|
    [%r{^#{vocab.to_uri}}, prefix]
  end

  vocabs = {}

  graph.each_statement do |statement|
    statement.to_a.each do |field|
      next unless RDF::URI === field
      field = field.to_s
      patterns.each do |pattern, vocab|
        if pattern =~ field
          vocabs[vocab] = true
        end
      end
    end
  end

  vocabs.keys
end