Merge pull request #11323 from mkllnk/dfc-import-context

Use known, statically cached DFC context
This commit is contained in:
Maikel
2023-08-16 11:35:57 +10:00
committed by GitHub
10 changed files with 69 additions and 604 deletions

View File

@@ -1,7 +1,5 @@
# frozen_string_literal: true
require "data_food_consortium/connector/connector"
# Controller used to provide the SuppliedProducts API for the DFC application
# SuppliedProducts are products that are managed by an enterprise.
module DfcProvider

View File

@@ -2,64 +2,10 @@
# Our interface to the DFC Connector library.
module DfcIo
CONTEXT = JSON.parse <<~JSON
{
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"skos" : "http://www.w3.org/2004/02/skos/core#",
"dfc": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#",
"dc": "http://purl.org/dc/elements/1.1/#",
"dfc-b": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#",
"dfc-p": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#",
"dfc-t": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#",
"dfc-m": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#",
"dfc-pt": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#",
"dfc-f": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#",
"ontosec": "http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#",
"dfc-p:hasUnit":{ "@type":"@id" },
"dfc-b:hasUnit":{ "@type":"@id" },
"dfc-b:hasQuantity":{ "@type":"@id" },
"dfc-p:hasType":{ "@type":"@id" },
"dfc-b:hasType":{ "@type":"@id" },
"dfc-b:references":{ "@type":"@id" },
"dfc-b:referencedBy":{ "@type":"@id" },
"dfc-b:offeres":{ "@type":"@id" },
"dfc-b:supplies":{ "@type":"@id" },
"dfc-b:defines":{ "@type":"@id" },
"dfc-b:affiliates":{ "@type":"@id" },
"dfc-b:hasCertification":{ "@type":"@id" },
"dfc-b:manages":{ "@type":"@id" },
"dfc-b:offeredThrough":{ "@type":"@id" },
"dfc-b:hasBrand":{ "@type":"@id" },
"dfc-b:hasGeographicalOrigin":{ "@type":"@id" },
"dfc-b:hasClaim":{ "@type":"@id" },
"dfc-b:hasAllergenDimension":{ "@type":"@id" },
"dfc-b:hasNutrientDimension":{ "@type":"@id" },
"dfc-b:hasPhysicalDimension":{ "@type":"@id" },
"dfc:owner":{ "@type":"@id" },
"dfc-t:hostedBy":{ "@type":"@id" },
"dfc-t:hasPivot":{ "@type":"@id" },
"dfc-t:represent":{ "@type":"@id" }
}
JSON
# The HashSerializer expects only string values.
# This context is only used to shorten URIs.
SERIALIZER_CONTEXT = CONTEXT.select { |_key, value| value.is_a?(String) }.freeze
# Serialise DFC Connector subjects as JSON-LD string.
#
# This is a re-implementation of the Connector.export to provide our own context.
def self.export(*subjects)
return "" if subjects.empty?
serializer = VirtualAssembly::Semantizer::HashSerializer.new(SERIALIZER_CONTEXT)
hashes = subjects.map do |subject|
# JSON::LD needs a context on every input using prefixes.
subject.serialize(serializer).merge("@context" => CONTEXT)
end
json_ld = JSON::LD::API.compact(hashes, CONTEXT)
JSON.generate(json_ld)
DfcLoader.connector.export(*subjects)
end
end

View File

@@ -1,5 +1,7 @@
# frozen_string_literal: true
require "data_food_consortium/connector/connector"
class DfcLoader
def self.connector
@connector ||= load_vocabularies

View File

@@ -15,11 +15,11 @@ describe DfcIo do
expect(DfcIo.export).to eq ""
end
it "embeds the context" do
it "refers to the DFC context URI" do
json = DfcIo.export(person)
result = JSON.parse(json)
expect(result["@context"]).to be_a Hash
expect(result["@context"]).to eq "https://www.datafoodconsortium.org"
end
it "uses the context to shorten URIs" do

