2017-06-12 3 views
1

Je voudrais ajouter un triple RDF d'un fichier xls dans une ontologie OWL RDF/XML en utilisant l'API OWL. Je suis ce topic et réussi à le faire avec OWL API v 3.4 mais je ne peux pas le faire avec v4.3 (et tout le reste de mon programme utilise v4.3).Ajout d'une ontologie RDF-triple dans owl (OWL API 4.3.0)

Voici le code du sujet ci-dessus travaillant pour la version 3.4:

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); 
    } 
} 

Je sais que certains Packages changed from v3.X to 4.X et il semble que le TurtleParser n'est plus un AnonymousNodeChecker? Dans cet exemple, le consommateur ne gère pas le 2 RDF-Triple dans la v4.3. Si quelqu'un parvient à exécuter cet exemple et à imprimer les deux relations (OWLObjectProperty/OWLDataproperty), ce serait sympa =). Ma dépendance:

<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> 

Merci beaucoup.

P.S. : Je suis français, désolé si ma syntaxe n'est pas parfaite

+0

Pour clarifier mon problème, je veux mettre mon triple dans une ontologie cible dans laquelle toute la classe et la relation sont définies. De plus, je ne connais pas à l'avance le type de triplet que je souhaite ajouter (ClassAssertion, DataPropertyAxiom, ObjectPropertyAxiom). C'est pourquoi je pensais que le RDFConsumer et un Parser pouvaient m'aider. –

Répondre

1

OWLAPI n'est pas orienté RDF, et par conséquent les classes liées RDF sont uniquement destinées à son infrastructure d'analyse, pas pour l'utilisation des entrées - c'est pourquoi les interfaces et les implémentations peuvent changer sans avertissement.

Dans ce cas, vous pouvez contourner le problème en utilisant uniquement les interfaces de module API. Les triplets que vous ajoutez sont des assertions de propriété d'objet et de données, et peuvent être créés en tant que tels via une instance OWLDataFactory.

manager.addAxiom(factory.getOWLObjectPropertyAssertionAxiom(likes, tom, anna); 
manager.addAxiom(factory.getOWLDataPropertyAssertionAxiom(age, tom, factory.getOWLLiteral(35)); 
+0

Merci pour votre réponse. Mon problème est que je ne sais pas à l'avance si mon triple est une relation object/dataProperty/ClassAssertion. Est-il possible d'utiliser l'instance OWLDataFactory sans définir OWLType (OWLEntity) et la relation OWL? Sachant que je veux mettre le triple dans une ontologie cible avec toute la relation définie à l'intérieur. –

+0

Utiliser l'analyseur pour contourner cela ne fonctionnera pas bien. Cela ne peut fonctionner qu'avec ce qui est dans l'ontologie. Une solution de contournement où vous connaissez la propriété iri et devez savoir s'il s'agit d'une propriété d'objet ou de données consiste à utiliser les méthodes owlontology pour vérifier - il existe des méthodes ... pour vérifier la signature de l'ontologie. En outre, vous devez savoir si l'objet est une entité ou un littéral – Ignazio

+0

"Cela ne peut fonctionner qu'avec ce qui est dans l'ontologie": c'est pourquoi j'ai pensé à l'analyseur, je ne crée pas de nouvelle propriété DataProperty ou ObjectProperty ou classe dans mon triple RDF; juste instance de classe existant dans l'ontologie cible et relation entre la nouvelle instance (utilisant Object/DataProperty existant). Mais je vais essayer votre solution puisque vous me dites que c'est possible en utilisant OWLDataFactory. –

0

Le code que j'utilise pour lire dans mon fichier Excel, générer le triple et l'ajouter dans mon ontologie cible:

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(); 
    } 
} 

Et la méthode qui transforme une chaîne en un IRI si elle a un PREFIX dans.

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; 
} 

Et la classe Triple:

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; 
} 
}