2010-07-29 20 views

Trả lời

1

Ngữ cảnh là gì? OWL là một định dạng bản thể đọc được đọc bởi http://protege.stanford.edu/.

+0

tôi muốn đọc tệp .owl trong java và hiển thị các khái niệm và khái niệm phụ của nó – nagender

0

Bạn có một số tùy chọn.

Tệp .owl là các tệp văn bản và bạn có thể hiển thị chúng theo cách đó.

.owl sử dụng XML, vì vậy bạn có thể coi nó như một tài liệu XML. Tham khảo http://www.w3.org/TR/owl-xmlsyntax/http://www.w3.org/TR/owl2-overview/ để biết danh sách các thẻ và những gì chúng đại diện.

Hoặc bạn có thể sử dụng API OWL. Bạn có thể tải nó tại địa chỉ: http://owlapi.sourceforge.net/index.html, và có một số ví dụ tại http://owlapi.sourceforge.net/documentation.html

Hiển thị và OWL ontology là hơi khó khăn vì thông tin bạn muốn hiển thị có thể được liên kết cao, vì vậy cấu trúc của nó là của một đồ thị chứ không phải tuần tự hoặc dạng bảng. Có thể cho các lớp được phân lớp nhiều lớp con khác, và phân loại theo chu kỳ là có thể. Tức là, A có thể là một phân lớp của B, có thể là một phân lớp của C, có thể là một phân lớp của A.

5

API OWL trong mã nguồn (http://owlapi.sourceforge.net/) có tất cả các chức năng cơ bản, mặc dù tài liệu đủ. Bạn có thể sẽ lãng phí thời gian để tìm ra cách các hàm phức tạp không được hiển thị trong các ví dụ.

Tôi khuyên bạn nên sử dụng API Protege cho các tệp OWL. (http://protegewiki.stanford.edu/wiki/ProtegeOWL_API_Programmers_Guide /). API này có tài liệu tốt và wiki dễ điều hướng. Các tệp OWL không dễ làm việc xung quanh vì tính chất ngữ nghĩa của nó và việc xây dựng API của riêng bạn có thể không dễ dàng. Protege cũng có SWRL API nếu bạn muốn xử lý tiên đề và quy tắc.

0

Dưới đây là một ví dụ để phân tích một ontology OWL với thư viện OWL API:

import static org.semanticweb.owlapi.search.Searcher.annotations; 
import static org.semanticweb.owlapi.vocab.OWLRDFVocabulary.RDFS_LABEL; 

import java.util.ArrayList; 
import java.util.List; 

import org.semanticweb.owlapi.apibinding.OWLManager; 
import org.semanticweb.owlapi.model.IRI; 
import org.semanticweb.owlapi.model.OWLAnnotation; 
import org.semanticweb.owlapi.model.OWLAnnotationProperty; 
import org.semanticweb.owlapi.model.OWLClass; 
import org.semanticweb.owlapi.model.OWLDataFactory; 
import org.semanticweb.owlapi.model.OWLException; 
import org.semanticweb.owlapi.model.OWLLiteral; 
import org.semanticweb.owlapi.model.OWLOntology; 
import org.semanticweb.owlapi.model.OWLOntologyCreationException; 
import org.semanticweb.owlapi.model.OWLOntologyManager; 

public class OwlParser { 

    private final OWLOntology ontology; 
    private OWLDataFactory df; 

    public OwlParser(OWLOntology ontology, OWLDataFactory df) { 
     this.ontology = ontology; 
     this.df = df; 
    } 

    public void parseOntology() 
      throws OWLOntologyCreationException { 

     for (OWLClass cls : ontology.getClassesInSignature()) { 
      String id = cls.getIRI().toString(); 
      String label = get(cls, RDFS_LABEL.toString()).get(0); 
      System.out.println(label + " [" + id + "]"); 
     } 
    } 

    private List<String> get(OWLClass clazz, String property) { 
     List<String> ret = new ArrayList<>(); 

     final OWLAnnotationProperty owlProperty = df 
       .getOWLAnnotationProperty(IRI.create(property)); 
     for (OWLOntology o : ontology.getImportsClosure()) { 
      for (OWLAnnotation annotation : annotations(
        o.getAnnotationAssertionAxioms(clazz.getIRI()), owlProperty)) { 
       if (annotation.getValue() instanceof OWLLiteral) { 
        OWLLiteral val = (OWLLiteral) annotation.getValue(); 
        ret.add(val.getLiteral()); 
       } 
      } 
     } 
     return ret; 
    } 

    public static void main(String[] args) throws OWLException, 
      InstantiationException, IllegalAccessException, 
      ClassNotFoundException { 

     String x = "http://ontology.neuinfo.org/NIF/Dysfunction/NIF-Dysfunction.owl"; 

     IRI documentIRI = IRI.create(x); 
     OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); 
     OWLOntology ontology = manager 
       .loadOntologyFromOntologyDocument(documentIRI); 
     OwlParser parser = new OwlParser(ontology, manager.getOWLDataFactory()); 
     parser.parseOntology(); 
    } 
} 
0

Có thêm một cách sử dụng api jena trong JAVA nhưng bạn phải tạo SDB hoặc TDB file cho file OWL cho . Sau đó, bạn có thể truy vấn bằng SPARQL. JENA API

Các vấn đề liên quan