1

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 !

ThomasFrancart
  • 470
  • 3
  • 13
  • I guess you should have look at the class `TurtleRenderer` in that case. Especially the method `renderObject` should do what you're interested in. – UninformedUser Aug 11 '23 at 09:28
  • What do you plan to do with these triples? Internally they don't exist except when parsing/writing to a triple oriented format (turtle, rdf/xml, ntriples...). – Ignazio Aug 11 '23 at 19:59
  • I am planning to write them to a triple-oriented format. Basically what I am building is a way for a user to enter a Manchester syntax class expression in a spreadsheet, and output the corresponding RDF triples. – ThomasFrancart Aug 21 '23 at 14:56

2 Answers2

0

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

Ignazio
  • 10,504
  • 1
  • 14
  • 25
  • Unfortunately this seems to be the only solution... At least it would be nice to be able to retrieve the triples as objects instead of being forced to serialize, and then reparse the serialisation. – ThomasFrancart Aug 22 '23 at 07:06
0

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;
    }
ThomasFrancart
  • 470
  • 3
  • 13
  • 1
    An alternative would be to work with RioStorer - These are classes that interface between an OWLOntology and a set of Statements, where the translation from OWLAPI objects to triples is carried out but not all the way to String content, so it provides a bit more structure and a bit less parsing of plain text. However it's not a straightforward job either way. – Ignazio Aug 22 '23 at 19:27