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

时间:2018-03-13 10:17:18

标签: rdf owl ontology turtle-rdf

我正在尝试设计一个基于产品组件对产品进行分类的本体。在下面的示例中,我有一个类Ingredient,其中包含一个实例eggs。我想将apple_tart添加到不包含eggs的所有类别的产品中。因此,在这种情况下,apple_tart将添加到课程GlutenFriendly而非VeganFriendly

enter image description here

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。

1 个答案:

答案 0 :(得分:1)

执行所需(我知道)的唯一方法是使用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> .

在我的博客中,我给出了这个例子的详细解释:Classification with SHACL rules