datafoodconsortium/connector 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

datafoodconsortium/connector

Composer 安装命令:

composer require datafoodconsortium/connector

包简介

The connector makes things simplier when integrating with the DFC.

README 文档

README

The Data Food Consortium (DFC) Connector is a tool to help you to integrate the DFC standard within you application.

Each concept of the DFC ontology can be manipulated with the help of the corresponding class supplied by the connector.

This connector will also help you to import and export data so you can exchange information with other DFC standard compliant platforms.

The Data Food Consortium project (DFC) aims to provide interoperability between food supply chain platforms.

Get started

The API reference is available here.

Install

You can install the connector with composer:

composer require datafoodconsortium/connector

Load the taxonomies

You can then load our different SKOS taxonomies providing the corresponding JSON-LD files:

$connector->import("/path/to/measures.json");
$connector->import("/path/to/facets.json");
$connector->import("/path/to/productTypes.json");
$connector->import("/path/to/vocabulary.json");

These taxonomies are accessible directly from the connector, like:

// 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");

Object creation

You can create objects by calling the new operator.

Remark: each newly created object will be saved into the store provided to the Connector. This store will allow you to access to the referenced objects more easily. You can disable this behavior passing the doNotStore: true parameter when constructing the objects.

Remark: Except for anonymous objects (blank nodes), the semanticId constructor parameter is mandatory. All the other parameters are optional.

$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")]
});

Available concreate classes:

  • Address
  • AllergenCharacteristic
  • Catalog
  • CatalogItem
  • CustomerCategory
  • Enterprise
  • NutrientCharacteristic
  • Offer
  • Order
  • OrderLine
  • Person
  • PhysicalCharacteristic
  • PlannedConsumptionFlow
  • PlannedProductionFlow
  • PlannedTransformation
  • Price
  • QuantitativeValue
  • Quantity
  • SaleSession
  • SKOSConcept
  • SuppliedProduct
  • TechnicalProduct

Object accessors and mutators

Read object properties (accessor)

You can read the properties of an object using getter methods.

$suppliedProduct->getDescription();

The previous method returned a simple string. But an object ofen contains other objects. In the semantic web, every object has its own URI (it is stored at an other location on the network). So we will store only a reference to these contained objects using their URI. They are called "referenced objects".

To access a referenced object using the connector there is nothing special to do:

$addresses = $person->getLocalizations();

Remark: Running the previous code sample will trigger a call to the fetch function of the connector. If the referenced object it is not already in the connector store, it will be downloaded from the network.

Change object properties (mutator)

If you want to change a property after the creation of the object, you can use its proper setter method like:

// Set the quantity of the product
$suppliedProduct->setQuantity(new QuantitiveValue(connector: $connector, unit: $kilogram, value: 2.6));

You can also add a value to properties that are array:

// Add a new certification to the product
$suppliedProduct->addCertification($connector->fetch("dfc-f:AocFR"));

Export objects

With the Connector, you can export DFC object(s). The default exporter exports objects to JSON-LD:

$connector->export([$suppliedProduct]));

This will output DFC compliant valid JSON-LD like:

{
  "@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"
    }
  ]
}

Import objects

The DFC Connector provides method to import data. The default importer imports JSON-LD data.

To import objects from JSON-LD, use:

$objects = $connector->import($jsonAsAString));

The default fetch function looks like:

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);
}

You can pass a custom function via the Connector::setFetchFunction method.

Configure

You can adapt different components of the connector to your needs with the following connector methods:

// 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);

See the Semantizer documentation for more details.

Examples

Planned transformation loop

$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]);

datafoodconsortium/connector 适用场景与选型建议

datafoodconsortium/connector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「RDF」 「Semantic Web」 「object model」 「data food consortium」 「short supply chain」 「farming」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 datafoodconsortium/connector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 datafoodconsortium/connector 我们能提供哪些服务?
定制开发 / 二次开发

基于 datafoodconsortium/connector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-15