2011-04-30 124 views
9

我有一些數據三元組,我想寫在某種基本的OWL本體中。我一直喜歡三胞胎:如何在Java中創建本體?

Delhi is part of India 

India is an Asian country 

請注意,我喜歡的關係 「是一個」, 「-的一部分」,或 「關聯到」。構建本體的最簡單方法是什麼?任何工作示例或對示例網站的引用都將非常有幫助!

+0

什麼是您遇到的問題,信息提取或耶拿API? – 2011-04-30 10:28:34

+0

我需要一個簡單的例子來創建基於上述語句或三元組的OWL文件。我是初學者,對本體或者jena知之甚少。創建貓頭鷹模型的一個簡單例子將對我有所幫助! – wasimbhalli 2011-04-30 10:38:29

回答

8

在你的問題中混淆了很多不同的東西,我強烈建議你花點時間(遠離鍵盤!)來思考你想要在這裏實現的目標。

首先,地理本體可能變得相當複雜,並且在這方面已經做了很多工作。可能最明顯的出發點是GeoNames ontology,它爲地理特徵提供了名稱,包括像德里這樣的城市和印度等國家。至少您應該在應用程序中重複使用這些名稱,因爲這樣可以最大限度地提高您的數據可以成功與其他可用鏈接數據源結合的機會。

但是,你可能不希望你的應用程序中的整個GeoNames(我猜),所以你還需要清楚爲什麼你需要一個本體論。處理這個問題的一個好方法是從你的應用程序的外部:而不是擔心使用哪種Jena模型,首先思考通過使用本體完成句子的方法,我的應用程序的用戶將能夠...「。然後這應該引導您爲本體建立一些能力問題(請參閱,例如,section 3 of this guide)。一旦你知道你想要代表什麼類型的信息,以及你需要應用什麼種類的查詢,你的技術選擇就會更清晰。我意識到這些應用程序通常是迭代開發的,並且您會想盡早嘗試一些代碼,但我仍然主張在開始編碼之前更清楚地考慮目標。

你意味着你想用Jena來驅動一個網站。這裏有很多選擇。不要被術語語義網誤導 - 這實際上意味着將類似網絡的特性引入數據集,而不是將語義本身放入可讀的網頁中。雖然你可以這麼做,而且很多人都這樣做,但你的架構中還需要一些額外的層次。我們通常使用兩種方法之一:在一個servlet容器中使用Jena和模板引擎(如Velocity),或使用Ruby Web框架並通過JRuby驅動Jena。解決這個問題的方法還有很多:Jena沒有直接解決Web發佈問題,但可以在任何基於Java的Web框架中使用它。

最後,關於命名空間,你應該在可能的情況下重新使用現有的詞彙表,並因此重用名稱空間。不要爲已在數據網絡上表示的東西創建新名稱。使用GeoNames或DbPedia或其他任何適合的其他已發佈詞彙表。如果它們不合適,那麼您應該創建一個新名稱,而不是以不兼容的方式使用現有名稱。在這種情況下,您應該使用應用程序的Web域(例如您的公司或大學)作爲命名空間的基礎。理想情況下,您應該在名稱空間的基本URL處發佈您的本體,但根據本地Web策略有時可能難以安排。

+0

好的,謝謝,我需要學習更多關於本體的知識! – wasimbhalli 2011-05-02 04:26:31

0

您只需聲明一個由主題,對象和謂詞組成的三元組類。 「有-A」是謂語,所以你的本體元素看起來像:

"Dehli", "is-in", "India" 
"India", "is-in", "Asia" 
"India", "is-a", "country" 

這並沒有解決,當然查詢,但由於一個體面的數據存儲(甚至是數據庫會做),你可以啓動通過體面的查詢機制構建靈活的本體。

當然,JENA遠遠超過了這個創造的能力;它的確提供了語義查詢的東西,還有更好的資源定義和分辨率。然而,它比簡單的三重結構涉及更多。這一切都取決於你需要什麼。

+0

我只是想創建一個基於它的本體文件,我試圖使用ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM),但我在定義我自己的語句等問題。我也不知道使用什麼名稱空間?任何示例? – wasimbhalli 2011-04-30 11:28:08

+0

向我們展示您到目前爲止的代碼,也許我們可以提供幫助。 – 2011-04-30 12:03:56

+0

我無法爲我的問題編寫精確的代碼。 – wasimbhalli 2011-04-30 12:28:17

1

看看斯坦福大學的Protege。這是一個本體編輯器。

2

我建議從曼徹斯特大學OWL API。通過這種方式,您可以開始在Java中「實時」創建本體,並且只需要一個方法調用,就可以按照您的首選格式(RDF,曼徹斯特語法等)對其進行序列化,內存表示。通過這種方式,您可以在程序的上下文中快速創建原型並對實體進行實驗。