View File

@@ -1,5 +1,6 @@
# frozen_string_literal: true
require_relative "context"
require_relative "importer"
module DataFoodConsortium

View File

@@ -0,0 +1,56 @@
# frozen_string_literal: true
# Preload the DFC context.
#
# Similar to: https://github.com/ruby-rdf/json-ld-preloaded/
module DataFoodConsortium
module Connector
class Context < JSON::LD::Context
VERSION_1_8 = JSON.parse <<~JSON
{
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"skos" : "http://www.w3.org/2004/02/skos/core#",
"dfc": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#",
"dc": "http://purl.org/dc/elements/1.1/#",
"dfc-b": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#",
"dfc-p": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#",
"dfc-t": "https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#",
"dfc-m": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#",
"dfc-pt": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#",
"dfc-f": "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#",
"ontosec": "http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#",
"dfc-p:hasUnit":{ "@type":"@id" },
"dfc-b:hasUnit":{ "@type":"@id" },
"dfc-b:hasQuantity":{ "@type":"@id" },
"dfc-p:hasType":{ "@type":"@id" },
"dfc-b:hasType":{ "@type":"@id" },
"dfc-b:references":{ "@type":"@id" },
"dfc-b:referencedBy":{ "@type":"@id" },
"dfc-b:offeres":{ "@type":"@id" },
"dfc-b:supplies":{ "@type":"@id" },
"dfc-b:defines":{ "@type":"@id" },
"dfc-b:affiliates":{ "@type":"@id" },
"dfc-b:hasCertification":{ "@type":"@id" },
"dfc-b:manages":{ "@type":"@id" },
"dfc-b:offeredThrough":{ "@type":"@id" },
"dfc-b:hasBrand":{ "@type":"@id" },
"dfc-b:hasGeographicalOrigin":{ "@type":"@id" },
"dfc-b:hasClaim":{ "@type":"@id" },
"dfc-b:hasAllergenDimension":{ "@type":"@id" },
"dfc-b:hasNutrientDimension":{ "@type":"@id" },
"dfc-b:hasPhysicalDimension":{ "@type":"@id" },
"dfc:owner":{ "@type":"@id" },
"dfc-t:hostedBy":{ "@type":"@id" },
"dfc-t:hasPivot":{ "@type":"@id" },
"dfc-t:represent":{ "@type":"@id" }
}
JSON
add_preloaded("http://www.datafoodconsortium.org/") { parse(VERSION_1_8) }
alias_preloaded(
"http://static.datafoodconsortium.org/ontologies/context.json",
"http://www.datafoodconsortium.org/"
)
end
end
end

View File

@@ -91,13 +91,6 @@ RSpec.configure do |config|
expectations.syntax = :expect
end
config.around(:each, vcr: true) do |example|
# The DFC Connector fetches the context when loaded.
VCR.use_cassette("dfc-context") do
example.run
end
end
# Enable caching in any specs tagged with `caching: true`.
config.around(:each, :caching) do |example|
caching = ActionController::Base.perform_caching

File diff suppressed because one or more lines are too long

View File

