以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Semantic Web(语义Web)/描述逻辑/本体 』  (http://bbs.xml.org.cn/list.asp?boardid=2)
----  jena规则推理时遇到的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=2&rootid=&id=65376)


--  作者:hrballen
--  发布时间:8/4/2008 8:18:00 PM

--  jena规则推理时遇到的问题
我是一个初学者,在进行jena规则推理时,遇到些问题,就是推理时规则的编写,还有推理的步骤。请高手指点一下,下面代码哪里有问题?在编写规则时,rule1:(?x http://www.owl-ontologies.com/Animal.owl#111111?y,其中的111111指的是在编写本体时的ObjectPropertyma吗?在输出查询结果时,
Zoo | Herbivore 下面是空的,不知他应该是实例,还是什么?请高手细致地讲一下

import java.util.Iterator;          

import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.db.IDBConnection;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
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.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;

public class  sparqltuili{
public static final String strDriver = "com.mysql.jdbc.Driver"; // path of driver class
     public static final String strURL = "jdbc:mysql://localhost/protege_db"; // URL of database
     public static final String strUser = "root"; // database user id
     public static final String strPassWord = ""; // database password
     public static final String strDB =  "MySQL"; // database type
     public static void main(String[] args){
      
        try{
            // 创建一个数据库连接
              IDBConnection con = new DBConnection ( strURL, strUser, strPassWord, strDB );
            
              // 加载数据库驱动类,需要处理异常
              try{
               System.out.println(strDriver);
                   Class.forName(strDriver);
              }catch(ClassNotFoundException e) {
                   System.out.println("ClassNotFoundException, Driver is not available...");
              }
              String filePath = "file:C:\\Program Files\\Protege_3.1\\Animal.owl";
              
       sparqltuili.createDBModelFromFile(con, "1protege_db",filePath);            
    OntModel model = sparqltuili.getModelFromDB(con, "1protege_db");
    sparqltuili.simpleReadOntology(model);
    
    
      String rule = "[rule1:(?x http://www.owl-ontologies.com/Animal.owl#Belonging ?y) " +
     
       "->(?y http://www.owl-ontologies.com/Animal.owl#Belonged ?x)]";
     
   /*查询语句*/
    String queryString = "PREFIX Animal:<http://www.owl-ontologies.com/Animal.owl#> " +
   "SELECT ?Zoo ?Herbivore " +
   "WHERE { ?Zoo Animal:Belonged ?Herbivore} ";
    
     /*创建推理机*/
     Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule));
  
     InfModel inf = ModelFactory.createInfModel(reasoner2, model);
     Query query = QueryFactory.create(queryString);
     
     QueryExecution qe = QueryExecutionFactory.create(query, inf);
     ResultSet results = qe.execSelect();
     /*打印结果*/
     ResultSetFormatter.out(System.out, results, query);
     qe.close();
     }catch(Exception e){e.printStackTrace();}}
    
     /* 从文件读取本体并将其存入数据库 */
    public static OntModel createDBModelFromFile(IDBConnection con, String name,
             String filePath) {
              ModelMaker maker = ModelFactory.createModelRDBMaker(con);
              Model base = maker.createModel(name);
              OntModel newmodel = ModelFactory.createOntologyModel(
         getModelSpec(maker), base);
              newmodel.read(filePath);
              return newmodel;
         }
     /* 从数据库中得到已存入本体 */
   public static OntModel getModelFromDB(IDBConnection con, String name) {
         ModelMaker maker = ModelFactory.createModelRDBMaker(con);
         Model base = maker.getModel(name);
         OntModel newmodel = ModelFactory.createOntologyModel(
                  getModelSpec(maker), base);
         return newmodel;
          }
   public static OntModelSpec getModelSpec(ModelMaker maker) {
      OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
      spec.setImportModelMaker(maker);
      return spec;
}
    /* 简单读取本体中的各个class */
   public static void simpleReadOntology(OntModel model) {
      for (Iterator i = model.listClasses(); i.hasNext();) {
          OntClass c = (OntClass) i.next();
          System.out.println(c.getLocalName());
      }
  }


--  作者:aximou
--  发布时间:8/6/2008 3:45:00 PM

--  
本体文件一并也贴上来吧!
--  作者:hrballen
--  发布时间:8/28/2008 7:22:00 PM

