datafoodconsortium-connector 1.0.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +7 -0
  2. data/lib/datafoodconsortium/connector/address.rb +51 -0
  3. data/lib/datafoodconsortium/connector/agent.rb +62 -0
  4. data/lib/datafoodconsortium/connector/allergen_characteristic.rb +49 -0
  5. data/lib/datafoodconsortium/connector/allergen_dimension.rb +34 -0
  6. data/lib/datafoodconsortium/connector/catalog_item.rb +62 -0
  7. data/lib/datafoodconsortium/connector/certification.rb +34 -0
  8. data/lib/datafoodconsortium/connector/characteristic.rb +47 -0
  9. data/lib/datafoodconsortium/connector/characteristic_dimension.rb +34 -0
  10. data/lib/datafoodconsortium/connector/connector.rb +73 -0
  11. data/lib/datafoodconsortium/connector/customer_category.rb +45 -0
  12. data/lib/datafoodconsortium/connector/defined_product.rb +157 -0
  13. data/lib/datafoodconsortium/connector/enterprise.rb +86 -0
  14. data/lib/datafoodconsortium/connector/geographical_origin.rb +34 -0
  15. data/lib/datafoodconsortium/connector/json_ld_serializer.rb +39 -0
  16. data/lib/datafoodconsortium/connector/nature_origin.rb +34 -0
  17. data/lib/datafoodconsortium/connector/nutrient_characteristic.rb +49 -0
  18. data/lib/datafoodconsortium/connector/offer.rb +56 -0
  19. data/lib/datafoodconsortium/connector/order.rb +33 -0
  20. data/lib/datafoodconsortium/connector/order_line.rb +33 -0
  21. data/lib/datafoodconsortium/connector/part_origin.rb +34 -0
  22. data/lib/datafoodconsortium/connector/person.rb +57 -0
  23. data/lib/datafoodconsortium/connector/physical_characteristic.rb +49 -0
  24. data/lib/datafoodconsortium/connector/product_type.rb +34 -0
  25. data/lib/datafoodconsortium/connector/quantitative_value.rb +45 -0
  26. data/lib/datafoodconsortium/connector/repository.rb +33 -0
  27. data/lib/datafoodconsortium/connector/skos_concept.rb +85 -0
  28. data/lib/datafoodconsortium/connector/skos_parser.rb +133 -0
  29. data/lib/datafoodconsortium/connector/skos_parser_element.rb +78 -0
  30. data/lib/datafoodconsortium/connector/supplied_product.rb +41 -0
  31. data/lib/datafoodconsortium/connector/unit.rb +34 -0
  32. data/lib/datafoodconsortium/connector.rb +34 -0
  33. metadata +73 -0
