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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5225fd19ef0b6bf69a76d49a056356e3b597fc0a1218f49987cb7d067fa1ffc
4
- data.tar.gz: 399517064eca65753f2d915f7807c105f87e08eb68bf00b1d5b4c18bc4fc16d4
3
+ metadata.gz: b7948a960df0e1c5980954d67ab8b386029789fe867d06bcef4b1ff8944bacec
4
+ data.tar.gz: 8ad3b6dd4c57b621e4a712424b8fcfa7a8b1c0efec75cde535554617ad72e6d6
5
5
  SHA512:
6
- metadata.gz: a6164d4c281396c07d80f69011fbf391ff92a5eac637d8028f95bee0efba3f41856bc8bd2a174775cacd3f9630e58ff4d9bbb8f98d932a3ff6cb62b26b4331b9
7
- data.tar.gz: d94f2752a2c5eac1d675c1bce1248dea25f2491f96ce17a2535592af8c725af915ac7bd2b97e7261259367cd167611737ad96bb7cb5cb99434aa65df70bae263
6
+ metadata.gz: 45c695953704de93af40ed04d87e73abc0084d93cce29c28fca0925ec5d61d69b0db467370ad6a7b4e5ae6c93897b16a97d67b6ba8f82940241d9edda285f473
7
+ data.tar.gz: 125396ea6bbc7ed9f9313f14e95f6a8568896c0e7f219a268a3f5419e24a8bae292f470fc7f6abb58b977c5129e0953b06398c7eea657bcd5fe0faa137bdc0a3
@@ -24,8 +24,9 @@
24
24
  require "virtual_assembly/semantizer"
25
25
 
26
26
  class DataFoodConsortium::Connector::Address
27
+ include VirtualAssembly::Semantizer::SemanticObject
27
28
 
28
- include VirtualAssembly::Semantizer::SemanticObject
29
+ SEMANTIC_TYPE = "dfc-b:Address".freeze
29
30
 
30
31
  # @return [String]
31
32
  attr_accessor :street
@@ -51,10 +52,10 @@ class DataFoodConsortium::Connector::Address
51
52
  @city = city
52
53
  @country = country
53
54
  self.semanticType = "dfc-b:Address"
54
- registerSemanticProperty("dfc-b:hasStreet") { self.street }
55
- registerSemanticProperty("dfc-b:hasPostalCode") { self.postalCode }
56
- registerSemanticProperty("dfc-b:hasCity") { self.city }
57
- registerSemanticProperty("dfc-b:hasCountry") { self.country }
55
+ registerSemanticProperty("dfc-b:hasStreet", &method("street")).valueSetter = method("street=")
56
+ registerSemanticProperty("dfc-b:hasPostalCode", &method("postalCode")).valueSetter = method("postalCode=")
57
+ registerSemanticProperty("dfc-b:hasCity", &method("city")).valueSetter = method("city=")
58
+ registerSemanticProperty("dfc-b:hasCountry", &method("country")).valueSetter = method("country=")
58
59
  end
59
60
 
60
61
 
@@ -25,8 +25,9 @@
25
25
  require "virtual_assembly/semantizer"
26
26
 
27
27
  class DataFoodConsortium::Connector::Agent
28
+ include VirtualAssembly::Semantizer::SemanticObject
28
29
 
29
- include VirtualAssembly::Semantizer::SemanticObject
30
+ SEMANTIC_TYPE = "dfc-b:Agent".freeze
30
31
 
31
32
  # @return [IAddress]
32
33
  attr_accessor :localizations
@@ -57,11 +58,11 @@ class DataFoodConsortium::Connector::Agent
57
58
  @websites = websites
58
59
  @socialMedias = socialMedias
59
60
  self.semanticType = "dfc-b:Agent"
