How-to serialize OWLClassExpression to triples

48 Views Asked by At

I am trying to parse a Manchester syntax class expression, and retrieve its corresponding RDF serialization triples. This question helped me a lot : OWLAPI : "ParserException" while converting String to Class Expression using ManchesterOWLSyntaxParser.

I want to move from Pizza and (not (hasTopping some FishTopping)) and (not (hasTopping some MeatTopping)) to

(_:fe7fad725e5c44e081bfa45c97ad15731, http://www.w3.org/2002/07/owl#intersectionOf, _:fe7fad725e5c44e081bfa45c97ad15732) [null]
(_:fe7fad725e5c44e081bfa45c97ad15731, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Class) [null]
(_:fe7fad725e5c44e081bfa45c97ad15732, http://www.w3.org/1999/02/22-rdf-syntax-ns#first, http://www.co-ode.org/ontologies/pizza/pizza.owl#Pizza) [null]
(_:fe7fad725e5c44e081bfa45c97ad15732, http://www.w3.org/1999/02/22-rdf-syntax-ns#rest, _:fe7fad725e5c44e081bfa45c97ad15733) [null]
(_:fe7fad725e5c44e081bfa45c97ad15733, http://www.w3.org/1999/02/22-rdf-syntax-ns#first, _:fe7fad725e5c44e081bfa45c97ad15734) [null]
(_:fe7fad725e5c44e081bfa45c97ad15733, http://www.w3.org/1999/02/22-rdf-syntax-ns#rest, _:fe7fad725e5c44e081bfa45c97ad15736) [null]
(_:fe7fad725e5c44e081bfa45c97ad15734, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Class) [null]
(_:fe7fad725e5c44e081bfa45c97ad15734, http://www.w3.org/2002/07/owl#complementOf, _:fe7fad725e5c44e081bfa45c97ad15735) [null]
(_:fe7fad725e5c44e081bfa45c97ad15735, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Restriction) [null]
(_:fe7fad725e5c44e081bfa45c97ad15735, http://www.w3.org/2002/07/owl#onProperty, http://www.co-ode.org/ontologies/pizza/pizza.owl#hasTopping) [null]
(_:fe7fad725e5c44e081bfa45c97ad15735, http://www.w3.org/2002/07/owl#someValuesFrom, http://www.co-ode.org/ontologies/pizza/pizza.owl#FishTopping) [null]
(_:fe7fad725e5c44e081bfa45c97ad15736, http://www.w3.org/1999/02/22-rdf-syntax-ns#first, _:fe7fad725e5c44e081bfa45c97ad15737) [null]
(_:fe7fad725e5c44e081bfa45c97ad15736, http://www.w3.org/1999/02/22-rdf-syntax-ns#rest, http://www.w3.org/1999/02/22-rdf-syntax-ns#nil) [null]
(_:fe7fad725e5c44e081bfa45c97ad15737, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Class) [null]
(_:fe7fad725e5c44e081bfa45c97ad15737, http://www.w3.org/2002/07/owl#complementOf, _:fe7fad725e5c44e081bfa45c97ad15738) [null]
(_:fe7fad725e5c44e081bfa45c97ad15738, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2002/07/owl#Restriction) [null]
(_:fe7fad725e5c44e081bfa45c97ad15738, http://www.w3.org/2002/07/owl#onProperty, http://www.co-ode.org/ontologies/pizza/pizza.owl#hasTopping) [null]
(_:fe7fad725e5c44e081bfa45c97ad15738, http://www.w3.org/2002/07/owl#someValuesFrom, http://www.co-ode.org/ontologies/pizza/pizza.owl#MeatTopping) [null]

I can get from the original String to an OWLClassExpression. But then how do I get the corresponding set of triples from the OWLClassExpression ?

Thanks !

2

There are 2 best solutions below

1
On BEST ANSWER

Put the classes in an axiom, the axiom in an ontology, and save with a triple oriented format such as ntriples.

1
On

For those interested, here is what I did - how complicated for what seems to be a simple operation !

// the class expression for which I want to retrieve the triples
OWLClassExpression expr = ... ;

// add it as a equivalent class in a temp ontology
OWLOntology tempOntology = manager.createOntology();
tempOntology.add(df.getOWLEquivalentClassesAxiom(df.getOWLClass("http://x"), expr));

// serialize in Turtle
TurtleDocumentFormat turtle = new TurtleDocumentFormat();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
tempOntology.saveOntology(turtle, baos);

// reparse the turtle to get the triples
Model ontologyModel = new LinkedHashModel();
StatementCollector collector = new StatementCollector();
RDFParser parser = RDFParserRegistry.getInstance().get(RDFFormat.TURTLE).get().getParser();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
parser.setRDFHandler(collector);
parser.parse(bais);

// save in a Model
 ontologyModel.addAll(collector.getStatements());

// retrieve the fake entity to which the equivalentClass is attached
ValueFactory factory = SimpleValueFactory.getInstance();
Value equivalentClassEntity = ontologyModel.filter(
                    factory.createIRI("http://x"),
                    factory.createIRI("http://www.w3.org/2002/07/owl#equivalentClass"),
                    null
).objects().iterator().next();

// then recurse through the class expression triples
Model theInterestingTriples = retrieveStatementsTreeRec(ontologyModel, (Resource)equivalentClassEntity);

// and print
for (Statement statement : theInterestingTriples) {
            System.out.println(statement);
        }

/**
 * retrieves the triples recursively on blank nodes
 **/
public static Model retrieveStatementsTreeRec(final Model m, Resource r) {
        final Model output = new LinkedHashModel();
        Model statementsWhereRIsSubject = m.filter(r, null, null);
        output.addAll(statementsWhereRIsSubject);
        statementsWhereRIsSubject.forEach(s -> {
            if(s.getObject() instanceof BNode) {
                output.addAll(retrieveStatementsTreeRec(m,(Resource)s.getObject()));
            }   
        });
        return output;
    }