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) ⇒ LdpToOaMapper

Returns a new instance of LdpToOaMapper.



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

def initialize ldp_anno
  @ldp_anno = ldp_anno
  @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.



13
14
15
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 13

def id
  @id
end

#oa_graphObject

Returns the value of attribute oa_graph.



13
14
15
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 13

def oa_graph
  @oa_graph
end

Class Method Details

.ldp_to_oa(ldp_anno) ⇒ Object

maps an AnnotationLdp to an OA RDF::Graph



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

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

Instance Method Details

#extract_baseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 22

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?('/')
    container_path = Triannon.config[:ldp]['uber_container']
    if container_path
      container_path.strip!
      container_path = container_path[1..-1] if container_path.start_with?('/')
      container_path.chop! if container_path.end_with?('/')
      stored_url = "#{stored_url}/#{container_path}"
    end
    @id = root_subject_solns[0].s.to_s.split("#{stored_url}/").last
    base_url = Triannon.config[:triannon_base_url]
    base_url.strip!
    base_url.chop! if base_url[-1] == '/'
    @root_uri = RDF::URI.new(base_url + "/#{@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



52
53
54
55
56
57
58
59
60
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 52

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



62
63
64
65
66
67
68
69
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 62

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



183
184
185
186
187
188
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
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 183

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 104

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 78

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



130
131
132
133
134
135
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
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 130

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