60
- registerSemanticProperty("dfc-b:hasAddress") { self.localizations }
61
- registerSemanticProperty("dfc-b:hasPhoneNumber") { self.phoneNumbers }
62
- registerSemanticProperty("dfc-b:email") { self.emails }
63
- registerSemanticProperty("dfc-b:websitePage") { self.websites }
64
- registerSemanticProperty("dfc-b:hasSocialMedia") { self.socialMedias }
61
+ registerSemanticProperty("dfc-b:hasAddress", &method("localizations")).valueSetter = method("localizations=")
62
+ registerSemanticProperty("dfc-b:hasPhoneNumber", &method("phoneNumbers")).valueSetter = method("phoneNumbers=")
63
+ registerSemanticProperty("dfc-b:email", &method("emails")).valueSetter = method("emails=")
64
+ registerSemanticProperty("dfc-b:websitePage", &method("websites")).valueSetter = method("websites=")
65
+ registerSemanticProperty("dfc-b:hasSocialMedia", &method("socialMedias")).valueSetter = method("socialMedias=")
65
66
  end
66
67
 
67
68
 
@@ -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
-
24
23
  require "datafoodconsortium/connector/characteristic"
25
24
 
25
+
26
26
  require "virtual_assembly/semantizer"
27
27
 
28
28
  class DataFoodConsortium::Connector::AllergenCharacteristic < DataFoodConsortium::Connector::Characteristic
29
29
 
30
-
30
+ SEMANTIC_TYPE = "dfc-b:AllergenCharacteristic".freeze
31
31
 
32
32
  # @return [ISKOSConcept]
33
33
  attr_accessor :allergenDimension
@@ -39,7 +39,7 @@ class DataFoodConsortium::Connector::AllergenCharacteristic < DataFoodConsortium
39
39
  super(unit: unit, value: value)
40
40
  @allergenDimension = allergenDimension
41
41
  self.semanticType = "dfc-b:AllergenCharacteristic"
42
- registerSemanticProperty("dfc-b:hasAllergenDimension") { self.allergenDimension }
42
+ registerSemanticProperty("dfc-b:hasAllergenDimension", &method("allergenDimension")).valueSetter = method("allergenDimension=")
43
43
  end
44
44
 
45
45
 
@@ -27,8 +27,9 @@
27
27
  require "virtual_assembly/semantizer"
28
28
 
29
29
  class DataFoodConsortium::Connector::Catalog
30
+ include VirtualAssembly::Semantizer::SemanticObject
30
31
 
31
- include VirtualAssembly::Semantizer::SemanticObject
32
+ SEMANTIC_TYPE = "dfc-b:Catalog".freeze
32
33
 
33
34
  # @return [ICatalogItem]
34
35
  attr_accessor :items
@@ -44,8 +45,8 @@ class DataFoodConsortium::Connector::Catalog
44
45
  @items = items
45
46
  @maintainers = maintainers
46
47
  self.semanticType = "dfc-b:Catalog"
47
- registerSemanticProperty("dfc-b:lists") { self.items }
48
- registerSemanticProperty("dfc-b:maintainedBy") { self.maintainers }
48
+ registerSemanticProperty("dfc-b:lists", &method("items")).valueSetter = method("items=")
49
+ registerSemanticProperty("dfc-b:maintainedBy", &method("maintainers")).valueSetter = method("maintainers=")
49
50
  end
50
51
 
51
52
 
@@ -27,8 +27,9 @@
27
27
  require "virtual_assembly/semantizer"
28
28
 
29
29
  class DataFoodConsortium::Connector::CatalogItem
30
+ include VirtualAssembly::Semantizer::SemanticObject
30
31
 
31
- include VirtualAssembly::Semantizer::SemanticObject
32
+ SEMANTIC_TYPE = "dfc-b:CatalogItem".freeze
32
33
 
33
34
  # @return [IDefinedProduct]
34
35
  attr_accessor :product
@@ -59,11 +60,11 @@ class DataFoodConsortium::Connector::CatalogItem
59
60
  @offers = offers
60
61
  @catalogs = catalogs
61
62
  self.semanticType = "dfc-b:CatalogItem"
