datafoodconsortium-connector 1.0.0.pre.alpha.9 → 1.0.0.pre.alpha.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/lib/datafoodconsortium/connector/address.rb +6 -5
  3. data/lib/datafoodconsortium/connector/agent.rb +7 -6
  4. data/lib/datafoodconsortium/connector/allergen_characteristic.rb +3 -3
  5. data/lib/datafoodconsortium/connector/catalog.rb +4 -3
  6. data/lib/datafoodconsortium/connector/catalog_item.rb +7 -6
  7. data/lib/datafoodconsortium/connector/characteristic.rb +1 -3
  8. data/lib/datafoodconsortium/connector/connector.rb +10 -9
  9. data/lib/datafoodconsortium/connector/context.rb +72 -0
  10. data/lib/datafoodconsortium/connector/customer_category.rb +3 -2
  11. data/lib/datafoodconsortium/connector/defined_product.rb +18 -17
  12. data/lib/datafoodconsortium/connector/enterprise.rb +11 -11
  13. data/lib/datafoodconsortium/connector/importer.rb +112 -0
  14. data/lib/datafoodconsortium/connector/json_ld_serializer.rb +7 -14
  15. data/lib/datafoodconsortium/connector/nutrient_characteristic.rb +2 -2
  16. data/lib/datafoodconsortium/connector/offer.rb +6 -5
  17. data/lib/datafoodconsortium/connector/order.rb +7 -6
  18. data/lib/datafoodconsortium/connector/order_line.rb +7 -6
  19. data/lib/datafoodconsortium/connector/person.rb +5 -5
  20. data/lib/datafoodconsortium/connector/phone_number.rb +4 -3
  21. data/lib/datafoodconsortium/connector/physical_characteristic.rb +3 -3
  22. data/lib/datafoodconsortium/connector/price.rb +5 -4
  23. data/lib/datafoodconsortium/connector/quantitative_value.rb +4 -3
  24. data/lib/datafoodconsortium/connector/quantity.rb +4 -3
  25. data/lib/datafoodconsortium/connector/sale_session.rb +6 -5
  26. data/lib/datafoodconsortium/connector/skos_concept.rb +10 -7
  27. data/lib/datafoodconsortium/connector/skos_helper.rb +14 -0
  28. data/lib/datafoodconsortium/connector/skos_parser.rb +122 -99
  29. data/lib/datafoodconsortium/connector/skos_parser_element.rb +72 -53
  30. data/lib/datafoodconsortium/connector/social_media.rb +4 -3
  31. data/lib/datafoodconsortium/connector/supplied_product.rb +2 -2
  32. data/lib/datafoodconsortium/connector/technical_product.rb +2 -2
  33. data/lib/datafoodconsortium/connector.rb +19 -13
  34. metadata +6 -40
  35. data/lib/datafoodconsortium/connector/allergen_dimension.rb +0 -34
  36. data/lib/datafoodconsortium/connector/certification.rb +0 -34
  37. data/lib/datafoodconsortium/connector/characteristic_dimension.rb +0 -34
  38. data/lib/datafoodconsortium/connector/geographical_origin.rb +0 -34
  39. data/lib/datafoodconsortium/connector/nature_origin.rb +0 -34
  40. data/lib/datafoodconsortium/connector/part_origin.rb +0 -34
  41. data/lib/datafoodconsortium/connector/product_type.rb +0 -34
  42. data/lib/datafoodconsortium/connector/repository.rb +0 -33
  43. data/lib/datafoodconsortium/connector/unit.rb +0 -34
@@ -27,8 +27,9 @@
27
27
  require "virtual_assembly/semantizer"
28
28
 
29
29
  class DataFoodConsortium::Connector::OrderLine
30
+ include VirtualAssembly::Semantizer::SemanticObject
30
31
 
31
- include VirtualAssembly::Semantizer::SemanticObject
32
+ SEMANTIC_TYPE = "dfc-b:OrderLine".freeze
32
33
 
33
34
  # @return [String]
34
35
  attr_accessor :description
@@ -59,11 +60,11 @@ class DataFoodConsortium::Connector::OrderLine
59
60
  @offer = offer
60
61
  @order = order
61
62
  self.semanticType = "dfc-b:OrderLine"
