0

Jena を使用して RDF ファイルを読み取っていますが、特定のステートメントだけの情報を取得したいと考えています。以下は、読むサンプルコード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.rdfJena モデルにロードする Java コード:

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>

ありがとう

4

1 に答える 1

3

作業データ

実際に操作できるデータを次に示します。これは、プレフィックスが定義されたデータに基づいており、作成および作業rdf用の追加のプレフィックスがいくつかあります。これらはオプションですが、データで使用されていたため、 のプレフィックス定義が必要でした。便宜上使用しただけですが、おそらく別のものが定義されているでしょう。ctag:SocialTagcsys:InstanceInfochttp://example.org/c#

<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 を使用してデータを取得する簡単な方法が 2 つあります。1 つ目は、ステートメントを取得するためのメソッドを提供する Jena のモデル API を使用することです。2 つ目は、SPARQL クエリを使用することです。Jena のコマンド ライン ツールを使用して SPARQL クエリを実行できますが、Java プログラムから実行することもできます。

モデル API の使用

results目的の出力を格納するモデルを作成し、SocialTags とその名前と重要性を定義するステートメントを から取得し、inputステートメントを にコピーするJava コードを次に示します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 の使用

次の SPARQLconstructクエリも SocialTags を取得し、目的のグラフを作成します。

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 .
}

inputモデルでそのクエリを実行する Java コードを次に示します。

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

今すぐ一緒に

上記のリストはinput、データを定義して読み込む実際の例の一部にすぎません。リスト全体は次のとおりです。

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>
于 2013-08-02T12:49:54.767 に答える