ONTOLOGY AP

Một phần của tài liệu sử dụng ontology và owl biểu diễn và giải các bài toán logic LOGIC PUZZLEs (Trang 58)

Các thuộc tính cá thể (Individuals)

3.2.3ONTOLOGY AP

(http://jena.apache.org/documentation/ontology/)

Ta giới thiệu sơ qua về Jena ontology API phục vụ cho vấn đề xử lý liên quan đến Ontology Trong Jena, lớp OntClass biểu diễn cho 1 lớp ontology hay RDFS và OntModel biểu diễn cho Ontology model

Và ta có thể biểu diễn owl:ObjectProperty tương đương với http://www.w3.org/2002/07/owl#ObjectProperty

Một trong những điểm mạnh của việc sử dụng ứng dụng dựa trên ontology là cho phép khả năng suy luận dựa trên mô hình (reasoning/inference) như hình sau

Mô hình suy luận của Jena

Graph là một interface của Jena hỗ trợ cấu thành tập các RDF triples.

Ví dụ sử dụng

Resource r = myModel.getResource( myNS + "DigitalCamera" ); OntClass cls = r.as( OntClass.class );

Tạo Ontology Model

OntModel m = ModelFactory.createOntologyModel();

Or

OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );

Các URI của OWL định nghĩa sẵn

Ontology language

RDFS OWL Full OWL DL OWL Lite

Sau đây là các thông số Ontology Model Specification dùng làm tham số cho hàm createOntologyModel() ở trên OntModelSpec OWL_MEM OWL_MEM_TRANS_INF OWL_MEM_RULE_INF OWL_MEM_MICRO_RULE_INF OWL_MEM_MINI_RULE_INF OWL_DL_MEM OWL_DL_MEM_RDFS_INF OWL_DL_MEM_TRANS_INF OWL_DL_MEM_RULE_INF

OntModelSpec OWL_LITE_MEM OWL_LITE_MEM_TRANS_INF OWL_LITE_MEM_RDFS_INF OWL_LITE_MEM_RULES_INF RDFS_MEM RDFS_MEM_TRANS_INF RDFS_MEM_RDFS_INF

Để tạo 1 model với specification, ta gọi ModelFactory như sau :

OntModel m = ModelFactory.createOntologyModel( <model spec> );

Ví dụ

OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF );

Cách khác

OntModelSpec s = new OntModelSpec( OntModelSpec.OWL_MEM ); s.setDocumentManager( myDocMgr );

OntModel m = ModelFactory.createOntologyModel( s );

Xử lý ontology phức tạp và xử lý imports

Một ontology có thể import 1 ontology khác. Jena xử lý bằng cách base model của một ontology model thực sự là 1 tập hợp các models, mỗi cái cho các imported model như hình sau

Mô hình ontology phức hợp trong Jena

Ta nạp một tài liệu ontology và mô hình ontology bằng cách dùng hàm read, có nhiều phiên bản khác nhau như sau

read( String url )

read( Reader reader, String base ) read( InputStream reader, String base ) read( String url, String lang )

read( Reader reader, String base, String Lang ) read( InputStream reader, String base, String Lang )

Theo mặc định, một ontology model khi đọc 1 tài liệu ontology nó sẽ xác định và load các imports. Một tài liệu ontology có thể chứa 1 lớp Ontology, chứa metadata về tài liệu đó, ví dụ

<owl:Ontology rdf:about="">

<owl:imports rdf:resource="http://jena.apache.org/examples/example-ont" /> </owl:Ontology>

Mỗi 1 imported model được giữ trong 1 đồ thị riêng biệt, tách biệt với đồ thị gốc ontology. Base model sẽ thay đổi khi ta update, ta có thể lấy base model nhu sau

Model base = myOntModel.getBaseModel();

Imports trong Jena được xử lý đệ quy như hình 4.

The ontology document manager

Mỗi model ontology có 1 document manager cho nó, hỗ trợ trong việc xử lý các tài liệu ontology. Ta có 1 bộ quản lý document toàn cục được dùng bởi các model ontology, ta dùng như sau

OntDocumentManager mgr = new OntDocumentManager(); // set mgr's properties now

... some code ... // now use it

OntModelSpec s = new OntModelSpec( OntModelSpec.RDFS_MEM ); s.setDocumentManager( mgr );

OntModel m = ModelFactory.createOntologyModel( s );

Document manager policy

Jena dùng policy file dạng RDF để lưu các configs. Đường dẫn search cho file policy là

file:./etc/ont-policy.rdf;file:ont-policy.rdf, ta có thể lấy default policy ở thư mục etc/ ở thư mục cài đặt Jena.

Ta có thể set các thuộc tính cho document manager trong file policy như sau

<!-- policy for controlling the document manager's behaviour --> <processImports rdf:datatype="&xsd;boolean">true</processImports> <cacheModels rdf:datatype="&xsd;boolean">true</cacheModels> </DocumentManagerPolicy> Ví dụ <OntologySpec>

<publicURI rdf:resource="... the public URI to map from..." /> <altURL rdf:resource="... the local URL to map to ..." />

<!-- optional ontology language term -->

<language rdf:resource="... encoding used ..." /> <!-- optional prefix to associate with the public URL --> <prefix rdf:datatype="&xsd;string">a prefix</prefix> </OntologySpec>

<OntologySpec>

<!-- local version of the RDFS vocabulary -->

