Class: Triannon::Graph
- Inherits:
-
Object
- Object
- Triannon::Graph
- Defined in:
- lib/triannon/graph.rb
Overview
a wrapper class for RDF::Graph that adds methods specific to Triannon this is intended to be used for an RDF::Graph of a single annotation
Class Method Summary collapse
-
.anno_query ⇒ RDF::Query
Query for a subject :s with type of RDF::OpenAnnotation.Annotation.
-
.subject_statements(subject, graph) ⇒ Array[RDF::Statement]
given an RDF::Resource (an RDF::Node or RDF::URI), look for all the statements with that object as the subject, and recurse through the graph to find all descendant statements pertaining to the subject.
Instance Method Summary collapse
-
#annotated_at ⇒ String
The datetime from the annotatedAt property, or nil.
-
#body_chars ⇒ Array<String>
For all bodies that are of type ContentAsText, get the characters as a single String in the returned Array.
-
#id_as_url ⇒ String
The id of this annotation as a url string, or nil if it is a Node.
-
#initialize(rdf_graph) ⇒ Graph
constructor
instantiate this class for an RDF::Graph of a single annotation.
-
#jsonld_iiif ⇒ Object
Json-ld representation of graph with IIIF context as a url.
-
#jsonld_oa ⇒ Object
Json-ld representation of graph with OpenAnnotation context as a url.
-
#make_null_relative_uri_out_of_blank_node ⇒ Object
transform an outer blank node into a null relative URI.
-
#method_missing(sym, *args, &block) ⇒ Object
send unknown methods to RDF::Graph.
-
#motivated_by ⇒ Array<String>
Array of urls expressing the OA motivated_by values.
-
#predicate_urls(predicate) ⇒ Array<String>
Urls for the predicate, as an Array of Strings.
-
#remove_has_body_statements ⇒ Object
remove all RDF::OpenAnnotation.hasBody statements and any other statements associated with body objects.
-
#remove_has_target_statements ⇒ Object
remove all RDF::OpenAnnotation.hasTarget statements and any other statements associated with body objects.
-
#remove_non_base_statements ⇒ Object
remove all RDF::OpenAnnotation.hasBody and .hasTarget statements and any other statements associated with body and target objects, leaving all statements to be stored as part of base object in LDP store.
-
#remove_predicate_and_its_object_statements(predicate) ⇒ Object
remove all such predicate statements and any other statements associated with predicates’ objects.
Constructor Details
#initialize(rdf_graph) ⇒ Graph
instantiate this class for an RDF::Graph of a single annotation
32 33 34 |
# File 'lib/triannon/graph.rb', line 32 def initialize(rdf_graph) @graph = rdf_graph end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
send unknown methods to RDF::Graph
168 169 170 |
# File 'lib/triannon/graph.rb', line 168 def method_missing(sym, *args, &block) @graph.send sym, *args, &block end |
Class Method Details
.anno_query ⇒ RDF::Query
Returns query for a subject :s with type of RDF::OpenAnnotation.Annotation.
23 24 25 26 |
# File 'lib/triannon/graph.rb', line 23 def self.anno_query q = RDF::Query.new q << [:s, RDF.type, RDF::OpenAnnotation.Annotation] end |
.subject_statements(subject, graph) ⇒ Array[RDF::Statement]
given an RDF::Resource (an RDF::Node or RDF::URI), look for all the statements with that object
as the subject, and recurse through the graph to find all descendant statements pertaining to the subject
13 14 15 16 17 18 19 20 |
# File 'lib/triannon/graph.rb', line 13 def self.subject_statements(subject, graph) result = [] graph.query([subject, nil, nil]).each { |stmt| result << stmt subject_statements(stmt.object, graph).each { |s| result << s } } result.uniq end |
Instance Method Details
#annotated_at ⇒ String
Returns The datetime from the annotatedAt property, or nil.
107 108 109 110 111 112 |
# File 'lib/triannon/graph.rb', line 107 def annotated_at solution = @graph.query [nil, RDF::OpenAnnotation.annotatedAt, nil] if solution && solution.size == 1 solution.first.object.to_s end end |
#body_chars ⇒ Array<String>
For all bodies that are of type ContentAsText, get the characters as a single String in the returned Array.
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/triannon/graph.rb', line 93 def body_chars result = [] q = RDF::Query.new q << [nil, RDF::OpenAnnotation.hasBody, :body] q << [:body, RDF.type, RDF::Content.ContentAsText] q << [:body, RDF::Content.chars, :body_chars] solns = @graph.query q solns.each { |soln| result << soln.body_chars.value } result end |
#id_as_url ⇒ String
Returns the id of this annotation as a url string, or nil if it is a Node.
55 56 57 58 59 60 61 62 |
# File 'lib/triannon/graph.rb', line 55 def id_as_url solution = @graph.query self.class.anno_query if solution && solution.size == 1 rdf_resource = solution.first.s rdf_resource.to_s if rdf_resource.is_a?(RDF::URI) # TODO: raise exception if not a URI? end end |
#jsonld_iiif ⇒ Object
Returns json-ld representation of graph with IIIF context as a url.
45 46 47 48 49 50 |
# File 'lib/triannon/graph.rb', line 45 def jsonld_iiif inline_context = @graph.dump(:jsonld, :context => Triannon::JsonldContext::IIIF_CONTEXT_URL) hash_from_json = JSON.parse(inline_context) hash_from_json["@context"] = Triannon::JsonldContext::IIIF_CONTEXT_URL hash_from_json.to_json end |
#jsonld_oa ⇒ Object
Returns json-ld representation of graph with OpenAnnotation context as a url.
37 38 39 40 41 42 |
# File 'lib/triannon/graph.rb', line 37 def jsonld_oa inline_context = @graph.dump(:jsonld, :context => Triannon::JsonldContext::OA_DATED_CONTEXT_URL) hash_from_json = JSON.parse(inline_context) hash_from_json["@context"] = Triannon::JsonldContext::OA_DATED_CONTEXT_URL hash_from_json.to_json end |
#make_null_relative_uri_out_of_blank_node ⇒ Object
transform an outer blank node into a null relative URI
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/triannon/graph.rb', line 148 def make_null_relative_uri_out_of_blank_node anno_stmts = @graph.query([nil, RDF.type, RDF::OpenAnnotation.Annotation]) anno_rdf_obj = anno_stmts.first.subject if anno_rdf_obj.is_a?(RDF::Node) # use null relative URI representation of blank node anno_subject = RDF::URI.new else # it's already a URI anno_subject = anno_rdf_obj end Triannon::Graph.subject_statements(anno_rdf_obj, @graph).each { |s| if s.subject == anno_rdf_obj && anno_subject != anno_rdf_obj @graph << RDF::Statement({:subject => anno_subject, :predicate => s.predicate, :object => s.object}) @graph.delete s end } end |
#motivated_by ⇒ Array<String>
Returns Array of urls expressing the OA motivated_by values.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/triannon/graph.rb', line 65 def motivated_by motivations = [] q = self.class.anno_query.dup q << [:s, RDF::OpenAnnotation.motivatedBy, :motivated_by] solution = @graph.query q if solution && solution.size > 0 solution.each {|res| motivations << res.motivated_by.to_s } end # TODO: raise exception if none? (validation) motivations end |
#predicate_urls(predicate) ⇒ Array<String>
Returns urls for the predicate, as an Array of Strings.
81 82 83 84 85 86 87 88 89 |
# File 'lib/triannon/graph.rb', line 81 def predicate_urls(predicate) urls = [] predicate_solns = @graph.query [nil, predicate, nil] predicate_solns.each { |predicate_stmt | predicate_obj = predicate_stmt.object urls << predicate_obj.to_str.strip if predicate_obj.is_a?(RDF::URI) } urls end |
#remove_has_body_statements ⇒ Object
remove all RDF::OpenAnnotation.hasBody statements and any other statements associated with body objects
126 127 128 |
# File 'lib/triannon/graph.rb', line 126 def remove_has_body_statements remove_predicate_and_its_object_statements RDF::OpenAnnotation.hasBody end |
#remove_has_target_statements ⇒ Object
remove all RDF::OpenAnnotation.hasTarget statements and any other statements associated with body objects
131 132 133 |
# File 'lib/triannon/graph.rb', line 131 def remove_has_target_statements remove_predicate_and_its_object_statements RDF::OpenAnnotation.hasTarget end |
#remove_non_base_statements ⇒ Object
remove all RDF::OpenAnnotation.hasBody and .hasTarget statements
and any other statements associated with body and target objects,
leaving all statements to be stored as part of base object in LDP store
120 121 122 123 |
# File 'lib/triannon/graph.rb', line 120 def remove_non_base_statements remove_has_target_statements remove_has_body_statements end |
#remove_predicate_and_its_object_statements(predicate) ⇒ Object
remove all such predicate statements and any other statements associated with predicates’ objects
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/triannon/graph.rb', line 136 def remove_predicate_and_its_object_statements(predicate) predicate_stmts = @graph.query([nil, predicate, nil]) predicate_stmts.each { |pstmt| pred_obj = pstmt.object Triannon::Graph.subject_statements(pred_obj, @graph).each { |s| @graph.delete s } unless !Triannon::Graph.subject_statements(pred_obj, @graph) @graph.delete pstmt } end |