Search code examples
javaowlowl-api

Parse a .ttl file and map it to a Java class


I am new to OWL 2, and I want to parse a ".ttl" file with OWL API, but I found that OWL API is not same as the API I used before. It seems that I should write a "visitor" if I want to get the content within a OWLAxiom or OWLEntity, and so on. I have read some tutorials, but I didn't get the proper way to do it. Also, I found the tutorials searched were use older version of owl api. So I want a detailed example to parse a instance, and store the content to a Java class.


I have made some attempts, my codes are as follows, but I don't know to go on.


OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

File file = new File("./source.ttl");

OWLOntology localAcademic = manager.loadOntologyFromOntologyDocument(file);

Stream<OWLNamedIndividual> namedIndividualStream = localAcademic.individualsInSignature();

Iterator<OWLNamedIndividual> iterator = namedIndividualStream.iterator();

while (iterator.hasNext()) {

     OWLNamedIndividual namedIndividual = iterator.next();

}

Instance for example are as follows. Specially, I want store the "@en" in the object of "ecrm:P3_has_note".


<http://data.doremus.org/performance/4db95574-8497-3f30-ad1e-f6f65ed6c896>
    a                      mus:M42_Performed_Expression_Creation ;
    ecrm:P3_has_note       "Créée par Teodoro Anzellotti, son commanditaire, en novembre 1995 à Rotterdam"@en ;
    ecrm:P4_has_time-span  <http://data.doremus.org/performance/4db95574-8497-3f30-ad1e-f6f65ed6c896/time> ;
    ecrm:P9_consists_of    [ a                        mus:M28_Individual_Performance ;
                             ecrm:P14_carried_out_by  "Teodoro Anzellotti"
                           ] ;
    ecrm:P9_consists_of    [ a                        mus:M28_Individual_Performance ;
                             ecrm:P14_carried_out_by  "à Rotterdam"
                           ] ;
    efrbroo:R17_created    <http://data.doremus.org/expression/2fdd40f3-f67c-30a0-bb03-f27e69b9f07f> ;
    efrbroo:R19_created_a_realisation_of
            <http://data.doremus.org/work/907de583-5247-346a-9c19-e184823c9fd6> ;
    efrbroo:R25_performed  <http://data.doremus.org/expression/b4bb1588-dd83-3915-ab55-b8b70b0131b5> .

The contents I want are as follows:


class Instance{
    String subject;
    Map<String, Set<Object>> predicateToObject = new HashMap<String,Set<Object>>();
}

class Object{
    String value;
    String type;
    String language = null;
}

The version of owlapi I am using is 5.1.0. I download the jar and the doc from there. I just want to know how to get the content I need in the java class.


If there are some tutorials that describe the way to do it, please tell me.

Thanks a lot.


Update: I have known how to do it, when I finish it, I will write an answer, I hope it can help latecomers of OWLAPI.


Thanks again.


Solution

  • What you need, once you have the individual, is to retrieve the data property assertion axioms and collect the literals asserted for each property.

    So, in the for loop in your code:

    // Let's rename your Object class to Literal so we don't get confused with java.lang.Object
    Instance instance = new Instance();
    localAcademic.dataPropertyAssertionAxioms()
        .forEach(ax -> instance.predicateToObject.put(
            ax.getProperty().getIRI().toString(),
            Collections.singleton(new Literal(ax.getObject))));
    

    This code assumes properties only appear once - if your properties appear multiple times, you'll have to check whether a set already exists for the property and just add to it instead of replacing the value in the map. I left that out to simplify the example.

    A visitor is not necessary for this scenario, because you already know what axiom type you're interested in and what methods to call on it. It could have been written as an OWLAxiomVisitor implementing only visit(OWLDataPropertyAssertionAxiom) but in this case there would be little advantage in doing so.