如何在Java中创建本体?

我有一些数据三元组,我想用某种基本的OWL本体编写。 我有三胞胎像:

Delhi is part of India 

要么

 India is an Asian country 

请注意,我的关系类似于“is-a”,“part-of”或“related-to”。 构建本体的最简单方法是什么? 任何工作示例或对示例网站的引用都将是非常有用的!

你的问题中有很多不同的东西混在一起,我强烈建议你花点时间(远离键盘!)来思考你想要在这里实现的目标。

首先,地理本体可以变得非常复杂,并且在这个领域已经做了很多工作。 可能明显的起点是GeoNames本体 ,它为地理特征命名,包括Dehli等城市和印度等国家。 至少应该为应用程序中的位置重用这些名称,因为这样可以最大限度地提高数据与其他可用链接数据源成功连接的可能性。

但是,您可能不希望应用程序中存在整个GeoNames(我猜),因此您还需要明确为什么需要本体。 解决这个问题的一个好方法是从你的应用程序的外部:而不是担心使用哪种Jena模型,首先考虑完成句子的方法“使用本体,我的应用程序的用户将能够。 ..“ 然后,这应该引导您为您的本体建立一些能力问题 (例如,参见本指南的第3部分 )。 一旦你知道你想要代表什么类型的信息,以及你需要应用什么类型的查询,你的技术选择将更加清晰。 我意识到这些应用程序通常是迭代开发的,并且您希望尽早尝试一些代码,但我仍然主张在开始编码之前更清楚地了解目的地。

您暗示您想使用Jena来驱动网站。 这里有很多选择。 不要被术语“ 语义网”误导 – 这实际上意味着将类似网络的特性引入到内联数据集中,而不是将语义本身放入人类可读的网页中。 虽然您可以这样做,而且很多人都这样做,但您的架构中还需要一些额外的层。 我们通常使用以下两种方法之一:在servlet容器中使用Jena和模板引擎(如Velocity) ,或使用Ruby Web框架并通过JRuby驱动Jena。 还有许多其他方法可以解决这个特定问题:Jena没有直接解决Web发布问题,但它可以在任何基于Java的Web框架中使用。

最后,关于命名空间,您应该尽可能重用现有的词汇表,从而重用命名空间。 不要为已经在某个地方的数据网上有表示的东西组成新名称。 使用GeoNames,或DbPedia ,或其他任何适合的已发布词汇表。 如果它们不适合,那么您应该创建一个新名称,而不是以不兼容的方式使用现有名称。 在这种情况下,您应该使用应用程序的Web域(例如您的公司或大学)作为命名空间的基础。 理想情况下,您应该在命名空间的基本URL上发布本体,但有时可能很难根据本地Web策略进行排列。

我建议曼彻斯特大学提供OWL API 。 通过这种方式,您可以开始在Java中“动态”创建本体,并且可以使用单个方法调用,如果需要,可以按照首选格式(RDF,曼彻斯特语法等)对其进行序列化,或者直接在记忆表现。 通过这种方式,您可以在程序的上下文中快速构建和实验本体。

有关库及其主要组件的概述,我建议由库的创建者提供的教程( 代码教程 ),它涵盖了90%的基本需求。

PS:Protégé基于OWL Api,您也可以按照建议尝试,但特别是在开始时我更喜欢快速使用本体并切换到像Protege这样的工程环境,当我的思绪足够清晰时。 另外,使用外部本体,你需要学习如何导航它,恕我直言,它一开始真的不值得。

看看斯坦福大学的Protege 。 这是一个本体编辑器。

您只需声明一个由主题,对象和谓词组成的三元组类。 “has-a”是一个谓词,所以你的本体元素看起来像:

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

当然,这并不能解决查询,但是如果有一个像样的数据存储(即使是数据库也可以),你可以开始构建一个具有良好查询机制的灵活本体。

当然,JENA的能力远胜于此; 它确实提供了语义查询的东西,以及更好的资源定义和分辨率。 然而,它比简单的三重结构涉及更多; 这一切都取决于你需要什么。

 /** - This is maven dependencies for owl-api  net.sourceforge.owlapi owlapi-api   net.sourceforge.owlapi owlapi-apibinding  * 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 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); }