OWL RDF/TTL 根据属性创建类的实例成员

2024-01-10

我正在尝试设计一个本体,该本体将根据产品组件对产品进行分类。在下面的例子中我有一堂课Ingredient有一个实例eggs。我想添加apple_tart不包含的所有类别的产品eggs。所以在这种情况下apple_tart将被添加到班级中GlutenFriendly并不是VeganFriendly.

bakery:VeganFriendly rdf:type owl:Class ;
               rdfs:subClassOf [ rdf:type owl:Restriction ;
                                 owl:onProperty :hasIngredient ;
                                 owl:hasValue :eggs
                               ]
               owl:disjointWith bakery:Ingredient .

所以我试图在两者之间建立负关联eggs和班级VeganFriendly?上面的代码将添加产品eggs...

EIDT:我也愿意接受能够产生相同效果的新设计。最好只使用 OWL/RDF/RDFS。

编辑:这就是我最终所做的https://github.com/uxdxdev/bakery-ontology https://github.com/uxdxdev/bakery-ontology


做你需要的事情的唯一方法(据我所知)是使用SHACL https://github.com/TopQuadrant/shacl规则。假设您有以下 RDF 数据:

@prefix bakery: <http://bakery.com/ns#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .


bakery:hasIngredient rdf:type owl:ObjectProperty ;
   rdfs:domain bakery:BakeryGood ;
   rdfs:range bakery:Ingredient .

bakery:VeganFriendly rdf:type owl:Class ;
   owl:subClassOf bakery:Ingredient . 

bakery:NonVeganFriendly rdf:type owl:Class ;
   owl:subClassOf bakery:Ingredient . 

bakery:GlutenFree rdf:type owl:Class ;
   owl:subClassOf bakery:Ingredient  . 

bakery:NonGlutenFree rdf:type owl:Class ;
   owl:subClassOf bakery:Ingredient  .      

bakery:Apple a bakery:VeganFriendly, bakery:GlutenFree .

bakery:Egg a bakery:NonVeganFriendly, bakery:GlutenFree .

bakery:Flour a bakery:VeganFriendly, bakery:NonGlutenFree .

bakery:AlmondFlour a bakery:VeganFriendly, bakery:GlutenFree .

bakery:RiceMilk a bakery:VeganFriendly, bakery:GlutenFree .


bakery:AppleTartA
  a bakery:BakedGood ;
  bakery:hasIngredient bakery:Apple, bakery:Egg, bakery:Flour .

bakery:AppleTartB
  a bakery:BakedGood ;
  bakery:hasIngredient bakery:Apple, bakery:RiceMilk, bakery:AlmondFlour .

bakery:AppleTartC
  a bakery:BakedGood ;
  bakery:hasIngredient bakery:Apple, bakery:RiceMilk, bakery:Flour .

bakery:AppleTartD
   a bakery:BakedGood ;
   bakery:hasIngredient bakery:Apple, bakery:Egg, bakery:AlmondFlour .  

您可以指定以下形状规则文件:

@prefix bakery: <http://bakery.com/ns#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix dash: <http://datashapes.org/dash#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .


bakery:bakery:NonVeganFriendly
    a rdfs:Class, sh:NodeShape .

bakery:bakery:NonGlutenFree
    a rdfs:Class, sh:NodeShape .

bakery:BakedGood
    a rdfs:Class, sh:NodeShape ;
    sh:property [
        sh:path bakery:hasIngredient ;
        sh:class bakery:Ingredient ;
        sh:nodeKind sh:IRI ;
        sh:minCount 1 ;
    ] ;
    sh:rule [
        a sh:TripleRule ;
        sh:subject sh:this ;
        sh:predicate rdf:type ;
        sh:object bakery:VeganBakedGood ;
        sh:condition bakery:BakedGood ;
        sh:condition [
            sh:property [
                sh:path bakery:hasIngredient ;
                sh:qualifiedValueShape [ sh:class bakery:NonVeganFriendly ] ;
                sh:qualifiedMaxCount 0 ;
            ] ;
        ] ;
    ] ; 
    sh:rule [
        a sh:TripleRule ;
        sh:subject sh:this ;
        sh:predicate rdf:type ;
        sh:object bakery:NonVeganBakedGood ;
        sh:condition bakery:BakedGood ;
        sh:condition [
            sh:property [
                sh:path bakery:hasIngredient ;
                sh:qualifiedValueShape [ sh:class bakery:NonVeganFriendly ] ;
                sh:qualifiedMinCount 1 ;
            ] ;
        ] ;
    ] ; 
    sh:rule [
        a sh:TripleRule ;
        sh:subject sh:this ;
        sh:predicate rdf:type ;
        sh:object bakery:GlutenFreeBakedGood ;
        sh:condition bakery:BakedGood ;
        sh:condition [
            sh:property [
                sh:path bakery:hasIngredient ;
                sh:qualifiedValueShape [ sh:class bakery:NonGlutenFree ] ;
                sh:qualifiedMaxCount 0 ;
            ] ;
        ] ;
    ] ; 
    sh:rule [
        a sh:TripleRule ;
        sh:subject sh:this ;
        sh:predicate rdf:type ;
        sh:object bakery:NonGlutenFreeBakedGood ;
        sh:condition bakery:BakedGood ;
        sh:condition [
            sh:property [
                sh:path bakery:hasIngredient ;
                sh:qualifiedValueShape [ sh:class bakery:NonGlutenFree] ;
                sh:qualifiedMinCount 1 ;
            ] ;
        ] ;
    ] .