62
- registerSemanticProperty("dfc-b:description") { self.description }
63
- registerSemanticProperty("dfc-b:quantity") { self.quantity }
64
- registerSemanticProperty("dfc-b:hasPrice") { self.price }
65
- registerSemanticProperty("dfc-b:concerns") { self.offer }
66
- registerSemanticProperty("dfc-b:partOf") { self.order }
63
+ registerSemanticProperty("dfc-b:description", &method("description")).valueSetter = method("description=")
64
+ registerSemanticProperty("dfc-b:quantity", &method("quantity")).valueSetter = method("quantity=")
65
+ registerSemanticProperty("dfc-b:hasPrice", &method("price")).valueSetter = method("price=")
66
+ registerSemanticProperty("dfc-b:concerns", &method("offer")).valueSetter = method("offer=")
67
+ registerSemanticProperty("dfc-b:partOf", &method("order")).valueSetter = method("order=")
67
68
  end
68
69
 
69
70
 
@@ -20,15 +20,15 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
+ require "datafoodconsortium/connector/agent"
23
24
 
24
25
 
25
26
 
26
- require "datafoodconsortium/connector/agent"
27
27
  require "virtual_assembly/semantizer"
28
28
 
29
29
  class DataFoodConsortium::Connector::Person < DataFoodConsortium::Connector::Agent
30
30
 
31
-
31
+ SEMANTIC_TYPE = "dfc-b:Person".freeze
32
32
 
33
33
  # @return [String]
34
34
  attr_accessor :firstName
@@ -54,9 +54,9 @@ class DataFoodConsortium::Connector::Person < DataFoodConsortium::Connector::Age
54
54
  @lastName = lastName
55
55
  @affiliatedOrganizations = affiliatedOrganizations
56
56
  self.semanticType = "dfc-b:Person"
57
- registerSemanticProperty("dfc-b:firstName") { self.firstName }
58
- registerSemanticProperty("dfc-b:familyName") { self.lastName }
59
- registerSemanticProperty("dfc-b:affiliates") { self.affiliatedOrganizations }
57
+ registerSemanticProperty("dfc-b:firstName", &method("firstName")).valueSetter = method("firstName=")
58
+ registerSemanticProperty("dfc-b:familyName", &method("lastName")).valueSetter = method("lastName=")
59
+ registerSemanticProperty("dfc-b:affiliates", &method("affiliatedOrganizations")).valueSetter = method("affiliatedOrganizations=")
60
60
  end
61
61
 
62
62
 
@@ -24,8 +24,9 @@
24
24
  require "virtual_assembly/semantizer"
25
25
 
26
26
  class DataFoodConsortium::Connector::PhoneNumber
27
+ include VirtualAssembly::Semantizer::SemanticObject
27
28
 
28
- include VirtualAssembly::Semantizer::SemanticObject
29
+ SEMANTIC_TYPE = "dfc-b:PhoneNumber".freeze
29
30
 
30
31
  # @return [Integer]
31
32
  attr_accessor :countryCode
@@ -41,8 +42,8 @@ class DataFoodConsortium::Connector::PhoneNumber
41
42
  @countryCode = countryCode
42
43
  @phoneNumber = phoneNumber
43
44
  self.semanticType = "dfc-b:PhoneNumber"
44
- registerSemanticProperty("dfc-b:countryCode") { self.countryCode }
45
- registerSemanticProperty("dfc-b:phoneNumber") { self.phoneNumber }
45
+ registerSemanticProperty("dfc-b:countryCode", &method("countryCode")).valueSetter = method("countryCode=")
46
+ registerSemanticProperty("dfc-b:phoneNumber", &method("phoneNumber")).valueSetter = method("phoneNumber=")
46
47
  end
47
48
 
48
49
 
@@ -20,14 +20,14 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
+ require "datafoodconsortium/connector/characteristic"
23
24
 
24
25
 
25
- require "datafoodconsortium/connector/characteristic"
26
26
  require "virtual_assembly/semantizer"
27
27
 
28
28
  class DataFoodConsortium::Connector::PhysicalCharacteristic < DataFoodConsortium::Connector::Characteristic
29
29
 
30
-
30
+ SEMANTIC_TYPE = "dfc-b:PhysicalCharacteristic".freeze
31
31
 
32
32
  # @return [ISKOSConcept]
33
33
  attr_accessor :physicalDimension
