如何使用java从.owl文件获取注释

如何使用java从.owl文件获取注释,java,Java,我有一个包含基因本体注释的.owl文件。我需要使用java从.OWL文件中检索注释source forge()中的OWL API具有所有基本功能, 下面是一个使用OWLAPI库解析OWL本体的示例 import static org.semanticweb.owlapi.search.Searcher.annotations; import static org.semanticweb.owlapi.vocab.OWLRDFVocabulary.RDFS_LABEL; import java.

我有一个包含基因本体注释的.owl文件。我需要使用java从.OWL文件中检索注释source forge()中的OWL API具有所有基本功能, 下面是一个使用OWLAPI库解析OWL本体的示例

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();
    }
}
导入静态org.semanticweb.owlapi.search.Searcher.annotations;
导入静态org.semanticweb.owlapi.vocab.owlrdfvaculary.RDFS_标签;
导入java.util.ArrayList;
导入java.util.List;
导入org.semanticweb.owlapi.apibinding.OWLManager;
导入org.semanticweb.owlapi.model.IRI;
导入org.semanticweb.owlapi.model.owlanotation;
导入org.semanticweb.owlapi.model.OWLAnnotationProperty;
导入org.semanticweb.owlapi.model.OWLClass;
导入org.semanticweb.owlapi.model.OWLDataFactory;
导入org.semanticweb.owlapi.model.OWLException;
导入org.semanticweb.owlapi.model.OWLLiteral;
导入org.semanticweb.owlapi.model.OWLOntology;
导入org.semanticweb.owlapi.model.owletologyCreationException;
导入org.semanticweb.owlapi.model.owletologyManager;
公共类OwlParser{
私有本体论;
私人数据工厂df;
公共OwlParser(owldontology本体,OWLDataFactory-df){
本体=本体;
this.df=df;
}
公共本体()
抛出OntologyCreationException{
for(OWLClass:ontology.getClassesInSignature()){
字符串id=cls.getIRI().toString();
字符串label=get(cls,RDFS_label.toString()).get(0);
System.out.println(标签+“[”+id+“]”);
}
}
私有列表获取(owlclasclazz,String属性){
List ret=new ArrayList();
最终OWLAnnotationProperty owlProperty=df
.getOWLAnnotationProperty(IRI.create(property));
for(owlo:ontology.getImportClosure()){
用于(OWL)注释:注释(
o、 getAnnotationAssertionAxioms(clazz.getIRI()),owlProperty)){
if(注释.getValue()实例OWLLiteral){
OWLLiteral val=(OWLLiteral)annotation.getValue();
ret.add(val.getLiteral());
}
}
}
返回ret;
}
公共静态void main(字符串[]args)引发异常,
实例化异常,非法访问异常,
ClassNotFoundException{
字符串x=”http://ontology.neuinfo.org/NIF/Dysfunction/NIF-Dysfunction.owl";
IRI documentIRI=IRI.create(x);
OWLOntologyManager=OWLManager.createOWLOntologyManager();
本体=管理器
.loadOntologyFromOntologyDocument(文档IRI);
OwlParser parser=新的OwlParser(ontology,manager.getOWLDataFactory());
parser.parseOntology();
}
}

我能收到你的邮件吗,这样我们就可以成交了!!线程“main”java.lang.IndexOutOfBoundsException:索引:0,大小:0位于java.util.ArrayList.rangeCheck(未知源代码)位于java.util.ArrayList.get(未知源代码)位于OwlParser.parseOntology(OwlParser.java:49)位于OwlParser.main(OwlParser.java:84)它给出了错误。我试图解决它,但无法解决@杰伊·史密斯