




已阅读5页,还剩50页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
protg-owl api programmers guide- Original Author: Holger Knublauch- Currently maintained by: Protg staff members- Last updated: July 30, 2009- Please send corrections, comments, and questions to the protege-owl mailing listContents Overview Installation & Getting Started Basics Working with OWL Models Names, Namespace prefixes, and URIs Understanding the Model Interfaces Creating Named Classes and Individuals Using Datatype Properties and Datatype Values Using Object Properties to Build Relationships between Resources Working with References to External/Untyped Resources Property Domains Advanced Class Definitions (OWL DL) Restrictions Logical Class Definitions (Unions, Intersections, Complements) Enumerated Classes Creating Defined Classes Advanced Topics Querying the OWLModel RDF(S) and OWL Reacting on Changes using Listeners Loading and Saving Files Working with Multi-File Projects and TripleStores Working with Jena Models Protg Programming Protg-OWL and the Core Protg API Changes from the old Protg-OWL API Protg User Interface Programming Protg Plug-in Development Building Semantic Web Applications OverviewThe Protg-OWL API is an open-source Java library for the Web Ontology Language (OWL) and RDF(S). The API provides classes and methods to load and save OWL files, to query and manipulate OWL data models, and to perform reasoning based on Description Logic engines. Furthermore, the API is optimized for the implementation of graphical user interfaces.The API is designed to be used in two contexts: For the development of components that are executed inside of the Protg-OWL editors user interface For the development of stand-alone applications (e.g., Swing applications, Servlets, or Eclipse plug-ins) Protg is a flexible, configurable platform for the development of arbitrary(任意的) model-driven applications and components. Protg has an open architecture that allows programmers to integrate plug-ins, which can appear as separate tabs, specific user interface components (widgets,小器具), or perform any other task on the current model. The Protg-OWL editor provides many editing and browsing facilities for OWL models, and therefore can serve as an attractive starting point for rapid application development. Developers can initially wrap their components into a Protg tab widget and later extract them to distribute them as part of a stand-alone(独一无二) application.This guide will introduce you to some basic concepts of the Protg-OWL API, no matter whether you intend to use it for stand-alone applications or Protg plug-ins. The guide tries to illustrate the API by examples, and most examples create parts of OWL ontologies such as properties and restrictions. The complete source code for most of the examples can be found in the tegex.owlx.examples package in the Protg-OWL source code. If you have trouble getting started, please post questions on the protege-owl mailing list. Installation & Getting StartedThe Protg-OWL editor is bundled with the full installation of Protg. In addition to installing Protg, you should download the source code from the Protg Subversion repository. The source code is the most reliable reference for the systems functionality and browsing the source code with a Java IDE is a great way to learn the API. Detailed instructions on how to download source code from our repository are available on the developer documentation page. The URL to download the Protg-OWL source code using a Subversion client is: /repos/protege/owl/trunk/.You will also find Javadoc for the API useful. In particular, you will need to understand the interfaces from the tegex.owl.model package.Following a successful installation, your directory structure should look approximately as follows:Now let us configure our Java project and write a small Hello World application. If you are using a Java IDE such as Eclipse or IntelliJ, select the Protg installation folder as your project home. Next, add all the JAR files from the installation to your project classpath. Set your compiler output path to plugins/classes.Then create a Java class such as the following:package com.demo.application;import tegex.owl.model.OWLModel;import tegex.owl.model.OWLNamedClass;import tegex.owl.ProtegeOWL;public class OWLAPIDemoApplication public static void main(String args) OWLModel owlModel = ProtegeOWL.createJenaOWLModel(); owlModel.getNamespaceManager().setDefaultNamespace(#); OWLNamedClass worldClass = owlModel.createOWLNamedClass(World); System.out.println(Class URI: + worldClass.getURI(); Execute this program stand-alone. The output should be Class URI: #World.BasicsWorking with OWL ModelsThe Protg-OWL API is centered around a collection of Java interfaces from the model package. These interfaces provide access to the OWL model and its elements like classes, properties, and individuals. Application developers should not access the implementation of these interfaces (such as DefaultRDFIndividual) directly, but only operate on the interfaces. Using these interfaces you dont have to worry about the internal details of how Protg stores ontologies. Everything is abstracted into interfaces and your code should not make any assumptions about the specific implementation.The most important model interface is OWLModel, which provides access to the top-level container of the resources in the ontology. You can use OWLModel to create, query, and delete resources of various types and then use the objects returned by the OWLModel to do specific operations. For example, the following snippet creates a new OWLNamedClass (which corresponds to owl:Class in OWL), and then gets its URI:OWLModel owlModel = ProtegeOWL.createJenaOWLModel();OWLNamedClass worldClass = owlModel.createOWLNamedClass(World);System.out.println(Class URI: + worldClass.getURI();Note that the class ProtegeOWL provides a couple of convenient static methods to create OWLModels, also from existing OWL files. For example, you can load an existing ontology from the web using String uri = /ontologies/pizza/2007/02/12/pizza.owl; OWLModel owlModel = ProtegeOWL.createJenaOWLModelFromURI(uri);Names, Namespace prefixes, and URIsOWL and RDF resources are globally identified by their URIs, such as /travel.owl#Destination. However, since URIs are long and often inconvenient to handle, the primary access and identification mechanism for ontological resources in the Protg-OWL API is their name. A name is a short form consisting of local name and an optional prefix. Prefixes are typically defined in the ontology to abbreviate names of imported resources. For example, instead of writing /2002/07/owl#Class, we can write owl:Class because owl is a prefix for the namespace /2002/07/owl#. Similarly, if an ontology imports the travel ontology from above, it can define a prefix to access all imported classes and properties with travel, e.g. travel:Destination. If we are inside the default namespace of the ontology, the prefix is empty, i.e., the name of the resource is only Destination. Application developers can take control of namespace prefixes using the NamespaceManager object. In order to access the current NamespaceManager of an OWLModel, you can use OWLModel.getNamespaceManager(). Assuming we have loaded the travel ontology as a default namespace, we can access the resources contained in the OWLModel using the following example calls: OWLNamedClass destinationClass = owlModel.getOWLNamedClass(Destination); OWLObjectProperty hasContactProperty = owlModel.getOWLObjectProperty(hasContact); OWLDatatypeProperty hasZipCodeProperty = owlModel.getOWLDatatypeProperty(hasZipCode); OWLIndividual sydney = owlModel.getOWLIndividual(Sydney);. and use the objects to perform further queries or operations on the corresponding resources. For example, in order to extract the URI for a named object, you can use the RDFResource.getURI() method.Understanding the Model InterfacesThe interfaces of the model package are arranged in an inheritance hierarchy. An overview of the available interfaces can be found in the API diagram, contributed by Matthew Horridge. (You may want to print this diagram and leave it on your desk while you are using the API.) The base interface of all resources is RDFResource, from which subinterfaces for classes, properties, and individuals are derived: RDFResourceRDFSClassRDFPropertyRDFIndividualRDFResource defines basic operations for all resources, in particular getting and setting property values. RDFProperty is the base interface for rdf:Properties and its subtypes owl:DatatypeProperty and owl:ObjectProperty. For classes, the hierarchy gets quite complex because of the various types of anonymous classes in OWL. This will be handled later.Here is some example code that creates a simple class, an individual of that class, and then assigns a couple of properties. OWLModel owlModel = ProtegeOWL.createJenaOWLModel(); OWLNamedClass personClass = owlModel.createOWLNamedClass(Person); OWLDatatypeProperty ageProperty = owlModel.createOWLDatatypeProperty(age); ageProperty.setRange(owlModel.getXSDint(); ageProperty.setDomain(personClass); OWLObjectProperty childrenProperty = owlModel.createOWLObjectProperty(children); childrenProperty.setRange(personClass); childrenProperty.setDomain(personClass); RDFIndividual darwin = personClass.createRDFIndividual(Darwin); darwin.setPropertyValue(ageProperty, new Integer(0); RDFIndividual holgi = personClass.createRDFIndividual(Holger); holgi.setPropertyValue(childrenProperty, darwin); holgi.setPropertyValue(ageProperty, new Integer(33);Creating Named Classes and IndividualsThe Protg-OWL API makes a clear distinction between named classes and anonymous classes. Named classes are used to create individuals, while anonymous classes are used to specify logical characteristics (restrictions) of named classes. We will handle anonymous classes later, but lets look at named classes now.To reflect the OWL specification, there are two types of named classes: RDFSNamedClass (rdfs:Class) and OWLNamedClass (owl:Class). Unless you are explicitly working in RDF, you will most likely create OWL classes. After you have created the classes, you can arrange them in an subclass relationship: OWLNamedClass personClass = owlModel.createOWLNamedClass(Person); / Create subclass (complicated version) OWLNamedClass brotherClass = owlModel.createOWLNamedClass(Brother); brotherClass.addSuperclass(personClass); brotherClass.removeSuperclass(owlModel.getOWLThingClass();In this example, the class Brother is created first as a top-level class. The method OWLModel.createOWLNamedClass(String name) makes new classes by default a subclass of owl:Thing only. Then, Person is added to the superclasses, leading to a situation in which both Person and owl:Thing are parents. Therefore, owl:Thing needs to be removed afterwards.A much more convenient way of creating a subclass of Person is as follows: OWLNamedClass sisterClass = owlModel.createOWLNamedSubclass(Sister, personClass);The resulting inheritance hierarchy of the code above is:PersonBrotherSisterA simple recursive call can then be used to print arbitrary hierarchies with indentation: printClassTree(personClass, ); private static void printClassTree(RDFSClass cls, String indentation) System.out.println(indentation + cls.getName(); for (Iterator it = cls.getSubclasses(false).iterator(); it.hasNext();) RDFSClass subclass = (RDFSClass) it.next(); printClassTree(subclass, indentation + ); In the recursive routine, RDFSClass.getSubclasses() takes a boolean argument. When set to true, this will return not only the direct subclasses of the current class, but also the subclasses of the subclasses, etc.Named classes can be used to generate individuals. The instances of a class can then be queried using the RDFSClass.getInstances() method: OWLIndividual individual = brotherClass.createOWLIndividual(Hans); Collection brothers = brotherClass.getInstances(false); assert (brothers.contains(hans); assert (brothers.size() = 1);There is a crucial distinction between direct and indirect instances. The individual Hans is a direct instance of Brother, but not a direct instance of Person. However, it is also an indirect instance of Person, because Brother is a subclass of Person, i.e., every Brother is also a Person. Programmers are able to select whether their calls shall address only the direct or also the indirect instances using a boolean parameter: assert (personClass.getInstanceCount(false) = 0); assert (personClass.getInstanceCount(true) = 0); assert (personClass.getInstances(true).contains(hans);The inverse query to get the type/class of an individual can be made using the RDFResource.getRDFType() family of methods. For example, Hans has the rdf:type Brother, and it also has the (indirect) type Person: assert (hans.getRDFType().equals(brotherClass); assert (hans.hasRDFType(brotherClass); assert !(hans.hasRDFType(personClass, false); assert (hans.hasRDFType(personClass, true);If the life cycle of a resource is over, it can be deleted using the RDFResource.delete() method. The API uses the same convention as other APIs, where delete means to completely destroy the object, whereas remove only deletes references to the object. This means that when you call delete on a resource, then all its property values are removed but not deleted. hans.delete();Using Datatype Properties and Datatype ValuesTo create an owl:DatatypeProperty, you can use OWLModel.createOWLDatatypeProperty(String name). By default, datatype properties can take any datatype value such as strings and integers. OWL defines several XML Schema datatypes that can be used to restrict the range of properties. The most popular XML Schema datatypes are xsd:string, xsd:int, xsd:float, and xsd:boolean. For example, if you want to limit your property to only take string values, you can restrict its range using: OWLDatatypeProperty property = owlModel.createOWLDatatypeProperty(name); name.setRange(owlModel.getXSDstring();. where the call OWLModel.getXSDstring() returns a reference to the RDFSDatatype xsd:string. Other default datatypes are accessible using similar OWLModel.getXSD. methods. More complex datatypes can be accessed using their URI: RDFSDatatype dateType = owlModel.getRDFSDatatypeByName(xsd:date);For the default datatypes, property values are conveniently handled using corresponding Java data types. For example, if you assign property values for a string property, you can simply pass a String object to the setPropertyValue call. Corresponding mappings exist into other data types: individual.setPropertyValue(stringProperty, MyString); individual.setPropertyValue(intProperty, new Integer(42); individual.setPropertyValue(floatProperty, new Float(4.2); individual.setPropertyValue(booleanProperty, Boolean.TRUE);The inverse getter methods will also deliver the objects in their simplest possible forms: String stringValue = (String) individual.getPropertyValue(stringProperty); Integer intValue = (Integer) individual.getPropertyValue(intProperty); Float float = (Float) individual.setPropertyValue(floatProperty); Boolean boolean = (Boolean) individual.setPropertyValue(booleanProperty);Values of all other data types are wrapped into objects of the class RDFSLiteral. A literal combines a value together with its datatype. Values are stored as strings and need to be unwrapped by the user code. The following example is used to assign a value of the XML Schema datatype xsd:date. RDFSDatatype xsdDate = owlModel.getRDFSDatatypeByName(xsd:date); OWLDatatypeProperty dateProperty = owlModel.createOWLDatatypeProperty( dateProperty, xsdDate); RDFSLiteral dateLiteral = owlModel.createRDFSLiteral(1971-07-06, xsdDate); individual.setPropertyValue(dateProperty, dateLiteral); RDFSLiteral myDate = (RDFSLiteral) individual.getPropertyValue(dateProperty); System.out.println(Date: + myDate);. will print out Date: 1971-07-06.RDFSLiterals are also used to bundle string values together with a language tag: RDFSLiteral langLiteral = owlModel.createRDFSLiteral(Wert, de); individual.setPropertyValue(stringProperty, langLiteral); RDFSLiteral result = (RDFSLiteral) individual.getPropertyValue(stringProperty); assert (result.getLanguage().equals(de); assert (result.getString().equals(Wert);To summarize, datatype values are handled either as primitive objects (String, Integer, Float, Boolean), or RDFSLiterals. If we have a literal of a default data type, the
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 泥土工施工合同范本
- 2023年1月国开电大法学本科《中国法律史》期末纸质考试试题及答案
- 合同范本哪个
- 报废托盘出售合同范本
- 2025年口服给药制度考试试题及参考答案
- 职业暴露的应急处理流程试题及答案
- 网络安全从业人员专业素养试题(含答案)
- 2025年智能化办公系统整体解决方案销售与维护服务合同
- 2025年度医疗器械生产厂质量监控部门员工服务协议书
- 2025年新能源充电桩运营租赁合作协议(含电价优惠条款)
- 2025标准建设银行贷款合同范本
- 校家社培训家长课件
- 2025供应链合同范本
- 2025年北京市中考道德与法治试卷试题真题(含答案详解)
- 产品偏离许可管理办法
- 食品行业标准化管理体系的构建研究
- 2025专精特新小巨人打分表(密件)
- 湖北农商行面试题目及答案
- 对便秘患者的健康教育
- 2025年中国热敏标签市场调查研究报告
- 仓库不良品管理制度
评论
0/150
提交评论