@@ -39,7 +39,7 @@ class DataFoodConsortium::Connector::PhysicalCharacteristic < DataFoodConsortium
39
39
  super(unit: unit, value: value)
40
40
  @physicalDimension = physicalDimension
41
41
  self.semanticType = "dfc-b:PhysicalCharacteristic"
42
- registerSemanticProperty("dfc-b:hasPhysicalDimension") { self.physicalDimension }
42
+ registerSemanticProperty("dfc-b:hasPhysicalDimension", &method("physicalDimension")).valueSetter = method("physicalDimension=")
43
43
  end
44
44
 
45
45
 
@@ -25,8 +25,9 @@
25
25
  require "virtual_assembly/semantizer"
26
26
 
27
27
  class DataFoodConsortium::Connector::Price
28
+ include VirtualAssembly::Semantizer::SemanticObject
28
29
 
29
- include VirtualAssembly::Semantizer::SemanticObject
30
+ SEMANTIC_TYPE = "dfc-b:Price".freeze
30
31
 
31
32
  # @return [Real]
32
33
  attr_accessor :value
@@ -46,9 +47,9 @@ class DataFoodConsortium::Connector::Price
46
47
  @vatRate = vatRate
47
48
  @unit = unit
48
49
  self.semanticType = "dfc-b:Price"
49
- registerSemanticProperty("dfc-b:value") { self.value }
50
- registerSemanticProperty("dfc-b:VATrate") { self.vatRate }
51
- registerSemanticProperty("dfc-b:hasUnit") { self.unit }
50
+ registerSemanticProperty("dfc-b:value", &method("value")).valueSetter = method("value=")
51
+ registerSemanticProperty("dfc-b:VATrate", &method("vatRate")).valueSetter = method("vatRate=")
52
+ registerSemanticProperty("dfc-b:hasUnit", &method("unit")).valueSetter = method("unit=")
52
53
  end
53
54
 
54
55
 
@@ -25,8 +25,9 @@
25
25
  require "virtual_assembly/semantizer"
26
26
 
27
27
  class DataFoodConsortium::Connector::QuantitativeValue
28
+ include VirtualAssembly::Semantizer::SemanticObject
28
29
 
29
- include VirtualAssembly::Semantizer::SemanticObject
30
+ SEMANTIC_TYPE = "dfc-b:QuantitativeValue".freeze
30
31
 
31
32
  # @return [ISKOSConcept]
32
33
  attr_accessor :unit
@@ -41,8 +42,8 @@ class DataFoodConsortium::Connector::QuantitativeValue
41
42
  @unit = unit
42
43
  @value = value
43
44
  self.semanticType = "dfc-b:QuantitativeValue"
44
- registerSemanticProperty("dfc-b:hasUnit") { self.unit }
45
- registerSemanticProperty("dfc-b:value") { self.value }
45
+ registerSemanticProperty("dfc-b:hasUnit", &method("unit")).valueSetter = method("unit=")
46
+ registerSemanticProperty("dfc-b:value", &method("value")).valueSetter = method("value=")
46
47
  end
47
48
 
48
49
 
@@ -25,8 +25,9 @@
25
25
  require "virtual_assembly/semantizer"
26
26
 
27
27
  class DataFoodConsortium::Connector::Quantity
28
+ include VirtualAssembly::Semantizer::SemanticObject
28
29
 
29
- include VirtualAssembly::Semantizer::SemanticObject
30
+ SEMANTIC_TYPE = "dfc-b:Quantity".freeze
30
31
 
31
32
  # @return [ISKOSConcept]
32
33
  attr_accessor :unit
@@ -41,8 +42,8 @@ class DataFoodConsortium::Connector::Quantity
41
42
  @unit = unit
42
43
  @value = value
43
44
  self.semanticType = "dfc-b:Quantity"
44
- registerSemanticProperty("dfc-b:hasUnit") { self.unit }
45
- registerSemanticProperty("dfc-b:value") { self.value }
45
+ registerSemanticProperty("dfc-b:hasUnit", &method("unit")).valueSetter = method("unit=")
46
+ registerSemanticProperty("dfc-b:value", &method("value")).valueSetter = method("value=")
46
47
  end
47
48
 
48
49
 
@@ -25,8 +25,9 @@
25
25
  require "virtual_assembly/semantizer"
26
26
 
