Class: Annotations2triannon::OpenAnnotation

Inherits:
Object
  • Object
show all
Defined in:
lib/annotations2triannon/open_annotation.rb

Overview

class OpenAnnotation < Resource

Constant Summary collapse

CONTENT =
RDF::Vocab::CNT
OA =
RDF::Vocab::OA
OA_CONTEXT =
'http://www.w3.org/ns/oa.jsonld'
IIIF_CONTEXT =
'http://iiif.io/api/presentation/2/context.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph = RDF::Graph.new, id = nil) ⇒ OpenAnnotation

instantiate this class

Parameters:

  • graph (RDF::Graph) (defaults to: RDF::Graph.new)

    for an open annotation

  • id (UUID|URI|String) (defaults to: nil)

    to identify an open annotation

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/annotations2triannon/open_annotation.rb', line 19

def initialize(graph=RDF::Graph.new, id=nil)
  @@agent ||= Annotations2triannon::AGENT
  raise ArgumentError, 'graph must be RDF::Graph instance' unless graph.instance_of? RDF::Graph
  if graph.empty?
    # create a new open annotation
    @graph = graph
    id.nil? ? @id = get_id : @id = RDF::URI.parse(id)
    insert_annotation
  else
    @graph = graph
    raise ArgumentError, 'graph must be an open annotation' unless is_annotation?
    if id.nil?
      @id = get_id
    else
    end
  end
end

Instance Attribute Details

#graphObject

an RDF::Graph



14
15
16
# File 'lib/annotations2triannon/open_annotation.rb', line 14

def graph
  @graph
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/annotations2triannon/open_annotation.rb', line 13

def id
  @id
end

Instance Method Details

#annotatedAtArray<String>|nil

Returns The datetime from the annotatedAt object(s).

Returns:

  • (Array<String>|nil)

    The datetime from the annotatedAt object(s)



222
223
224
225
# File 'lib/annotations2triannon/open_annotation.rb', line 222

def annotatedAt
  q = [nil, OA.annotatedAt, nil]
  @graph.query(q).collect {|s| s.object }
end

#annotatedByArray<String>|nil

Returns The identity for the annotatedBy object(s).

Returns:

  • (Array<String>|nil)

    The identity for the annotatedBy object(s)



204
205
206
207
# File 'lib/annotations2triannon/open_annotation.rb', line 204

def annotatedBy
  q = [:s, OA.annotatedBy, :o]
  @graph.query(q).collect {|s| s.object }
end

#annotatedBy?(uri = nil) ⇒ boolean

Returns True if the open annotation has any annotatedBy ‘uri’.

Parameters:

  • uri (RDF::URI|String|nil) (defaults to: nil)

    Any object of an annotatedBy predicate

Returns:

  • (boolean)

    True if the open annotation has any annotatedBy ‘uri’



211
212
213
214
215
# File 'lib/annotations2triannon/open_annotation.rb', line 211

def annotatedBy?(uri=nil)
  uri = RDF::URI.parse(uri) unless uri.nil?
  q = [nil, OA.annotatedBy, uri]
  @graph.query(q).size > 0
end

#as_jsonldObject

A json-ld representation of the open annotation



242
243
244
245
# File 'lib/annotations2triannon/open_annotation.rb', line 242

def as_jsonld
  provenance
  JSON::LD::API::fromRDF(@graph)
end

#body_contentAsTextObject



113
114
115
# File 'lib/annotations2triannon/open_annotation.rb', line 113

def body_contentAsText
  body_type CONTENT.ContentAsText
end

#body_contentAsText?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/annotations2triannon/open_annotation.rb', line 117

def body_contentAsText?
  body_contentAsText.size > 0
end

#body_contentCharsArray<String>

For all bodies that are of type ContentAsText, get the characters as a single String in the returned Array.

Returns:

  • (Array<String>)

    body chars as Strings, in an Array (one element for each contentAsText body)



123
124
125
126
127
128
# File 'lib/annotations2triannon/open_annotation.rb', line 123

def body_contentChars
  q = RDF::Query.new
  q << [:body, RDF.type, CONTENT.ContentAsText]
  q << [:body, CONTENT.chars, :body_chars]
  body_graph.query(q).collect {|s| s.body_chars.value }
end

#body_graphObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/annotations2triannon/open_annotation.rb', line 95

def body_graph
  return @body_graph unless @body_graph.nil?
  g = RDF::Graph.new
  hasBody.each do |b|
    @graph.query( [b, :p, :o] ).each_statement {|s| g << s}
    # if b.uri?
    #   begin
    #     b_resource = Resource.new(b)
    #     b_resource.rdf.each_statement {|s| g << s}
    #   rescue
    #     # Nothing to be done here; the Resource#rdf method
    #     # will log errors in RDF retrieval
    #   end
    # end
  end
  @body_graph = g
end

#body_semanticTagObject



130
131
132
# File 'lib/annotations2triannon/open_annotation.rb', line 130

