我如何在耶拿写下以下内容?

时间:2011-12-09 00:39:21

标签: rdf jena

如何为以下内容编写Jena语句:

:process1 sio:' is denoted by' : process effect_result


' is denoted by'只是一个标签,而siohttp://semanticscience.org/resource/中的命名空间。

1 个答案:

答案 0 :(得分:2)

首先,您需要定义一个定义属性的本体。这不是严格必要的,但这绝对是一种很好的做法。理想情况下,您的本体应该可以在Web上找到您指定给它的命名空间地址(在您的情况下为http://semanticscience.org/resource)。这可能是本体的一个片段,只要我理解你想要建模的东西:

@prefix sio: <http://semanticscience.org/resource/>.
@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#> .

<> a owl:Ontology;
    rdfs:label "Process ontology";
    dc:author "hamid";
.

sio:denotedBy
    a owl:ObjectProperty;
    rdfs:domain sio:Process;
    rdfs:range sio:ProcessEffect;
    rdfs:label "is denoted by"@en;
.

在Java代码中,您只需要引用本体中定义的URI。大致有两种方法可以做到这一点:

  • 在代码中手动定义常量
  • 使用Jena从您的本体文件中自动生成Java常量

第二种方法使用耶拿的schemagen tool。我实际上建议最终使用这种方法,但是为了开始,您可能会发现在代码中手动定义常量会更容易。这样做的主要缺点是程序维护:如果将来更改本体文件,则需要记住更新手动创建的常量,否则您将发现自己试图找到代码中突然出现的错误。

这是一个手动定义常量的示例类,然后参考process1定义processEffect1

package test;


// Imports
///////////////

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;

public class ProcessTest
{
    @SuppressWarnings( value = "unused" )
    private static final Logger log = LoggerFactory.getLogger( ProcessTest.class );

    public static final String SIO = "http://semanticscience.org/resource/";
    public static final Resource Process = ResourceFactory.createResource( SIO + "Process" );
    public static final Resource ProcessEffect = ResourceFactory.createResource( SIO + "ProcessEffect" );
    public static final Property denotedBy = ResourceFactory.createProperty( SIO + "denotedBy" );

    public void run() {
        // create a model to hold the RDF statements we want to define
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );

        // inform m about the sio: prefix - not necessary, but makes the output prettier
        m.setNsPrefix( "sio", SIO );

        // create the individual for the process being defined
        Individual p1 = m.createIndividual( SIO + "process1", Process );

        // create the individual for the process effect
        Individual pe1 = m.createIndividual( SIO + "processEffect1", ProcessEffect );

        // the process is denoted by the effect
        p1.addProperty( denotedBy, pe1 );

        // for illustration, we just print the contents of the model now
        m.write( System.out, "Turtle" );
    }

    public static void main( String[] args ) {
        new ProcessTest().run();
    }
}

产生以下输出:

@prefix rdfs:    <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sio:     <http://semanticscience.org/resource/> .
@prefix owl:     <http://www.w3.org/2002/07/owl#> .
@prefix xsd:     <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

sio:processEffect1
      a       sio:ProcessEffect .

sio:process1
      a       sio:Process ;
      sio:denotedBy sio:processEffect1 .