62
- registerSemanticProperty("dfc-b:references") { self.product }
63
- registerSemanticProperty("dfc-b:sku") { self.sku }
64
- registerSemanticProperty("dfc-b:stockLimitation") { self.stockLimitation }
65
- registerSemanticProperty("dfc-b:offeredThrough") { self.offers }
66
- registerSemanticProperty("dfc-b:listedIn") { self.catalogs }
63
+ registerSemanticProperty("dfc-b:references", &method("product")).valueSetter = method("product=")
64
+ registerSemanticProperty("dfc-b:sku", &method("sku")).valueSetter = method("sku=")
65
+ registerSemanticProperty("dfc-b:stockLimitation", &method("stockLimitation")).valueSetter = method("stockLimitation=")
66
+ registerSemanticProperty("dfc-b:offeredThrough", &method("offers")).valueSetter = method("offers=")
67
+ registerSemanticProperty("dfc-b:listedIn", &method("catalogs")).valueSetter = method("catalogs=")
67
68
  end
68
69
 
69
70
 
@@ -21,14 +21,12 @@
21
21
  # SOFTWARE.
22
22
 
23
23
 
24
- require "datafoodconsortium/connector/quantitative_value"
25
24
 
25
+ require "datafoodconsortium/connector/quantitative_value"
26
26
  require "virtual_assembly/semantizer"
27
27
 
28
28
  class DataFoodConsortium::Connector::Characteristic < DataFoodConsortium::Connector::QuantitativeValue
29
29
 
30
-
31
-
32
30
 
33
31
  # @param unit [ISKOSConcept]
34
32
  # @param value [Real]
@@ -21,6 +21,8 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'singleton'
24
+ require 'datafoodconsortium/connector/context'
25
+ require 'datafoodconsortium/connector/importer'
24
26
  require 'datafoodconsortium/connector/json_ld_serializer'
25
27
 
26
28
  class DataFoodConsortium::Connector::Connector
@@ -37,6 +39,10 @@ class DataFoodConsortium::Connector::Connector
37
39
  return @exporter.process(subject, *subjects)
38
40
  end
39
41
 
42
+ def import(json_string_or_io)
43
+ DataFoodConsortium::Connector::Importer.new.import(json_string_or_io)
44
+ end
45
+
40
46
  def loadFacets(data)
41
47
  @FACETS = loadThesaurus(data)
42
48
  end
@@ -57,14 +63,7 @@ class DataFoodConsortium::Connector::Connector
57
63
  # used to prefix properties
58
64
  # so the DFC's context can be used.
59
65
  # See https://github.com/datafoodconsortium/connector-ruby/issues/11.
60
- inputContext = {
61
- "dfc-b" => "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#",
62
- "dfc-p" => "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#",
63
- "dfc-t" => "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#",
64
- "dfc-m" => "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#",
65
- "dfc-pt" => "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#",
66
- "dfc-f" => "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#"
67
- }
66
+ inputContext = DataFoodConsortium::Connector::Context.inputContext
68
67
 
69
68
  @context = "https://www.datafoodconsortium.org"
70
69
 
@@ -77,7 +76,9 @@ class DataFoodConsortium::Connector::Connector
77
76
  end
78
77
 
79
78
  def loadThesaurus(data)
80
- return @parser.parse(data[0]["@graph"])
79
+ # The root element may be an array or the ontology.
80
+ data = data[0] if data.is_a?(Array)
81
+ @parser.parse(data["@graph"])
81
82
  end
82
83
 
83
84
  end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json/ld'