def body_semanticTag
  body_type OA.SemanticTag
end

#body_semanticTag?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/annotations2triannon/open_annotation.rb', line 134

def body_semanticTag?
  body_semanticTag.size > 0
end

#body_type(uri = nil) ⇒ Object



138
139
140
141
# File 'lib/annotations2triannon/open_annotation.rb', line 138

def body_type(uri=nil)
  uri = RDF::URI.parse(uri) unless uri.nil?
  body_graph.query([:body, RDF.type, uri])
end

#get_idObject



37
38
39
40
41
# File 'lib/annotations2triannon/open_annotation.rb', line 37

def get_id
  return @id unless @id.nil?
  q = [nil, RDF.type, OA.Annotation]
  @id = @graph.query(q).collect {|s| s.subject }.first || RDF::URI.parse(UUID.generate)
end

#hasBodyArray

Returns The hasBody object(s).

Returns:

  • (Array)

    The hasBody object(s)



86
87
88
89
# File 'lib/annotations2triannon/open_annotation.rb', line 86

def hasBody
  q = [nil, OA.hasBody, nil]
  @graph.query(q).collect {|s| s.object }
end

#hasBody?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/annotations2triannon/open_annotation.rb', line 91

def hasBody?
  hasBody.length > 0
end

#hasTargetArray

Returns The hasTarget object(s).

Returns:

  • (Array)

    The hasTarget object(s)



71
72
73
74
# File 'lib/annotations2triannon/open_annotation.rb', line 71

def hasTarget
  q = [nil, OA.hasTarget, nil]
  @graph.query(q).collect {|s| s.object }
end

#hasTarget?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/annotations2triannon/open_annotation.rb', line 76

def hasTarget?
  hasTarget.length > 0
end

#insert_annotatedAt(datetime = rdf_now) ⇒ Object



217
218
219
# File 'lib/annotations2triannon/open_annotation.rb', line 217

def insert_annotatedAt(datetime=rdf_now)
  @graph.insert([@id, OA.annotatedAt, datetime])
end

#insert_annotatedBy(annotator = nil) ⇒ Object



199
200
201
# File 'lib/annotations2triannon/open_annotation.rb', line 199

def insert_annotatedBy(annotator=nil)
  @graph.insert([@id, OA.annotatedBy, annotator])
end

#insert_annotationObject



53
54
55
56
57
# File 'lib/annotations2triannon/open_annotation.rb', line 53

def insert_annotation
  s = [@id, RDF.type, OA.Annotation]
  @graph.delete(s)
  @graph.insert(s)
end

#insert_hasBody(body) ⇒ Object



80
81
82
83
# File 'lib/annotations2triannon/open_annotation.rb', line 80

def insert_hasBody(body)
  # TODO: raise ValueError when body is outside hasBody range?
  @graph.insert([@id, OA.hasBody, body])
end

#insert_hasTarget(target) ⇒ Object



65
66
67
68
# File 'lib/annotations2triannon/open_annotation.rb', line 65

def insert_hasTarget(target)
  # TODO: raise ValueError when target is outside hasTarget range?
  @graph.insert([@id, OA.hasTarget, target])
end

#insert_motivatedBy(motivation) ⇒ Object

Insert an ?o for [id, OA.motivatedBy, ?o] where ?o is ‘motivation’

Parameters:

  • motivation (String|URI)

    An open annotation motivation



145
146
147
148
149
# File 'lib/annotations2triannon/open_annotation.rb', line 145

def insert_motivatedBy(motivation)
  # TODO: only accept values allowed by OA.motivationBy range?
  motivation = RDF::URI.parse(motivation)
  @graph.insert([@id, OA.motivatedBy, motivation])
end

#insert_motivatedByCommentingObject

Insert [id, OA.motivatedBy, OA.commenting]



168
169
170
# File 'lib/annotations2triannon/open_annotation.rb', line 168

def insert_motivatedByCommenting
  insert_motivatedBy OA.commenting
end

#insert_motivatedByTaggingObject

Insert [id, OA.motivatedBy, OA.tagging]



184
185
186
# File 'lib/annotations2triannon/open_annotation.rb', line 184

def insert_motivatedByTagging
  insert_motivatedBy OA.tagging
end

#is_annotation?boolean

Returns true if RDF.type is OA.Annotation.

Returns:

  • (boolean)

    true if RDF.type is OA.Annotation



60
61
62
63
# File 'lib/annotations2triannon/open_annotation.rb', line 60

def is_annotation?
  q = [@id, RDF.type, OA.Annotation]
  @graph.query(q).size > 0
end

#motivatedBy(uri = nil) ⇒ Array

Find any matching ?o for ?s OA.motivatedBy ?o where ?o is ‘uri’

Parameters:

  • uri (RDF::URI|String|nil) (defaults to: nil)

    Any object of a motivatedBy predicate

Returns:

  • (Array)

    The motivatedBy object(s)



154
155
156
157
158
# File 'lib/annotations2triannon/open_annotation.rb', line 154

