1

私は Jena を初めて使用し、Java の経験があまりありません。モデルからステートメントを削除するプログラムを作成しようとしています。(グラフを使用して SPARUL クエリを実行する代替方法については知っていますが、代わりにモデルを使用したいと考えています)。model.remove(statement) を使用してみましたが、適切に行っていないようです。上記の方法の例を検索しましたが、見つかりませんでした。誰かが私が間違っているところを助けてくれますか? ステートメントを1つ削除することから始めたいと思います。その後、複数のステートメントを削除したいと思います。以下のコードを含めています。助けてくれてありがとう

     import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.VCARD;

 public class Test3 extends Object  {

 public static void main(String[] args) throws IOException {

     final String  inputFileName = "vc-db-1.rdf";
     Model model = ModelFactory.createDefaultModel();

     InputStream in = FileManager.get().open(inputFileName);
     if (in == null) {
         throw new IllegalArgumentException ( "File: " + inputFileName + " not found");
     }

      model.read(new InputStreamReader(in), "");
     in.close();

     System.out.println( "== Before removal ==" );
     model.write( System.out);

     System.out.println( "\n\n== After removal ==" );



     model.remove( model.createResource( "http://somewhere/JohnSmith" ),
             VCARD.FN, // or ResourceFactory.createProperty( "http://www.w3.org/2001/vcard-rdf/3.0#FN" );
             ResourceFactory.createTypedLiteral( "John Smith" ));
     model.write( System.out);
 }

 }

出力は次のとおりです。

   == Before removal ==
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#" > 

.....Other statements
      <rdf:Description rdf:about="http://somewhere/JohnSmith/">
        <vCard:N rdf:nodeID="A3"/>
        <vCard:FN>John Smith</vCard:FN>
      </rdf:Description>
      <rdf:Description rdf:nodeID="A3">
        <vCard:Given>John</vCard:Given>
        <vCard:Family>Smith</vCard:Family>
      </rdf:Description>
    </rdf:RDF>


    == After removal ==
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#" > 
...Other Statements
      <rdf:Description rdf:about="http://somewhere/JohnSmith/">
        <vCard:N rdf:nodeID="A3"/>
        <vCard:FN>John Smith</vCard:FN>
      </rdf:Description>
      <rdf:Description rdf:nodeID="A3">
        <vCard:Given>John</vCard:Given>
        <vCard:Family>Smith</vCard:Family>
      </rdf:Description>
    </rdf:RDF>

ステートメントを削除した後、何らかの形でモデルへの変更を「確認/コミット」する必要がありますか?

4

1 に答える 1

3

表示しているデータとプログラムで作成しているリソースは異なります。あなたのプログラムでは、作成されるリソースは

http://somewhere/JohnSmith

しかし、データでは、それは

http://somewhere/JohnSmith/

末尾に/. これに対処したら、次のコードのようにトリプルを削除できます。

import java.io.ByteArrayInputStream;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.vocabulary.VCARD;

public class RemoveStatementExample {
    public static void main(String[] args) {
        final String n3content = "" + 
                "@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n" +
                "@prefix vCard:   <http://www.w3.org/2001/vcard-rdf/3.0#> .\n" +
                "<http://somewhere/JohnSmith/>\n" +
                "  vCard:FN \"John Smith\" ;\n" +
                "  vCard:N [ vCard:Family \"Smith\" ;\n" +
                "            vCard:Given \"John\"\n" +
                "          ] .\n" +
                "";
        final Model model = ModelFactory.createDefaultModel()
                            .read( new ByteArrayInputStream( n3content.getBytes()), null, "N3" );

        // before removal
        System.out.println( "== before removal ==" );
        model.write( System.out, "N3" );

        // remove the statement.  Note that in your data, "John Smith" is an 
        // *untyped* literal, so we use createLiteral( "John Smith" ) rather than
        // createTypedLiteral( "John Smith" ).
        model.remove( model.createResource( "http://somewhere/JohnSmith/" ),
                      VCARD.FN,
                      model.createLiteral( "John Smith" ));

        System.out.println( "\n\n== after removal ==" );
        model.write( System.out, "N3" );
    }
}

出力は次のとおりです。

== before removal ==
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix vCard:   <http://www.w3.org/2001/vcard-rdf/3.0#> .

<http://somewhere/JohnSmith/>
      vCard:FN "John Smith" ;
      vCard:N [ vCard:Family "Smith" ;
                vCard:Given "John"
              ] .


== after removal ==
@prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix vCard:   <http://www.w3.org/2001/vcard-rdf/3.0#> .

<http://somewhere/JohnSmith/>
      vCard:N [ vCard:Family "Smith" ;
                vCard:Given "John"
              ] .
于 2013-07-29T02:47:11.887 に答える