如何使用java从RDF文件中获取特定的标签相关信息 [英] how to get specific tag related information from RDF file using java

查看:171
本文介绍了如何使用java从RDF文件中获取特定的标签相关信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jena读取RDF文件,但我想获取的信息只是特定的语句。以下是我的示例代码: sample.rdf

I am using Jena to read an RDF file, but I want to get the information just specific statements. Below is my sample code to read sample.rdf.

sample.rdf

<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/Instance/143">
    <c:length>4</c:length>
    <c:offset>6588</c:offset>
    <c:suffix> network, specific mechanisms for implementing</c:suffix>
    <c:exact>VoIP</c:exact>
    <c:prefix>applications. Topics include imple-menting a 
   </c:prefix>
    <c:detection>[applications. Topics include imple-
menting a ]VoIP[ network, specific mechanisms for implementing]</c:detection>
    <c:subject rdf:resource="http://d.opencalais.com/genericHasher-1/1bc26b65-5ef5-306d-9203-fd0f8aa3ba18"/>
    <c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
    <rdf:type rdf:resource="http://s.opencalais.com/1/type/sys/InstanceInfo"/>
  </rdf:Description>

<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
    <c:originalValue>Cisco IOS</c:originalValue>
    <c:importance>2</c:importance>
    <c:name>Cisco IOS</c:name>
    <c:socialtag rdf:resource="http://d.opencalais.com/genericHasher-1/8ed51994-de69-3307-acf7-be18cc0d06e2"/>
    <c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
    <rdf:type rdf:resource="http://s.opencalais.com/1/type/tag/SocialTag"/>
  </rdf:Description>

sample.rdf 加载到的Java代码一个Jena模型:

Java code that loads sample.rdf into a Jena Model:

public class FirstRDFReader extends Object {
   public static void main (String args[]) {
       String inputFile="C://Sample.rdf";
      Model model = ModelFactory.createDefaultModel();
   try{
   InputStream in =new  FileInputStream(inputFile);
    if (in == null) {  
     System.out.println("File not found");
     }  
     model.read(in," ");
    model.write(System.out);
}catch(Exception e){}
  }
}

期望的输出:

<rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
<c:importance>2</c:importance>
 <c:name>Cisco IOS</c:name>
</rdf:Description>

谢谢

推荐答案

工作数据



这是我们可以实际使用的一些数据。它基于您的数据,定义了 rdf 前缀,以及一些额外的前缀用于生成 ctag:SocialTag csys:InstanceInfo 工作。这些是可选的,但是需要 c 的前缀定义,因为它已在您的数据中使用。为方便起见,我只是使用了 http://example.org/c# ,但你可能还有其他的定义。

Working Data

Here's some data that we can actually work with. It's based on your data, with the rdf prefix defined, and some additional prefixes for making ctag:SocialTag and csys:InstanceInfo work. Those are optional, but a prefix definition for c was required, since it was used in your data. I just used http://example.org/c# for convenience, but you've probably got something else defined.

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:c="http://example.org/c#"
    xmlns:ctag="http://s.opencalais.com/1/type/tag/"
    xmlns:csys="http://s.opencalais.com/1/type/sys/">
  <ctag:SocialTag rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
    <c:originalValue>Cisco IOS</c:originalValue>
    <c:importance>2</c:importance>
    <c:name>Cisco IOS</c:name>
    <c:socialtag rdf:resource="http://d.opencalais.com/genericHasher-1/8ed51994-de69-3307-acf7-be18cc0d06e2"/>
    <c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
  </ctag:SocialTag>
  <csys:InstanceInfo rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/Instance/143">
    <c:length>4</c:length>
    <c:offset>6588</c:offset>
    <c:suffix> network, specific mechanisms for implementing</c:suffix>
    <c:exact>VoIP</c:exact>
    <c:prefix>applications. Topics include imple-menting a 
   </c:prefix>
    <c:detection>[applications. Topics include imple-
menting a ]VoIP[ network, specific mechanisms for implementing]</c:detection>
    <c:subject rdf:resource="http://d.opencalais.com/genericHasher-1/1bc26b65-5ef5-306d-9203-fd0f8aa3ba18"/>
    <c:docId rdf:resource="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73"/>
  </csys:InstanceInfo>
</rdf:RDF>

一旦你在某处获得了这些数据,有两种简单的方法可以使用Jena将其输出。第一种是使用Jena的Model API,它提供了检索语句的方法。第二种是使用SPARQL查询。您可以使用Jena的命令行工具运行SPARQL查询,也可以从Java程序运行。

Once you've got that data somewhere, there are two easy ways to get it out using the Jena. The first is to use Jena's Model API which provides methods for retrieving statements. The second is to use a SPARQL query. You can run the SPARQL query using Jena's command line tools, but also from a Java program.

这是创建结果的Java代码用于存储所需输出的模型,从输入中检索SocialTag和定义其名称和重要性的语句,并将语句复制到结果

Here's Java code that creates a results model to store the desired output, retrieves SocialTags and the statements defining their name and importance from input, and copies the statement into results.

