如何在 Java 中创建本体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5841123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an ontology in Java?
提问by wasimbhalli
I've some data triplets that I want to write in some sort of basic OWL ontology. I've triplets like:
我有一些数据三元组,我想用某种基本的 OWL 本体来编写。我有这样的三胞胎:
Delhi is part of India
or
或者
India is an Asian country
Note that I've relations like "is-a", "part-of", or "related-to". What's the simplest way to build an ontology? Any working example or a reference to an example website will be great help!
请注意,我有“is-a”、“part-of”或“related-to”等关系。构建本体的最简单方法是什么?任何工作示例或对示例网站的引用都会有很大帮助!
采纳答案by Ian Dickinson
There are a lot of different things mixed up in your question, I strongly suggest you take a bit of time (away from the keyboard!) to think through what you're trying to achieve here.
您的问题中混杂了很多不同的东西,我强烈建议您花一点时间(远离键盘!)仔细考虑一下您要在这里实现的目标。
Firstly, geographic ontologies can get quite complex, and a lot of work has already been done in this area. Probably the obvious starting point is the GeoNames ontology, which gives names to geographic features, including cities like Dehli and countries like India. At the very least you should re-use those names for the places in your application, as that will maximise the chances that your data can be successfully joined with other available linked-data sources.
首先,地理本体会变得非常复杂,在这方面已经做了很多工作。可能明显的起点是GeoNames 本体,它为地理特征命名,包括像德里这样的城市和像印度这样的国家。至少您应该为应用程序中的地点重复使用这些名称,因为这将最大限度地增加您的数据与其他可用链接数据源成功连接的机会。
However, you probably don't want the whole of GeoNames in your application (I'm guessing), so you also need to be clear whyyou need an ontology at all. A good way to approach this is from the outside of your application: rather than worry about which kind of Jena model to use, start by thinking through ways to complete the sentence "using the ontology, a user of my application will be able to ...". That should then lead you on to establishing some competency questions(see, for example, section 3 of this guide) for your ontology. Once you know what kinds of information you want to represent, and what kinds of queries you need to apply to it, your technology choices will be much clearer. I realise that these applications are typically developed iteratively, and you'll want to try some code out fairly early on, but I still advocate getting your destination more clearly in mind before you start your coding journey.
但是,您可能不希望在您的应用程序中使用整个 GeoNames(我猜),因此您还需要清楚为什么需要本体。解决这个问题的一个好方法是从应用程序的外部:与其担心要使用哪种 Jena 模型,不如先思考如何完成句子“使用本体,我的应用程序的用户将能够 . ..”。这应该会引导您建立一些能力问题(例如,请参阅本指南的第 3 部分) 为您的本体。一旦您知道您想要表示什么样的信息,以及您需要对其应用什么样的查询,您的技术选择就会更加清晰。我意识到这些应用程序通常是迭代开发的,您会希望尽早尝试一些代码,但我仍然主张在您开始编码之旅之前更清楚地了解您的目标。
You imply that you want to use Jena to drive a web site. There are many choices here. Don't be mislead by the term semantic web- this actually means bringing web-like qualities to interlined data sets, rather than putting semantics into human readable web pages per se. While you can do so, and many people do, you'll need some additional layers in your architecture. We typically use one of two approaches: using Jena with a templating engine, such as Velocity, in a servlets container, or using a Ruby web framework and driving Jena via JRuby. There are many other ways to solve this particular problem: Jena doesn't address web publishing directly, but it can be used within any Java-based web framework.
您暗示要使用 Jena 来驱动网站。这里有很多选择。不要被术语语义 Web误导——这实际上意味着将类似 Web 的特性引入到内联数据集,而不是将语义放入人类可读的网页本身。虽然您可以这样做,而且很多人都这样做,但您的架构中需要一些额外的层。我们通常使用以下两种方法之一:在 servlet 容器中使用带有模板引擎(例如Velocity )的Jena ,或使用 Ruby Web 框架并通过JRuby驱动 Jena 。有许多其他方法可以解决这个特定问题:Jena 不直接解决 Web 发布问题,但它可以在任何基于 Java 的 Web 框架中使用。
Finally, regarding namespaces, you should really re-use existing vocabularies, and hence namespaces, where possible. Don't make up new names for things which already have representations on the web of data somewhere. Use GeoNames, or DbPedia, or any of the many other published vocabularies where they fit. If they don't fit, then you should create a new name rather than use an existing name in a non-compatible way. In this case, you should use the web domain of your application (e.g. your company or university) as the basis for the namespace. Ideally, you should publish your ontology at the base URL of the namespace, but this can sometimes be hard to arrange depending on local web policies.
最后,关于命名空间,您应该真正重用现有的词汇表,从而尽可能重用命名空间。不要为已经在某处数据网络上表示的事物重新命名。使用 GeoNames 或DbPedia,或许多其他适合它们的已发布词汇表中的任何一个。如果它们不合适,那么您应该创建一个新名称,而不是以不兼容的方式使用现有名称。在这种情况下,您应该使用应用程序的 Web 域(例如您的公司或大学)作为命名空间的基础。理想情况下,您应该在命名空间的基本 URL 上发布本体,但有时这可能很难根据本地 Web 策略进行安排。
回答by Alessandro S.
I suggest OWL APIfrom Manchester University. In this way you can start to create your ontology "on the fly" in Java, and with a single method invocation you can serialize it in your preferred format (RDF, Manchester Syntax etc) if you need, or directly working on the in-memory representation. In this way you can rapidly prototype and experiment your ontology in the context of your program.
我建议曼彻斯特大学的OWL API。通过这种方式,您可以开始在 Java 中“即时”创建您的本体,并且通过单个方法调用,您可以根据需要将其序列化为您喜欢的格式(RDF、曼彻斯特语法等),或者直接处理 in-内存表示。通过这种方式,您可以在程序的上下文中快速原型化和试验本体。
For an overview of the library and its main componenets I suggest the tutorial (code tutorial) provided by the creator of the library, it covers 90% of the basic needs.
对于库及其主要组件的概述,我建议使用库创建者提供的教程(代码教程),它涵盖了 90% 的基本需求。
PS: Protégé is based on OWL Api, you can also try it as suggested, but expecially in the beginning I preferred to rapidly play with ontologies and switch to some engineering environment like Protege when my mind was clear enough. In addition, with an external ontology you would need to learn how to navigate it, that IMHO it is really not worth in the very beginning.
PS:Protégé是基于OWL Api的,你也可以按照建议尝试,但特别是一开始我更喜欢快速玩ontology,当我头脑清醒时切换到Protege这样的工程环境。此外,对于外部本体,您需要学习如何导航它,恕我直言,一开始真的不值得。
回答by Joseph Ottinger
You'd just declare a triplet class consisting of a subject, object, and predicate. "has-a" is a predicate, so your ontology elements would look like:
您只需声明一个由主语、宾语和谓词组成的三元组。“has-a”是一个谓词,所以你的本体元素看起来像:
"Dehli", "is-in", "India"
"India", "is-in", "Asia"
"India", "is-a", "country"
This doesn't address queries, of course, but given a decent data store (even a database would do) you could start to build a flexible ontology with a decent query mechanism.
当然,这并不能解决查询问题,但是如果有一个不错的数据存储(即使是数据库也可以),您就可以开始构建一个具有良好查询机制的灵活本体。
JENA is far, far more capable than what this would create, of course; it does provide the semantic query stuff, as well as farbetter resource definition and resolution. However, it's a lot more involved than a simple triplet structure; it all depends on what you need.
当然,JENA 远比这将创造的能力强大得多;它提供的语义查询的东西,以及远更好的资源定义和分辨率。然而,它比简单的三元组结构要复杂得多;这一切都取决于你需要什么。
回答by skordemir
/**
- 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);
}