并使用以下代码(使用 SHACL 的 Jena 实现)来执行您的规则:

package org.shacl.tutorial;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.reasoner.Reasoner;
import org.apache.jena.reasoner.ReasonerRegistry;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.topbraid.shacl.rules.RuleUtil;
import org.topbraid.spin.util.JenaUtil;

public class ShaclClassification {
  private static Logger logger = LoggerFactory.getLogger(ShaclValidation.class);
  // Why This Failure marker
  private static final Marker WTF_MARKER = MarkerFactory.getMarker("WTF");

  public static void main(String[] args) {
    try {   
      Path path = Paths.get(".").toAbsolutePath().normalize();

      String data = "file:" + path.toFile().getAbsolutePath() + "/src/main/resources/bakery.ttl";
      String shape = "file:" + path.toFile().getAbsolutePath() + "/src/main/resources/bakeryRules.ttl";      

      Reasoner reasoner = ReasonerRegistry.getRDFSReasoner();
      Model dataModel = JenaUtil.createDefaultModel();
      dataModel.read(data);
      Model infModel = ModelFactory.createInfModel(reasoner, dataModel);
      Model shapeModel = JenaUtil.createDefaultModel();
      shapeModel.read(shape);
      Model inferenceModel = JenaUtil.createDefaultModel();

      inferenceModel = RuleUtil.executeRules(infModel, shapeModel, inferenceModel, null);

      String inferences = path.toFile().getAbsolutePath() + "/src/main/resources/inferences.ttl";
      File inferencesFile = new File(inferences);
      inferencesFile.createNewFile();     
      OutputStream reportOutputStream = new FileOutputStream(inferencesFile);

      RDFDataMgr.write(reportOutputStream, inferenceModel, RDFFormat.TTL);    
    } catch (Throwable t) {
      logger.error(WTF_MARKER, t.getMessage(), t);
    }   
  }
}

这将为您提供以下输出:

<http://bakery.com/ns#AppleTartC>
        a       <http://bakery.com/ns#NonGlutenFreeBakedGood> , <http://bakery.com/ns#VeganBakedGood> .

<http://bakery.com/ns#AppleTartB>
        a       <http://bakery.com/ns#GlutenFreeBakedGood> , <http://bakery.com/ns#VeganBakedGood> .

<http://bakery.com/ns#AppleTartA>
        a       <http://bakery.com/ns#NonGlutenFreeBakedGood> , <http://bakery.com/ns#NonVeganBakedGood> .

<http://bakery.com/ns#AppleTartD>
    a       <http://bakery.com/ns#GlutenFreeBakedGood> , <http://bakery.com/ns#NonVeganBakedGood> .

在我的博客上我对这个例子做了详细的解释:使用 SHACL 规则进行分类 https://henrietteharmse.com/2018/03/15/classification-with-shacl-rules/.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OWL RDF/TTL 根据属性创建类的实例成员 的相关文章

