owlcs / owlapi

OWL API main repository
822 stars 315 forks source link

Question: how to convert a RDF TTL to an Object/Class? #1014

Closed BoraBak closed 2 years ago

BoraBak commented 3 years ago

I have a TTL file format which I want to convert/unmarshal to its relevant classes. Does this capability exists? If so, can someone please supply an example for it?

ignazio1977 commented 3 years ago

Hi, can you be more specific about which classes are you referring to? Do you wish to parse the input and access the ontology within, including any declared OWLClass, or is it something else?

If you want to access the OWL classes declared in the ontology file, then TTL is one of the supported formats and all you need to do is parse it as in the existing documentation https://github.com/owlcs/owlapi/wiki/Tutorial:-A-starter's-starter

BoraBak commented 3 years ago

Hi @ignazio1977, thanks for answering. What I mean is:

Also, posted a question in stackverflow.

BoraBak commented 3 years ago

So I loaded the TTL file, such that I'm holding an ontology.

        File file = new File("src/main/java/hosts.ttl");

        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);
        ontology.saveOntology(new FunctionalSyntaxDocumentFormat(), System.out);

The thing is, I can't understand how I transfer it to POJO's.

ignazio1977 commented 3 years ago

OWLAPI doesn't support turning an owl ontology into beanlike objects. What it supports is access to the axioms, name, and annotations for the ontology, as well as adding and removing axioms.

All of these are regular java classes, the only real difference with pojo is that setters are absent (OWLOntology is the only mutable object to come out of parsing).

If you can describe what you intend to do with the objects, we might have something more precise to recommend.

BoraBak commented 3 years ago

The TTL file I have, is an output coming from an RDFox engine calculations. At the end of the day, I'd like to present those calculations, i.e. the TTL output, via a UI. For that, I have a backend service which responsible for converting it to something (POJO) which will be sent to the fronted service. At least, this is the purpose...

ignazio1977 commented 3 years ago

I don't have experience with RDFox myself, from what I can find its output should be plain RDF triples - also, I'm assuming no blank nodes in the output you want to show, otherwise it would be quite difficult to create a visual representation that makes sense without a connection to the input data.

Plain RDF would not contain an ontology, so parsing the file like above will give you an anonymous ontology. The triples should be complete axioms, so you can list them with OWLOntology::getAxioms(), and a toString() call on each will give you a textual representation of them. This might not be good enough for your needs, but will allow you to explore the data to get an idea of what they look like.

If the RDFox output contains triples that are not OWL axioms, the OWLAPI will not be able to parse them. In that case, you are probably better off using a RDF oriented API, such as Apache Jena.

BoraBak commented 3 years ago

The RDFox output contains simple triples. A .ttl file example:

@prefix owl:    <http://www.w3.org/2002/07/owl#> .
@prefix ns0:    <http://example.org/prop#> .

ns0:Host_48d491d1-7998-59d6-b3ed-07a0868d7536_531
    a                                 ns0:Host ;
    ns0:Exploitable                   "XXE", "PEC";
    ns0:Account                       ns0:Account_0abb0c18-9fb7-57f2-8bcd-2cb07ffbe865_161 ;
    ns0:HostSignificance              "3" ;
    ns0:ID                            "48d491d1-7998-59d6-b3ed-07a0868d7536_531" .

ns0:Account_0abb0c18-9fb7-57f2-8bcd-2cb07ffbe865_161
    a              ns0:Account ;
    ns0:GroupName  "Administrators" ;
    ns0:ID         "0abb0c18-9fb7-57f2-8bcd-2cb07ffbe865_161" ;
    ns0:Privileges "SeRemoteInteractiveLogonRight" ;
    ns0:UserId     "161" ;
    ns0:Username   "administrator" .

By simple triples I mean without even having the metadata of its properties. For example, not having the following info:

ns0:Host
    a owl:ObjectProperty, owl:Class .

ns0:Account
    a owl:ObjectProperty, owl:DatatypeProperty, owl:Class .

ns0:Exploitable
    a owl:DatatypeProperty .

ns0:GroupName
    a owl:DatatypeProperty .

ns0:HostSignificance
    a owl:DatatypeProperty .

ns0:ID
    a owl:DatatypeProperty .

ns0:IP
    a owl:DatatypeProperty .

ns0:LocalAccountTokenFilterPolicy
    a owl:DatatypeProperty .

ns0:LsaRunAsPPL
    a owl:DatatypeProperty .

ns0:Privileges
    a owl:DatatypeProperty .

ns0:UserId
    a owl:DatatypeProperty .

ns0:Username
    a owl:DatatypeProperty .
ignazio1977 commented 3 years ago

Thanks for the example. Lacking declarations, it means that the OWLAPI will most likely have troubles parsing some of the triples - i.e., UserId might be annotations or data property; RDF does not care but OWL does, so OWLAPI will default to annotation property, which might be incorrect. To fix this, you'd have to modify the file before processing to add an import to the relevant ontology with entity declarations, and possibly to fix RDF only constructs. I'm not sure if the extra processing required is worth your time or if you would be better off using a different library for the process. I'd consider using Apache Jena myself, in this situation.