Class: BBCDataService::RDFParser

Inherits:
Object
  • Object
show all
Includes:
Vocabulary
Defined in:
lib/bbc_data_service/rdf_parser.rb

Class Method Summary collapse

Methods included from Vocabulary

included

Class Method Details

.execute_query(graph, query, mappings) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/bbc_data_service/rdf_parser.rb', line 114

def self.execute_query(graph, query, mappings)
  query_values = ""
  mappings.each do |vocabulary, values|
    values.each do |key, value|
      query_values << ":#{key} => fixture.#{key},"
    end
  end
  "{#{query_values}}"
end

.filter_graph(solutions, filter) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/bbc_data_service/rdf_parser.rb', line 81

def self.filter_graph(solutions, filter)
  if filter[:type] == :uri
    solutions.filter(filter[:column].to_sym => RDF::URI(filter[:value]))
  else
    solutions.filter(filter[:column].to_sym => RDF::Literal(filter[:value], :datatype => self.filter_type(filter[:type])))
  end
end

.filter_type(filter) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/bbc_data_service/rdf_parser.rb', line 89

def self.filter_type(filter)
  case filter
  when :string
    return "xsd:string"
  when :xml_string
    return "http://www.w3.org/2001/XMLSchema#string"
  else
    raise "Unsupported type passed"
  end
end

.generate_query(mappings, rdf_type) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bbc_data_service/rdf_parser.rb', line 100

def self.generate_query(mappings, rdf_type)
  query_hash = ""
  if rdf_type
    rdf_vocabulary, rdf_value = rdf_type.split(":")
    query_hash << "RDF.type => self.vocabularies[:#{rdf_vocabulary}].#{rdf_value},"
  end
  mappings.each do |vocabulary, values|
    values.each do |key, value|
      query_hash << "self.vocabularies[:#{vocabulary}].#{value} => :#{key},"
    end
  end
  self.instance_eval("{#{query_hash}}")
end

.parse_feed(registered_fixture, options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bbc_data_service/rdf_parser.rb', line 10

def self.parse_feed(registered_fixture, options)
  feed_url = registered_fixture[:feed_url]
  mappings = registered_fixture[:mappings]
  rdf_type = registered_fixture[:rdf_type]
  graph = RDF::Graph.load(feed_url)
  query = RDF::Query.new({
    :fixture => self.generate_query(mappings, rdf_type)
  })
  results = []
  fixtures = []
  if options[:filter]
    solutions = query.execute(graph)
    fixtures = self.filter_graph(solutions, options[:filter])
  elsif registered_fixture[:filter]
    solutions = query.execute(graph)
    fixtures = self.filter_graph(solutions, registered_fixture[:filter])
  else
    fixtures = query.execute(graph)
  end
  fixtures.each do |fixture|
    current_fixture = BBCDataService::FeedFixture.new(self.instance_eval(self.execute_query(graph, query, mappings)))
    current_fixture.rdf_object = fixture
    current_fixture = self.process_relationships(graph, registered_fixture, current_fixture, fixture)
    results << current_fixture
  end
  results
end

.process_relationships(graph, fixture_data, current_fixture, rdf_fixture) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bbc_data_service/rdf_parser.rb', line 38

def self.process_relationships(graph, fixture_data, current_fixture, rdf_fixture)
  relationships = fixture_data[:relationships]
  if relationships
    relationships.each do |relationship|
      key = relationship.first
      values = relationship.last
      # Parse this set of RDF, then add to an array on the main object
      if values[:foreign_key]
        if values[:foreign_key].is_a? Hash
          # If hash, the we are use the main objects RDF URI
          main_object_id = rdf_fixture[:fixture]
          key_query = RDF::Query.new({
            main_object_id => self.generate_query(values[:foreign_key][:mappings], values[:foreign_key][:rdf_type])
          })
          key_results = key_query.execute(graph)
          puts key_results.inspect
          #key_fixture = BBCDataService::FeedFixture.new(self.instance_eval(self.execute_query(graph, key_query, values[:foreign_key][:mappings])))
          #foreign_key = key_fixture.key
          #puts foreign_key
        else
          foreign_key = self.instance_eval("current_fixture.#{values[:foreign_key]}")
        end
        query = RDF::Query.new({
          RDF::URI(foreign_key) => self.generate_query(values[:mappings], values[:rdf_type])
        })
      else
        query = RDF::Query.new({
          :fixture => self.generate_query(values[:mappings], values[:rdf_type])
        })
      end
      results = []
      fixtures = query.execute(graph)
      fixtures.each do |fixture|
        relationship_fixture = BBCDataService::FeedFixture.new(self.instance_eval(self.execute_query(graph, query, values[:mappings])))
        relationship_fixture.rdf_object = fixture
        results << relationship_fixture
      end
      current_fixture.add_relationship(key, results)
    end
  end
  current_fixture
end