From 835aefa458186485eef60d3845315c478a95ff23 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Wed, 20 Jul 2022 15:36:05 +0200 Subject: [PATCH] re-implement the lang filter for the query builder --- Gemfile | 2 +- Gemfile.lock | 4 +- lib/goo/base/resource.rb | 2 +- lib/goo/sparql/mixins/solution_lang_filter.rb | 38 ++++++++++++------- lib/goo/sparql/solutions_mapper.rb | 34 ++++++++--------- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/Gemfile b/Gemfile index 2ca6a3b78..30167e355 100644 --- a/Gemfile +++ b/Gemfile @@ -18,4 +18,4 @@ group :profiling do gem 'thin' end -gem 'sparql-client', github: 'ncbo/sparql-client', branch: 'master' +gem 'sparql-client', github: 'ontoportal-lirmm/sparql-client', branch: 'master' diff --git a/Gemfile.lock b/Gemfile.lock index 93e4d6e13..8480f2872 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT - remote: https://github.com/ncbo/sparql-client.git - revision: fb4a89b420f8eb6dda5190a126b6c62e32c4c0c9 + remote: https://github.com/ontoportal-lirmm/sparql-client.git + revision: aed51baf4106fd0f3d0e3f9238f0aad9406aa3f0 branch: master specs: sparql-client (1.0.1) diff --git a/lib/goo/base/resource.rb b/lib/goo/base/resource.rb index 345040699..13c4b61a0 100644 --- a/lib/goo/base/resource.rb +++ b/lib/goo/base/resource.rb @@ -385,7 +385,7 @@ def self.map_attributes(inst,equivalent_predicates=nil) o else literal = o - index, lang_val = lang_filter.main_lang_filter inst.id.to_s, attr, literal, literal + index, lang_val = lang_filter.main_lang_filter inst.id.to_s, attr, literal lang_val.to_s if index.eql? :no_lang end end diff --git a/lib/goo/sparql/mixins/solution_lang_filter.rb b/lib/goo/sparql/mixins/solution_lang_filter.rb index fb4d116ba..efb339434 100644 --- a/lib/goo/sparql/mixins/solution_lang_filter.rb +++ b/lib/goo/sparql/mixins/solution_lang_filter.rb @@ -7,13 +7,11 @@ def initialize @other_languages_values = {} end - def other_languages_values - @other_languages_values - end + attr_reader :other_languages_values - def main_lang_filter(id, attr, old_values, new_value) - index, value = lang_index old_values, new_value - save_other_lang_val(id, attr, index, new_value) unless index.eql? :no_lang + def main_lang_filter(id, attr, value) + index, value = lang_index value + save_other_lang_val(id, attr, index, value) unless index.nil? ||index.eql?(:no_lang) [index, value] end @@ -22,11 +20,22 @@ def fill_models_with_other_languages(models_by_id, list_attributes) languages_values.each do |attr, index_values| model_attribute_val = models_by_id[id].instance_variable_get("@#{attr.to_s}") values = languages_values_to_set(index_values, model_attribute_val) - + m = models_by_id[id] + value = nil + is_struct = m.respond_to?(:klass) if !values.nil? && list_attributes.include?(attr) - models_by_id[id].send("#{attr.to_s}=", values || [], on_load: true) + value = values || [] + elsif !values.nil? - models_by_id[id].send("#{attr.to_s}=", values.first || nil, on_load: true) + value = values.first || nil + end + + if value + if is_struct + m[attr] = value + else + m.send("#{attr}=", value, on_load: true) + end end end end @@ -52,14 +61,17 @@ def languages_values_to_set(language_values, no_lang_values) private - def lang_index(object, new_value) - lang = new_value.language + def lang_index(object) + return [nil, object] unless object.is_a?(RDF::Literal) + + lang = object.language + if lang.nil? [:no_lang, object] else index = Goo.language_includes(lang) index = index ? index.to_s.to_sym : :not_matched - [index, new_value] + [index, object] end end @@ -72,8 +84,6 @@ def save_other_lang_val(id, attr, index, value) @other_languages_values[id][attr][index] += Array(value.to_s) end end - - def matched_languages(index_values, model_attribute_val) not_matched_lang = index_values[:not_matched] diff --git a/lib/goo/sparql/solutions_mapper.rb b/lib/goo/sparql/solutions_mapper.rb index e990beefc..585f3ca72 100644 --- a/lib/goo/sparql/solutions_mapper.rb +++ b/lib/goo/sparql/solutions_mapper.rb @@ -6,7 +6,7 @@ class SolutionMapper def initialize(aggregate_projections, bnode_extraction, embed_struct, incl_embed, klass_struct, models_by_id, - properties_to_include, unmapped, variables,ids, options) + properties_to_include, unmapped, variables, ids, options) @aggregate_projections = aggregate_projections @bnode_extraction = bnode_extraction @@ -72,11 +72,13 @@ def map_each_solutions(select) next end - object, objects_new = get_value_object(id, objects_new, object, list_attributes, v) - add_object_to_model(id, object, v, var_set_hash) + # if multiple language values are included for a given property, set the + # corresponding model attribute to the English language value - NCBO-1662 + language, object = get_object_language(id, object, predicate) + object, objects_new = get_value_object(id, objects_new, object, list_attributes, predicate) + add_object_to_model(id, object, predicate, language) end @lang_filter.fill_models_with_other_languages(@models_by_id, list_attributes) - init_unloaded_attributes(found, list_attributes) return @models_by_id if @bnode_extraction @@ -91,7 +93,6 @@ def map_each_solutions(select) #next level of embed attributes include_embed_attributes(@incl_embed, objects_new) if @incl_embed && !@incl_embed.empty? - #bnodes blank_nodes = objects_new.select { |id, obj| id.is_a?(RDF::Node) && id.anonymous? } include_bnodes(blank_nodes, @models_by_id) unless blank_nodes.empty? @@ -103,6 +104,10 @@ def map_each_solutions(select) private + def get_object_language(id, object, predicate) + @lang_filter.main_lang_filter id, predicate, object + end + def init_unloaded_attributes(found, list_attributes) return if @incl.nil? @@ -167,25 +172,20 @@ def get_value_object(id, objects_new, object, list_attributes, predicate) object.uniq! end end - [object,objects_new] + [object, objects_new] end - def add_object_to_model(id, object, predicate, var_set_hash) + def add_object_to_model(id, object, predicate, lang) if @models_by_id[id].respond_to?(:klass) @models_by_id[id][predicate] = object unless object.nil? && !@models_by_id[id][predicate].nil? elsif !@models_by_id[id].class.handler?(predicate) && - !(object.nil? && !@models_by_id[id].instance_variable_get("@#{predicate}").nil?) && - predicate != :id - # if multiple language values are included for a given property, set the - # corresponding model attribute to the English language value - NCBO-1662 - if object.is_a?(RDF::Literal) - key = "#{predicate}#__#{id}" - @models_by_id[id].send("#{predicate}=", object, on_load: true) unless var_set_hash[key] - lang = object.language - var_set_hash[key] = true if %i[EN en].include?(lang) - else + !(object.nil? && !@models_by_id[id].instance_variable_get("@#{predicate}").nil?) && + predicate != :id + + if (lang&.eql?(:no_lang)) || !lang @models_by_id[id].send("#{predicate}=", object, on_load: true) end + end end