有關圖書館及其主要組成部分的概述,我建議由圖書館的創建者提供的教程(code tutorial)涵蓋了90%的基本需求。 PS:Protégé基於OWL Api,你也可以按照建議嘗試,但特別是在開始時,我更願意快速玩本體,並在我的頭腦清晰的情況下切換到像Protege這樣的工程環境。另外,對於外部本體,您需要學習如何導航它,恕我直言,它在開始時真的不值得。

+1

我沒有注意到這裏已經有了一個可以接受的答案,我希望我能證明對別人有用:) – 2012-12-30 10:41:26

0
/** 
    - This is maven dependencies for owl-api 
    <dependency> 
     <groupId>net.sourceforge.owlapi</groupId> 
     <artifactId>owlapi-api</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>net.sourceforge.owlapi</groupId> 
     <artifactId>owlapi-apibinding</artifactId> 
    </dependency> 

    * First of all you need to initialize ontology: 

**/ 
        private OWLDataFactory factory; 

        private PrefixManager pm; 

        private OWLOntology ontology; 

        private String pmString = "#"; 

        private OWLOntologyManager manager; 

        private OWLReasoner reasoner; 

        private ShortFormEntityChecker entityChecker; 

        private BidirectionalShortFormProviderAdapter bidirectionalShortFormProviderAdapter; 

      private void initializeOntology(String fileContent) 
          throws OWLOntologyCreationException { 
         InputStream bstream = new ByteArrayInputStream(fileContent.getBytes()); 
         this.manager = OWLManager.createOWLOntologyManager(); 
         this.ontology = this.manager.loadOntologyFromOntologyDocument(bstream); 
         IRI ontologyIRI = this.ontology.getOntologyID().getOntologyIRI(); 
         this.pm = new DefaultPrefixManager(ontologyIRI.toString() 
           + this.pmString); 
         this.factory = this.manager.getOWLDataFactory(); 

         ReasonerFactory factory = new ReasonerFactory(); 
         this.reasoner = factory.createReasoner(this.ontology); 

         Set<OWLOntology> onts = new HashSet<>(); 
         onts.add(this.ontology); 

         DefaultPrefixManager defaultPrefixManager = new DefaultPrefixManager(
           this.pm); 
         ShortFormProvider shortFormProvider = new ManchesterOWLSyntaxPrefixNameShortFormProvider(
           defaultPrefixManager); 
         this.bidirectionalShortFormProviderAdapter = new BidirectionalShortFormProviderAdapter(
           this.manager, onts, shortFormProvider); 
         this.entityChecker = new ShortFormEntityChecker(
           this.bidirectionalShortFormProviderAdapter); 

        } 

/* 
    After that you need to define your classes and the relations of the classes. These relations calls as object properties in ontology. Instance of each ontology class calls as individual and the attributies of the classes (for person name, age , adress) calls as data-property. 
*/ 
// To create a new individual of an ontology class : 



     public OWLClass getClass(String className) { 
       return this.factory.getOWLClass(":" + className, this.pm); 
      } 




      public OWLNamedIndividual createInvidual(String cls, String invname) { 
             OWLNamedIndividual res = this.factory.getOWLNamedIndividual(":" 
               + invname, this.pm); 
             this.manager.addAxiom(this.ontology, 
               this.factory.getOWLDeclarationAxiom(res)); 
             OWLClassAssertionAxiom axiom = this.factory.getOWLClassAssertionAxiom(
               getClass(cls), res); 
             this.manager.addAxiom(this.ontology, axiom); 
             return res; 
      } 

// To create an object property : 

// This method will create an object property named prop if it is not exist. 



     public OWLObjectProperty getObjectProperty(String prop) { 
        return this.factory.getOWLObjectProperty(":" + prop, this.pm); 
       } 

      public void addObjectProperty(String propname, OWLNamedIndividual prop, 
         OWLNamedIndividual obj) { 
        OWLObjectPropertyAssertionAxiom axiom = this.factory 
          .getOWLObjectPropertyAssertionAxiom(
            getObjectProperty(propname), obj, prop); 
        this.manager.addAxiom(this.ontology, axiom); 
       } 

    // And finally , to add a data-property to individuals : 

     public OWLDataProperty getDataProperty(String prop) { 
         return this.factory.getOWLDataProperty(":" + prop, this.pm); 
        } 

     public void addDataProperty(String propname, boolean propvalue, 
         OWLNamedIndividual inv) { 
        OWLAxiom axiom = this.factory.getOWLDataPropertyAssertionAxiom(
          getDataProperty(propname), inv, propvalue); 
        this.manager.addAxiom(this.ontology, axiom); 
     }