2013-07-28 25 views
1

我是Jena的新成員,對Java沒有多少經驗。我正在試圖製作一個從模型中刪除語句的程序。 (我知道使用圖形並運行SPARUL查詢的替代方法,但我想使用模型來代替)。我嘗試過使用model.remove(聲明),但似乎我沒有做好。我已經搜索了上述方法的示例,但找不到一個。有人可以幫助我,我做錯了嗎?我想先刪除一條語句。之後,我想刪除多個語句。我包含下面的代碼。在此先感謝您的幫助在Jena中使用model.remove(語句)

 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> 

我需要以某種方式刪除報表後「確認/提交」更改模型?

回答

2

您顯示的數據和您在程序中創建的資源不同。在你的程序中,所創建的資源是

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

感謝約書亞,有沒有通過,我可以刪除報表,而無需創建屬性的方式或資源?我想將RDF文件讀入模型中,然後從中刪除特定的語句(就像在SQL中使用刪除語句一樣)。謝謝。 – Syed

+0

@Syed您可以使用'null'作爲通配符,但您將不得不提供_some_方法來標識要刪除哪些語句。你怎麼知道你想刪除哪些。在上面的例子中,你試圖直接將URI寫入代碼,並修復你的例子,你所要做的就是將它們封裝爲新資源(「http:// somewhere/JohnSmith」,...) 。你真的想刪除哪些陳述? –

+0

@Syed我已經更新了答案,以根據您的示例顯示特定的調用「remove」,它看起來像您想要使用的。 –