随机推荐

  • Spark DataFrame 重新分区和 Parquet 分区

    我在列上使用重新分区来将数据存储在镶木地板中 但 我看到没有 parquet 分区文件的数量与 不 Rdd 分区 rdd分区之间是否没有相关性 和镶木地板隔断 当我将数据写入镶木地板分区并使用 Rdd 时 重新分区 然后我从 parquet
  • 带有 C# 内部访问修饰符的 Doxygen

    我正在使用 Doxygen 为我正在处理的 C 项目生成一些 API 文档 我在这个项目中有相当多的 内部 功能 并且不希望 Doxygen 在它生成的生成的 html 中生成这些签名 我已尝试启用 HIDE FRIEND COMPOUND
  • macOS 中的 UIGraphicsBeginImageContextWithOptions 模拟

    以下是我用来在 iOS 中缩放图像的代码 即将 500x500 图像缩放到 100x100 图像 然后存储缩放后的副本 UIImage image UIImage originalImage scaledToSize CGSize desi
  • NaN 作为字典中的键

    谁能向我解释以下行为 gt gt gt import numpy as np gt gt gt np nan 5 np nan 5 gt gt gt float64 np nan 5 float64 np nan KeyError nan
  • 切换到共享运行时后 Office 插件加载失败

    我跟着配置您的 Office 加载项以使用共享 JavaScript 运行时 https learn microsoft com en us office dev add ins develop configure your add in
  • 如何使用searchkick根据某些条件进行索引

    我正在使用searchkick 和rails4 我有一个活动记录 People 其属性为 a b c 如何仅当 b 等于 type1 时才建立索引 否则不建立索引 目前我所知道的是 def search data a a b b c c e
  • java等待光标显示问题

    我在应用程序中显示等待光标时遇到问题 只要鼠标位于定义其自身光标的面板上方 等待光标就不会出现 如果面板不改变光标 则会出现等待光标 我附上 SSCE 来准确解释我的问题 public class BusyCursorTest extend
  • VS 2017 中的 MySQL 和 MVC 实体框架无法正常工作

    我正在尝试启动 MVC EF Visual Studio 2017 项目 我在本地实例上使用 MySQL 设置了数据连接 但是当我创建 ADO net 数据模型时 出现图中所示的错误 这里还有另一篇文章 无法对实体框架 6 使用 MySQL
  • 对数据库的访问进行队列以避免多个缓存项

    我有一个与音乐相关的 ASP NET 网站 它在第一次请求时缓存数据库中的大量静态信息 有时 应用程序负载较重时会重置应用程序并清除缓存 然后所有 http 请求都会转到数据库以检索静态数据并将其缓存以供其他请求使用 如何确保只有一个请求进
  • 表达式树生成的 IL 是否经过优化?

    好吧 这只是好奇心 对现实世界没有帮助 我知道使用表达式树 您可以像常规 C 编译器一样即时生成 MSIL 由于编译器可以决定优化 我很想问在执行期间生成的 IL 的情况如何Expression Compile 基本上有两个问题 因为在编译
  • ResizeObserver 一 vs 多性能

    The 调整大小观察者 https wicg github io ResizeObserver has an 观察 https wicg github io ResizeObserver dom resizeobserver observe
  • 保留源 shell 脚本而不退出终端

    我正在编写一个 shell 脚本来保存一些击键并避免拼写错误 我想将脚本保留为单个文件 该文件调用内部方法 函数并在出现问题时终止函数without离开航站楼 my script sh bin bash exit if no git if
  • 为什么方法定义返回符号?

    当你定义一个方法时 它会返回一个与该方法同名的符号 这有道理吗 或者它只是作为您创建它的验证 Like so def something end gt something IRb always显示调用结果inspect计算的最后一个表达式的
  • StringBuilder容量()

    我注意到capacity方法返回StringBuilder没有逻辑的能力 方式 有时它的值等于字符串长度 有时它更大 有没有一个方程式可以知道它的逻辑是什么 我将尝试用一些例子来解释这一点 public class StringBuilde
  • 编译错误:“stddef.h:没有这样的文件或目录”

    每当我尝试编译此代码时 总是会出现以下错误 In file included from usr include wchar h 6 0 from usr lib gcc i686 pc cygwin 4 9 2 include c cwch
  • 在 Inno Setup 中将字符串编码为 Base64(Inno Setup 的 Unicode 版本)

    Problem 我尝试使用帕斯卡函数EncodeStringBase64 假设 Inno Setup 可以访问 Pascal 标准库 但它无法找到它并提供一个Unknown Identifier error https www freepa
  • 使用 GDI+ 将修改后的图像保存到原始文件

    我正在从文件加载位图图像 当我尝试将图像保存到另一个文件时 出现以下错误 GDI 中发生一般错误 我相信这是因为文件被图像对象锁定 好的 所以尝试调用 Image Clone 函数 这仍然锁定文件 唔 接下来 我尝试从 FileStream
  • 删除目录下的所有文件

    我需要使用 Qt 删除目录中的所有文件 该目录中的所有文件都将具有扩展名 txt 我不想删除目录本身 有谁知道我该怎么做 我看过 QDir 但没有运气 比约恩斯的答案被调整为不会永远循环 QString path whatever QDir
  • 版本 4.0.0 文档中的示例中的 Identity Server 范围无效

    我正在使用 IdentityServer4 遵循以下文档https identityserver4 readthedocs io en latest quickstarts 1 client credentials html https i
  • OWL RDF/TTL 根据属性创建类的实例成员

    我正在尝试设计一个本体 该本体将根据产品组件对产品进行分类 在下面的例子中我有一堂课Ingredient有一个实例eggs 我想添加apple tart不包含的所有类别的产品eggs 所以在这种情况下apple tart将被添加到班级中Gl