Class: Triannon::LdpToOaMapper

Inherits:
Object
  • Object
show all
Defined in:
app/services/triannon/ldp_to_oa_mapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ldp_anno, root_container) ⇒ LdpToOaMapper

Returns a new instance of LdpToOaMapper.

Parameters:



19
20
21
22
23
24
25
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 19

def initialize(ldp_anno, root_container)
  @ldp_anno = ldp_anno
  @root_container = root_container
  @ldp_anno_graph = ldp_anno.stripped_graph
  g = RDF::Graph.new
  @oa_graph = OA::Graph.new g
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



15
16
17
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 15

def id
  @id
end

#oa_graphObject

Returns the value of attribute oa_graph.



15
16
17
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 15

def oa_graph
  @oa_graph
end

Class Method Details

.ldp_to_oa(ldp_anno, root_container) ⇒ Object

maps a Triannon::AnnotationLdp to an OA RDF::Graph

Parameters:



7
8
9
10
11
12
13
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 7

def self.ldp_to_oa(ldp_anno, root_container)
  mapper = Triannon::LdpToOaMapper.new(ldp_anno, root_container)
  mapper.extract_base
  mapper.extract_bodies
  mapper.extract_targets
  mapper.oa_graph
end

Instance Method Details

#extract_baseObject

load statements from base anno container into @oa_graph



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 28

def extract_base
  root_subject_solns = @ldp_anno_graph.query OA::Graph.anno_query
  if root_subject_solns.count == 1
    stored_url = Triannon.config[:ldp]['url'].strip
    stored_url.chop! if stored_url.end_with?('/')
    uber_container = Triannon.config[:ldp]['uber_container'].strip
    if uber_container
      uber_container = uber_container[1..-1] if uber_container.start_with?('/')
      uber_container.chop! if uber_container.end_with?('/')
      stored_url = "#{stored_url}/#{uber_container}/#{@root_container}"
    end
    @id = root_subject_solns[0].s.to_s.split("#{stored_url}/").last
    base_url = Triannon.config[:triannon_base_url].strip
    base_url.chop! if base_url[-1] == '/'
    @root_uri = RDF::URI.new(base_url + "/#{@root_container}/#{@id}")
  end

  @ldp_anno_graph.each_statement do |stmnt|
    if stmnt.predicate == RDF.type && stmnt.object == RDF::Vocab::OA.Annotation
      @oa_graph << [@root_uri, RDF.type, RDF::Vocab::OA.Annotation]
    elsif stmnt.predicate == RDF::Vocab::OA.motivatedBy
      @oa_graph << [@root_uri, stmnt.predicate, stmnt.object]
    elsif stmnt.predicate == RDF::Vocab::OA.annotatedAt
      @oa_graph << [@root_uri, stmnt.predicate, stmnt.object]
    end
  end
end

#extract_bodiesObject

load statements from anno body containers into @oa_graph



57
58
59
60
61
62
63
64
65
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 57

def extract_bodies
  @ldp_anno.body_uris.each { |body_uri|
    if !map_external_ref(body_uri, RDF::Vocab::OA.hasBody) &&
        !map_content_as_text(body_uri, RDF::Vocab::OA.hasBody) &&
        !map_specific_resource(body_uri, RDF::Vocab::OA.hasBody)
      map_choice(body_uri, RDF::Vocab::OA.hasBody)
    end
  }
end

#extract_targetsObject

load statements from anno target containers into @oa_graph



68
69
70
71
72
73
74
75
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 68

def extract_targets
  @ldp_anno.target_uris.each { |target_uri|
    if !map_external_ref(target_uri, RDF::Vocab::OA.hasTarget) &&
        !map_specific_resource(target_uri, RDF::Vocab::OA.hasTarget)
      map_choice(target_uri, RDF::Vocab::OA.hasTarget)
    end
  }
end

#map_choice(uri_obj, predicate) ⇒ Boolean

