Class: Qa::LinkedData::GraphService

Inherits:
Object
  • Object
show all
Defined in:
app/services/qa/linked_data/graph_service.rb

Class Method Summary collapse

Class Method Details

.deep_copy(graph:) ⇒ Object



57
58
59
60
61
62
63
# File 'app/services/qa/linked_data/graph_service.rb', line 57

def deep_copy(graph:)
  new_graph = RDF::Graph.new
  graph.statements.each do |st|
    new_graph.insert(st.dup)
  end
  new_graph
end

.filter(graph:, language: nil, remove_blanknode_subjects: false) ⇒ RDF::Graph

Create a new graph with statements filtered out

Parameters:

  • graph (RDF::Graph)

    the original graph to be filtered

  • language (Array<Symbol>) (defaults to: nil)

    will keep any statement whose object’s language matches the language filter (only applies to statements that respond to language) (e.g. [:fr] or [:en, :fr])

  • remove_blanknode_subjects (Boolean) (defaults to: false)

    will remove any statement whose subject is a blanknode, if true

Returns:

  • (RDF::Graph)

    a new instance of graph with statements not matching the filters removed



21
22
23
24
25
26
27
28
29
# File 'app/services/qa/linked_data/graph_service.rb', line 21

def filter(graph:, language: nil, remove_blanknode_subjects: false)
  return graph unless graph.present?
  return graph unless language.present? || remove_blanknode_subjects
  filtered_graph = deep_copy(graph: graph)
  filtered_graph.statements.each do |st|
    filtered_graph.delete(st) if filter_out_blanknode(remove_blanknode_subjects, st.subject) || filter_out_language(graph, language, st)
  end
  filtered_graph
end

.load_graph(url:) ⇒ RDF::Graph

Retrieve linked data from specified url

Parameters:

  • url (String)

    from which to retrieve linked data

Returns:

  • (RDF::Graph)

    graph of linked data



9
10
11
12
13
# File 'app/services/qa/linked_data/graph_service.rb', line 9

def load_graph(url:)
  RDF::Graph.load(url)
rescue IOError => e
  process_error(e, url)
end

.object_values(graph:, subject:, predicate:) ⇒ Array

Get object values from the graph that have the subject-predicate.

Parameters:

  • graph (RDF::Graph)

    the graph to search

  • subject (RDF::URI)

    the URI of the subject

  • predicate (RDF::URI)

    the URI of the predicate

Returns:

  • (Array)

    all object values for the subject-predicate pair



36
37
38
39
40
41
42
# File 'app/services/qa/linked_data/graph_service.rb', line 36

def object_values(graph:, subject:, predicate:)
  values = []
  graph.query([subject, predicate, :object]) do |statement|
    values << statement.object
  end
  values
end

.subjects_for_object_value(graph:, predicate:, object_value:) ⇒ Array

Get subjects that have the object value for the predicate in the graph.

Parameters:

  • graph (RDF::Graph)

    the graph to search

  • predicate (RDF::URI)

    the URI of the predicate

  • object_value (String)

    the object value

Returns:

  • (Array)

    all subjects for the predicate-object_value pair



49
50
51
52
53
54
55
# File 'app/services/qa/linked_data/graph_service.rb', line 49

def subjects_for_object_value(graph:, predicate:, object_value:)
  subjects = []
  graph.query([:subject, predicate, object_value]) do |statement|
    subjects << statement.subject
  end
  subjects
end