4
+
5
+ # Preload the DFC context.
6
+ #
7
+ # Similar to: https://github.com/ruby-rdf/json-ld-preloaded/
8
+ module DataFoodConsortium
9
+ module Connector
10
+ class Context < JSON::LD::Context
11
+ VERSION_1_8 = JSON.parse <<~JSON
12
+ {
13
+ "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
14
+ "skos" : "http://www.w3.org/2004/02/skos/core#",
15
+ "dfc": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#",
16
+ "dc": "http://purl.org/dc/elements/1.1/#",
17
+ "dfc-b": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#",
18
+ "dfc-p": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#",
19
+ "dfc-t": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#",
20
+ "dfc-m": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#",
21
+ "dfc-pt": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#",
22
+ "dfc-f": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#",
23
+ "ontosec": "http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#",
24
+ "dfc-p:hasUnit":{ "@type":"@id" },
25
+ "dfc-b:hasUnit":{ "@type":"@id" },
26
+ "dfc-b:hasQuantity":{ "@type":"@id" },
27
+ "dfc-p:hasType":{ "@type":"@id" },
28
+ "dfc-b:hasType":{ "@type":"@id" },
29
+ "dfc-b:references":{ "@type":"@id" },
30
+ "dfc-b:referencedBy":{ "@type":"@id" },
31
+ "dfc-b:offeres":{ "@type":"@id" },
32
+ "dfc-b:supplies":{ "@type":"@id" },
33
+ "dfc-b:defines":{ "@type":"@id" },
34
+ "dfc-b:affiliates":{ "@type":"@id" },
35
+ "dfc-b:hasCertification":{ "@type":"@id" },
36
+ "dfc-b:manages":{ "@type":"@id" },
37
+ "dfc-b:offeredThrough":{ "@type":"@id" },
38
+ "dfc-b:hasBrand":{ "@type":"@id" },
39
+ "dfc-b:hasGeographicalOrigin":{ "@type":"@id" },
40
+ "dfc-b:hasClaim":{ "@type":"@id" },
41
+ "dfc-b:hasAllergenDimension":{ "@type":"@id" },
42
+ "dfc-b:hasNutrientDimension":{ "@type":"@id" },
43
+ "dfc-b:hasPhysicalDimension":{ "@type":"@id" },
44
+ "dfc:owner":{ "@type":"@id" },
45
+ "dfc-t:hostedBy":{ "@type":"@id" },
46
+ "dfc-t:hasPivot":{ "@type":"@id" },
47
+ "dfc-t:represent":{ "@type":"@id" }
48
+ }
49
+ JSON
50
+
51
+ add_preloaded("http://www.datafoodconsortium.org/") { parse(VERSION_1_8) }
52
+
53
+ # This is the actual file the DFC website refers to in a link header.
54
+ alias_preloaded(
55
+ "https://www.datafoodconsortium.org/wp-content/plugins/wordpress-context-jsonld/context.jsonld",
56
+ "http://www.datafoodconsortium.org/"
57
+ )
58
+
59
+ # This is the old URL that's not online anymore.
60
+ # Keep it for compatiblity with all versions before 1.8.
61
+ alias_preloaded(
62
+ "http://static.datafoodconsortium.org/ontologies/context.json",
63
+ "http://www.datafoodconsortium.org/"
64
+ )
65
+
66
+ # The hash serializer expects only string values in the context.
67
+ def self.inputContext
68
+ @inputContext = VERSION_1_8.select { |key, value| value.is_a? String }
69
+ end
70
+ end
71
+ end
72
+ end
@@ -24,8 +24,9 @@
24
24
  require "virtual_assembly/semantizer"
25
25
 
26
26
  class DataFoodConsortium::Connector::CustomerCategory
27
+ include VirtualAssembly::Semantizer::SemanticObject
27
28
 
28
- include VirtualAssembly::Semantizer::SemanticObject
29
+ SEMANTIC_TYPE = "dfc-b:CustomerCategory".freeze
29
30
 
30
31
  # @return [String]
31
32
  attr_accessor :description
@@ -36,7 +37,7 @@ class DataFoodConsortium::Connector::CustomerCategory
36
37
  super(semanticId)
37
38
  @description = description
38
39
  self.semanticType = "dfc-b:CustomerCategory"
39
- registerSemanticProperty("dfc-b:description") { self.description }
40
+ registerSemanticProperty("dfc-b:description", &method("description")).valueSetter = method("description=")
40
41
  end
41
42
 
42
43
 
@@ -30,8 +30,9 @@
30
30
  require "virtual_assembly/semantizer"
31
31
 
32
32
  class DataFoodConsortium::Connector::DefinedProduct
33
+ include VirtualAssembly::Semantizer::SemanticObject
33
34
 