if uri_obj has a type of RDF::Vocab::OA.Choice, then this is a skolemized blank node;

add appropriate statements to @oa_graph to represent the blank node and its contents and return true

to be added to @oa_graph, e.g. RDF::Vocab::OA.hasTarget

Parameters:

  • uri_obj (RDF::URI)

    the object that may have type RDF::Vocab::OA.Choice

  • predicate (RDF::URI)

    the predicate for [@root_uri, predicate, (choice)] statement

Returns:

  • (Boolean)

    true if it adds statements to @oa_graph, false otherwise



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 189

def map_choice uri_obj, predicate
  solns = @ldp_anno_graph.query [uri_obj, RDF.type, RDF::Vocab::OA.Choice]
  if solns.count > 0
    blank_node = RDF::Node.new
    @oa_graph << [@root_uri, predicate, blank_node]

    default_obj = nil
    item_objs = []
    choice_stmts = OA::Graph.subject_statements(uri_obj, @ldp_anno_graph)
    choice_stmts.each { |stmt|
      if stmt.predicate == RDF::Vocab::OA.default
        default_obj = stmt.object
        # assume it is either ContentAsText or external ref
        if !map_content_as_text(default_obj, RDF::Vocab::OA.default, blank_node)
          map_external_ref(default_obj, RDF::Vocab::OA.default, blank_node)
        end
      elsif stmt.predicate == RDF::Vocab::OA.item
        item_objs << stmt.object
        # assume it is either ContentAsText or external ref
        if !map_content_as_text(stmt.object, RDF::Vocab::OA.item, blank_node)
          map_external_ref(stmt.object, RDF::Vocab::OA.item, blank_node)
        end
      end
    }

    # We can't know we'll hit item and default statements in graph first,
    # so we must do another pass through the statements to get that information
    choice_stmts.each { |stmt|
      if stmt.subject == uri_obj && stmt.object != default_obj && !item_objs.include?(stmt.object)
        @oa_graph << [blank_node, stmt.predicate, stmt.object]
      # there shouldn't be any other unmapped statements present
      end
    }
    true
  else
    false
  end
end

#map_content_as_text(uri_obj, predicate, subject_obj = @root_uri) ⇒ Boolean

if uri_obj has a type of RDF::Vocab::CNT.ContentAsText, then this is a skolemized blank node;

add appropriate statements to @oa_graph to represent the blank node and its contents and return true

to be added to @oa_graph, e.g. RDF::Vocab::OA.hasTarget

Parameters:

  • uri_obj (RDF::URI)

    the object that may type RDF::Vocab::CNT.ContentAsText

  • predicate (RDF::URI)

    the predicate for [subject_obj, predicate, (ext_url)] statement

  • subject_obj (RDF::URI) (defaults to: @root_uri)

    the subject object to get the predicate statement; defaults to @root_uri

Returns:

  • (Boolean)

    true if it adds statements to @oa_graph, false otherwise



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 110

def map_content_as_text uri_obj, predicate, subject_obj = @root_uri
  solns = @ldp_anno_graph.query [uri_obj, RDF.type, RDF::Vocab::CNT.ContentAsText]
  if solns.count > 0
    blank_node = RDF::Node.new
    @oa_graph << [subject_obj, predicate, blank_node]

    OA::Graph.subject_statements(uri_obj, @ldp_anno_graph).each { |stmt|
      if stmt.subject == uri_obj
        @oa_graph << [blank_node, stmt.predicate, stmt.object]
      else
        # it is a descendant statment - take as is
        @oa_graph << stmt
      end
    }
    true
  else
    false
  end
end

#map_external_ref(uri_obj, predicate, subject_obj = @root_uri) ⇒ Boolean

if uri_obj is the subject of a Triannon.externalReference then add appropriate

statements to @oa_graph and return true

to be added to @oa_graph, e.g. RDF::Vocab::OA.hasTarget

