Module: Grom::GraphMapper

Included in:
Base
Defined in:
lib/grom/graph_mapper.rb

Instance Method Summary collapse

Instance Method Details

#create_hash_from_ttl(ttl_data) ⇒ Object


15
16
17
18
19
20
21
22
23
# File 'lib/grom/graph_mapper.rb', line 15

def create_hash_from_ttl(ttl_data)
  hash = {}
  RDF::Turtle::Reader.new(ttl_data) do |reader|
    reader.each_statement do |statement|
      statement_mapper(statement, hash)
    end
  end
  hash.values
end

#get_id(uri) ⇒ Object


11
12
13
# File 'lib/grom/graph_mapper.rb', line 11

def get_id(uri)
  uri == RDF.type.to_s ? 'type' : uri.to_s.split("/").last
end

#get_ttl_data(uri) ⇒ Object


7
8
9
# File 'lib/grom/graph_mapper.rb', line 7

def get_ttl_data(uri)
  Net::HTTP.get(URI(uri))
end

#statement_mapper(statement, hash) ⇒ Object


25
26
27
28
29
# File 'lib/grom/graph_mapper.rb', line 25

def statement_mapper(statement, hash)
  subject = get_id(statement.subject)
  hash[subject] ||= { :id => subject }
  hash[subject][get_id(statement.predicate).to_sym] = statement.object.to_s
end

#statements_with_mapper(ttl_data) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/grom/graph_mapper.rb', line 31

def statements_with_mapper(ttl_data)
  hash = {}
  RDF::Turtle::Reader.new(ttl_data) do |reader|
    reader.each_statement do |statement|
      subject = get_id(statement.subject)
      hash[subject] ||= {:id => subject}
      predicate = get_id(statement.predicate)
      if (predicate == "connect")
        (hash[subject][predicate.to_sym] ||= []) << get_id(statement.object.to_s)
      else
        hash[subject][predicate.to_sym] = statement.object.to_s
      end
    end
  end
  hash.values
end

#through_split_graph(ttl_data) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/grom/graph_mapper.rb', line 48

def through_split_graph(ttl_data)
  associated_hash, through_hash = {}, {}
  RDF::Turtle::Reader.new(ttl_data) do |reader|
    reader.each_statement do |s|
      if (s.subject.to_s =~ URI::regexp) == 0
        subject = get_id(s.subject)
        associated_hash[subject] ||= { :id => subject }
        associated_hash[subject][get_id(s.predicate).to_sym] = s.object.to_s
      else
        through_hash[s.subject.to_s] ||= {}
        if get_id(s.predicate) == "connect"
          through_hash[s.subject.to_s][:associated_object_id] = get_id(s.object)
        elsif get_id(s.predicate) == "objectId"
          through_hash[s.subject.to_s][:id] = get_id(s.object)
        else
          through_hash[s.subject.to_s][get_id(s.predicate).to_sym] = s.object.to_s
        end
      end
    end
  end

  { associated_class_hash: associated_hash, through_class_hash: through_hash }
end