--  
<?xml version="1.0"?>
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns="http://www.owl-ontologies.com/unnamed.owl#"
  xml:base="http://www.owl-ontologies.com/unnamed.owl">
  <owl:Ontology rdf:about=""/>
  <owl:Class rdf:ID="Animal">
    <owl:disjointWith>
      <owl:Class rdf:ID="Plant"/>
    </owl:disjointWith>
  </owl:Class>
  <owl:Class rdf:ID="Herbivore">
    <rdfs:subClassOf rdf:resource="#Animal"/>
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:allValuesFrom>
          <owl:Class rdf:about="#Plant"/>
        </owl:allValuesFrom>
        <owl:onProperty>
          <owl:ObjectProperty rdf:ID="eat"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:Class rdf:ID="小样">
    <rdfs:subClassOf rdf:resource="#Herbivore"/>
  </owl:Class>
  <owl:Class rdf:about="#Plant">
    <owl:disjointWith rdf:resource="#Animal"/>
  </owl:Class>
  <owl:Class rdf:ID="Leaf">
    <rdfs:subClassOf rdf:resource="#Plant"/>
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:allValuesFrom>
          <owl:Class rdf:ID="Branch"/>
        </owl:allValuesFrom>
        <owl:onProperty>
          <owl:TransitiveProperty rdf:ID="is_part_of"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:Class rdf:ID="Giaffe">
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="#eat"/>
        </owl:onProperty>
        <owl:allValuesFrom rdf:resource="#Leaf"/>
      </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf rdf:resource="#Herbivore"/>
  </owl:Class>
  <owl:Class rdf:about="#Branch">
    <rdfs:subClassOf rdf:resource="#Plant"/>
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:onProperty>
          <owl:TransitiveProperty rdf:about="#is_part_of"/>
        </owl:onProperty>
        <owl:allValuesFrom>
          <owl:Class rdf:ID="Tree"/>
        </owl:allValuesFrom>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:Class rdf:ID="Zoo"/>
  <owl:Class rdf:ID="Lion">
    <rdfs:subClassOf>
      <owl:Class rdf:ID="Carnivore"/>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
      <owl:Restriction>
        <owl:allValuesFrom rdf:resource="#Herbivore"/>
        <owl:onProperty>
          <owl:ObjectProperty rdf:about="#eat"/>
        </owl:onProperty>
      </owl:Restriction>
    </rdfs:subClassOf>
  </owl:Class>
  <owl:Class rdf:about="#Carnivore">
    <owl:equivalentClass>
      <owl:Class>
        <owl:intersectionOf rdf:parseType="Collection">
          <owl:Restriction>
            <owl:allValuesFrom rdf:resource="#Animal"/>
            <owl:onProperty>
              <owl:ObjectProperty rdf:ID="maineat"/>
            </owl:onProperty>
          </owl:Restriction>
          <owl:Class rdf:about="#Animal"/>
        </owl:intersectionOf>
      </owl:Class>
    </owl:equivalentClass>
  </owl:Class>
  <owl:Class rdf:about="#Tree">
    <rdfs:subClassOf rdf:resource="#Plant"/>
  </owl:Class>
  <owl:ObjectProperty rdf:ID="eated">
    <owl:inverseOf>
      <owl:ObjectProperty rdf:about="#eat"/>
    </owl:inverseOf>
  </owl:ObjectProperty>
  <owl:ObjectProperty rdf:ID="Belonging">
    <rdfs:range rdf:resource="#Zoo"/>
    <rdfs:domain rdf:resource="#Animal"/>
    <rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
    >属于</rdfs:comment>
  </owl:ObjectProperty>
  <owl:ObjectProperty rdf:about="#maineat">
    <rdfs:subPropertyOf>
      <owl:ObjectProperty rdf:about="#eat"/>
    </rdfs:subPropertyOf>
    <owl:inverseOf>
      <owl:ObjectProperty rdf:ID="inverse_of_eat_14"/>
    </owl:inverseOf>
  </owl:ObjectProperty>
  <owl:ObjectProperty rdf:about="#eat">
    <owl:inverseOf rdf:resource="#eated"/>
  </owl:ObjectProperty>
  <owl:ObjectProperty rdf:about="#inverse_of_eat_14">
    <rdfs:subPropertyOf rdf:resource="#eated"/>
    <owl:inverseOf rdf:resource="#maineat"/>
  </owl:ObjectProperty>
  <owl:DatatypeProperty rdf:ID="Tel">
    <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <rdfs:domain rdf:resource="#Zoo"/>
    <rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
    >电话号码</rdfs:comment>
  </owl:DatatypeProperty>
  <owl:DatatypeProperty rdf:ID="Address">
    <rdfs:domain rdf:resource="#Zoo"/>
    <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <rdfs:comment rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
    >地址属性</rdfs:comment>
  </owl:DatatypeProperty>
  <owl:TransitiveProperty rdf:about="#is_part_of">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ObjectProperty"/>
  </owl:TransitiveProperty>
  <Lion rdf:ID="ShangLion">
    <eat>
      <Giaffe rdf:ID="Heihei">
        <eated rdf:resource="#ShangLion"/>
        <Belonging>
          <Zoo rdf:ID="Shanghai_Zoo">
            <Tel xml:lang="en">011-12345678</Tel>
            <Address xml:lang="en">Shanghai road</Address>
          </Zoo>
        </Belonging>
      </Giaffe>
    </eat>
    <Belonging rdf:resource="#Shanghai_Zoo"/>
  </Lion>
  <Lion rdf:ID="BeiLion">
    <Belonging>
      <Zoo rdf:ID="Beijing_Zoo">
        <Tel xml:lang="en">010-12345678</Tel>
        <Address xml:lang="en">Beijing Xizhimen Road</Address>
      </Zoo>
    </Belonging>
  </Lion>
  <Lion rdf:ID="TianLion">
    <Belonging>
      <Zoo rdf:ID="Tianjin_Zoo">
        <Address xml:lang="en">Tianjin road</Address>
        <Tel xml:lang="en">012-12345678</Tel>
      </Zoo>
    </Belonging>
  </Lion>
  <Branch rdf:ID="Branch_1"/>
  <Leaf rdf:ID="Leaf_2"/>
  <Tree rdf:ID="Tree_3"/>
  <Giaffe rdf:ID="Baibai">
    <Belonging rdf:resource="#Tianjin_Zoo"/>
  </Giaffe>
  <Giaffe rdf:ID="Lanlan">
    <Belonging rdf:resource="#Beijing_Zoo"/>
  </Giaffe>
</rdf:RDF>

<!-- Created with Protege (with OWL Plugin 2.1, Build 284)  http://protege.stanford.edu -->


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms