java - 在耶拿使用 model.remove (语句)

标签 java jena

我是 Jena 的新手,对 Java 没有太多经验。我正在尝试制作一个从模型中删除语句的程序。 (我知道使用图形并运行 SPARUL 查询的替代方法,但我想改用模型)。我尝试过使用 model.remove(statement) 但似乎我做得不正确。我搜索了上述方法的示例,但找不到。有人可以帮我解决我做错的地方吗?我想从删除一条语句开始。之后我想删除多个语句。我包括下面的代码。预先感谢您的帮助

     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>

删除语句后我是否需要以某种方式“确认/提交”对模型的更改?

最佳答案

您显示的数据和您在程序中创建的资源不同。在您的程序中,正在创建的资源是

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"
              ] .

关于java - 在耶拿使用 model.remove (语句),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912395/

相关文章:

Java程序Opencv unsatisfiedlinkerror

java - 大字节数组使用的堆比预期多

java - 读取目录中的所有RDF文件

java - 将 Json-Ld 对象数组读入 Apache Jena 中的模型。如何从模型中检索单个对象?

windows - Cygwin 上的 tdbloader : java. lang.NoClassDefFoundError

java - 在哪里可以找到 Apache Jena 版本历史记录?

java - 使用 processBuilder 执行 shell 命令并与之交互

java - 用于开始 Java 源文件的有效关键字

java - 从日期字符串中获取月份

java - API Jena - 我的界面的按钮 "Next"无法正常工作