27
27
  class DataFoodConsortium::Connector::SaleSession
28
+ include VirtualAssembly::Semantizer::SemanticObject
28
29
 
29
- include VirtualAssembly::Semantizer::SemanticObject
30
+ SEMANTIC_TYPE = "dfc-b:SaleSession".freeze
30
31
 
31
32
  # @return [String]
32
33
  attr_accessor :beginDate
@@ -52,10 +53,10 @@ class DataFoodConsortium::Connector::SaleSession
52
53
  @quantity = quantity
53
54
  @offers = offers
54
55
  self.semanticType = "dfc-b:SaleSession"
55
- registerSemanticProperty("dfc-b:beginDate") { self.beginDate }
56
- registerSemanticProperty("dfc-b:endDate") { self.endDate }
57
- registerSemanticProperty("dfc-b:quantity") { self.quantity }
58
- registerSemanticProperty("dfc-b:lists") { self.offers }
56
+ registerSemanticProperty("dfc-b:beginDate", &method("beginDate")).valueSetter = method("beginDate=")
57
+ registerSemanticProperty("dfc-b:endDate", &method("endDate")).valueSetter = method("endDate=")
58
+ registerSemanticProperty("dfc-b:quantity", &method("quantity")).valueSetter = method("quantity=")
59
+ registerSemanticProperty("dfc-b:lists", &method("offers")).valueSetter = method("offers=")
59
60
  end
60
61
 
61
62
 
@@ -24,10 +24,12 @@
24
24
 
25
25
 
26
26
  require "virtual_assembly/semantizer"
27
-
27
+ require 'datafoodconsortium/connector/skos_helper'
28
28
  class DataFoodConsortium::Connector::SKOSConcept
29
+ include VirtualAssembly::Semantizer::SemanticObject
30
+ include DataFoodConsortium::Connector::SKOSHelper
29
31
 
30
- include VirtualAssembly::Semantizer::SemanticObject
32
+ SEMANTIC_TYPE = "skos:Concept".freeze
31
33
 
32
34
  # @return [ISKOSConcept]
33
35
  attr_accessor :broaders
@@ -51,12 +53,13 @@ class DataFoodConsortium::Connector::SKOSConcept
51
53
  @broaders = broaders
52
54
  @schemes = schemes
53
55
  @narrowers = narrowers
54
- @prefLabels = prefLabels
56
+ # Sort locale alphabetically
57
+ @prefLabels = prefLabels.sort.to_h
55
58
  self.semanticType = "skos:Concept"
56
- registerSemanticProperty("skos:broader") { self.broaders }
57
- registerSemanticProperty("skos:inScheme") { self.schemes }
58
- registerSemanticProperty("skos:narrower") { self.narrowers }
59
- registerSemanticProperty("skos:prefLabel") { self.prefLabels }
59
+ registerSemanticProperty("skos:broader", &method("broaders")).valueSetter = method("broaders=")
60
+ registerSemanticProperty("skos:inScheme", &method("schemes")).valueSetter = method("schemes=")
61
+ registerSemanticProperty("skos:narrower", &method("narrowers")).valueSetter = method("narrowers=")
62
+ registerSemanticProperty("skos:prefLabel", &method("prefLabels")).valueSetter = method("prefLabels=")
60
63
  end
61
64
 
62
65
 
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DataFoodConsortium::Connector::SKOSHelper
4
+ def addAttribute(name, value)
5
+ self.instance_variable_set("@#{name}", value)
6
+ self.define_singleton_method(name) do
7
+ instance_variable_get("@#{name}")
8
+ end
9
+ end
10
+
11
+ def hasAttribute(name)
12
+ self.methods.include?(:"#{name}")
13
+ end
14
+ end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # MIT License
2
- #
4
+ #
3
5
  # Copyright (c) 2023 Maxime Lecoq <maxime@lecoqlibre.fr>
4
- #
6
+ #
5
7
  # Permission is hereby granted, free of charge, to any person obtaining a copy
6
8
  # of this software and associated documentation files (the "Software"), to deal
7
9
  # in the Software without restriction, including without limitation the rights
8
10
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
11
  # copies of the Software, and to permit persons to whom the Software is
10
12
  # furnished to do so, subject to the following conditions:
11
- #
13
+ #
12
14
  # The above copyright notice and this permission notice shall be included in all
