Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: bug in the get_rest_mapping method #156

Merged
4 changes: 2 additions & 2 deletions lib/ontologies_linked_data/mappings/mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
31 changes: 27 additions & 4 deletions lib/ontologies_linked_data/models/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -137,8 +135,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
Expand Down
12 changes: 3 additions & 9 deletions lib/ontologies_linked_data/monkeypatches/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

##
Expand Down
4 changes: 2 additions & 2 deletions lib/ontologies_linked_data/serializers/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/ontologies_linked_data/serializers/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
115 changes: 85 additions & 30 deletions test/models/test_mappings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -308,4 +288,79 @@ 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:)
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