Class: Triannon::LdpToOaMapper
- Inherits:
-
Object
- Object
- Triannon::LdpToOaMapper
- Defined in:
- app/services/triannon/ldp_to_oa_mapper.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#oa_graph ⇒ Object
Returns the value of attribute oa_graph.
Class Method Summary collapse
-
.ldp_to_oa(ldp_anno) ⇒ Object
maps an AnnotationLdp to an OA RDF::Graph.
Instance Method Summary collapse
- #extract_base ⇒ Object
- #extract_bodies ⇒ Object
- #extract_targets ⇒ Object
-
#initialize(ldp_anno) ⇒ LdpToOaMapper
constructor
A new instance of LdpToOaMapper.
-
#map_choice(uri_obj, predicate) ⇒ Boolean
if uri_obj has a type of RDF::OpenAnnotation.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.
-
#map_content_as_text(uri_obj, predicate, subject_obj = @root_uri) ⇒ Boolean
if uri_obj has a type of RDF::Content.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.
-
#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.
-
#map_specific_resource(uri_obj, predicate) ⇒ Boolean
if uri_obj has a type of RDF::OpenAnnotation.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.
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 = Triannon::Graph.new g end |
Instance Attribute Details
#id ⇒ Object
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_graph ⇒ Object
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_base ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 22 def extract_base root_subject_solns = @ldp_anno_graph.query Triannon::Graph.anno_query if root_subject_solns.count == 1 @id = root_subject_solns[0].s.to_s.split('/').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::OpenAnnotation.Annotation @oa_graph << [@root_uri, RDF.type, RDF::OpenAnnotation.Annotation] elsif stmnt.predicate == RDF::OpenAnnotation.motivatedBy @oa_graph << [@root_uri, stmnt.predicate, stmnt.object] elsif stmnt.predicate == RDF::OpenAnnotation.annotatedAt @oa_graph << [@root_uri, stmnt.predicate, stmnt.object] end end end |
#extract_bodies ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 43 def extract_bodies @ldp_anno.body_uris.each { |body_uri| if !map_external_ref(body_uri, RDF::OpenAnnotation.hasBody) && !map_content_as_text(body_uri, RDF::OpenAnnotation.hasBody) && !map_specific_resource(body_uri, RDF::OpenAnnotation.hasBody) map_choice(body_uri, RDF::OpenAnnotation.hasBody) end } end |
#extract_targets ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 53 def extract_targets @ldp_anno.target_uris.each { |target_uri| if !map_external_ref(target_uri, RDF::OpenAnnotation.hasTarget) && !map_specific_resource(target_uri, RDF::OpenAnnotation.hasTarget) map_choice(target_uri, RDF::OpenAnnotation.hasTarget) end } end |
#map_choice(uri_obj, predicate) ⇒ Boolean
if uri_obj has a type of RDF::OpenAnnotation.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::OpenAnnotation.hasTarget
174 175 176 177 178 179 180 181 182 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 |
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 174 def map_choice uri_obj, predicate solns = @ldp_anno_graph.query [uri_obj, RDF.type, RDF::OpenAnnotation.Choice] if solns.count > 0 blank_node = RDF::Node.new @oa_graph << [@root_uri, predicate, blank_node] default_obj = nil item_objs = [] choice_stmts = Triannon::Graph.subject_statements(uri_obj, @ldp_anno_graph) choice_stmts.each { |stmt| if stmt.predicate == RDF::OpenAnnotation.default default_obj = stmt.object # assume it is either ContentAsText or external ref if !map_content_as_text(default_obj, RDF::OpenAnnotation.default, blank_node) map_external_ref(default_obj, RDF::OpenAnnotation.default, blank_node) end elsif stmt.predicate == RDF::OpenAnnotation.item item_objs << stmt.object # assume it is either ContentAsText or external ref if !map_content_as_text(stmt.object, RDF::OpenAnnotation.item, blank_node) map_external_ref(stmt.object, RDF::OpenAnnotation.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::Content.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::OpenAnnotation.hasTarget
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 95 def map_content_as_text uri_obj, predicate, subject_obj = @root_uri solns = @ldp_anno_graph.query [uri_obj, RDF.type, RDF::Content.ContentAsText] if solns.count > 0 blank_node = RDF::Node.new @oa_graph << [subject_obj, predicate, blank_node] Triannon::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::OpenAnnotation.hasTarget
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 69 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] Triannon::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::OpenAnnotation.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::OpenAnnotation.hasTarget
121 122 123 124 125 126 127 128 129 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 |
# File 'app/services/triannon/ldp_to_oa_mapper.rb', line 121 def map_specific_resource uri_obj, predicate solns = @ldp_anno_graph.query [uri_obj, RDF.type, RDF::OpenAnnotation.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 = Triannon::Graph.subject_statements(uri_obj, @ldp_anno_graph) specific_res_stmts.each { |stmt| if stmt.predicate == RDF::OpenAnnotation.hasSource # expecting a hash URI source_obj = stmt.object if source_obj.to_s.match("#{uri_obj.to_s}#source") source_has_ext_uri = map_external_ref source_obj, RDF::OpenAnnotation.hasSource, blank_node end elsif stmt.predicate == RDF::OpenAnnotation.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::OpenAnnotation.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 |