13
15
  # copies or substantial portions of the Software.
14
- #
16
+ #
15
17
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
18
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
19
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -20,124 +22,145 @@
20
22
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
23
  # SOFTWARE.
22
24
 
25
+ require 'datafoodconsortium/connector/skos_helper'
23
26
  require 'datafoodconsortium/connector/skos_concept'
24
27
  require 'datafoodconsortium/connector/skos_parser_element'
25
28
 
26
29
  class DataFoodConsortium::Connector::SKOSInstance
30
+ include DataFoodConsortium::Connector::SKOSHelper
27
31
 
28
- def addAttribute(name, value)
29
- self.instance_variable_set("@#{name}", value)
30
- self.define_singleton_method(name) do
31
- return instance_variable_get("@#{name}")
32
- end
33
- end
34
-
35
- def hasAttribute(name)
36
- return self.methods.include?(:"#{name}")
37
- end
38
-
32
+ # Return a list of singelton methods, ie the list of Concept available
33
+ def topConcepts
34
+ self.methods(false).sort
35
+ end
39
36
  end
40
37
 
41
38
  class DataFoodConsortium::Connector::SKOSParser
39
+ CONCEPT_SCHEMES = ["Facet", "productTypes"].freeze
40
+
41
+ def self.concepts
42
+ @concepts ||= {}
43
+ end
44
+
45
+ def initialize
46
+ @results = DataFoodConsortium::Connector::SKOSInstance.new
47
+ @skosConcepts = {}
48
+ @rootElements = []
49
+ @broaders = {}
50
+ # Flag used to tell the parser to use SkosConcept object when parsing data from Concept Scheme
51
+ # defined in CONCEPT_SCHEMES
52
+ @useSkosConcept = false
53
+ end
54
+
55
+ def parse(data)
56
+ init
57
+
58
+ data.each do |element|
59
+ current = DataFoodConsortium::Connector::SKOSParserElement.new(element)
60
+
61
+ setSkosConceptFlag(current)
62
+
63
+ if current.isConcept? || current.isCollection?
64
+ if !@skosConcepts.has_key?(current.id)
65
+ concept = createSKOSConcept(current)
66
+ @skosConcepts[current.id] = concept
67
+ end
42
68
 
43
- def initialize()
44
- @results = DataFoodConsortium::Connector::SKOSInstance.new
45
- @skosConcepts = {}
46
- @rootElements = []
47
- @broaders = {}
48
- end
49
-
50
- def parse(data)
51
- init()
52
-
53
- data.each do |element|
54
- current = DataFoodConsortium::Connector::SKOSParserElement.new(element)
55
-
56
- if (current.isConcept?() || current.isCollection?())
57
- if (!@skosConcepts.has_key?(current.id))
58
- concept = createSKOSConcept(current)
59
- @skosConcepts[current.id] = concept
60
- end
61
-
62
- if (current.hasBroader())
63
- current.broader.each do |broaderId|
64
- if (!@broaders.has_key?(broaderId))
65
- @broaders[broaderId] = []
66
- end
67
-
68
- @broaders[broaderId].push(current.id)
69
- end
70
-
71
- # No broader, save the concept to the root
72
- else
73
- @rootElements.push(current.id)
74
- end
69
+ if current.hasBroader
70
+ current.broader.each do |broaderId|
71
+ if !@broaders.has_key?(broaderId)
72
+ @broaders[broaderId] = []
75
73
  end
76
- end
77
-
78
- @rootElements.each do |rootElementId|
79
- setResults(@results, rootElementId)
80
- end
81
74
 
82
- return @results
75
+ @broaders[broaderId].push(current.id)
76
+ end
77
+ # No broader, save the concept to the root
78
+ else
79
+ @rootElements.push(current.id)
80
+ end
81
+ end
83
82
  end
84
83
 
85
- protected
86
-
87
- def createSKOSConcept(element)
88
- skosConcept = DataFoodConsortium::Connector::SKOSConcept.new(element.id)
89
- skosConcept.semanticType = element.type
90
- return skosConcept
84
+ @rootElements.each do |rootElementId|
85
+ setResults(@results, rootElementId)
91
86
  end
92
87
 
