1

I would like to add some RDF triple from an xls file into an OWL RDF/XML ontology using OWL API. I follow this topic and manage to do it with OWL API v 3.4 but i can't do it with v4.3 (and all the rest of my program use v4.3).

Here is the code of the topic above working for the 3.4 version :

import java.io.Reader;

import org.coode.owlapi.rdfxml.parser.OWLRDFConsumer;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;

import uk.ac.manchester.cs.owl.owlapi.turtle.parser.TurtleParser;


public class ExampleOWLRDFConsumer {
public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
    // Create an ontology.
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLDataFactory factory = manager.getOWLDataFactory();
    OWLOntology ontology = manager.createOntology();

    // Create some named individuals and an object property.
    String ns = "http://example.org/";
    OWLNamedIndividual tom = factory.getOWLNamedIndividual(IRI.create(ns + "Tom"));
    OWLObjectProperty likes = factory.getOWLObjectProperty(IRI.create(ns + "likes"));
    OWLDataProperty age = factory.getOWLDataProperty(IRI.create(ns + "age"));
    OWLNamedIndividual anna = factory.getOWLNamedIndividual(IRI.create(ns + "Anna"));

    // Add the declarations axioms to the ontology so that the triples involving
    // these are understood (otherwise the triples will be ignored).
    for (OWLEntity entity : new OWLEntity[] {tom, likes, age, anna}) {
        manager.addAxiom(ontology, factory.getOWLDeclarationAxiom(entity));
    }

    // Print the the ontology to see that the entities are declared. 
    // The important result is
    //  <NamedIndividual rdf:about="http://example.org/Tom"/>
    // with no properties
    manager.saveOntology(ontology, System.out);

    // Create an OWLRDFConsumer for the ontology.
    OWLRDFConsumer consumer = new OWLRDFConsumer(ontology, new TurtleParser((Reader) null), new OWLOntologyLoaderConfiguration());

    // The consumer handles (IRI,IRI,IRI) and (IRI,IRI,OWLLiteral) triples.
    consumer.handle(tom.getIRI(), likes.getIRI(), anna.getIRI());
    consumer.handle(tom.getIRI(), age.getIRI(), factory.getOWLLiteral(35));

    // Print the ontology to see the new object and data property assertions.  The import contents is
    // still Tom: 
    //   <NamedIndividual rdf:about="http://example.org/Tom">
    //     <example:age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">35</example:age>
    //     <example:likes rdf:resource="http://example.org/Anna"/>
    //  </NamedIndividual>
    manager.saveOntology(ontology, System.out);
    }
}

I know that some Packages changed from v3.X to 4.X and it seems that the TurtleParser is not anymore a AnonymousNodeChecker ? In this example, the consumer don't handle the 2 RDF-Triple in v4.3. If someone manage to run this example and print the two relations (OWLObjectProperty / OWLDataproperty), it would be nice =). My dependency :

<parent>
    <artifactId>owlapi-parent</artifactId>
    <groupId>net.sourceforge.owlapi</groupId>
    <version>4.3.0</version>
</parent>

<dependency>
    <groupId>net.sourceforge.owlapi</groupId>
    <artifactId>owlapi-compatibility</artifactId>
    <version>4.3.0</version>
</dependency>

Thanks a lot.

P.S. : I'm french, sorry if my syntax is not perfect

Romain G
  • 11
  • 2
  • To clarify my problem, I want to put my triple in a target ontology in which all the class and relation are defined. Furthermore, I don't know in advance the type of triple I want to add (ClassAssertion, DataPropertyAxiom, ObjectPropertyAxiom). That's why I thought that the RDFConsumer and a Parser could help me. – Romain G Jun 13 '17 at 07:26

2 Answers2

1

OWLAPI is not RDF oriented, and as a consequence the RDF related classes are only intended for its parsing infrastructure, not for input use - that's why interfaces and implementations can change without warning.

In this case, you can get around the problem by using api module interfaces only. The triples you're adding are object and data property assertions, and can be created as such through an OWLDataFactory instance.

