新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Semantic Web(语义Web,语义网或语义万维网, Web 3.0)及相关理论,如:Ontology(本体,本体论), OWL(Web Ontology Langauge,Web本体语言), Description Logic(DL, 描述逻辑),RDFa,Ontology Engineering等。
    [返回] 计算机科学论坛W3CHINA.ORG讨论区 - Web新技术讨论『 Semantic Web(语义Web)/描述逻辑/本体 』 → 请教:OntModel::createIndividual() 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 7905 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 请教:OntModel::createIndividual() 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     xirufeng 美女呀,离线,快来找我吧!
      
      
      等级:大二期末(Java考了96分!)
      文章:77
      积分:322
      门派:XML.ORG.CN
      注册:2005/12/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xirufeng发送一个短消息 把xirufeng加入好友 查看xirufeng的个人资料 搜索xirufeng在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xirufeng的博客楼主
    发贴心情 请教:OntModel::createIndividual()

    在已经有的文件owl文件的基础上,执行createIndividual()后,为什么只可以保存最新的实例,而且并不能在owl文件中体现?
    谢谢!

       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/20 10:46:00
     
     jpz6311whu 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      等级:研三(收到微软亚洲研究院的Offer了)(版主)
      文章:1718
      积分:10610
      门派:W3CHINA.ORG
      注册:2005/4/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jpz6311whu发送一个短消息 把jpz6311whu加入好友 查看jpz6311whu的个人资料 搜索jpz6311whu在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jpz6311whu的博客2
    发贴心情 
    你把完整的程序源代码和完整的owl文件贴出来,并说明运行结果。
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/20 10:49:00
     
     xirufeng 美女呀,离线,快来找我吧!
      
      
      等级:大二期末(Java考了96分!)
      文章:77
      积分:322
      门派:XML.ORG.CN
      注册:2005/12/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xirufeng发送一个短消息 把xirufeng加入好友 查看xirufeng的个人资料 搜索xirufeng在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xirufeng的博客3
    发贴心情 
    结果就是只能保存一次的结果
    D:\make>javac createInstance.java

    D:\make>java createInstance
    Enter your resource:钢笔
    input individualName:
    英雄牌
    the input individualName is:英雄牌
    the property:discounts value=5.2
    5.2
    the property:usertype value=买

    the property:pen_color value=红

    the property:price value=25
    25
    the property:commodityname value=英雄牌钢笔
    英雄牌钢笔
    http://www.owl-ontologies.com/unnamed.owl#英雄牌

    import java.util.Vector;
    import com.hp.hpl.jena.rdf.model.*;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.*;
    import com.hp.hpl.jena.ontology.*;
    import com.hp.hpl.jena.reasoner.*;
    import com.hp.hpl.jena.vocabulary.*;
    import com.hp.hpl.jena.reasoner.rulesys.*;
    import com.hp.hpl.jena.util.*;
    import com.hp.hpl.jena.rdf.model.Resource;
    import com.hp.hpl.jena.ontology.OntProperty;
    import com.hp.hpl.jena.util.iterator.ExtendedIterator;
    import com.hp.hpl.jena.ontology.OntProperty;
    import com.hp.hpl.jena.ontology.OntResource;


    import java.util.*;
    import java.lang.*;
    import java.io.*;
    import java.util.Hashtable;
    import java.util.List;
    import java.util.ArrayList;
    import java.io.DataInputStream;

    public class createInstance
    {
      public void instance()throws IOException
      {
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader input = new BufferedReader(reader);
        System.out.print("Enter your resource:");
        String res = input.readLine();
        OntModel model = ModelFactory.createOntologyModel();
        OntDocumentManager dm = model.getDocumentManager();
        dm.addAltEntry("http://protege.stanford.edu/plugins/owl/protege",
                       "file:/wyh/seller.owl");
        model.read("http://protege.stanford.edu/plugins/owl/protege");
        String sellerNS = "http://www.owl-ontologies.com/unnamed.owl#";
        OntClass resource = model.getOntClass(sellerNS + res);
        System.out.println("input individualName:");
        String individualName=input.readLine();
        System.out.println("the input individualName is:"+individualName);
        Individual createIndividual=model.createIndividual(sellerNS+individualName,resource);
        ExtendedIterator propertyIterator=resource.listDeclaredProperties();
        while(propertyIterator.hasNext())
        {
          OntProperty ontproperty1=model.getOntProperty(propertyIterator.next().toString());
          if(ontproperty1.isDatatypeProperty())
          {
            System.out.print("the property:"+ontproperty1.getLocalName()+" value=");
            String propertyValue=input.readLine();
            createIndividual.addProperty(ontproperty1,propertyValue);
            System.out.println(propertyValue);
          }
          else if(ontproperty1.isObjectProperty())
          {
            ExtendedIterator toClassProperty=null;
            OntClass toClass=null;
            if(ontproperty1.getLocalName().equals("book_author"))
            {
              toClass=model.getOntClass(sellerNS+"person");
              toClassProperty=toClass.listDeclaredProperties();
              while(toClassProperty.hasNext())
              {
              OntProperty ontproperty2=model.getOntProperty(
                                         toClassProperty.next().toString());
              System.out.print("the property:"+ontproperty2.getLocalName()+" value=");
              String propertyValue=input.readLine();
              createIndividual.addProperty(ontproperty2,propertyValue);
              System.out.println(propertyValue);
              }

            }
            if(ontproperty1.getLocalName().equals("user"))
            {
              toClass=model.getOntClass(sellerNS+"person");
              toClassProperty=toClass.listDeclaredProperties();
              while(toClassProperty.hasNext())
              {
              OntProperty ontproperty2=model.getOntProperty(
                                         toClassProperty.next().toString());
              System.out.print("the property:"+ontproperty2.getLocalName()+" value=");
              String propertyValue=input.readLine();
              createIndividual.addProperty(ontproperty2,propertyValue);
              System.out.println(propertyValue);
              }

            }
            else if(ontproperty1.getLocalName().equals("book_keywords"))
            {
              toClass=model.getOntClass(sellerNS+"book_content");
              toClassProperty=toClass.listDeclaredProperties();
              while(toClassProperty.hasNext())
              {
              OntProperty ontproperty2=model.getOntProperty(
                                         toClassProperty.next().toString());
              System.out.print("the property:"+ontproperty2.getLocalName()+" value=");
              String propertyValue=input.readLine();
              createIndividual.addProperty(ontproperty2,propertyValue);
              System.out.println(propertyValue);
              }

            }
            else if(ontproperty1.getLocalName().equals("book_layout_rectangle"))
            {
              toClass=model.getOntClass(sellerNS+"book_rectangle_info");
              toClassProperty=toClass.listDeclaredProperties();
              while(toClassProperty.hasNext())
              {
              OntProperty ontproperty2=model.getOntProperty(
                                         toClassProperty.next().toString());
              System.out.print("the property:"+ontproperty2.getLocalName()+" value=");
              String propertyValue=input.readLine();
              createIndividual.addProperty(ontproperty2,propertyValue);
              System.out.println(propertyValue);
              }

            }
    /*       toClassProperty=toClass.listDeclaredProperties();
            while(toClassProperty.hasNext())
            {
              OntProperty ontproperty2=model.getOntProperty(
                                         toClassProperty.next().toString());
              System.out.println("the property:"+ontproperty2.getLocalName()+"value=");
              String propertyValue=input.readLine();
              System.out.println(propertyValue);
            }*/
          }
        }
        Iterator listIndivi=resource.listInstances();
        while(listIndivi.hasNext())
        {
          System.out.println(listIndivi.next());
        }
      }
      public static void main(String[] arags)throws IOException
      {
        createInstance creInstance=new createInstance();
        creInstance.instance();
      }
    }

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/20 10:55:00
     
     xirufeng 美女呀,离线,快来找我吧!
      
      
      等级:大二期末(Java考了96分!)
      文章:77
      积分:322
      门派:XML.ORG.CN
      注册:2005/12/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xirufeng发送一个短消息 把xirufeng加入好友 查看xirufeng的个人资料 搜索xirufeng在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xirufeng的博客4
    发贴心情 
    下次再这样创建一个之后,上次的http://www.owl-ontologies.com/unnamed.owl#英雄牌 就不显示了
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/20 10:58:00
     
     jpz6311whu 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      等级:研三(收到微软亚洲研究院的Offer了)(版主)
      文章:1718
      积分:10610
      门派:W3CHINA.ORG
      注册:2005/4/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jpz6311whu发送一个短消息 把jpz6311whu加入好友 查看jpz6311whu的个人资料 搜索jpz6311whu在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jpz6311whu的博客5
    发贴心情 
    你在程序中创建新的本体实例,并对实例添加属性。
    但是这些操作都是在内存中进行的,如果要把数据持久化,还需要把本体库写回到文件中:
    Writing RDF
    Jena has methods for reading and writing RDF as XML. These can be used to save an RDF model to a file and later read it back in again.

    Tutorial 3 created a model and wrote it out in triple form. Tutorial 4 modifies tutorial 3 to write the model in RDF XML form to the standard output stream. The code again, is very simple: model.write can take an OutputStream argument.

    // now write the model in XML form to a file
    model.write(System.out);The output should look something like this:

    <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#'
    >
      <rdf:Description rdf:about='http://somewhere/JohnSmith'>
        <vcard:FN>John Smith</vcard:FN>
        <vcard:N rdf:nodeID="A0"/>
      </rdf:Description>
      <rdf:Description rdf:nodeID="A0">
        <vcard:Given>John</vcard:Given>
        <vcard:Family>Smith</vcard:Family>
      </rdf:Description>
    </rdf:RDF>
    The RDF specifications specify how to represent RDF as XML. The RDF XML syntax is quite complex. The reader is referred to the primer being developed by the RDFCore WG for a more detailed introduction. However, let's take a quick look at how to interpret the above.

    RDF is usually embedded in an <rdf:RDF> element. The element is optional if there are other ways of know that some XML is RDF, but it is usually present. The RDF element defines the two namespaces used in the document. There is then an <rdf:Description> element which describes the resource whose URI is "http://somewhere/JohnSmith". If the rdf:about attribute was missing, this element would represent a blank node.

    The <vcard:FN> element describes a property of the resource. The property name is the "FN" in the vcard namespace. RDF converts this to a URI reference by concatenating the URI reference for the namespace prefix and "FN", the local name part of the name. This gives a URI reference of "http://www.w3.org/2001/vcard-rdf/3.0#FN". The value of the property is the literal "John Smith".

    The <vcard:N> element is a resource. In this case the resource is represented by a relative URI reference. RDF converts this to an absolute URI reference by concatenating it with the base URI of the current document.

    There is an error in this RDF XML; it does not exactly represent the Model we created. The blank node in the Model has been given a URI reference. It is no longer blank. The RDF/XML syntax is not capable of representing all RDF Models; for example it cannot represent a blank node which is the object of two statements. The 'dumb' writer we used to write this RDF/XML makes no attempt to write correctly the subset of Models which can be written correctly. It gives a URI to each blank node, making it no longer blank.

    Jena has an extensible interface which allows new writers for different serialization languages for RDF to be easily plugged in. The above call invoked the standard 'dumb' writer. Jena also includes a more sophisticated RDF/XML writer which can be invoked by specifying another argument to the write() method call:

    // now write the model in XML form to a file
    model.write(System.out, "RDF/XML-ABBREV");
    This writer, the so called PrettyWriter, takes advantage of features of the RDF/XML abbreviated syntax to write a Model more compactly. It is also able to preserve blank nodes where that is possible. It is however, not suitable for writing very large Models, as its performance is unlikely to be acceptable. To write large files and preserve blank nodes, write in N-Triples format:

    // now write the model in XML form to a file
    model.write(System.out, "N-TRIPLE");
      This will produce output similar to that of tutorial 3 which conforms to the N-Triples specification.

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/20 11:21:00
     
     xirufeng 美女呀,离线,快来找我吧!
      
      
      等级:大二期末(Java考了96分!)
      文章:77
      积分:322
      门派:XML.ORG.CN
      注册:2005/12/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xirufeng发送一个短消息 把xirufeng加入好友 查看xirufeng的个人资料 搜索xirufeng在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xirufeng的博客6
    发贴心情 
    非常感谢!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/20 15:23:00
     
     xirufeng 美女呀,离线,快来找我吧!
      
      
      等级:大二期末(Java考了96分!)
      文章:77
      积分:322
      门派:XML.ORG.CN
      注册:2005/12/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xirufeng发送一个短消息 把xirufeng加入好友 查看xirufeng的个人资料 搜索xirufeng在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xirufeng的博客7
    发贴心情 
    我按照上边介绍的做了,还是一样,真不明白是怎么回事

    以下是引用jpz6311whu在2006-3-20 11:21:00的发言:
    你在程序中创建新的本体实例,并对实例添加属性。
    但是这些操作都是在内存中进行的,如果要把数据持久化,还需要把本体库写回到文件中:
    Writing RDF
    Jena has methods for reading and writing RDF as XML. These can be used to save an RDF model to a file and later read it back in again.

    Tutorial 3 created a model and wrote it out in triple form. Tutorial 4 modifies tutorial 3 to write the model in RDF XML form to the standard output stream. The code again, is very simple: model.write can take an OutputStream argument.

    // now write the model in XML form to a file
    model.write(System.out);The output should look something like this:

    <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#'
      >
       <rdf:Description rdf:about='http://somewhere/JohnSmith'>
         <vcard:FN>John Smith</vcard:FN>
         <vcard:N rdf:nodeID="A0"/>
       </rdf:Description>
       <rdf:Description rdf:nodeID="A0">
         <vcard:Given>John</vcard:Given>
         <vcard:Family>Smith</vcard:Family>
       </rdf:Description>
    </rdf:RDF>
    The RDF specifications specify how to represent RDF as XML. The RDF XML syntax is quite complex. The reader is referred to the primer being developed by the RDFCore WG for a more detailed introduction. However, let's take a quick look at how to interpret the above.

    RDF is usually embedded in an <rdf:RDF> element. The element is optional if there are other ways of know that some XML is RDF, but it is usually present. The RDF element defines the two namespaces used in the document. There is then an <rdf:Description> element which describes the resource whose URI is "http://somewhere/JohnSmith". If the rdf:about attribute was missing, this element would represent a blank node.

    The <vcard:FN> element describes a property of the resource. The property name is the "FN" in the vcard namespace. RDF converts this to a URI reference by concatenating the URI reference for the namespace prefix and "FN", the local name part of the name. This gives a URI reference of "http://www.w3.org/2001/vcard-rdf/3.0#FN". The value of the property is the literal "John Smith".

    The <vcard:N> element is a resource. In this case the resource is represented by a relative URI reference. RDF converts this to an absolute URI reference by concatenating it with the base URI of the current document.

    There is an error in this RDF XML; it does not exactly represent the Model we created. The blank node in the Model has been given a URI reference. It is no longer blank. The RDF/XML syntax is not capable of representing all RDF Models; for example it cannot represent a blank node which is the object of two statements. The 'dumb' writer we used to write this RDF/XML makes no attempt to write correctly the subset of Models which can be written correctly. It gives a URI to each blank node, making it no longer blank.

    Jena has an extensible interface which allows new writers for different serialization languages for RDF to be easily plugged in. The above call invoked the standard 'dumb' writer. Jena also includes a more sophisticated RDF/XML writer which can be invoked by specifying another argument to the write() method call:

    // now write the model in XML form to a file
    model.write(System.out, "RDF/XML-ABBREV");
      This writer, the so called PrettyWriter, takes advantage of features of the RDF/XML abbreviated syntax to write a Model more compactly. It is also able to preserve blank nodes where that is possible. It is however, not suitable for writing very large Models, as its performance is unlikely to be acceptable. To write large files and preserve blank nodes, write in N-Triples format:

    // now write the model in XML form to a file
    model.write(System.out, "N-TRIPLE");
       This will produce output similar to that of tutorial 3 which conforms to the N-Triples specification.



    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/22 9:38:00
     
     jpz6311whu 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      等级:研三(收到微软亚洲研究院的Offer了)(版主)
      文章:1718
      积分:10610
      门派:W3CHINA.ORG
      注册:2005/4/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jpz6311whu发送一个短消息 把jpz6311whu加入好友 查看jpz6311whu的个人资料 搜索jpz6311whu在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jpz6311whu的博客8
    发贴心情 
    你把完整的程序源代码和完整的owl文件贴出来,并说明运行结果。
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/22 10:12:00
     
     xirufeng 美女呀,离线,快来找我吧!
      
      
      等级:大二期末(Java考了96分!)
      文章:77
      积分:322
      门派:XML.ORG.CN
      注册:2005/12/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给xirufeng发送一个短消息 把xirufeng加入好友 查看xirufeng的个人资料 搜索xirufeng在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看xirufeng的博客9
    发贴心情 
    方法一:下边红色字体另外加的,这样的话,还是只能保存一次。
    方法二:如果自己定义outputstream的话,能编译,但不能运行。定义如下:
              File file=new File("d:/wyh/seller.owl");
              FileOutputStream out=new FileOutputStream(file);
              model.write(out);   
    异常如下:
    Exception in thread "main" com.hp.hpl.jena.rdf.arp.ParseException: {E301} Premat
    ure end of file.
            at com.hp.hpl.jena.rdf.arp.ARPFilter.parseWarning(ARPFilter.java:559)
            at com.hp.hpl.jena.rdf.arp.ARPFilter.parseWarning(ARPFilter.java:552)
            at com.hp.hpl.jena.rdf.arp.ParserSupport.saxException(ParserSupport.java
    :179)
            at com.hp.hpl.jena.rdf.arp.RDFParser.saxEx(RDFParser.java:260)
            at com.hp.hpl.jena.rdf.arp.RDFParser.oneWhite(RDFParser.java:248)
            at com.hp.hpl.jena.rdf.arp.RDFParser.whiteOrErr(RDFParser.java:196)
            at com.hp.hpl.jena.rdf.arp.RDFParser.rdfFile(RDFParser.java:98)
            at com.hp.hpl.jena.rdf.arp.ARPFilter.parse(ARPFilter.java:314)
            at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:221)
            at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:209)
            at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:261)
            at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:206)
            at com.hp.hpl.jena.ontology.impl.OntModelImpl.read(OntModelImpl.java:189
    4)
            at com.hp.hpl.jena.ontology.impl.OntModelImpl.read(OntModelImpl.java:184
    6)
            at createInstance.instance(createInstance.java:41)
            at createInstance.main(createInstance.java:144)
    com.hp.hpl.jena.shared.JenaException: rethrew: {E301} Premature end of file.
            at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:226)
            at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:209)
            at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:261)
            at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:206)
            at com.hp.hpl.jena.ontology.impl.OntModelImpl.read(OntModelImpl.java:189
    4)
            at com.hp.hpl.jena.ontology.impl.OntModelImpl.read(OntModelImpl.java:184
    6)
            at createInstance.instance(createInstance.java:41)
            at createInstance.main(createInstance.java:144)
    Caused by: com.hp.hpl.jena.rdf.arp.ParseException: {E301} Premature end of file.

            at com.hp.hpl.jena.rdf.arp.ARPFilter.parseWarning(ARPFilter.java:559)
            at com.hp.hpl.jena.rdf.arp.ARPFilter.parseWarning(ARPFilter.java:552)
            at com.hp.hpl.jena.rdf.arp.ParserSupport.saxException(ParserSupport.java
    :179)
            at com.hp.hpl.jena.rdf.arp.RDFParser.saxEx(RDFParser.java:260)
            at com.hp.hpl.jena.rdf.arp.RDFParser.oneWhite(RDFParser.java:248)
            at com.hp.hpl.jena.rdf.arp.RDFParser.whiteOrErr(RDFParser.java:196)
            at com.hp.hpl.jena.rdf.arp.RDFParser.rdfFile(RDFParser.java:98)
            at com.hp.hpl.jena.rdf.arp.ARPFilter.parse(ARPFilter.java:314)
            at com.hp.hpl.jena.rdf.arp.JenaReader.read(JenaReader.java:221)
            ... 7 more
    以下是引用xirufeng在2006-3-20 10:55:00的发言:
    结果就是只能保存一次的结果
    D:\make>javac createInstance.java

    D:\make>java createInstance
    Enter your resource:钢笔
    input individualName:
    英雄牌
    the input individualName is:英雄牌
    the property:discounts value=5.2
    5.2
    the property:usertype value=买

    the property:pen_color value=红

    the property:price value=25
    25
    the property:commodityname value=英雄牌钢笔
    英雄牌钢笔
    http://www.owl-ontologies.com/unnamed.owl#英雄牌

    import java.util.Vector;
    import com.hp.hpl.jena.rdf.model.*;
    import com.hp.hpl.jena.rdf.model.ModelFactory;
    import com.hp.hpl.jena.rdf.*;
    import com.hp.hpl.jena.ontology.*;
    import com.hp.hpl.jena.reasoner.*;
    import com.hp.hpl.jena.vocabulary.*;
    import com.hp.hpl.jena.reasoner.rulesys.*;
    import com.hp.hpl.jena.util.*;
    import com.hp.hpl.jena.rdf.model.Resource;
    import com.hp.hpl.jena.ontology.OntProperty;
    import com.hp.hpl.jena.util.iterator.ExtendedIterator;
    import com.hp.hpl.jena.ontology.OntProperty;
    import com.hp.hpl.jena.ontology.OntResource;


    import java.util.*;
    import java.lang.*;
    import java.io.*;
    import java.util.Hashtable;
    import java.util.List;
    import java.util.ArrayList;
    import java.io.DataInputStream;

    public class createInstance
    {
       public void instance()throws IOException
       {
         InputStreamReader reader = new InputStreamReader(System.in);
         BufferedReader input = new BufferedReader(reader);
         System.out.print("Enter your resource:");
         String res = input.readLine();
         OntModel model = ModelFactory.createOntologyModel();
         OntDocumentManager dm = model.getDocumentManager();
         dm.addAltEntry("http://protege.stanford.edu/plugins/owl/protege",
                        "file:/wyh/seller.owl");
         model.read("http://protege.stanford.edu/plugins/owl/protege");
         String sellerNS = "http://www.owl-ontologies.com/unnamed.owl#";
         OntClass resource = model.getOntClass(sellerNS + res);
         System.out.println("input individualName:");
         String individualName=input.readLine();
         System.out.println("the input individualName is:"+individualName);
         Individual createIndividual=model.createIndividual(sellerNS+individualName,resource);
         ExtendedIterator propertyIterator=resource.listDeclaredProperties();
         while(propertyIterator.hasNext())
         {
           OntProperty ontproperty1=model.getOntProperty(propertyIterator.next().toString());
           if(ontproperty1.isDatatypeProperty())
           {
             System.out.print("the property:"+ontproperty1.getLocalName()+" value=");
             String propertyValue=input.readLine();
             createIndividual.addProperty(ontproperty1,propertyValue);
             System.out.println(propertyValue);
           }
           else if(ontproperty1.isObjectProperty())
           {
             ExtendedIterator toClassProperty=null;
             OntClass toClass=null;
             if(ontproperty1.getLocalName().equals("book_author"))
             {
               toClass=model.getOntClass(sellerNS+"person");
               toClassProperty=toClass.listDeclaredProperties();
               while(toClassProperty.hasNext())
               {
               OntProperty ontproperty2=model.getOntProperty(
                                          toClassProperty.next().toString());
               System.out.print("the property:"+ontproperty2.getLocalName()+" value=");
               String propertyValue=input.readLine();
               createIndividual.addProperty(ontproperty2,propertyValue);
               System.out.println(propertyValue);
               }

             }
             if(ontproperty1.getLocalName().equals("user"))
             {
               toClass=model.getOntClass(sellerNS+"person");
               toClassProperty=toClass.listDeclaredProperties();
               while(toClassProperty.hasNext())
               {
               OntProperty ontproperty2=model.getOntProperty(
                                          toClassProperty.next().toString());
               System.out.print("the property:"+ontproperty2.getLocalName()+" value=");
               String propertyValue=input.readLine();
               createIndividual.addProperty(ontproperty2,propertyValue);
               System.out.println(propertyValue);
               }

             }
             else if(ontproperty1.getLocalName().equals("book_keywords"))
             {
               toClass=model.getOntClass(sellerNS+"book_content");
               toClassProperty=toClass.listDeclaredProperties();
               while(toClassProperty.hasNext())
               {
               OntProperty ontproperty2=model.getOntProperty(
                                          toClassProperty.next().toString());
               System.out.print("the property:"+ontproperty2.getLocalName()+" value=");
               String propertyValue=input.readLine();
               createIndividual.addProperty(ontproperty2,propertyValue);
               System.out.println(propertyValue);
               }

             }
             else if(ontproperty1.getLocalName().equals("book_layout_rectangle"))
             {
               toClass=model.getOntClass(sellerNS+"book_rectangle_info");
               toClassProperty=toClass.listDeclaredProperties();
               while(toClassProperty.hasNext())
               {
               OntProperty ontproperty2=model.getOntProperty(
                                          toClassProperty.next().toString());
               System.out.print("the property:"+ontproperty2.getLocalName()+" value=");
               String propertyValue=input.readLine();
               createIndividual.addProperty(ontproperty2,propertyValue);
               System.out.println(propertyValue);
               }

             }
      /*       toClassProperty=toClass.listDeclaredProperties();
             while(toClassProperty.hasNext())
             {
               OntProperty ontproperty2=model.getOntProperty(
                                          toClassProperty.next().toString());
               System.out.println("the property:"+ontproperty2.getLocalName()+"value=");
               String propertyValue=input.readLine();
               System.out.println(propertyValue);
             }*/
           }
         }
         model.write(System.out);
         Iterator listIndivi=resource.listInstances();
         while(listIndivi.hasNext())
         {
           System.out.println(listIndivi.next());
         }
       }
       public static void main(String[] arags)throws IOException
       {
         createInstance creInstance=new createInstance();
         creInstance.instance();
       }
    }



    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/23 8:40:00
     
     jpz6311whu 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      等级:研三(收到微软亚洲研究院的Offer了)(版主)
      文章:1718
      积分:10610
      门派:W3CHINA.ORG
      注册:2005/4/12

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jpz6311whu发送一个短消息 把jpz6311whu加入好友 查看jpz6311whu的个人资料 搜索jpz6311whu在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jpz6311whu的博客10
    发贴心情 
    你看看报错的地方:
    at createInstance.instance(createInstance.java:41)
    并不是write出错了,是在这个之前的read就有错
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/3/23 14:18:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Semantic Web(语义Web)/描述逻辑/本体 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/10/7 14:10:35

    本主题贴数15,分页: [1] [2]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    2,984.375ms