以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Semantic Web(语义Web)/描述逻辑/本体 』  (http://bbs.xml.org.cn/list.asp?boardid=2)
----  【求助】jena中有关statment的修改问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=2&rootid=&id=78016)


--  作者:yyc0424
--  发布时间:10/29/2009 10:46:00 AM

--  【求助】jena中有关statment的修改问题
如题。
我现在将一个本体用jena来处理成一组statment的形式,比如把Animal.owl处理成以下格式:
http://www.owl-ontologies.com/Ontology1244799080.owl#BeiLion http://www.w3.org/1999/02/22-rdf-syntax-ns#type  http://www.owl-ontologies.com/Ontology1244799080.owl#Lion  .
http://www.owl-ontologies.com/Ontology1244799080.owl#BeiLion http://www.owl-ontologies.com/Ontology1244799080.owl#belonging  http://www.owl-ontologies.com/Ontology1244799080.owl#Beijing_Zoo  .
http://www.owl-ontologies.com/Ontology1244799080.owl#Address http://www.w3.org/2000/01/rdf-schema#comment  地址^^http://www.w3.org/2001/XMLSchema#string  .
http://www.owl-ontologies.com/Ontology1244799080.owl#Address http://www.w3.org/1999/02/22-rdf-syntax-ns#type  http://www.w3.org/2002/07/owl#DatatypeProperty  .
http://www.owl-ontologies.com/Ontology1244799080.owl#Address http://www.w3.org/2000/01/rdf-schema#range  http://www.w3.org/2001/XMLSchema#string  .
http://www.owl-ontologies.com/Ontology1244799080.owl#Address http://www.w3.org/2000/01/rdf-schema#domain  http://www.owl-ontologies.com/Ontology1244799080.owl#Zoo  .
http://www.owl-ontologies.com/Ontology1244799080.owl#Tel http://www.w3.org/2000/01/rdf-schema#comment  电话号码^^http://www.w3.org/2001/XMLSchema#string  .

现在我想把里面所有的http://www.w3.org/2002/07/owl#DatatypeProperty改成http://www.w3.org/2002/07/owl#DatatypeProperty111111
程序如下:
package example;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

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.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

public class Ev {
 public static void main(String[] args) {
  
  Model model=ModelFactory.createDefaultModel();
  StmtIterator iter;
  
  String no="http://www.w3.org/2002/07/owl#DatatypeProperty111111";
  Resource type=model.getResource(no);
     try

     {   model.read(new FileInputStream("D:\\Animal.owl"),""); }

     catch(IOException ioe)

     {

            System.err.println(ioe.toString());

     }
 iter = model.listStatements();
while (iter.hasNext()) {
    Statement stmt      = iter.nextStatement();  // get next statement
    Resource  subject   = stmt.getSubject();     // get the subject
    Property  predicate = stmt.getPredicate();   // get the predicate
    RDFNode   object    = stmt.getObject();      // get the object
    if(object.toString().equals("http://www.w3.org/2002/07/owl#DatatypeProperty"))
        object=type;
    System.out.print(subject.toString());
    System.out.print(" " + predicate.toString() + " ");
    System.out.print(" " + object.toString() + " ");

    System.out.println(" .");
}
try

{
  model.write(new FileOutputStream("D:\\Animals.owl")) ;
      

}

catch(IOException ioe)

{ System.err.println(ioe.toString()); }

 }}
显示Statement的时候没有问题,但是写到D:\\Animals.owl时,却没有完成修改。不知道为什么?还请各位帮忙,多谢!


--  作者:yyc0424
--  发布时间:10/30/2009 3:52:00 PM

--  
找到解决方法了呵呵,自己不会使用有关iterator 的同步之类的解决方法,使用的是比较笨的方法。
可以另外建立一个模型newmodel,将最后生成的subject,predicate和object等直接使用add()的方法写入就行了
--  作者:Humphrey
--  发布时间:11/3/2009 2:51:00 PM

--  
感谢楼主分享经验!
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
6,500.000ms