From e758198645969224830ca671286f2294beebd8b1 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 5 Sep 2022 04:17:52 +0200 Subject: [PATCH 1/7] add url prefix replacement utilities functions to base model --- lib/ontologies_linked_data/models/base.rb | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/ontologies_linked_data/models/base.rb b/lib/ontologies_linked_data/models/base.rb index 7742329d..a0b928f2 100644 --- a/lib/ontologies_linked_data/models/base.rb +++ b/lib/ontologies_linked_data/models/base.rb @@ -137,8 +137,33 @@ def self.goo_aggregates_to_load(attributes = []) included_aggregates end + def self.replace_url_prefix_to_id(id) + if replace_url_prefix?(id) + id = RDF::IRI.new(id.to_s.sub(LinkedData.settings.rest_url_prefix, LinkedData.settings.id_url_prefix)) + end + id + end + + def self.replace_url_id_to_prefix(id) + if replace_url_id?(id) + id.to_s.gsub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix) + else + id + end + end + + def self.replace_url_prefix?(id) + LinkedData.settings.replace_url_prefix && id.to_s.start_with?(LinkedData.settings.rest_url_prefix) + end + + def self.replace_url_id?(id) + LinkedData.settings.replace_url_prefix && id.to_s.start_with?(LinkedData.settings.id_url_prefix) + end + private + + ## # Looks for an object 'owner' and looks in Thread.current[:remote_user] # to see if the user for this request matches the owner From 22aefee3712e2cd33eb152cf3a05b7eebfda5f9a Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 5 Sep 2022 09:19:31 +0200 Subject: [PATCH 2/7] update with the new replace_url_prefix_to_id in the base find --- lib/ontologies_linked_data/models/base.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/ontologies_linked_data/models/base.rb b/lib/ontologies_linked_data/models/base.rb index a0b928f2..6f58e736 100644 --- a/lib/ontologies_linked_data/models/base.rb +++ b/lib/ontologies_linked_data/models/base.rb @@ -27,12 +27,10 @@ def delete(*args) # Override find method to make sure the id matches what is in the RDF store # Only do this if the setting is enabled, string comparison sucks def self.find(id, *options) - if LinkedData.settings.replace_url_prefix && id.to_s.start_with?(LinkedData.settings.rest_url_prefix) - id = RDF::IRI.new(id.to_s.sub(LinkedData.settings.rest_url_prefix, LinkedData.settings.id_url_prefix)) - end + id = replace_url_prefix_to_id(id) # Handle `+` to ` ` conversion here because Sinatra doesn't do it for URI's - id = id.gsub("+", " ") unless id.start_with?("http") + id = id.gsub('+', ' ') unless id.start_with?('http') super(id, *options) end From 65740afb893e8215592c94bf3f29d2940ddb636a Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 5 Sep 2022 09:20:11 +0200 Subject: [PATCH 3/7] update with the new replace_url_prefix_to_id every where its used --- lib/ontologies_linked_data/monkeypatches/object.rb | 12 +++--------- lib/ontologies_linked_data/serializers/json.rb | 4 ++-- lib/ontologies_linked_data/serializers/xml.rb | 4 ++-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/ontologies_linked_data/monkeypatches/object.rb b/lib/ontologies_linked_data/monkeypatches/object.rb index 067e1c83..deadf71c 100644 --- a/lib/ontologies_linked_data/monkeypatches/object.rb +++ b/lib/ontologies_linked_data/monkeypatches/object.rb @@ -155,16 +155,10 @@ def convert_nonstandard_types(value, options, &block) ## # If the config option is set, turn http://data.bioontology.org urls into the configured REST url def convert_url_prefix(value) - if LinkedData.settings.replace_url_prefix - if value.is_a?(String) && value.start_with?(LinkedData.settings.id_url_prefix) - value = value.sub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix) - end - - if (value.is_a?(Array) || value.is_a?(Set)) && value.first.is_a?(String) && value.first.start_with?(LinkedData.settings.id_url_prefix) - value = value.map {|v| v.sub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix)} - end + tmp = Array(value).map do |val| + LinkedData::Models::Base.replace_url_id_to_prefix(val) end - value + value.is_a?(Array) || value.is_a?(Set) ? tmp : tmp.first end ## diff --git a/lib/ontologies_linked_data/serializers/json.rb b/lib/ontologies_linked_data/serializers/json.rb index bb8d9b6c..7d8c5034 100644 --- a/lib/ontologies_linked_data/serializers/json.rb +++ b/lib/ontologies_linked_data/serializers/json.rb @@ -11,8 +11,8 @@ def self.serialize(obj, options = {}) # Add the id to json-ld attribute if current_cls.ancestors.include?(LinkedData::Hypermedia::Resource) && !current_cls.embedded? && hashed_obj.respond_to?(:id) - prefixed_id = LinkedData.settings.replace_url_prefix ? hashed_obj.id.to_s.gsub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix) : hashed_obj.id.to_s - hash["@id"] = prefixed_id + prefixed_id = LinkedData::Models::Base.replace_url_id_to_prefix(hashed_obj.id) + hash["@id"] = prefixed_id.to_s end # Add the type hash["@type"] = current_cls.type_uri.to_s if hash["@id"] && current_cls.respond_to?(:type_uri) diff --git a/lib/ontologies_linked_data/serializers/xml.rb b/lib/ontologies_linked_data/serializers/xml.rb index 575158eb..a694ee0a 100644 --- a/lib/ontologies_linked_data/serializers/xml.rb +++ b/lib/ontologies_linked_data/serializers/xml.rb @@ -9,8 +9,8 @@ def self.serialize(obj, options) current_cls = hashed_obj.respond_to?(:klass) ? hashed_obj.klass : hashed_obj.class # Add the id and type if current_cls.ancestors.include?(LinkedData::Hypermedia::Resource) && !current_cls.embedded? - prefixed_id = LinkedData.settings.replace_url_prefix ? hashed_obj.id.to_s.gsub(LinkedData.settings.id_url_prefix, LinkedData.settings.rest_url_prefix) : hashed_obj.id.to_s - hash["id"] = prefixed_id + prefixed_id = LinkedData::Models::Base.replace_url_id_to_prefix(hashed_obj.id) + hash["id"] = prefixed_id.to_s hash["type"] = current_cls.type_uri.to_s end From 454e87d0eaf75e15a31bd4f4f741cd4b3ca939bc Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 5 Sep 2022 10:08:39 +0200 Subject: [PATCH 4/7] refactor rest mappings tests by extracting some commun methods --- test/models/test_mappings.rb | 85 +++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/test/models/test_mappings.rb b/test/models/test_mappings.rb index 0bf1aff0..856811e5 100644 --- a/test/models/test_mappings.rb +++ b/test/models/test_mappings.rb @@ -226,40 +226,20 @@ def test_mappings_two_ontologies end def test_mappings_rest - mapping_term_a = ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Image_Algorithm", - "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Image", - "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Integration_and_Interoperability_Tools" ] - submissions_a = [ -"http://data.bioontology.org/ontologies/MAPPING_TEST1/submissions/latest", -"http://data.bioontology.org/ontologies/MAPPING_TEST1/submissions/latest", -"http://data.bioontology.org/ontologies/MAPPING_TEST1/submissions/latest" ] - mapping_term_b = ["http://purl.org/incf/ontology/Computational_Neurosciences/cno_alpha.owl#cno_0000202", - "http://purl.org/incf/ontology/Computational_Neurosciences/cno_alpha.owl#cno_0000203", - "http://purl.org/incf/ontology/Computational_Neurosciences/cno_alpha.owl#cno_0000205" ] - submissions_b = [ -"http://data.bioontology.org/ontologies/MAPPING_TEST2/submissions/latest", -"http://data.bioontology.org/ontologies/MAPPING_TEST2/submissions/latest", -"http://data.bioontology.org/ontologies/MAPPING_TEST2/submissions/latest" ] - relations = [ "http://www.w3.org/2004/02/skos/core#exactMatch", - "http://www.w3.org/2004/02/skos/core#closeMatch", - "http://www.w3.org/2004/02/skos/core#relatedMatch" ] - user = LinkedData::Models::User.where.include(:username).all[0] - assert user != nil + mapping_term_a, mapping_term_b, submissions_a, submissions_b, relations, user = rest_mapping_data + mappings_created = [] 3.times do |i| - process = LinkedData::Models::MappingProcess.new - process.name = "proc#{i}" - process.relation = RDF::URI.new(relations[i]) - process.creator= user - process.save - classes = [] - classes << LinkedData::Mappings.read_only_class( - mapping_term_a[i], submissions_a[i]) - classes << LinkedData::Mappings.read_only_class( - mapping_term_b[i], submissions_b[i]) - mappings_created << LinkedData::Mappings.create_rest_mapping(classes, process) + classes = get_mapping_classes(term_a:mapping_term_a[i], term_b: mapping_term_b[i], + submissions_a: submissions_a[i], submissions_b: submissions_b[i]) + + mappings_created << create_rest_mapping(relation: RDF::URI.new(relations[i]), + user: user, + classes: classes, + name: "proc#{i}") end + ont_id = submissions_a.first.split("/")[0..-3].join("/") latest_sub = LinkedData::Models::Ontology.find(RDF::URI.new(ont_id)).first.latest_submission LinkedData::Mappings.create_mapping_counts(Logger.new(TestLogFile.new)) @@ -308,4 +288,49 @@ def test_mappings_rest end assert_equal 3, rest_mapping_count end + private + + def get_mapping_classes(term_a:, term_b:, submissions_a:, submissions_b:) + classes = [] + classes << LinkedData::Mappings.read_only_class( + term_a, submissions_a) + classes << LinkedData::Mappings.read_only_class( + term_b, submissions_b) + classes + end + + def rest_mapping_data + mapping_term_a = ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Image_Algorithm", + "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Image", + "http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Integration_and_Interoperability_Tools" ] + submissions_a = [ + "http://data.bioontology.org/ontologies/MAPPING_TEST1/submissions/latest", + "http://data.bioontology.org/ontologies/MAPPING_TEST1/submissions/latest", + "http://data.bioontology.org/ontologies/MAPPING_TEST1/submissions/latest" ] + mapping_term_b = ["http://purl.org/incf/ontology/Computational_Neurosciences/cno_alpha.owl#cno_0000202", + "http://purl.org/incf/ontology/Computational_Neurosciences/cno_alpha.owl#cno_0000203", + "http://purl.org/incf/ontology/Computational_Neurosciences/cno_alpha.owl#cno_0000205" ] + submissions_b = [ + "http://data.bioontology.org/ontologies/MAPPING_TEST2/submissions/latest", + "http://data.bioontology.org/ontologies/MAPPING_TEST2/submissions/latest", + "http://data.bioontology.org/ontologies/MAPPING_TEST2/submissions/latest" ] + relations = [ "http://www.w3.org/2004/02/skos/core#exactMatch", + "http://www.w3.org/2004/02/skos/core#closeMatch", + "http://www.w3.org/2004/02/skos/core#relatedMatch" ] + + user = LinkedData::Models::User.where.include(:username).all[0] + assert user != nil + + [mapping_term_a, mapping_term_b, submissions_a, submissions_b, relations, user] + end + + def create_rest_mapping(relation:, user:, name:, classes:) + process = LinkedData::Models::MappingProcess.new + process.name = name + process.relation = relation + process.creator= user + process.save + + LinkedData::Mappings.create_rest_mapping(classes, process) + end end From f365f83b3613124354798621503561883045fc25 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 5 Sep 2022 10:08:59 +0200 Subject: [PATCH 5/7] add get_rest_mapping unit test --- test/models/test_mappings.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/models/test_mappings.rb b/test/models/test_mappings.rb index 856811e5..d7acab13 100644 --- a/test/models/test_mappings.rb +++ b/test/models/test_mappings.rb @@ -288,6 +288,36 @@ def test_mappings_rest end assert_equal 3, rest_mapping_count end + + def test_get_rest_mapping + mapping_term_a, mapping_term_b, submissions_a, submissions_b, relations, user = rest_mapping_data + + classes = get_mapping_classes(term_a:mapping_term_a[0], term_b: mapping_term_b[0], + submissions_a: submissions_a[0], submissions_b: submissions_b[0]) + + mappings_created = [] + mappings_created << create_rest_mapping(relation: RDF::URI.new(relations[0]), + user: user, + classes: classes, + name: "proc#{0}") + + assert_equal 1, mappings_created.size + created_mapping_id = mappings_created.first.id + + refute_nil LinkedData::Mappings.get_rest_mapping(created_mapping_id) + + old_replace = LinkedData.settings.replace_url_prefix + LinkedData.settings.replace_url_prefix = true + + old_rest_url = LinkedData.settings.rest_url_prefix + LinkedData.settings.rest_url_prefix = 'data.test.org' + + refute_nil LinkedData::Mappings.get_rest_mapping(LinkedData::Models::Base.replace_url_id_to_prefix(created_mapping_id)) + + LinkedData.settings.rest_url_prefix = old_rest_url + LinkedData.settings.replace_url_prefix = old_replace + end + private def get_mapping_classes(term_a:, term_b:, submissions_a:, submissions_b:) From 94eeb6533cbf501f88df8feaac868a0cef60a1a9 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 5 Sep 2022 10:10:54 +0200 Subject: [PATCH 6/7] save the created rest mapping id with the initializer --- lib/ontologies_linked_data/mappings/mappings.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ontologies_linked_data/mappings/mappings.rb b/lib/ontologies_linked_data/mappings/mappings.rb index 33272848..df8cb1df 100644 --- a/lib/ontologies_linked_data/mappings/mappings.rb +++ b/lib/ontologies_linked_data/mappings/mappings.rb @@ -437,7 +437,7 @@ def self.create_rest_mapping(classes,process) graph_insert << [c.id, RDF::URI.new(rest_predicate), backup_mapping.id] Goo.sparql_update_client.insert_data(graph_insert, graph: sub.id) end - mapping = LinkedData::Models::Mapping.new(classes,"REST",process) + mapping = LinkedData::Models::Mapping.new(classes,"REST",process, backup_mapping.id) return mapping end From dc35d5070699284505b05a6a9f366335765ac386 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Mon, 5 Sep 2022 10:11:25 +0200 Subject: [PATCH 7/7] use replace_url_prefix_to_id in the get_rest_mapping method --- lib/ontologies_linked_data/mappings/mappings.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ontologies_linked_data/mappings/mappings.rb b/lib/ontologies_linked_data/mappings/mappings.rb index df8cb1df..77709c14 100644 --- a/lib/ontologies_linked_data/mappings/mappings.rb +++ b/lib/ontologies_linked_data/mappings/mappings.rb @@ -380,7 +380,7 @@ def self.get_rest_mapping(mapping_id) GRAPH ?s2 { ?c2 <#{rest_predicate}> ?uuid . } -FILTER(?uuid = <#{mapping_id}>) +FILTER(?uuid = <#{LinkedData::Models::Base.replace_url_prefix_to_id(mapping_id)}>) FILTER(?s1 != ?s2) } LIMIT 1 eos