93
- def getValueWithoutPrefix(property)
94
- name = 'undefined'
95
-
96
- if (!property.include?('http') && property.include?(':'))
97
- name = property.split(':')[1]
98
- elsif (property.include?('#'))
99
- name = property.split('#')[1]
100
- end
88
+ @results
89
+ end
101
90
 
102
- name = name.gsub('-', '_');
91
+ protected
103
92
 
104
- # workaround to fix concepts starting with a number
105
- # see https://github.com/datafoodconsortium/connector-ruby/issues/3
106
- # see https://github.com/datafoodconsortium/ontology/issues/66
107
- if (name.match?("^[0-9]"))
108
- name = "_" + name
109
- end
93
+ def createSKOSConcept(element)
94
+ skosConcept = DataFoodConsortium::Connector::SKOSConcept.new(
95
+ element.id, broaders: element.broader, narrowers: element.narrower, prefLabels: element.label
96
+ )
97
+ skosConcept.semanticType = element.type
98
+
99
+ self.class.concepts[element.id] = skosConcept
100
+
101
+ skosConcept
102
+ end
110
103
 
111
- return name.upcase
104
+ def getValueWithoutPrefix(property)
105
+ name = 'undefined'
106
+
107
+ if !property.include?('http') && property.include?(':')
108
+ name = property.split(':')[1]
109
+ elsif property.include?('#')
110
+ name = property.split('#')[1]
112
111
  end
113
112
 
114
- private
115
-
116
- def init()
117
- @results = DataFoodConsortium::Connector::SKOSInstance.new
118
- @skosConcepts = {}
119
- @rootElements = []
120
- @broaders = {}
113
+ name = name.gsub('-', '_')
114
+
115
+ # workaround to fix concepts starting with a number
116
+ # see https://github.com/datafoodconsortium/connector-ruby/issues/3
117
+ # see https://github.com/datafoodconsortium/ontology/issues/66
118
+ name = "_" + name if name.match?("^[0-9]")
119
+
120
+ name.upcase
121
+ end
122
+
123
+ private
124
+
125
+ def init
126
+ @results = DataFoodConsortium::Connector::SKOSInstance.new
127
+ @skosConcepts = {}
128
+ @rootElements = []
129
+ @broaders = {}
130
+ @useSkosConcept = false
131
+ end
132
+
133
+ def setResults(parent, id)
134
+ name = getValueWithoutPrefix(id)
135
+
136
+ if !parent.hasAttribute(name)
137
+ if @useSkosConcept && @skosConcepts[id]
138
+ parent.addAttribute(name, @skosConcepts[id])
139
+ else
140
+ parent.addAttribute(name, DataFoodConsortium::Connector::SKOSInstance.new)
141
+ end
121
142
  end
122
143
 
123
- def setResults(parent, id)
124
- name = getValueWithoutPrefix(id)
144
+ # Leaf concepts, stop the process
145
+ if !@broaders.has_key?(id)
146
+ parent.instance_variable_set("@#{name}", @skosConcepts[id])
147
+ return
148
+ end
125
149
 
126
- if (!parent.hasAttribute(name))
127
- parent.addAttribute(name, DataFoodConsortium::Connector::SKOSInstance.new)
128
- end
129
-
130
- # Leaf concepts, stop the process
131
- if (!@broaders.has_key?(id))
132
- parent.instance_variable_set("@#{name}", @skosConcepts[id])
133
- return
134
- end
150
+ @broaders[id].each do |narrower|
151
+ parentSkosInstance = parent.instance_variable_get("@#{name}")
135
152
 
136
- @broaders[id].each do |narrower|
137
- childName = getValueWithoutPrefix(narrower)
138
- parentSkosInstance = parent.instance_variable_get("@#{name}")
139
- setResults(parentSkosInstance, narrower) # recursive call
140
- end
153
+ setResults(parentSkosInstance, narrower) # recursive call
141
154
  end
155
+ end
142
156
 
143
- end
157
+ def setSkosConceptFlag(current)
158
+ @useSkosConcept = true if current.isConceptScheme? && matchingConceptSchemes(current)
159
+ end
160
+
161
+ def matchingConceptSchemes(current)
162
+ regex = /#{CONCEPT_SCHEMES.join("|")}/
163
+
164
+ current.id =~ regex
165
+ end
166
+ end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # MIT License
2
- #
4
+ #
3
5
  # Copyright (c) 2023 Maxime Lecoq <maxime@lecoqlibre.fr>