34
- include VirtualAssembly::Semantizer::SemanticObject
35
+ SEMANTIC_TYPE = "dfc-b:DefinedProduct".freeze
35
36
 
36
37
  # @return [String]
37
38
  attr_accessor :name
@@ -117,22 +118,22 @@ class DataFoodConsortium::Connector::DefinedProduct
117
118
  @natureOrigin = natureOrigin
118
119
  @partOrigin = partOrigin
119
120
  self.semanticType = "dfc-b:DefinedProduct"
120
- registerSemanticProperty("dfc-b:name") { self.name }
121
- registerSemanticProperty("dfc-b:description") { self.description }
122
- registerSemanticProperty("dfc-b:hasType") { self.productType }
123
- registerSemanticProperty("dfc-b:hasQuantity") { self.quantity }
124
- registerSemanticProperty("dfc-b:alcoholPercentage") { self.alcoholPercentage }
125
- registerSemanticProperty("dfc-b:lifetime") { self.lifetime }
126
- registerSemanticProperty("dfc-b:hasClaim") { self.claims }
127
- registerSemanticProperty("dfc-b:usageOrStorageCondition") { self.usageOrStorageConditions }
128
- registerSemanticProperty("dfc-b:hasAllergenCharacteristic") { self.allergenCharacteristics }
129
- registerSemanticProperty("dfc-b:hasNutrientCharacteristic") { self.nutrientCharacteristics }
130
- registerSemanticProperty("dfc-b:hasPhysicalCharacteristic") { self.physicalCharacteristics }
131
- registerSemanticProperty("dfc-b:hasGeographicalOrigin") { self.geographicalOrigin }
132
- registerSemanticProperty("dfc-b:referencedBy") { self.catalogItems }
133
- registerSemanticProperty("dfc-b:hasCertification") { self.certifications }
134
- registerSemanticProperty("dfc-b:hasNatureOrigin") { self.natureOrigin }
135
- registerSemanticProperty("dfc-b:hasPartOrigin") { self.partOrigin }
121
+ registerSemanticProperty("dfc-b:name", &method("name")).valueSetter = method("name=")
122
+ registerSemanticProperty("dfc-b:description", &method("description")).valueSetter = method("description=")
123
+ registerSemanticProperty("dfc-b:hasType", &method("productType")).valueSetter = method("productType=")
124
+ registerSemanticProperty("dfc-b:hasQuantity", &method("quantity")).valueSetter = method("quantity=")
125
+ registerSemanticProperty("dfc-b:alcoholPercentage", &method("alcoholPercentage")).valueSetter = method("alcoholPercentage=")
126
+ registerSemanticProperty("dfc-b:lifetime", &method("lifetime")).valueSetter = method("lifetime=")
127
+ registerSemanticProperty("dfc-b:hasClaim", &method("claims")).valueSetter = method("claims=")
128
+ registerSemanticProperty("dfc-b:usageOrStorageCondition", &method("usageOrStorageConditions")).valueSetter = method("usageOrStorageConditions=")
129
+ registerSemanticProperty("dfc-b:hasAllergenCharacteristic", &method("allergenCharacteristics")).valueSetter = method("allergenCharacteristics=")
130
+ registerSemanticProperty("dfc-b:hasNutrientCharacteristic", &method("nutrientCharacteristics")).valueSetter = method("nutrientCharacteristics=")
131
+ registerSemanticProperty("dfc-b:hasPhysicalCharacteristic", &method("physicalCharacteristics")).valueSetter = method("physicalCharacteristics=")
132
+ registerSemanticProperty("dfc-b:hasGeographicalOrigin", &method("geographicalOrigin")).valueSetter = method("geographicalOrigin=")
133
+ registerSemanticProperty("dfc-b:referencedBy", &method("catalogItems")).valueSetter = method("catalogItems=")
134
+ registerSemanticProperty("dfc-b:hasCertification", &method("certifications")).valueSetter = method("certifications=")
135
+ registerSemanticProperty("dfc-b:hasNatureOrigin", &method("natureOrigin")).valueSetter = method("natureOrigin=")
136
+ registerSemanticProperty("dfc-b:hasPartOrigin", &method("partOrigin")).valueSetter = method("partOrigin=")
136
137
  end