manager.addAxiom(factory.getOWLObjectPropertyAssertionAxiom(likes, tom, anna);
manager.addAxiom(factory.getOWLDataPropertyAssertionAxiom(age, tom, factory.getOWLLiteral(35));
Ignazio
  • 10,504
  • 1
  • 14
  • 25
  • Thanks for your answering. My problem is that in don't know in advance if my triple are object / dataProperty / ClassAssertion relationship. Is it possible to use the OWLDataFactory instance witout defining the OWLType (OWLEntity) and the OWL Relation ? Knowing that i want to put the triple in a target ontology with all the relation definined inside. – Romain G Jun 13 '17 at 07:09
  • Using the parser to get around that will not work well. It can only work with what is in the ontology already. A workaround where you know the property iri and need to know if it's an object or data property is to use owlontology methods to check - there are contains... methods to check the ontology signature. Also, you need to know if the object is an entity or a literal – Ignazio Jun 13 '17 at 07:43
  • 1
    "It can only work with what is in the ontology already" : that's why I thought about Parser, I don't create any new DataProperty or ObjectProperty or Class in my RDF-triple; just instance of class existing in the target ontology and relation between the new instance (using existing Object / DataProperty). But I will try your solution since you say me it's possible using OWLDataFactory. – Romain G Jun 13 '17 at 07:57
0

The code I use to read in my excel file, generate the triple and add it into my target ontology :

import java.io.File;
import java.io.FileInputStream;
import java.io.Reader;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.coode.owlapi.rdfxml.parser.OWLRDFConsumer;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyFormat;
import org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration;
import org.semanticweb.owlapi.model.OWLOntologyManager;

import uk.ac.manchester.cs.owl.owlapi.turtle.parser.TurtleParser;

public class ReadExcelFile {

public static void main(String[] args){
    try {

        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("./data/ReadExcelTest.xls"));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row;
        HSSFCell cell;

        String object = null;
        String predicat = null;
        String subject = null;
        String[] data = null;

        Object objSubject = null;
        Object objPredicat = null;
        Object objObject = null;

        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLDataFactory df = OWLManager.getOWLDataFactory();
        OWLOntology o = manager.loadOntologyFromOntologyDocument(new File("./targetOntology.owl"));

        IRI targetOntologyIRI= IRI.create("http://targetOntology#");
        IRI cntroIRI = IRI.create("http://informatics.mayo.edu/CNTRO#");
        IRI rdfsIRI = IRI.create("http://www.w3.org/2000/01/rdf-schema#");
        IRI rdfIRI = IRI.create("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        IRI owlIRI = IRI.create("http://www.w3.org/2002/07/owl#");

        int rows = sheet.getPhysicalNumberOfRows();

        int cols = 3;

        //Creation objet Triple
        for(int r = 1; r < rows; r++) {
            row = sheet.getRow(r);
            if(row != null) {
                Triple triple = new Triple(subject, predicat, object);
                for(int c = 0; c < cols; c++) {
                    cell = row.getCell(c);
                    if(cell != null) {
                        if(c == 0) {
                            triple.setSubject(cell.getStringCellValue());
                        }if(c == 1) {
                            triple.setPredicat(cell.getStringCellValue());
                        }if(c == 2) {
                            triple.setObject(cell.getStringCellValue());
                        }
                    }
                }


                objSubject = getIRIFromPrefixName(triple.getSubject());
                objPredicat = getIRIFromPrefixName(triple.getPredicat());
                objObject =  getIRIFromPrefixName(triple.getObject());

                OWLRDFConsumer consumer = new OWLRDFConsumer(o, new TurtleParser((Reader) null), new OWLOntologyLoaderConfiguration() );

                if (objObject.getClass() == String.class){
                    consumer.handle((IRI) objSubject, (IRI) objPredicat, df.getOWLLiteral((String) objObject));
                } else {
                    consumer.handle((IRI) objSubject, (IRI) objPredicat, (IRI) objObject);
                }

                System.out.println(objSubject.toString() + " " + objPredicat.toString() + " " + objObject.toString());
                System.out.println("    " + consumer.getLastAddedAxiom());
            }
        }

        OWLOntologyFormat format = manager.getOntologyFormat(o);
        Path path_instanced_ontology = Paths.get("./data/instanciedTargetOntology.owl");
        File instanced_ontology = new File(path_instanced_ontology.toString());
        manager.saveOntology(o, format, IRI.create(instanced_ontology.toURI()));

    } catch(Exception ioe) {
        ioe.printStackTrace();
    }
}

And the method that transform a String into an IRI if it have an PREFIX into.

static Object getIRIFromPrefixName(String attribute){

    IRI targetOntologyIRI= IRI.create("http://targetOntology#");
    IRI cntroIRI = IRI.create("http://informatics.mayo.edu/CNTRO#");
    IRI rdfsIRI = IRI.create("http://www.w3.org/2000/01/rdf-schema#");
    IRI rdfIRI = IRI.create("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    IRI owlIRI = IRI.create("http://www.w3.org/2002/07/owl#");

    String[] data;
    IRI iriAttribute = null;

    if (attribute.contains("CNTRO:")){
        data = attribute.split(":");
        iriAttribute = IRI.create(cntroIRI + data[1]);
        return iriAttribute;
    } else if (attribute.contains("MEO:")){
        data = attribute.split(":");
        iriAttribute = IRI.create(eventOntologyIRI + data[1]);
        return iriAttribute;
    } else if (attribute.contains("rdfs:")){
        data = attribute.split(":");
        iriAttribute = IRI.create(rdfsIRI + data[1]);
        return iriAttribute;
    } else if (attribute.contains("rdf:")){
        data = attribute.split(":");
        iriAttribute = IRI.create(rdfIRI + data[1]);
        return iriAttribute;
    } else if (attribute.contains("owl:")){
        data = attribute.split(":");
        iriAttribute = IRI.create(owlIRI + data[1]);
        return iriAttribute;
    }
    return attribute;
}

And the Triple class :

public class Triple {

private String subject;
private String predicat;
private String object;

public Triple(String subject, String predicat, String object) {
    super();
    this.subject = subject;
    this.predicat = predicat;
    this.object = object;
}

public String getSubject() {
    return subject;
}

public void setSubject(String subject) {
    this.subject = subject;
}

public String getPredicat() {
    return predicat;
}

public void setPredicat(String predicat) {
    this.predicat = predicat;
}

public String getObject() {
    return object;
}

public void setObject(String object) {
    this.object = object;
}
}
Romain G
  • 11
  • 2