@@ -3,7 +3,7 @@
require 'spec_helper'
require Rails.root.join('lib/data_food_consortium/connector/connector')
describe DataFoodConsortium::Connector::Importer, vcr: true do
describe DataFoodConsortium::Connector::Importer do
let(:connector) { DataFoodConsortium::Connector::Connector.instance }
let(:enterprise) do
DataFoodConsortium::Connector::Enterprise.new(

View File

@@ -57,66 +57,7 @@ paths:
examples:
test_example:
value:
"@context":
rdfs: http://www.w3.org/2000/01/rdf-schema#
skos: http://www.w3.org/2004/02/skos/core#
dfc: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#
dc: http://purl.org/dc/elements/1.1/#
dfc-b: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#
dfc-p: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#
dfc-t: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#
dfc-m: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#
dfc-pt: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#
dfc-f: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#
ontosec: http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#
dfc-p:hasUnit:
"@type": "@id"
dfc-b:hasUnit:
"@type": "@id"
dfc-b:hasQuantity:
"@type": "@id"
dfc-p:hasType:
"@type": "@id"
dfc-b:hasType:
"@type": "@id"
dfc-b:references:
"@type": "@id"
dfc-b:referencedBy:
"@type": "@id"
dfc-b:offeres:
"@type": "@id"
dfc-b:supplies:
"@type": "@id"
dfc-b:defines:
"@type": "@id"
dfc-b:affiliates:
"@type": "@id"
dfc-b:hasCertification:
"@type": "@id"
dfc-b:manages:
"@type": "@id"
dfc-b:offeredThrough:
"@type": "@id"
dfc-b:hasBrand:
"@type": "@id"
dfc-b:hasGeographicalOrigin:
"@type": "@id"
dfc-b:hasClaim:
"@type": "@id"
dfc-b:hasAllergenDimension:
"@type": "@id"
dfc-b:hasNutrientDimension:
"@type": "@id"
dfc-b:hasPhysicalDimension:
"@type": "@id"
dfc:owner:
"@type": "@id"
dfc-t:hostedBy:
"@type": "@id"
dfc-t:hasPivot:
"@type": "@id"
dfc-t:represent:
"@type": "@id"
"@context": https://www.datafoodconsortium.org
"@graph":
- "@id": http://test.host/api/dfc-v1.7/persons/12345
"@type": dfc-b:Person
@@ -175,66 +116,7 @@ paths:
examples:
test_example:
value:
"@context":
rdfs: http://www.w3.org/2000/01/rdf-schema#
skos: http://www.w3.org/2004/02/skos/core#
dfc: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#
dc: http://purl.org/dc/elements/1.1/#
dfc-b: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#
dfc-p: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#
dfc-t: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#
dfc-m: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#
dfc-pt: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#
dfc-f: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#
ontosec: http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#
dfc-p:hasUnit:
"@type": "@id"
dfc-b:hasUnit:
"@type": "@id"
dfc-b:hasQuantity:
"@type": "@id"
dfc-p:hasType:
"@type": "@id"
dfc-b:hasType:
"@type": "@id"
dfc-b:references:
"@type": "@id"
dfc-b:referencedBy:
"@type": "@id"
dfc-b:offeres:
"@type": "@id"
dfc-b:supplies:
"@type": "@id"
dfc-b:defines:
"@type": "@id"
dfc-b:affiliates:
"@type": "@id"
dfc-b:hasCertification:
"@type": "@id"
dfc-b:manages:
"@type": "@id"
dfc-b:offeredThrough:
"@type": "@id"
dfc-b:hasBrand:
"@type": "@id"
dfc-b:hasGeographicalOrigin:
"@type": "@id"
dfc-b:hasClaim:
"@type": "@id"
dfc-b:hasAllergenDimension:
"@type": "@id"
dfc-b:hasNutrientDimension:
"@type": "@id"
dfc-b:hasPhysicalDimension:
"@type": "@id"
dfc:owner:
"@type": "@id"
dfc-t:hostedBy:
"@type": "@id"
dfc-t:hasPivot:
"@type": "@id"
dfc-t:represent:
"@type": "@id"
"@context": https://www.datafoodconsortium.org
"@graph":
- "@id": http://test.host/api/dfc-v1.7/enterprises/10000/catalog_items/10001
"@type": dfc-b:CatalogItem
@@ -335,66 +217,7 @@ paths:
examples:
test_example:
value:
"@context":
rdfs: http://www.w3.org/2000/01/rdf-schema#
skos: http://www.w3.org/2004/02/skos/core#
dfc: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#
dc: http://purl.org/dc/elements/1.1/#
dfc-b: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#
dfc-p: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#
dfc-t: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#
dfc-m: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#
dfc-pt: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#
dfc-f: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#
ontosec: http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#
dfc-p:hasUnit:
"@type": "@id"
dfc-b:hasUnit:
"@type": "@id"
dfc-b:hasQuantity:
"@type": "@id"
dfc-p:hasType:
"@type": "@id"
dfc-b:hasType:
"@type": "@id"
dfc-b:references:
"@type": "@id"
dfc-b:referencedBy:
"@type": "@id"
dfc-b:offeres:
"@type": "@id"
dfc-b:supplies:
"@type": "@id"
dfc-b:defines:
"@type": "@id"
dfc-b:affiliates:
"@type": "@id"
dfc-b:hasCertification:
"@type": "@id"
dfc-b:manages:
"@type": "@id"
dfc-b:offeredThrough:
"@type": "@id"
dfc-b:hasBrand:
"@type": "@id"
dfc-b:hasGeographicalOrigin:
"@type": "@id"
dfc-b:hasClaim:
"@type": "@id"
dfc-b:hasAllergenDimension:
"@type": "@id"
dfc-b:hasNutrientDimension:
"@type": "@id"
dfc-b:hasPhysicalDimension:
"@type": "@id"
dfc:owner:
"@type": "@id"
dfc-t:hostedBy:
"@type": "@id"
dfc-t:hasPivot:
"@type": "@id"
dfc-t:represent:
"@type": "@id"
"@context": https://www.datafoodconsortium.org
"@graph":
- "@id": http://test.host/api/dfc-v1.7/enterprises/10000
"@type": dfc-b:Enterprise
@@ -441,66 +264,7 @@ paths:
examples:
test_example:
value:
"@context":
rdfs: http://www.w3.org/2000/01/rdf-schema#
skos: http://www.w3.org/2004/02/skos/core#
dfc: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#
dc: http://purl.org/dc/elements/1.1/#
dfc-b: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#
dfc-p: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#
dfc-t: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#
dfc-m: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#
dfc-pt: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#
dfc-f: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#
ontosec: http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#
dfc-p:hasUnit:
"@type": "@id"
dfc-b:hasUnit:
"@type": "@id"
dfc-b:hasQuantity:
"@type": "@id"
dfc-p:hasType:
"@type": "@id"
dfc-b:hasType:
"@type": "@id"
dfc-b:references:
"@type": "@id"
dfc-b:referencedBy:
"@type": "@id"
dfc-b:offeres:
"@type": "@id"
dfc-b:supplies:
"@type": "@id"
dfc-b:defines:
"@type": "@id"
dfc-b:affiliates:
"@type": "@id"
dfc-b:hasCertification:
"@type": "@id"
dfc-b:manages:
"@type": "@id"
dfc-b:offeredThrough:
"@type": "@id"
dfc-b:hasBrand:
"@type": "@id"
dfc-b:hasGeographicalOrigin:
"@type": "@id"
dfc-b:hasClaim:
"@type": "@id"
dfc-b:hasAllergenDimension:
"@type": "@id"
dfc-b:hasNutrientDimension:
"@type": "@id"
dfc-b:hasPhysicalDimension:
"@type": "@id"
dfc:owner:
"@type": "@id"
dfc-t:hostedBy:
"@type": "@id"
dfc-t:hasPivot:
"@type": "@id"
dfc-t:represent:
"@type": "@id"
"@context": https://www.datafoodconsortium.org
"@id": http://test.host/api/dfc-v1.7/persons/10000
"@type": dfc-b:Person
'404':
@@ -525,66 +289,7 @@ paths:
examples:
test_example:
value:
"@context":
rdfs: http://www.w3.org/2000/01/rdf-schema#
skos: http://www.w3.org/2004/02/skos/core#
dfc: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#
dc: http://purl.org/dc/elements/1.1/#
dfc-b: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#
dfc-p: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#
dfc-t: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#
dfc-m: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#
dfc-pt: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#
dfc-f: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#
ontosec: http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#
dfc-p:hasUnit:
"@type": "@id"
dfc-b:hasUnit:
"@type": "@id"
dfc-b:hasQuantity:
"@type": "@id"
dfc-p:hasType:
"@type": "@id"
dfc-b:hasType:
"@type": "@id"
dfc-b:references:
"@type": "@id"
dfc-b:referencedBy:
"@type": "@id"
dfc-b:offeres:
"@type": "@id"
dfc-b:supplies:
"@type": "@id"
dfc-b:defines:
"@type": "@id"
dfc-b:affiliates:
"@type": "@id"
dfc-b:hasCertification:
"@type": "@id"
dfc-b:manages:
"@type": "@id"
dfc-b:offeredThrough:
"@type": "@id"
dfc-b:hasBrand:
"@type": "@id"
dfc-b:hasGeographicalOrigin:
"@type": "@id"
dfc-b:hasClaim:
"@type": "@id"
dfc-b:hasAllergenDimension:
"@type": "@id"
dfc-b:hasNutrientDimension:
"@type": "@id"
dfc-b:hasPhysicalDimension:
"@type": "@id"
dfc:owner:
"@type": "@id"
dfc-t:hostedBy:
"@type": "@id"
dfc-t:hasPivot:
"@type": "@id"
dfc-t:represent:
"@type": "@id"
"@context": https://www.datafoodconsortium.org
"@id": http://test.host/api/dfc-v1.7/enterprises/10000/supplied_products/10001
"@type": dfc-b:SuppliedProduct
dfc-b:name: Apple
@@ -642,66 +347,7 @@ paths:
examples:
test_example:
value:
"@context":
rdfs: http://www.w3.org/2000/01/rdf-schema#
skos: http://www.w3.org/2004/02/skos/core#
dfc: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_FullModel.owl#
dc: http://purl.org/dc/elements/1.1/#
dfc-b: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_BusinessOntology.owl#
dfc-p: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_ProductGlossary.owl#
dfc-t: https://github.com/datafoodconsortium/ontology/releases/latest/download/DFC_TechnicalOntology.owl#
dfc-m: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/measures.rdf#
dfc-pt: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#
dfc-f: https://github.com/datafoodconsortium/taxonomies/releases/latest/download/facets.rdf#
ontosec: http://www.semanticweb.org/ontologies/2008/11/OntologySecurity.owl#
dfc-p:hasUnit:
"@type": "@id"
dfc-b:hasUnit:
"@type": "@id"
dfc-b:hasQuantity:
"@type": "@id"
dfc-p:hasType:
"@type": "@id"
dfc-b:hasType:
"@type": "@id"
dfc-b:references:
"@type": "@id"
dfc-b:referencedBy:
"@type": "@id"
dfc-b:offeres:
"@type": "@id"
dfc-b:supplies:
"@type": "@id"
dfc-b:defines:
"@type": "@id"
dfc-b:affiliates:
"@type": "@id"
dfc-b:hasCertification:
"@type": "@id"
dfc-b:manages:
"@type": "@id"
dfc-b:offeredThrough:
"@type": "@id"
dfc-b:hasBrand:
"@type": "@id"
dfc-b:hasGeographicalOrigin:
"@type": "@id"
dfc-b:hasClaim:
"@type": "@id"
dfc-b:hasAllergenDimension:
"@type": "@id"
dfc-b:hasNutrientDimension:
"@type": "@id"
dfc-b:hasPhysicalDimension:
"@type": "@id"
dfc:owner:
"@type": "@id"
dfc-t:hostedBy:
"@type": "@id"
dfc-t:hasPivot:
"@type": "@id"
dfc-t:represent:
"@type": "@id"
"@context": https://www.datafoodconsortium.org
"@id": http://test.host/api/dfc-v1.7/enterprises/10000/supplied_products/10001
"@type": dfc-b:SuppliedProduct
dfc-b:name: Pesto