public static Model queryWithAPI() { 
    // Create a model for the output, and add the prefix mappings
    // from the input model.  This step isn't necessary, but it 
    // makes the output easier to read.
    final Model results = ModelFactory.createDefaultModel();
    results.setNsPrefixes( input );

    // Iterate through the SocialTags in the data, and for each SocialTag s, retrieve
    // the statements [s, name, ?name] and [s, importance, ?importance] from the input
    // model, and add them to the results.
    for ( final ResIterator it = input.listResourcesWithProperty( RDF.type, SocialTag ); it.hasNext(); ) {
        final Resource socialTag = it.next();
        results.add( socialTag.getProperty( importance ));
        results.add( socialTag.getProperty( name ));
    }
    return results;
}



使用SPARQL



以下SPARQL 构造查询还检索SocialTags,然后构造所需的图形。

Using SPARQL

The following SPARQL construct query also retrieve SocialTags and then constructs the desired graph.

prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix ctag: <http://s.opencalais.com/1/type/tag/>
prefix c: <http://example.org/c#>
construct {
  ?tag c:name ?name ;
       c:importance ?importance .
}
where {
  ?tag a ctag:SocialTag ;
       c:name ?name ;
       c:importance ?importance .
}

这是在输入上执行该查询的Java代码 model。

public static Model queryWithSPARQL() { 
    // A SPARQL query that retrieves each SocialTag and its name
    // and importance, and constructs a model containing just the 
    // name and importance statements.
    final String query = "" +
            "prefix rdf: <"+RDF.getURI()+">\n" +
            "prefix ctag: <"+CTAG+">\n" +
            "prefix c: <"+C+">\n" +
            "construct {\n" +
            "  ?tag c:name ?name ;\n" +
            "       c:importance ?importance .\n" +
            "}\n" +
            "where {\n" +
            "  ?tag a ctag:SocialTag ;\n" +
            "       c:name ?name ;\n" +
            "       c:importance ?importance .\n" +
            "}";
    // Create and execute the query on the input model.
    return QueryExecutionFactory.create( query, input ).execConstruct();
}



现在一起



上面的列表只是定义输入的工作示例的片段,并读入数据。以下是整个列表:

All together now

The listings above are just snippets from a working example that defines input and reads in the data. Here's the entire listing:

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.vocabulary.RDF;

public class CalaisExample {
    static final String C = "http://example.org/c#";
    static final String CTAG = "http://s.opencalais.com/1/type/tag/";

    static final Resource SocialTag = ResourceFactory.createResource( CTAG+"SocialTag" );
    static final Property importance = ResourceFactory.createProperty( C+"importance" );
    static final Property name = ResourceFactory.createProperty( C+"name" );

    // Create a model for the input and read in the data.
    static final Model input = ModelFactory.createDefaultModel()
            .read( "file:///home/taylorj/tmp/jena-calais/calais.rdf" );

    public static void main(String[] args) {
        System.out.println( "== Using API ==" );
        queryWithAPI().write( System.out );
        System.out.println();
        System.out.println( "== Using SPARQL ==" );
        queryWithSPARQL().write( System.out );
    }

    public static Model queryWithAPI() { 
        // Create a model for the output, and add the prefix mappings
        // from the input model.  This step isn't necessary, but it 
        // makes the output easier to read.
        final Model results = ModelFactory.createDefaultModel();
        results.setNsPrefixes( input );

        // Iterate through the SocialTags in the data, and for each SocialTag s, retrieve
        // the statements [s, name, ?name] and [s, importance, ?importance] from the input
        // model, and add them to the results.
        for ( final ResIterator it = input.listResourcesWithProperty( RDF.type, SocialTag ); it.hasNext(); ) {
            final Resource socialTag = it.next();
            results.add( socialTag.getProperty( importance ));
            results.add( socialTag.getProperty( name ));
        }
        return results;
    }

    public static Model queryWithSPARQL() { 
        // A SPARQL query that retrieves each SocialTag and its name
        // and importance, and constructs a model containing just the 
        // name and importance statements.
        final String query = "" +
                "prefix rdf: <"+RDF.getURI()+">\n" +
                "prefix ctag: <"+CTAG+">\n" +
                "prefix c: <"+C+">" +
                "construct {\n" +
                "  ?tag c:name ?name ;\n" +
                "       c:importance ?importance .\n" +
                "}\n" +
                "where {\n" +
                "  ?tag a ctag:SocialTag ;\n" +
                "       c:name ?name ;\n" +
                "       c:importance ?importance .\n" +
                "}";
        // Create and execute the query on the input model.
        return QueryExecutionFactory.create( query, input ).execConstruct();
    }
}

这是输出:

== Using API ==
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:c="http://example.org/c#"
    xmlns:ctag="http://s.opencalais.com/1/type/tag/"
    xmlns:csys="http://s.opencalais.com/1/type/sys/" > 
  <rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
    <c:name>Cisco IOS</c:name>
    <c:importance>2</c:importance>
  </rdf:Description>
</rdf:RDF>

== Using SPARQL ==
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:c="http://example.org/c#"
    xmlns:ctag="http://s.opencalais.com/1/type/tag/"
    xmlns:csys="http://s.opencalais.com/1/type/sys/" > 
  <rdf:Description rdf:about="http://d.opencalais.com/dochash-1/6ee25504-ff98-34e4-af60-dde69f5ddf73/SocialTag/10">
    <c:importance>2</c:importance>
    <c:name>Cisco IOS</c:name>
  </rdf:Description>
</rdf:RDF>

这篇关于如何使用java从RDF文件中获取特定的标签相关信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