<publicURI rdf:resource="http://www.w3.org/2000/01/rdf-schema" /> <altURL rdf:resource="file:src/main/resources/rdf-schema.rdf" /> </OntologySpec> OntModel m = ModelFactory.createOntologyModel(); OntDocumentManager dm = m.getDocumentManager(); dm.addAltEntry( "http://www.eswc2006.org/technologies/ontology",

"file:" + JENA + "src/examples/resources/eswc-2006-09-21.rdf" ); m.read( "http://www.eswc2006.org/technologies/ontology" );

Ontology Resource (OntResource class)

Các ví dụ

// create the base model

String SOURCE = "http://www.eswc2006.org/technologies/ontology";

String NS = SOURCE + "#";

OntModel base = ModelFactory.createOntologyModel( OWL_MEM );

base.read( SOURCE, "RDF/XML" );

// create the reasoning model using the base

OntModel inf = ModelFactory.createOntologyModel( OWL_MEM_MICRO_RULE_INF, base );

// create a dummy paper for this example

OntClass paper = base.getOntClass( NS + "Paper" );

Individual p1 = base.createIndividual( NS + "paper1", paper );

// list the asserted types

for (Iterator<Resource> i = p1.listRDFTypes(); i.hasNext(); ) {

System.out.println( p1.getURI() + " is asserted in class " + i.next() );

}

// list the inferred types

p1 = inf.getIndividual( NS + "paper1" );

for (Iterator<Resource> i = p1.listRDFTypes(); i.hasNext(); ) {

System.out.println( p1.getURI() + " is inferred to be in class " + i.next() );

}

Resource r = m.getResource( NS + "Paper" ); OntClass paper = r.as( OntClass.class );

OntClass paper = m.createClass( NS + "Paper" ); OntClass bestPaper = m.createClass( NS + "BestPaper" );

Attribute

subClass superClass equivalentClass disjointWith

In ra danh sách các subclasses của 1 Artefact

OntClass artefact = m.getOntClass( NS + "Artefact" );

for (Iterator<OntClass> i = artefact.listSubClasses(); i.hasNext(); ) { OntClass c = i.next();

System.out.println( c.getURI() ); }

Ontology Properties

Thuộc tính hasProgramme

OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); OntClass programme = m.createClass( NS + "Programme" );

OntClass orgEvent = m.createClass( NS + "OrganizedEvent" );

ObjectProperty hasProgramme = m.createObjectProperty( NS + "hasProgramme" );

hasProgramme.addDomain( orgEvent ); body.addRange( programme );

Thêm bớt các thuộc tính

OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM ); m.read( "http://www.eswc2006.org/technologies/ontology" );

DatatypeProperty subDeadline = m.getDatatypeProperty( NS + "hasSubmissionDeadline" ); DatatypeProperty notifyDeadline = m.getDatatypeProperty( NS + "hasNotificationDeadline" ); DatatypeProperty cameraDeadline = m.getDatatypeProperty( NS + "hasCameraReadyDeadline" );

DatatypeProperty deadline = m.createDatatypeProperty( NS + "deadline" ); deadline.addDomain( m.getOntClass( NS + "Call" ) );

deadline.addRange( XSD.dateTime );

deadline.addSubProperty( subDeadline ); deadline.addSubProperty( notifyDeadline ); deadline.addSubProperty( cameraDeadline );

Functional Properties

public TransitiveProperty asTransitiveProperty(); public FunctionalProperty asFunctionalProperty(); public SymmetricProperty asSymmetricPropery();

public InverseFunctionalProperty asInverseFunctionalProperty();

public TransitiveProperty convertToTransitiveProperty(); public FunctionalProperty convertToFunctionalProperty(); public SymmetricProperty convertToSymmetricPropery();

public InverseFunctionalProperty convertToInverseFunctionalProperty();

Complex Class Expressions

Restriction class

// get restriction with a given URI

Restriction r = m.getRestriction( NS + "theName" );

// anonymous restriction on property p

OntProperty p = m.createOntProperty( NS + "p" ); Restriction anonR = m.createRestriction( p );

// get list of restrictions

Iterator<Restriction> i = m.listRestrictions(); while (i.hasNext()) {

Restriction r = i.next(); if (isTheOne( r )) { // handle the restriction } } OntProperty p = m.getProperty( NS + "p" ); Iterator<Restriction> i = p.listReferringRestrictions(); while (i.hasNext()) { Restriction r = i.next();

// now handle the restriction ... }

AllValuesFromRestriction avf = r.convertToAllValuesFromRestriction( c );

// access components of all values from restrictions

OntClass largeFormat = m.getOntClass( camNS + "Large-Format" );

for (Iterator<OntClass> i = LargeFormat.listSuperClasses( true ); i.hasNext(); ) { OntClass c = i.next();

if (c.isRestriction()) {

Restriction r = c.asRestriction();

if (r.isAllValuesFromRestriction()) {

AllValuesFromRestriction av = r.asAllValuesFromRestriction(); System.out.println( "AllValuesFrom class " +

av.getAllValuesFrom().getURI() +

" on property " + av.getOnProperty().getURI() ); }

} }

Ngoài ra còn những thuộc tính khác, có thể tham khảo theo link trên.

Một phần của tài liệu sử dụng ontology và owl biểu diễn và giải các bài toán logic LOGIC PUZZLEs (Trang 58)