def motivatedBy(uri=nil)
  uri = RDF::URI.parse(uri) unless uri.nil?
  q = [nil, OA.motivatedBy, uri]
  @graph.query(q).collect {|s| s.object }
end

#motivatedBy?(uri = nil) ⇒ boolean

Are there any matching ?o for [?s, OA.motivatedBy, ?o] where ?o is ‘uri’

Parameters:

  • uri (RDF::URI|String|nil) (defaults to: nil)

    Any object of a motivatedBy predicate

Returns:

  • (boolean)

    True if the open annotation has any motivatedBy ‘uri’



163
164
165
# File 'lib/annotations2triannon/open_annotation.rb', line 163

def motivatedBy?(uri=nil)
  motivatedBy(uri).length > 0
end

#motivatedByCommentingObject

Find all the matching ?s for [?s, OA.motivatedBy, OA.commenting]



173
174
175
176
# File 'lib/annotations2triannon/open_annotation.rb', line 173

def motivatedByCommenting
  q = [nil, OA.motivatedBy, OA.commenting]
  @graph.query(q).collect {|s| s.subject }
end

#motivatedByCommenting?Boolean

Are there any matching ?s for [?s, OA.motivatedBy, OA.commenting]

Returns:

  • (Boolean)


179
180
181
# File 'lib/annotations2triannon/open_annotation.rb', line 179

def motivatedByCommenting?
  motivatedByCommenting.length > 0
end

#motivatedByTaggingObject

Find all the matching ?s for [?s, OA.motivatedBy, OA.tagging]



189
190
191
192
# File 'lib/annotations2triannon/open_annotation.rb', line 189

def motivatedByTagging
  q = [nil, OA.motivatedBy, OA.tagging]
  @graph.query(q).collect {|s| s.subject }
end

#motivatedByTagging?Boolean

Are there any matching ?s for [?s, OA.motivatedBy, OA.tagging]

Returns:

  • (Boolean)


195
196
197
# File 'lib/annotations2triannon/open_annotation.rb', line 195

def motivatedByTagging?
  motivatedByTagging.length > 0
end

#open_annotation?boolean

Returns true if RDF.type is OA.Annotation, with OA.hasBody and OA.hasTarget.

Returns:

  • (boolean)

    true if RDF.type is OA.Annotation, with OA.hasBody and OA.hasTarget



44
45
46
47
48
49
50
51
# File 'lib/annotations2triannon/open_annotation.rb', line 44

def open_annotation?
  # TODO: check rules for basic open annotation
  q = RDF::Query.new
  q << [@id, RDF.type, OA.Annotation]
  q << [@id, OA.hasBody, :b]
  q << [@id, OA.hasTarget, :t]
  @graph.query(q).size > 0
end

#provenanceObject



231
232
233
234
235
236
237
238
239
# File 'lib/annotations2triannon/open_annotation.rb', line 231

def provenance
  # http://www.openannotation.org/spec/core/core.html#Provenance
  # When adding the agent, ensure it's not there already, also
  # an open annotation cannot have more than one oa:serializedAt.
  @graph.delete([nil,nil,@@agent])
  @graph.delete([nil, OA.serializedAt, nil])
  @graph << [@id, OA.serializedAt, rdf_now]
  @graph << [@id, OA.serializedBy, @@agent]
end

#rdf_nowObject



227
228
229
# File 'lib/annotations2triannon/open_annotation.rb', line 227

def rdf_now
  RDF::Literal.new(Time.now.utc, :datatype => RDF::XSD.dateTime)
end

#to_jsonld(context = nil) ⇒ Object

Returns json-ld representation of graph with default context.

Parameters:

  • context (String) (defaults to: nil)

    A JSON-LD context URI

Returns:

  • json-ld representation of graph with default context



249
250
251
252
253
254
255
256
# File 'lib/annotations2triannon/open_annotation.rb', line 249

def to_jsonld(context=nil)
  provenance
  if context.nil?
    @graph.dump(:jsonld, standard_prefixes: true)
  else
    @graph.dump(:jsonld, standard_prefixes: true, context: context)
  end
end

#to_jsonld_iiifObject

Returns json-ld representation of graph with IIIF context.

Returns:

  • json-ld representation of graph with IIIF context



259
260
261
# File 'lib/annotations2triannon/open_annotation.rb', line 259

def to_jsonld_iiif
  to_jsonld IIIF_CONTEXT
end

#to_jsonld_oaObject

Returns json-ld representation of graph with OpenAnnotation context.

Returns:

  • json-ld representation of graph with OpenAnnotation context



264
265
266
# File 'lib/annotations2triannon/open_annotation.rb', line 264

def to_jsonld_oa
  to_jsonld OA_CONTEXT
end

#to_ttlObject

A turtle string representation of the open annotation



269
270
271
272
# File 'lib/annotations2triannon/open_annotation.rb', line 269

def to_ttl
  provenance
  @graph.dump(:ttl, standard_prefixes: true)
end