Class: Qa::LinkedData::GraphService
- Inherits:
-
Object
- Object
- Qa::LinkedData::GraphService
- Defined in:
- app/services/qa/linked_data/graph_service.rb
Class Method Summary collapse
- .deep_copy(graph:) ⇒ Object
-
.filter(graph:, language: nil, remove_blanknode_subjects: false) ⇒ RDF::Graph
Create a new graph with statements filtered out.
-
.load_graph(url:) ⇒ RDF::Graph
Retrieve linked data from specified url.
-
.object_values(graph:, subject:, predicate:) ⇒ Array
Get object values from the graph that have the subject-predicate.
-
.subjects_for_object_value(graph:, predicate:, object_value:) ⇒ Array
Get subjects that have the object value for the predicate in the graph.
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 << st.dup end new_graph end |
.filter(graph:, language: nil, remove_blanknode_subjects: false) ⇒ RDF::Graph
Create a new graph with statements filtered out
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
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.
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.
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 |