Skip to content

Commit

Permalink
re-implement the lang filter for the query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Jul 20, 2022
1 parent 73ec8f3 commit 835aefa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/goo/base/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 24 additions & 14 deletions lib/goo/sparql/mixins/solution_lang_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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]
Expand Down
34 changes: 17 additions & 17 deletions lib/goo/sparql/solutions_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -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?

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 835aefa

Please sign in to comment.