137
138
 
138
139
 
@@ -20,7 +20,6 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
-
24
23
  require "datafoodconsortium/connector/agent"
25
24
 
26
25
 
@@ -31,11 +30,12 @@ require "datafoodconsortium/connector/agent"
31
30
 
32
31
 
33
32
 
33
+
34
34
  require "virtual_assembly/semantizer"
35
35
 
36
36
  class DataFoodConsortium::Connector::Enterprise < DataFoodConsortium::Connector::Agent
37
37
 
38
-
38
+ SEMANTIC_TYPE = "dfc-b:Enterprise".freeze
39
39
 
40
40
  # @return [String]
41
41
  attr_accessor :name
@@ -91,15 +91,15 @@ class DataFoodConsortium::Connector::Enterprise < DataFoodConsortium::Connector:
91
91
  @technicalProducts = technicalProducts
92
92
  @mainContact = mainContact
93
93
  self.semanticType = "dfc-b:Enterprise"
94
- registerSemanticProperty("dfc-b:name") { self.name }
95
- registerSemanticProperty("dfc-b:hasDescription") { self.description }
96
- registerSemanticProperty("dfc-b:VATnumber") { self.vatNumber }
97
- registerSemanticProperty("dfc-b:defines") { self.customerCategories }
98
- registerSemanticProperty("dfc-b:maintains") { self.catalogs }
99
- registerSemanticProperty("dfc-b:manages") { self.catalogItems }
100
- registerSemanticProperty("dfc-b:supplies") { self.suppliedProducts }
101
- registerSemanticProperty("dfc-b:proposes") { self.technicalProducts }
102
- registerSemanticProperty("dfc-b:hasMainContact") { self.mainContact }
94
+ registerSemanticProperty("dfc-b:name", &method("name")).valueSetter = method("name=")
95
+ registerSemanticProperty("dfc-b:hasDescription", &method("description")).valueSetter = method("description=")
96
+ registerSemanticProperty("dfc-b:VATnumber", &method("vatNumber")).valueSetter = method("vatNumber=")
97
+ registerSemanticProperty("dfc-b:defines", &method("customerCategories")).valueSetter = method("customerCategories=")
98
+ registerSemanticProperty("dfc-b:maintains", &method("catalogs")).valueSetter = method("catalogs=")
99
+ registerSemanticProperty("dfc-b:manages", &method("catalogItems")).valueSetter = method("catalogItems=")
100
+ registerSemanticProperty("dfc-b:supplies", &method("suppliedProducts")).valueSetter = method("suppliedProducts=")
101
+ registerSemanticProperty("dfc-b:proposes", &method("technicalProducts")).valueSetter = method("technicalProducts=")
102
+ registerSemanticProperty("dfc-b:hasMainContact", &method("mainContact")).valueSetter = method("mainContact=")
103
103
  end
104
104
 
