以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 Semantic Web(语义Web)/描述逻辑/本体 』  (http://bbs.xml.org.cn/list.asp?boardid=2)
----  忠心感谢,jena 推论一问 thanks  (http://bbs.xml.org.cn/dispbbs.asp?boardid=2&rootid=&id=44678)


--  作者:jiashin
--  发布时间:3/31/2007 12:29:00 PM

--  忠心感谢,jena 推论一问 thanks
hello,大家好

Q1:请问一下如何在jena做推理(有连接DIG)的动作, 像建立狮子类(Lion)和肉食动物类(Carnivore), 通过推理得到狮子是肉食动物, 我们在protege里定义Carnivore的Asserted Conditions 在 Necessary & sufficient 里定义allValuesFrom eat Animal,当按下Classify taxonomy就可以做正确推理, 问题是该怎么在jena设定这些动作。

Q2:还有需不需要写rule等等的, 定义rule有等于在protege定义Asserted Conditions吗。

麻烦各位帮忙,谢谢指导。

ps.有装好RACER


--  作者:jpz6311whu
--  发布时间:3/31/2007 1:04:00 PM

--  
请参看Jena doc中的:
Jena 2 Inference support
--  作者:jiashin
--  发布时间:3/31/2007 1:24:00 PM

--  
谢谢你回答

所以在jena中也可以做到在protege 里的Classify taxonomy动作吗。

那请问一下Q2麻烦解惑,谢谢


--  作者:jpz6311whu
--  发布时间:3/31/2007 2:43:00 PM

--  
可以啊
--  作者:jpz6311whu
--  发布时间:3/31/2007 2:45:00 PM

--  
jena支持好种推理机,有的要写rule,有的不用写,都能实现你说的
你看Jena 2 Inference support这个就知道了。。
--  作者:jiashin
--  发布时间:3/31/2007 3:55:00 PM

--  
再次谢谢您喔
你好人做到底,麻烦您给我一个例子,让我有想像的空间一下,谢谢

定义Carnivore 在 Necessary & sufficient 里定义allValuesFrom eat Animal,
Necessary & sufficient 该如何表示呢


--  作者:jpz6311whu
--  发布时间:3/31/2007 8:09:00 PM

--  
如果要举例子的话,请先把你说的这个例子的owl源代码贴出来看看:
Q1:请问一下如何在jena做推理(有连接DIG)的动作, 像建立狮子类(Lion)和肉食动物类(Carnivore), 通过推理得到狮子是肉食动物, 我们在protege里定义Carnivore的Asserted Conditions 在 Necessary & sufficient 里定义allValuesFrom eat Animal,当按下Classify taxonomy就可以做正确推理, 问题是该怎么在jena设定这些动作。
--  作者:jiashin
--  发布时间:3/31/2007 10:57:00 PM

--  
麻烦您了,感谢您。

<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>


<rdf:RDF xmlns="http://www.owl-ontologies.com/unnamed.owl#"
     xml:base="http://www.owl-ontologies.com/unnamed.owl"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about=""/>
    <owl:Class rdf:ID="Animal">
        <owl:disjointWith rdf:resource="#Plant"/>
    </owl:Class>
    <owl:Class rdf:ID="Branch">
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="#is_part_of"/>
                <owl:allValuesFrom rdf:resource="#Tree"/>
            </owl:Restriction>
        </rdfs:subClassOf>
        <rdfs:subClassOf rdf:resource="#Plant"/>
        <rdfs:comment rdf:datatype="&xsd;string">樹枝</rdfs:comment>
    </owl:Class>
    <owl:Class rdf:ID="Carnivore">
        <owl:equivalentClass>
            <owl:Class>
                <owl:intersectionOf rdf:parseType="Collection">
                    <owl:Class rdf:about="#Animal"/>
                    <owl:Restriction>
                        <owl:onProperty rdf:resource="#maineat"/>
                        <owl:allValuesFrom rdf:resource="#Herbivore"/>
                    </owl:Restriction>
                </owl:intersectionOf>
            </owl:Class>
        </owl:equivalentClass>
    </owl:Class>
    <owl:ObjectProperty rdf:ID="eat">
        <owl:inverseOf rdf:resource="#eated"/>
        <rdfs:comment rdf:datatype="&xsd;string"></rdfs:comment>
    </owl:ObjectProperty>
    <owl:ObjectProperty rdf:ID="eated">
        <owl:inverseOf rdf:resource="#eat"/>
        <rdfs:comment rdf:datatype="&xsd;string"></rdfs:comment>
    </owl:ObjectProperty>
    <owl:Class rdf:ID="Giraffe">
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="#eat"/>
                <owl:allValuesFrom rdf:resource="#Leaf"/>
            </owl:Restriction>
        </rdfs:subClassOf>
        <rdfs:subClassOf rdf:resource="#Herbivore"/>
    </owl:Class>
    <owl:Class rdf:ID="Herbivore">
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="#eat"/>
                <owl:allValuesFrom rdf:resource="#Plant"/>
            </owl:Restriction>
        </rdfs:subClassOf>
        <rdfs:subClassOf rdf:resource="#Animal"/>
    </owl:Class>
    <owl:ObjectProperty rdf:ID="inverse_of_eat_1">
        <owl:inverseOf rdf:resource="#maineat"/>
        <rdfs:subPropertyOf rdf:resource="#eated"/>
    </owl:ObjectProperty>
    <owl:ObjectProperty rdf:ID="is_part_of">
        <rdf:type rdf:resource="&owl;TransitiveProperty"/>
    </owl:ObjectProperty>
    <owl:Class rdf:ID="Leaf">
        <rdfs:subClassOf rdf:resource="#Plant"/>
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="#is_part_of"/>
                <owl:allValuesFrom rdf:resource="#Branch"/>
            </owl:Restriction>
        </rdfs:subClassOf>
        <rdfs:comment rdf:datatype="&xsd;string">樹葉</rdfs:comment>
    </owl:Class>
    <owl:Class rdf:ID="Lion">
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="#eat"/>
                <owl:allValuesFrom rdf:resource="#Herbivore"/>
            </owl:Restriction>
        </rdfs:subClassOf>
        <rdfs:subClassOf rdf:resource="#Animal"/>
    </owl:Class>
    <owl:ObjectProperty rdf:ID="maineat">
        <owl:inverseOf rdf:resource="#inverse_of_eat_1"/>
        <rdfs:subPropertyOf rdf:resource="#eat"/>
    </owl:ObjectProperty>
    <owl:Class rdf:ID="Plant">
        <owl:disjointWith rdf:resource="#Animal"/>
    </owl:Class>
    <owl:Class rdf:ID="Tree">
        <rdfs:subClassOf rdf:resource="#Plant"/>
        <rdfs:comment rdf:datatype="&xsd;string">樹</rdfs:comment>
    </owl:Class>
</rdf:RDF>


--  作者:nodone
--  发布时间:4/1/2007 9:50:00 AM

--  
学习
--  作者:jpz6311whu
--  发布时间:4/7/2007 4:37:00 PM

--  
package jena.jena.w3chinabbs.lioninference;

import com.hp.hpl.jena.ontology.OntModel;

/**
* The source of this problem is located at:
* http://bbs.w3china.org/dispbbs.asp?boardID=2&ID=44678
*
* Aim at getting infered ontModel from a base ontModel;
*
* @author jpz6311whu
*
*/
public interface JenaInference {
 /**
  * Get infered ontModel from a base ontModel
  *
  * @param Base
  *            ontModel
  *
  * @return Infered ontModel
  */
 public OntModel getInferedOntModel(OntModel base);
}


--  作者:jpz6311whu
--  发布时间:4/7/2007 4:38:00 PM

--  
package jena.jena.w3chinabbs.lioninference;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
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.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.reasoner.dig.DIGReasoner;
import com.hp.hpl.jena.reasoner.dig.DIGReasonerFactory;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;

/**
* Jena inferece using DIGReasoner
*
* @author jpz6311whu
*
*/
public class DIGInference implements JenaInference {

 public OntModel getInferedOntModel(OntModel base) {
  Model cModel = ModelFactory.createDefaultModel();
  Resource conf = cModel.createResource();
  conf.addProperty(ReasonerVocabulary.EXT_REASONER_URL, cModel
    .createResource("http://localhost:8080"));

  // create the reasoner factory and the reasoner
  DIGReasonerFactory drf = (DIGReasonerFactory) ReasonerRegistry
    .theRegistry().getFactory(DIGReasonerFactory.URI);
  DIGReasoner r = (DIGReasoner) drf.create(conf);

  OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM);
  spec.setReasoner(r);

  return ModelFactory.createOntologyModel(spec, base);

 }

}


--  作者:jpz6311whu
--  发布时间:4/7/2007 4:39:00 PM

--  
package jena.jena.w3chinabbs.lioninference;

import junit.framework.TestCase;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.PrintUtil;
import com.hp.hpl.jena.vocabulary.RDFS;

public class TestDIGInference extends TestCase {

 /*
  * Test method for 'jena.jena.w3chinabbs.lioninference.TestDIGInference.getInferedOntModel(OntModel)'
  */
 public void testGetInferedOntModel() {
  OntModel raw = ModelFactory.createOntologyModel(OntModelSpec.OWL_LITE_MEM);
  raw.read("file:resource/life.owl");
  DIGInference inference = new DIGInference();
  OntModel inf= inference.getInferedOntModel(raw);
  
  Resource lion=inf.getResource("http://www.owl-ontologies.com/unnamed.owl#Lion");
  assertNotNull(lion);
  Resource carnivore = inf.getResource("http://www.owl-ontologies.com/unnamed.owl#Carnivore");
  assertNotNull(carnivore);
  
  PrintUtil.printOut(inf.listStatements(lion,RDFS.subClassOf,(RDFNode)null));
  
  assertFalse(inf.getRawModel().contains(lion,RDFS.subClassOf,carnivore));
  assertTrue(inf.contains(lion,RDFS.subClassOf,carnivore));
 }

}


--  作者:jpz6311whu
--  发布时间:4/7/2007 4:46:00 PM

--  

此主题相关图片如下:
按此在新窗口浏览图片

--  作者:hijkpty
--  发布时间:4/6/2010 11:13:00 AM

--  
package iip.oms.matcher;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.ontology.*;
import java.util.Iterator;
public class JenaParse
{
public static void main(String[] args) {
  // 创建本体模型
  OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
  ontModel.read("file:D:/ontology/Conference_PCS_i_merge.owl"); // 读取文件,加载模型
//定义一个类作为模型中Animal类的等等价类,并添加注释

  OntClass cls = ontModel.createClass("HuiyiClass");

  cls.addComment("the EquivalentClass of Conference...", "EN");

//   通过完整的URI取得模型中的Animal类

  OntClass oc = ontModel.getOntClass("file:D:/ontology/Conference_PCS_i_merge.owl#Conference");
System.out.println(oc.toString());
  oc.addEquivalentClass(cls); // 将先前定义的类添加为Animal的等价类


  System.out.println("ObjectProperty: ");
for( Iterator obp = ontModel.listObjectProperties();obp.hasNext();)
{
  ObjectProperty op = (ObjectProperty) obp.next();
  System.out.println("  LocalName:"+op.getLocalName() +";  Domain:"+op.getDomain()+"; Range:"+op.getRange());
}
System.out.println();
System.out.println("DatatypeProperty: ");
for( Iterator dbp = ontModel.listDatatypeProperties();dbp.hasNext();)
{
  DatatypeProperty dp = ( DatatypeProperty) dbp.next();
  System.out.println("  LocalName:"+dp.getLocalName()+";  Domain:"+dp.getDomain()+";   Range:"+dp.getRange());
}
System.out.println();
  // 迭代显示模型中的类
  for (Iterator i = ontModel.listClasses(); i.hasNext();) {
   OntClass c = (OntClass) i.next();
   
   if (!c.isAnon()) { // 如果不是匿名类
    System.out.print("Class");
    
    // 获取类的URI并输出,在输出时对URI做了简化(将命名空间前缀省略)
    System.out.println(c.getModel().getGraph().getPrefixMapping().shortForm(c.getURI()));
   
    // 处理等价类
    for (Iterator e = c.listEquivalentClasses(); e.hasNext();) {
 
     OntClass ec = (OntClass) e.next();
    
     // 输出等价类
     System.out.println("等价类 :"+ec.getEquivalentClass());
    }  
    
  
    for (Iterator d = c.listDisjointWith(); d.hasNext();) {
      OntClass dc = (OntClass) d.next();
     
      // 输出等价类的注释
      System.out.println("互斥类:"+dc.getDisjointWith());
     }    
   
    // 迭代显示当前类的父类
    for (Iterator it = c.listSuperClasses(); it.hasNext();)
    {
     OntClass sp = (OntClass) it.next();
        String str = c.getModel().getGraph()
        .getPrefixMapping().shortForm(c.getURI()) // 获取URI
        + "'s superClass is " ;
     String strSP = sp.getURI();
     try{ // 另一种简化处理URI的方法
      str = str + ":" + strSP.substring(strSP.indexOf('#')+1);
      System.out.println("  Class" +str);
     }
     catch( Exception e ){
     }
    }

    // 迭代显示当前类的子类
    for (Iterator it = c.listSubClasses(); it.hasNext();)
    {
     System.out.print("  Class");
     OntClass sb = (OntClass) it.next();
     System.out.println(c.getModel().getGraph().getPrefixMapping()
       .shortForm(c.getURI())
       + "'s subClass is "
       + sb.getModel().getGraph().getPrefixMapping()
         .shortForm(sb.getURI()));
    }
    
    // 迭代显示与当前类相关的所有属性
    for(Iterator ipp = c.listDeclaredProperties(); ipp.hasNext();)
    {
     OntProperty p = (OntProperty)ipp.next();
     

      System.out.println("  associated property: "+p.getLocalName());
    }
   }
  }
  
}
}
//listDeclaredProperties(

请帮我看看这段代码有什么错误吗,不知为什么等价类解析不出来?


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