Skip to content

Commit

Permalink
Merge to master: Release 2.3.3 - Multilingual (#44)
Browse files Browse the repository at this point in the history
* Merge pull request #24 from ontoportal-lirmm/feature/support-multilingual-read-one-language-from-request-parameter

Feature : Support multilingual - Phase 1 - Read one language

* Merge branch pull request #32 from feature/language-return-all

* group unmapped properties by lang

* Feature: group unmapped properties by language  (#38)

* group unmapped properties by lang

* downcase language keys of unmapped properties

---------

Co-authored-by: Syphax bouazzouni <[email protected]>

* Feature: Support multi lingual -  add show_language argument to the attributes getters (#39)

* update define_method

* update solution mapper

* update get_preload_value

* Merge pull request #40 from ontoportal-lirmm/support-muli-lang-part-02

Fix: save_model_values if unmmaped condition

* Merge pull request #41 from ontoportal-lirmm/support-muli-lang-part-02

Feature: Refactor and fix the Language filter method

* Merge pull request #42 from ontoportal-lirmm/feature/support-multi-langual-search

Feature: Make map_attribute support showing all languages - Support multi language search

---------

Co-authored-by: HADDAD Zineddine <[email protected]>
  • Loading branch information
syphax-bouazzouni and haddadzineddine authored Dec 4, 2023
1 parent bd71542 commit 04419a8
Show file tree
Hide file tree
Showing 8 changed files with 695 additions and 596 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem "cube-ruby", require: "cube"
gem "faraday", '~> 1.9'
gem "rake"
gem "uuid"
gem "request_store"

group :test do
gem "minitest", '< 5.0'
Expand Down
15 changes: 9 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ GEM
public_suffix (>= 2.0.2, < 6.0)
builder (3.2.4)
coderay (1.1.3)
concurrent-ruby (1.1.10)
concurrent-ruby (1.2.2)
connection_pool (2.3.0)
cube-ruby (0.0.3)
daemons (1.4.1)
Expand Down Expand Up @@ -76,10 +76,10 @@ GEM
method_source (1.0.0)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2022.0105)
mime-types-data (3.2023.0218.1)
minitest (4.7.5)
multi_json (1.15.0)
multipart-post (2.2.3)
multipart-post (2.3.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
net-http-persistent (2.9.4)
Expand All @@ -88,7 +88,7 @@ GEM
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (5.0.1)
rack (2.2.6.2)
rack (2.2.6.3)
rack-accept (0.4.5)
rack (>= 0.4)
rack-post-body-to-params (0.1.8)
Expand All @@ -100,8 +100,10 @@ GEM
addressable (>= 2.2)
redis (5.0.6)
redis-client (>= 0.9.0)
redis-client (0.12.1)
redis-client (0.13.0)
connection_pool
request_store (1.5.1)
rack (>= 1.4)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
Expand Down Expand Up @@ -132,7 +134,7 @@ GEM
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
thread_safe (0.3.6)
tilt (2.0.11)
tilt (2.1.0)
tzinfo (0.3.61)
unf (0.1.4)
unf_ext
Expand All @@ -155,6 +157,7 @@ DEPENDENCIES
rack-accept
rack-post-body-to-params
rake
request_store
simplecov
simplecov-cobertura
sinatra
Expand Down
9 changes: 9 additions & 0 deletions lib/goo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Goo
# Define the languages from which the properties values will be taken
# It choose the first language that match otherwise return all the values
@@main_languages = %w[en]
@@requested_language = nil

@@configure_flag = false
@@sparql_backends = {}
Expand All @@ -54,6 +55,14 @@ def self.main_languages=(lang)
@@main_languages = lang
end

def self.requested_language
@@requested_language
end

def self.requested_language=(lang)
@@requested_language = lang
end

def self.language_includes(lang)
lang_str = lang.to_s
main_languages.index { |l| lang_str.downcase.eql?(l) || lang_str.upcase.eql?(l)}
Expand Down
50 changes: 22 additions & 28 deletions lib/goo/base/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Resource
attr_reader :modified_attributes
attr_reader :errors
attr_reader :aggregates
attr_reader :unmapped
attr_writer :unmapped

attr_reader :id

Expand Down Expand Up @@ -134,17 +134,29 @@ def missing_load_attributes

def unmapped_set(attribute,value)
@unmapped ||= {}
(@unmapped[attribute] ||= Set.new) << value
@unmapped[attribute] ||= Set.new
@unmapped[attribute].merge(Array(value)) unless value.nil?
end

def unmapped_get(attribute)
@unmapped[attribute]
end

def unmmaped_to_array
cpy = {}

@unmapped.each do |attr,v|
cpy[attr] = v.to_a
end
@unmapped = cpy
end

def unmapped(*args)
@unmapped.transform_values do |language_values|
self.class.not_show_all_languages?(language_values, args) ? language_values.values.flatten: language_values
end
end

def delete(*args)
if self.kind_of?(Goo::Base::Enum)
raise ArgumentError, "Enums cannot be deleted" unless args[0] && args[0][:init_enum]
Expand Down Expand Up @@ -341,13 +353,13 @@ def self.range_object(attr,id)



def self.map_attributes(inst,equivalent_predicates=nil)
def self.map_attributes(inst,equivalent_predicates=nil, include_languages: false)
if (inst.kind_of?(Goo::Base::Resource) && inst.unmapped.nil?) ||
(!inst.respond_to?(:unmapped) && inst[:unmapped].nil?)
raise ArgumentError, "Resource.map_attributes only works for :unmapped instances"
end
klass = inst.respond_to?(:klass) ? inst[:klass] : inst.class
unmapped = inst.respond_to?(:klass) ? inst[:unmapped] : inst.unmapped
unmapped = inst.respond_to?(:klass) ? inst[:unmapped] : inst.unmapped(include_languages: include_languages)
list_attrs = klass.attributes(:list)
unmapped_string_keys = Hash.new
unmapped.each do |k,v|
Expand Down Expand Up @@ -378,31 +390,18 @@ def self.map_attributes(inst,equivalent_predicates=nil)
object = unmapped_string_keys[attr_uri]
end

lang_filter = Goo::SPARQL::Solution::LanguageFilter.new

object = object.map do |o|
if o.is_a?(RDF::URI)
o
else
literal = o
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

object = object.compact

other_languages_values = lang_filter.other_languages_values
other_languages_values = other_languages_values[inst.id.to_s][attr] unless other_languages_values.empty?
unless other_languages_values.nil?
object = lang_filter.languages_values_to_set(other_languages_values, object)
if object.is_a?(Hash)
object = object.transform_values{|values| Array(values).map{|o|o.is_a?(RDF::URI) ? o : o.object}}
else
object = object.map {|o| o.is_a?(RDF::URI) ? o : o.object}
end

if klass.range(attr)
object = object.map { |o|
o.is_a?(RDF::URI) ? klass.range_object(attr,o) : o }
end
object = object.first unless list_attrs.include?(attr)

object = object.first unless list_attrs.include?(attr) || include_languages
if inst.respond_to?(:klass)
inst[attr] = object
else
Expand All @@ -411,11 +410,6 @@ def self.map_attributes(inst,equivalent_predicates=nil)
else
inst.send("#{attr}=",
list_attrs.include?(attr) ? [] : nil, on_load: true)
if inst.id.to_s == "http://purl.obolibrary.org/obo/IAO_0000415"
if attr == :definition
# binding.pry
end
end
end

end
Expand Down
35 changes: 33 additions & 2 deletions lib/goo/base/settings/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,18 @@ def shape_attribute(attr)
self.instance_variable_set("@#{attr}",value)
end
define_method("#{attr}") do |*args|
attr_value = self.instance_variable_get("@#{attr}")

if self.class.not_show_all_languages?(attr_value, args)
is_array = attr_value.values.first.is_a?(Array)
attr_value = attr_value.values.flatten
attr_value = attr_value.first unless is_array
end


if self.class.handler?(attr)
if @loaded_attributes.include?(attr)
return self.instance_variable_get("@#{attr}")
return attr_value
end
value = self.send("#{self.class.handler(attr)}")
self.instance_variable_set("@#{attr}",value)
Expand All @@ -266,7 +275,7 @@ def shape_attribute(attr)
end

if (not @persistent) or @loaded_attributes.include?(attr)
return self.instance_variable_get("@#{attr}")
return attr_value
else
# TODO: bug here when no labels from one of the main_lang available... (when it is called by ontologies_linked_data ontologies_submission)
raise Goo::Base::AttributeNotLoaded, "Attribute `#{attr}` is not loaded for #{self.id}. Loaded attributes: #{@loaded_attributes.inspect}."
Expand Down Expand Up @@ -372,6 +381,28 @@ def read_only(attributes)
instance
end

def show_all_languages?(args)
args.first.is_a?(Hash) && args.first.keys.include?(:include_languages) && args.first[:include_languages]
end

def not_show_all_languages?(values, args)
values.is_a?(Hash) && !show_all_languages?(args)
end

private

def set_no_list_by_default(options)
if options[:enforce].nil? or !options[:enforce].include?(:list)
options[:enforce] = options[:enforce] ? (options[:enforce] << :no_list) : [:no_list]
end
end
def set_data_type(options)
if options[:type]
options[:enforce] += Array(options[:type])
options[:enforce].uniq!
options.delete :type
end
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions lib/goo/sparql/loader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'request_store'
module Goo
module SPARQL
module Loader
Expand All @@ -6,8 +7,10 @@ class << self

def model_load(*options)
options = options.last
set_request_lang(options)
if options[:models] && options[:models].is_a?(Array) && \
(options[:models].length > Goo.slice_loading_size)

options = options.dup
models = options[:models]
include_options = options[:include]
Expand Down Expand Up @@ -96,6 +99,9 @@ def model_load_sliced(*options)

private

def set_request_lang(options)
options[:requested_lang] = RequestStore.store[:requested_lang]
end
def expand_equivalent_predicates(properties_to_include, eq_p)

return unless eq_p && !eq_p.empty?
Expand Down
Loading

0 comments on commit 04419a8

Please sign in to comment.