105
105
 
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "skos_parser"
4
+
5
+ module DataFoodConsortium
6
+ module Connector
7
+ class Importer
8
+ def self.type_map
9
+ unless @type_map
10
+ @type_map = {}
11
+ DataFoodConsortium::Connector.semantic_types.each do |type|
12
+ register_type(type)
13
+ end
14
+ end
15
+
16
+ @type_map
17
+ end
18
+
19
+ def self.register_type(clazz)
20
+ type_map[clazz::SEMANTIC_TYPE] = clazz
21
+ end
22
+
23
+ def self.prefixed_name(uri)
24
+ RDF::URI.new(uri).pname(prefixes: Context::VERSION_1_8)
25
+ end
26
+
27
+ def import(json_string_or_io)
28
+ @subjects = {}
29
+
30
+ graph = parse_rdf(json_string_or_io)
31
+ build_subjects(graph)
32
+ apply_statements(graph)
33
+
34
+ if @subjects.size > 1
35
+ @subjects.values
36
+ else
37
+ @subjects.values.first
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ # The `io` parameter can be a String or an IO instance.
44
+ def parse_rdf(io)
45
+ io = StringIO.new(io) if io.is_a?(String)
46
+ RDF::Graph.new << JSON::LD::API.toRdf(io)
47
+ end
48
+
49
+ def build_subjects(graph)
50
+ graph.query({ predicate: RDF.type }).each do |statement|
51
+ @subjects[statement.subject] = build_subject(statement)
52
+ end
53
+ end
54
+
55
+ def build_subject(type_statement)
56
+ # Not all subjects have an id, some are anonymous.
57
+ id = if type_statement.subject.respond_to?(:value)
58
+ type_statement.subject.value
59
+ end
60
+ type = type_statement.object.value
61
+ key = self.class.prefixed_name(type)
62
+ clazz = self.class.type_map[key]
63
+
64
+ clazz.new(*[id].compact)
65
+ end
66
+
67
+ def apply_statements(statements)
68
+ statements.each do |statement|
69
+ apply_statement(statement)
70
+ end
71
+ end
72
+
73
+ def apply_statement(statement)
74
+ subject = subject_of(statement)
75
+ property_uri = statement.predicate.value
76
+ value = resolve_object(statement.object)
77
+
78
+ property_id = self.class.prefixed_name(property_uri)
79
+
80
+ return unless subject.hasSemanticProperty?(property_id)
81
+
82
+ property = subject.semanticProperty(property_id)
83
+
84
+ if property.value.is_a?(Enumerable)
85
+ property.value << value
86
+ else
87
+ property.value = value
88
+ end
89
+ end
90
+
91
+ def subject_of(statement)
92
+ @subjects[statement.subject]
93
+ end
94
+
95
+ def resolve_object(object)
96
+ @subjects[object] || skos_concept(object) || object.object
97
+ end
98
+
99
+ def skos_concept(object)
100
+ return unless object.uri?
101
+
102
+ id = object.value.sub(
103
+ "http://static.datafoodconsortium.org/data/measures.rdf#", "dfc-m:"
104
+ ).sub(
105
+ "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#",
106
+ "dfc-m:"
107
+ )
108
+ SKOSParser.concepts[id]
109
+ end
110
+ end
111
+ end
112
+ end
@@ -30,27 +30,20 @@ class DataFoodConsortium::Connector::JsonLdSerializer
30
30
  @hashSerializer = VirtualAssembly::Semantizer::HashSerializer.new(inputContext)
31
31
  end
32
32
 
33
- def process(subject, *subjects)
34
- subjects.insert(0, subject)
33
+ def process(*subjects)
34
+ return "" if subjects.empty?
35
35
 
36
- if (subjects.empty?)
37
- return ""
38
- end
39
-
40
- inputs = []
41
-
42
36
  # Insert an input context on each subject so the properties could be prefixed. This way,
43
37
  # the DFC's context can be used.
44
38
  # See https://github.com/datafoodconsortium/connector-ruby/issues/11.
45
- subjects.each do |subject|
46
- input = { "@context" => "https://www.datafoodconsortium.org" }
47
- input.merge!(subject.serialize(@hashSerializer))
48
- inputs.push(input)
39
+ inputs = subjects.map do |subject|
40
+ # JSON::LD needs a context on every input using prefixes.
41
+ subject.serialize(@hashSerializer).merge("@context" => @outputContext)
49
42
  end
50
43
 
51
44
  jsonLd = JSON::LD::API.compact(inputs, @outputContext)
52
45
 
53
- return JSON.generate(jsonLd)
46
+ JSON.generate(jsonLd)
54
47
  end
55
48
 
56
- end
49
+ end
@@ -27,7 +27,7 @@ require "virtual_assembly/semantizer"
27
27
 
28
28
  class DataFoodConsortium::Connector::NutrientCharacteristic < DataFoodConsortium::Connector::Characteristic
29
29
 
30
-
30
+ SEMANTIC_TYPE = "dfc-b:NutrientCharacteristic".freeze
31
31
 
