datafoodconsortium / connector
该连接器可以简化与DFC的集成。
Requires
- php: >=8.1
- ml/json-ld: ^1.2
- virtual-assembly/semantizer: ^v1.0.0-beta2
This package is not auto-updated.
Last update: 2024-09-25 02:44:20 UTC
README
数据食品联盟(DFC)连接器是一个工具,可以帮助您在应用程序中集成DFC标准。
借助连接器提供的相应类,可以操作DFC本体中的每个概念。
此连接器还将帮助您导入和导出数据,以便您与其他DFC标准兼容的平台交换信息。
数据食品联盟项目(DFC)旨在提供食品供应链平台之间的互操作性。
开始使用
API参考文档可在此处找到。
安装
您可以使用composer安装连接器
composer require datafoodconsortium/connector
加载分类法
然后,您可以加载我们提供的不同SKOS分类法,提供相应的JSON-LD文件
$connector->import("/path/to/measures.json"); $connector->import("/path/to/facets.json"); $connector->import("/path/to/productTypes.json"); $connector->import("/path/to/vocabulary.json");
这些分类法可以直接从连接器访问,例如
// Example of a facet $fruit = $connector->fetch("dfc-f:Fruit"); // Example of an measure $kilogram = $connector->fetch("dfc-m:Kilogram"); // Example of a product type $tomato = $connector->fetch("dfc-pt:RoundTomato");
对象创建
您可以通过调用新操作符来创建对象。
注意:每个新创建的对象都将保存到连接器提供的存储中。此存储将允许您更轻松地访问引用对象。您可以通过在构造对象时传递doNotStore: true
参数来禁用此行为。
注意:除匿名对象(空节点)外,semanticId
构造函数参数是必需的。所有其他参数都是可选的。
$quantity = new QuantitativeValue( connector: $connector, // You have to pass a reference to the connector. value: 1.2, unit: $kilogram ); $allergenCharacteristic = new AllergenCharacteristic( connector: $connector, // You have to pass a reference to the connector. value: 1, unit: $kilogram, allergenDimension: $connector->fetch("dfc-m:Peanuts"); ); $nutrientCharacteristic = new NutrientCharacteristic( connector: $connector, // You have to pass a reference to the connector. value: 10, unit: $gram, nutrientDimension: $connector->fetch("dfc-m:Calcium") ); $physicalCharacteristic = new PhysicalCharacteristic({ connector: $connector, // You have to pass a reference to the connector. value: 100, unit: $gram, physicalDimension: $connector->fetch("dfc-m:Weight") }); $catalogItem = new CatalogItem({ connector: $connector, // You have to pass a reference to the connector. semanticId: "http://myplatform.com/catalogItem" }); $suppliedProduct = new SuppliedProduct({ connector: onnector, // You have to pass a reference to the connector. semanticId: "http://myplatform.com/tomato", description: "Awesome tomato", productType: $connector->fetch("dfc-pt:RoundTomato"), quantity: $quantity, totalTheoreticalStock: 2.23, alcoholPercentage: 0, lifetime: "a week", claims: [$connector->fetch("dfc-f:NoAddedSugar")], usageOrStorageConditions: "free text", allergenCharacteristics: [$allergenCharacteristic], nutrientCharacteristics: [$nutrientCharacteristic], physicalCharacteristics: [$physicalCharacteristic], geographicalOrigin: $connector->fetch("dfc-f:CentreValLoire"), catalogItems: [$catalogItem], certifications: [$connector->fetch("dfc-f:OrganicAB"), $connector->fetch("dfc-f:OrganicEU")], natureOrigin: [$connector->fetch("dfc-f:PlantOrigin")], partOrigin: [$connector->fetch("dfc-f:Fruit")] });
可用的具体类
地址
过敏特性
目录
目录项
客户类别
企业
营养成分特性
报价
订单
订单行
人员
物理特性
计划消费流程
计划生产流程
计划转换
价格
定量值
数量
销售会话
SKOS概念
供应产品
技术产品
对象访问器和修改器
读取对象属性(访问器)
您可以使用getter方法读取对象的属性。
$suppliedProduct->getDescription();
前一个方法返回了一个简单的字符串。但一个对象通常包含其他对象。在语义网中,每个对象都有自己的URI(它存储在网络上另一个位置)。因此,我们将只使用它们的URI存储这些包含对象的引用。它们被称为“引用对象”。
要使用连接器访问引用对象,无需做任何特殊操作
$addresses = $person->getLocalizations();
注意:运行前面的代码示例将触发连接器的fetch
函数调用。如果引用对象尚未在连接器存储中,它将从网络下载。
更改对象属性(修改器)
如果您想在对象创建后更改属性,可以使用其适当的setter方法,例如
// Set the quantity of the product $suppliedProduct->setQuantity(new QuantitiveValue(connector: $connector, unit: $kilogram, value: 2.6));
您还可以向是数组的属性添加值
// Add a new certification to the product $suppliedProduct->addCertification($connector->fetch("dfc-f:AocFR"));
导出对象
使用连接器,您可以导出DFC对象。默认导出器将对象导出到JSON-LD格式
$connector->export([$suppliedProduct]));
这将输出符合DFC标准的有效JSON-LD,如下所示
{ "@context": "http://static.datafoodconsortium.org/ontologies/context.json", "@graph": [ { "@id": "_:b1", "@type": "dfc-b:QuantitativeValue", "dfc-b:hasUnit": "dfc-m:Kilogram", "dfc-b:value": "1.2" }, { "@id": "_:b2", "@type": "dfc-b:AllergenCharacteristic", "dfc-b:hasAllergenDimension": "dfc-m:Peanuts", "dfc-b:hasUnit": "dfc-m:Kilogram", "dfc-b:value": "1" }, { "@id": "_:b4", "@type": "dfc-b:NutrientCharacteristic", "dfc-b:hasNutrientDimension": { "@id": "dfc-m:Calcium" }, "dfc-b:hasUnit": "dfc-m:Gram", "dfc-b:value": "10" }, { "@id": "_:b6", "@type": "dfc-b:PhysicalCharacteristic", "dfc-b:hasPhysicalDimension": "dfc-m:Weight", "dfc-b:hasUnit": "dfc-m:Gram", "dfc-b:value": "100" }, { "@id": "http://myplatform.com/tomato", "@type": "dfc-b:SuppliedProduct", "dfc-b:alcoholPercentage": "0", "dfc-b:description": "Awesome tomato", "dfc-b:hasAllergenCharacteristic": { "@id": "_:b2" }, "dfc-b:hasCertification": [ { "@id": "dfc-f:Organic-AB" }, { "@id": "dfc-f:Organic-EU" } ], "dfc-b:hasClaim": "dfc-f:NoAddedSugars", "dfc-b:hasGeographicalOrigin": "dfc-f:CentreValLoire", "dfc-b:hasNatureOrigin": { "@id": "dfc-f:PlantOrigin" }, "dfc-b:hasNutrientCharacteristic": { "@id": "_:b4" }, "dfc-b:hasPartOrigin": { "@id": "dfc-f:Fruit" }, "dfc-b:hasPhysicalCharacteristic": { "@id": "_:b6" }, "dfc-b:hasQuantity": "_:b1", "dfc-b:hasType": "dfc-pt:round-tomato", "dfc-b:lifetime": "a week", "dfc-b:referencedBy": "http://myplatform.com/catalogItem", "dfc-b:totalTheoreticalStock": "2.23", "dfc-b:usageOrStorageCondition": "free text" } ] }
导入对象
DFC连接器提供导入数据的方法。默认导入器导入JSON-LD数据。
要从JSON-LD导入对象,请使用
$objects = $connector->import($jsonAsAString));
默认的fetch函数如下所示
function getDefaultfetchFunction(string $semanticObjectId): string { $opts = array('http' => array('method' => "GET", 'header' => "Accept: application/ld+json")); $context = stream_context_create($opts); return file_get_contents($semanticObjectId, false, $context); }
您可以通过Connector::setFetchFunction
方法传递自定义函数。
配置
您可以使用以下连接器方法来适应连接器的不同组件以满足您的需求
// Set the function that will fetch the referenced objects when importing data. Connector::setFetchFunction(\Closure $fetch); // Set the object used to create new instances. Connector::setFactory(IFactory $factory);
有关更多详细信息,请参阅Semantizer文档。
示例
计划中的转换循环
$connector = new Connector(); $connector->import("./test/thesaurus/measures.json"); $connector->import("./test/thesaurus/vocabulary.json"); $quantity = new Quantity( connector: $connector, unit: $connector->fetch("dfc-m:Kilogram"), value: 1.0 ); $consumptionFlow = new PlannedConsumptionFlow( connector: $connector, semanticId: "http://example.org/consumptionFlow", quantity: $quantity ); $productionFlow = new PlannedProductionFlow( connector: $connector, semanticId: "http://example.org/productionFlow", quantity: $quantity ); $transformation = new PlannedTransformation( connector: $connector, semanticId: "http://example.org/transformation", consumptionFlow: $consumptionFlow, productionFlow: $productionFlow, transformationType: $connector->fetch("dfc-v:combine") ); echo $connector->export([$transformation, $consumptionFlow, $productionFlow]);