@@ -0,0 +1,85 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2023 Maxime Lecoq <maxime@lecoqlibre.fr>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+
24
+
25
+
26
+ require "virtual_assembly/semantizer"
27
+
28
+ class DataFoodConsortium::Connector::SKOSConcept
29
+
30
+ include VirtualAssembly::Semantizer::SemanticObject
31
+
32
+ attr_accessor :broaders
33
+ attr_accessor :schemes
34
+ attr_accessor :narrowers
35
+ attr_accessor :prefLabels
36
+
37
+ def initialize()
38
+ super()
39
+ self.semanticType = "http://www.w3.org/2004/02/skos/core#Concept"
40
+
41
+ self.broaders = []
42
+ self.schemes = []
43
+ self.narrowers = []
44
+ self.prefLabels = []
45
+ registerSemanticProperty("http://www.w3.org/2004/02/skos/core#broader") { self.broaders }
46
+ registerSemanticProperty("http://www.w3.org/2004/02/skos/core#inScheme") { self.schemes }
47
+ registerSemanticProperty("http://www.w3.org/2004/02/skos/core#narrower") { self.narrowers }
48
+ registerSemanticProperty("http://www.w3.org/2004/02/skos/core#prefLabel") { self.prefLabels }
49
+ end
50
+
51
+
52
+ def addBroader(broader)
53
+ self.broaders.push(broader)
54
+ end
55
+
56
+ def addScheme(scheme)
57
+ self.schemes.push(scheme)
58
+ end
59
+
60
+ def addNarrower(narrower)
61
+ self.narrowers.push(narrower)
62
+ end
63
+
64
+ def addPrefLabel(prefLabel)
65
+ self.prefLabels.push(prefLabel)
66
+ end
67
+
68
+ def removeBroader(broader)
69
+ raise "Not implemented"
70
+ end
71
+
72
+ def removeScheme(scheme)
73
+ raise "Not implemented"
74
+ end
75
+
76
+ def removeNarrower(narrower)
77
+ raise "Not implemented"
78
+ end
79
+
80
+ def removePrefLabel(prefLabel)
81
+ raise "Not implemented"
82
+ end
83
+
84
+
85
+ end
@@ -0,0 +1,133 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2023 Maxime Lecoq <maxime@lecoqlibre.fr>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'datafoodconsortium/connector/skos_concept'
24
+ require 'datafoodconsortium/connector/skos_parser_element'
25
+
26
+ class DataFoodConsortium::Connector::SKOSInstance
27
+
28
+ def addAttribute(name, value)
29
+ instance_variable_set("@#{name}", value)
30
+ self.class.send(:attr_reader, name)
31
+ end
32
+
33
+ def hasAttribute(name)
34
+ return self.methods.include?(:"#{name}")
35
+ end
36
+
37
+ end
38
+
39
+ class DataFoodConsortium::Connector::SKOSParser
40
+
41
+ def initialize()
42
+ @results = DataFoodConsortium::Connector::SKOSInstance.new
43
+ @skosConcepts = {}
44
+ @rootElements = []
45
+ @broaders = {}
46
+ end
47
+
48
+ def parse(data)
49
+ init()
50
+
51
+ data.each do |element|
52
+ current = DataFoodConsortium::Connector::SKOSParserElement.new(element)
53
+
54
+ if (current.isConcept?() || current.isCollection?())
55
+ if (!@skosConcepts.has_key?(current.id))
56
+ concept = createSKOSConcept(current)
57
+ @skosConcepts[current.id] = concept
58
+ end
59
+
60
+ if (current.hasBroader())
61
+ current.broader.each do |broaderId|
62
+ if (!@broaders.has_key?(broaderId))
63
+ @broaders[broaderId] = []
64
+ end
65
+
66
+ @broaders[broaderId].push(current.id)
67
+ end
68
+
69
+ # No broader, save the concept to the root
70
+ else
71
+ @rootElements.push(current.id)
72
+ end
73
+ end
74
+ end
75
+
76
+ @rootElements.each do |rootElementId|
77
+ setResults(@results, rootElementId)
78
+ end
79
+
80
+ return @results
81
+ end
82
+
83
+ protected
84
+
85
+ def createSKOSConcept(element)
86
+ skosConcept = DataFoodConsortium::Connector::SKOSConcept.new
87
+ skosConcept.semanticId = element.id
88
+ skosConcept.semanticType = element.type
89
+ return skosConcept
90
+ end
91
+
92
+ def getValueWithoutPrefix(property)
93
+ name = 'undefined'
94
+
95
+ if (!property.include?('http') && property.include?(':'))
96
+ name = property.split(':')[1]
97
+ elsif (property.include?('#'))
98
+ name = property.split('#')[1]
99
+ end
100
+
101
+ return name.gsub('-', '_').upcase
102
+ end
103
+
104
+ private
105
+
106
+ def init()
107
+ @results = DataFoodConsortium::Connector::SKOSInstance.new
108
+ @skosConcepts = {}
109
+ @rootElements = []
110
+ @broaders = {}
111
+ end
112
+
113
+ def setResults(parent, id)
114
+ name = getValueWithoutPrefix(id)
115
+
116
+ if (!parent.hasAttribute(name))
117
+ parent.addAttribute(name, DataFoodConsortium::Connector::SKOSInstance.new)
118
+ end
119
+
120
+ # Leaf concepts, stop the process
121
+ if (!@broaders.has_key?(id))
122
+ parent.instance_variable_set("@#{name}", @skosConcepts[id])
123
+ return
124
+ end
125
+
126
+ @broaders[id].each do |narrower|
127
+ childName = getValueWithoutPrefix(narrower)
128
+ parentSkosInstance = parent.instance_variable_get("@#{name}")
129
+ setResults(parentSkosInstance, narrower) # recursive call
130
+ end
131
+ end
132
+
133
+ end
@@ -0,0 +1,78 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2023 Maxime Lecoq <maxime@lecoqlibre.fr>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ class DataFoodConsortium::Connector::SKOSParserElement
24
+
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 = ""
51
+ 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"]
73
+ end
74
+
75
+ return id
76
+ end
77
+
78
+ end
@@ -0,0 +1,41 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2023 Maxime Lecoq <maxime@lecoqlibre.fr>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require "datafoodconsortium/connector/defined_product"
24
+ require "virtual_assembly/semantizer"
25
+
26
+ class DataFoodConsortium::Connector::SuppliedProduct < DataFoodConsortium::Connector::DefinedProduct
27
+
28
+
29
+
30
+
31
+ def initialize(name, description)
32
+ super(name, description)
33
+ self.semanticType = "http://static.datafoodconsortium.org/ontologies/DFC_BusinessOntology.owl#SuppliedProduct"
34
+
35
+
36
+
37
+ end
38
+
39
+
40
+
41
+ end
@@ -0,0 +1,34 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2023 Maxime Lecoq <maxime@lecoqlibre.fr>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require "datafoodconsortium/connector/skos_concept"
24
+
25
+ require "virtual_assembly/semantizer"
26
+
27
+ class DataFoodConsortium::Connector::Unit < DataFoodConsortium::Connector::SKOSConcept
28
+
29
+
30
+
31
+
32
+
33
+
34
+ end
@@ -0,0 +1,34 @@
1
+ module DataFoodConsortium
2
+ module Connector
3
+ require 'datafoodconsortium/connector/connector'
4
+ require 'datafoodconsortium/connector/json_ld_serializer'
5
+ require 'datafoodconsortium/connector/skos_parser_element'
6
+ require 'datafoodconsortium/connector/skos_parser'
7
+ require 'datafoodconsortium/connector/agent.rb'
8
+ require 'datafoodconsortium/connector/enterprise.rb'
9
+ require 'datafoodconsortium/connector/customer_category.rb'
10
+ require 'datafoodconsortium/connector/person.rb'
11
+ require 'datafoodconsortium/connector/repository.rb'
12
+ require 'datafoodconsortium/connector/catalog_item.rb'
13
+ require 'datafoodconsortium/connector/order.rb'
14
+ require 'datafoodconsortium/connector/offer.rb'
15
+ require 'datafoodconsortium/connector/order_line.rb'
16
+ require 'datafoodconsortium/connector/address.rb'
17
+ require 'datafoodconsortium/connector/geographical_origin.rb'
18
+ require 'datafoodconsortium/connector/physical_characteristic.rb'
19
+ require 'datafoodconsortium/connector/allergen_dimension.rb'
20
+ require 'datafoodconsortium/connector/allergen_characteristic.rb'
21
+ require 'datafoodconsortium/connector/nutrient_characteristic.rb'
22
+ require 'datafoodconsortium/connector/characteristic.rb'
23
+ require 'datafoodconsortium/connector/product_type.rb'
24
+ require 'datafoodconsortium/connector/part_origin.rb'
25
+ require 'datafoodconsortium/connector/quantitative_value.rb'
26
+ require 'datafoodconsortium/connector/unit.rb'
27
+ require 'datafoodconsortium/connector/certification.rb'
28
+ require 'datafoodconsortium/connector/characteristic_dimension.rb'
29
+ require 'datafoodconsortium/connector/nature_origin.rb'
30
+ require 'datafoodconsortium/connector/defined_product.rb'
31
+ require 'datafoodconsortium/connector/supplied_product.rb'
32
+ require 'datafoodconsortium/connector/skos_concept.rb'
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datafoodconsortium-connector
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Maxime Lecoq
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A library to easily integrate the DFC standard within your application.
14
+ email: maxime@lecoqlibre.fr
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/datafoodconsortium/connector.rb
20
+ - lib/datafoodconsortium/connector/address.rb
21
+ - lib/datafoodconsortium/connector/agent.rb
22
+ - lib/datafoodconsortium/connector/allergen_characteristic.rb
23
+ - lib/datafoodconsortium/connector/allergen_dimension.rb
24
+ - lib/datafoodconsortium/connector/catalog_item.rb
25
+ - lib/datafoodconsortium/connector/certification.rb
26
+ - lib/datafoodconsortium/connector/characteristic.rb
27
+ - lib/datafoodconsortium/connector/characteristic_dimension.rb
28
+ - lib/datafoodconsortium/connector/connector.rb
29
+ - lib/datafoodconsortium/connector/customer_category.rb
30
+ - lib/datafoodconsortium/connector/defined_product.rb
31
+ - lib/datafoodconsortium/connector/enterprise.rb
32
+ - lib/datafoodconsortium/connector/geographical_origin.rb
33
+ - lib/datafoodconsortium/connector/json_ld_serializer.rb
34
+ - lib/datafoodconsortium/connector/nature_origin.rb
35
+ - lib/datafoodconsortium/connector/nutrient_characteristic.rb
36
+ - lib/datafoodconsortium/connector/offer.rb
37
+ - lib/datafoodconsortium/connector/order.rb
38
+ - lib/datafoodconsortium/connector/order_line.rb
39
+ - lib/datafoodconsortium/connector/part_origin.rb
40
+ - lib/datafoodconsortium/connector/person.rb
41
+ - lib/datafoodconsortium/connector/physical_characteristic.rb
42
+ - lib/datafoodconsortium/connector/product_type.rb
43
+ - lib/datafoodconsortium/connector/quantitative_value.rb
44
+ - lib/datafoodconsortium/connector/repository.rb
45
+ - lib/datafoodconsortium/connector/skos_concept.rb
46
+ - lib/datafoodconsortium/connector/skos_parser.rb
47
+ - lib/datafoodconsortium/connector/skos_parser_element.rb
48
+ - lib/datafoodconsortium/connector/supplied_product.rb
49
+ - lib/datafoodconsortium/connector/unit.rb
50
+ homepage: https://github.com/datafoodconsortium/connector-ruby/
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">"
66
+ - !ruby/object:Gem::Version
67
+ version: 1.3.1
68
+ requirements: []
69
+ rubygems_version: 3.3.25
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Data Food Consortium connector
73
+ test_files: []