32
32
  # @return [ISKOSConcept]
33
33
  attr_accessor :nutrientDimension
@@ -39,7 +39,7 @@ class DataFoodConsortium::Connector::NutrientCharacteristic < DataFoodConsortium
39
39
  super(unit: unit, value: value)
40
40
  @nutrientDimension = nutrientDimension
41
41
  self.semanticType = "dfc-b:NutrientCharacteristic"
42
- registerSemanticProperty("dfc-b:hasNutrientDimension") { self.nutrientDimension }
42
+ registerSemanticProperty("dfc-b:hasNutrientDimension", &method("nutrientDimension")).valueSetter = method("nutrientDimension=")
43
43
  end
44
44
 
45
45
 
@@ -27,8 +27,9 @@
27
27
  require "virtual_assembly/semantizer"
28
28
 
29
29
  class DataFoodConsortium::Connector::Offer
30
+ include VirtualAssembly::Semantizer::SemanticObject
30
31
 
31
- include VirtualAssembly::Semantizer::SemanticObject
32
+ SEMANTIC_TYPE = "dfc-b:Offer".freeze
32
33
 
33
34
  # @return [IPrice]
34
35
  attr_accessor :price
@@ -54,10 +55,10 @@ class DataFoodConsortium::Connector::Offer
54
55
  @offeredItem = offeredItem
55
56
  @offeredTo = offeredTo
56
57
  self.semanticType = "dfc-b:Offer"
57
- registerSemanticProperty("dfc-b:hasPrice") { self.price }
58
- registerSemanticProperty("dfc-b:stockLimitation") { self.stockLimitation }
59
- registerSemanticProperty("dfc-b:offeredItem") { self.offeredItem }
60
- registerSemanticProperty("dfc-b:offeredTo") { self.offeredTo }
58
+ registerSemanticProperty("dfc-b:hasPrice", &method("price")).valueSetter = method("price=")
59
+ registerSemanticProperty("dfc-b:stockLimitation", &method("stockLimitation")).valueSetter = method("stockLimitation=")
60
+ registerSemanticProperty("dfc-b:offeredItem", &method("offeredItem")).valueSetter = method("offeredItem=")
61
+ registerSemanticProperty("dfc-b:offeredTo", &method("offeredTo")).valueSetter = method("offeredTo=")
61
62
  end
62
63
 
63
64
 
@@ -27,8 +27,9 @@
27
27
  require "virtual_assembly/semantizer"
28
28
 
29
29
  class DataFoodConsortium::Connector::Order
30
+ include VirtualAssembly::Semantizer::SemanticObject
30
31
 
31
- include VirtualAssembly::Semantizer::SemanticObject
32
+ SEMANTIC_TYPE = "dfc-b:Order".freeze
32
33
 
33
34
  # @return [String]
34
35
  attr_accessor :number
@@ -59,11 +60,11 @@ class DataFoodConsortium::Connector::Order
59
60
  @lines = lines
60
61
  @client = client
61
62
  self.semanticType = "dfc-b:Order"
62
- registerSemanticProperty("dfc-b:orderNumber") { self.number }
63
- registerSemanticProperty("dfc-b:date") { self.date }
64
- registerSemanticProperty("dfc-b:belongsTo") { self.saleSession }
65
- registerSemanticProperty("dfc-b:hasPart") { self.lines }
66
- registerSemanticProperty("dfc-b:orderedBy") { self.client }
63
+ registerSemanticProperty("dfc-b:orderNumber", &method("number")).valueSetter = method("number=")
64
+ registerSemanticProperty("dfc-b:date", &method("date")).valueSetter = method("date=")
65
+ registerSemanticProperty("dfc-b:belongsTo", &method("saleSession")).valueSetter = method("saleSession=")
66
+ registerSemanticProperty("dfc-b:hasPart", &method("lines")).valueSetter = method("lines=")
67
+ registerSemanticProperty("dfc-b:orderedBy", &method("client")).valueSetter = method("client=")
67
68
  end
68
69
 
69
70