4
- #
6
+ #
5
7
  # Permission is hereby granted, free of charge, to any person obtaining a copy
6
8
  # of this software and associated documentation files (the "Software"), to deal
7
9
  # in the Software without restriction, including without limitation the rights
8
10
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
11
  # copies of the Software, and to permit persons to whom the Software is
10
12
  # furnished to do so, subject to the following conditions:
11
- #
13
+ #
12
14
  # The above copyright notice and this permission notice shall be included in all
13
15
  # copies or substantial portions of the Software.
14
- #
16
+ #
15
17
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
18
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
19
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -22,57 +24,74 @@
22
24
 
23
25
  class DataFoodConsortium::Connector::SKOSParserElement
24
26
 
25
- attr_reader :id
26
- attr_reader :type
27
- attr_reader :broader
28
-
29
- def initialize(element)
30
- @broader = []
31
-
32
- if (element)
33
- @id = element["@id"]
34
-
35
- if (element["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"])
36
- @type = extractId(element["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"])
37
- elsif (element["@type"])
38
- @type = extractId(element["@type"])
39
- else
40
- @type = "undefined"
41
- end
42
-
43
- if (element["http://www.w3.org/2004/02/skos/core#broader"])
44
- element["http://www.w3.org/2004/02/skos/core#broader"].each do |broader|
45
- @broader.push(broader["@id"])
46
- end
47
- end
48
- else
49
- @id = ""
50
- @type = ""
27
+ attr_reader :id
28
+ attr_reader :type
29
+ attr_reader :broader
30
+ attr_reader :narrower
31
+ attr_reader :label
32
+
33
+ def initialize(element)
34
+ @broader = []
35
+ @narrower = []
36
+ @label = {}
37
+
38
+ if element
39
+ @id = element["@id"]
40
+
41
+ if element["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"]
42
+ @type = extractId(element["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"])
43
+ elsif element["@type"]
44
+ @type = extractId(element["@type"])
45
+ else
46
+ @type = "undefined"
47
+ end
48
+
49
+ if element["http://www.w3.org/2004/02/skos/core#broader"]
50
+ element["http://www.w3.org/2004/02/skos/core#broader"].each do |broader|
51
+ @broader.push(broader["@id"])
51
52
  end
52
- end
53
-
54
- def hasBroader()
55
- return @broader.count > 0
56
- end
57
-
58
- def isConcept?()
59
- return @type == "http://www.w3.org/2004/02/skos/core#Concept" || @type == "skos:Concept"
60
- end
61
-
62
- def isCollection?()
63
- return @type == "http://www.w3.org/2004/02/skos/core#Collection" || @type == "skos:Collection"
64
- end
65
-
66
- private
67
-
68
- def extractId(data)
69
- id = data[0]
70
-
71
- if (id["@id"])
72
- return id["@id"]
53
+ end
54
+
55
+ if element["http://www.w3.org/2004/02/skos/core#narrower"]
56
+ element["http://www.w3.org/2004/02/skos/core#narrower"].each do |narrower|
57
+ @narrower.push(narrower["@id"])
73
58
  end
59
+ end
74
60
 
75
- return id
61
+ if element["http://www.w3.org/2004/02/skos/core#prefLabel"]
62
+ element["http://www.w3.org/2004/02/skos/core#prefLabel"].each do |label|
63
+ @label[label["@language"].to_sym] = label["@value"]
64
+ end
65
+ end
66
+ else
67
+ @id = ""
68
+ @type = ""
76
69
  end
70
+ end
71
+
72
+ def hasBroader()
73
+ @broader.count > 0
74
+ end
75
+
76
+ def isConcept?()
77
+ @type == "http://www.w3.org/2004/02/skos/core#Concept" || @type == "skos:Concept"
78
+ end
79
+
80
+ def isCollection?()
81
+ @type == "http://www.w3.org/2004/02/skos/core#Collection" || @type == "skos:Collection"
82
+ end
83
+
84
+ def isConceptScheme?
85
+ @type == "http://www.w3.org/2004/02/skos/core#ConceptScheme"
86
+ end
87
+
88
+ private
89
+
90
+ def extractId(data)
91
+ id = data[0]
92
+
93
+ return id["@id"] if id["@id"]
77
94
 
78
- end
95
+ id
96
+ end
97
+ end