Parameters:

  • uri_obj (RDF::URI)

    the object that may have RDF::Triannon.externalReference

  • predicate (RDF::URI)

    the predicate for [subject_obj, predicate, (ext_url)] statement

  • subject_obj (RDF::URI) (defaults to: @root_uri)

    the subject object to get the predicate statement; defaults to @root_uri

Returns:

  • (Boolean)

    true if it adds statements to @oa_graph, false otherwise



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 84

def map_external_ref uri_obj, predicate, subject_obj = @root_uri
  solns = @ldp_anno_graph.query [uri_obj, RDF::Triannon.externalReference, nil]
  if solns.count > 0
    external_uri = solns.first.object
    @oa_graph << [subject_obj, predicate, external_uri]

    OA::Graph.subject_statements(uri_obj, @ldp_anno_graph).each { |stmt|
      if stmt.subject == uri_obj && stmt.predicate != RDF::Triannon.externalReference
        @oa_graph << [external_uri, stmt.predicate, stmt.object]
      else
        # we should never get here for external references ...
      end
    }
    true
  else
    false
  end
end

#map_specific_resource(uri_obj, predicate) ⇒ Boolean

if uri_obj has a type of RDF::Vocab::OA.SpecificResource, then this is a skolemized blank node;

add appropriate statements to @oa_graph to represent the blank node and its contents and return true

to be added to @oa_graph, e.g. RDF::Vocab::OA.hasTarget

Parameters:

  • uri_obj (RDF::URI)

    the object that may have type RDF::Vocab::OA.SpecificResource

  • predicate (RDF::URI)

    the predicate for [@root_uri, predicate, (sel_res)] statement

Returns:

  • (Boolean)

    true if it adds statements to @oa_graph, false otherwise



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 136

def map_specific_resource uri_obj, predicate
  solns = @ldp_anno_graph.query [uri_obj, RDF.type, RDF::Vocab::OA.SpecificResource]
  if solns.count > 0
    blank_node = RDF::Node.new
    @oa_graph << [@root_uri, predicate, blank_node]

    source_obj = nil
    selector_obj = nil
    selector_blank_node = nil
    specific_res_stmts = OA::Graph.subject_statements(uri_obj, @ldp_anno_graph)
    specific_res_stmts.each { |stmt|
      if stmt.predicate == RDF::Vocab::OA.hasSource
        # expecting a hash URI
        source_obj = stmt.object
        if source_obj.to_s.match("#{uri_obj}#source")
          source_has_ext_uri = map_external_ref source_obj, RDF::Vocab::OA.hasSource, blank_node
        end
      elsif stmt.predicate == RDF::Vocab::OA.hasSelector
        # this becomes a blank node.  Per http://www.openannotation.org/spec/core/specific.html#Selectors
        # "Typically if all of the information needed to resolve the Selector (or other Specifier)
        #  is present within the graph, such as is the case for the
        # FragmentSelector, TextQuoteSelector, TextPositionSelector and DataPositionSelector classes,
        # then there is no need to have a resolvable resource that provides the same information."
        selector_obj = stmt.object
        selector_blank_node = RDF::Node.new
        @oa_graph << [blank_node, RDF::Vocab::OA.hasSelector, selector_blank_node]
      end
    }

    # We can't know we'll hit hasSource and hasSelector statements in graph first,
    # so we must do another pass through the statements to get that information
    specific_res_stmts.each { |stmt|
      if stmt.subject == uri_obj && stmt.object != source_obj && stmt.object != selector_obj
        @oa_graph << [blank_node, stmt.predicate, stmt.object]
      elsif stmt.subject != source_obj
        if selector_blank_node && stmt.subject == selector_obj
          @oa_graph << [selector_blank_node, stmt.predicate, stmt.object]
        end
      # there shouldn't be any other statements present
      end
    }
    true
  else
    false
  end
end