[protege-owl] OWL import file overwritten

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[protege-owl] OWL import file overwritten

Wiley, James J. (DMS/CISD)
OWL import file overwritten

Hello All,

I have been digging through the archives for the past few days and have come across a number of confusing examples of how to deal with imports.  My current project requires the use of namespace prefixes and imports in an extensive manner.  In all cases I will need to import an ontology, extend the import , save the extended import under a different ontology, and then create individuals from the extended import and save that under yet a different ontology.  I finally got an implementation to work... or so I thought.  As it turns out, it only works once.  If I run the code a second time, I run into a null pointer exception because the Import1.owl file has had its OWLClass and OWLDatatypeProperty removed.  I'm using Protege 3.2 Beta and OWL Plugin 2.2 Build 322.

The following is the Import1.owl file and the source code for ImportTest.java.  Thanks in advance for your responses.

Import1.owl:

<?xml version="1.0"?>
<rdf:RDF
    xmlns="file:///c:/_dev/test/Import1.owl#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
  xml:base="file:///c:/_dev/test/Import1.owl">
  <owl:Ontology rdf:about=""/>
  <owl:Class rdf:ID="Import1Class"/>
  <owl:DatatypeProperty rdf:ID="import1DatatypeProperty">
    <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
  </owl:DatatypeProperty>
</rdf:RDF>

<!-- Created with Protege (with OWL Plugin 2.2, Build 322)  http://protege.stanford.edu -->

ImportTest.java:

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;

import com.hp.hpl.jena.util.FileUtils;

import edu.stanford.smi.protege.util.URIUtilities;
import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
import edu.stanford.smi.protegex.owl.jena.parser.ProtegeOWLParser;
import edu.stanford.smi.protegex.owl.model.OWLCardinality;
import edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty;
import edu.stanford.smi.protegex.owl.model.OWLIndividual;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
import edu.stanford.smi.protegex.owl.model.OWLObjectProperty;
import edu.stanford.smi.protegex.owl.model.OWLOntology;

public class ImportTest {

        /**
         * @param args
         */
        public static void main(String[] args) {
                JenaOWLModel owlModel = null;
                String import1Path = "C:\\_dev\\test\\Import1";
                String import2Path = "C:\\_dev\\test\\Import2";

                System.out.println("Creating initial OWL Model");
                // create a new model importing an external model
                try {
                        owlModel = ProtegeOWL.createJenaOWLModel();
                        URI uri = URIUtilities.createURI(import1Path + ".owl");
                        /*
                         * This step adds the import to the model so it can be accessed
                         * programatically using the prefix "imp1" before any class or
                         * property names.
                         */
                        ProtegeOWLParser.addImport(owlModel, uri, "imp1");
                        /*
                         * This step adds information to the model so when it is written to
                         * file and possibly imported later the proper OWL types are
                         * preserved. Otherwise OWLDatatypeProperties are converted to
                         * RDFDatatypeProperties, namespace prefixes are lost, and who knows
                         * what else?
                         */
                        OWLOntology owl = owlModel.getDefaultOWLOntology();
                        owl.addImports(uri);
                        String namespace = "file:///c:/_dev/test/Import2.owl#";
                        owlModel.getNamespaceManager().setDefaultNamespace(namespace);
                } catch (FileNotFoundException ex) {
                        System.out.println(ex.getMessage());
                        System.exit(-1);
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
                // extend the imported model
                /*
                 * This is where the null pointer exception is thrown on the second
                 * run of this code because the Import1.owl file has been changed
                 */
                OWLNamedClass imp1Class = owlModel
                                .getOWLNamedClass("imp1:Import1Class");
                OWLDatatypeProperty data = owlModel
                                .getOWLDatatypeProperty("imp1:import1DatatypeProperty");
                OWLNamedClass wholeClass = owlModel.createOWLNamedSubclass(
                                "wholeClass", imp1Class);
                OWLNamedClass partClass = owlModel.createOWLNamedSubclass("partClass",
                                imp1Class);
                // create a cardinality restriction on a datatype property
                OWLCardinality propr = owlModel.createOWLCardinality(data, 1);
                partClass.addSuperclass(propr);
                // create an object property to represent a whole/part relationship
                OWLObjectProperty hasPart = owlModel.createOWLObjectProperty("hasPart");
                // restrict its range to part class
                hasPart.setRange(partClass);
                // set whole class as the domain
                hasPart.addUnionDomainClass(wholeClass);
                // remove owl:Thing as a Domain
                hasPart.removeUnionDomainClass(owlModel.getOWLThingClass());
                // create a QCR
                OWLCardinality qcr = owlModel.createOWLCardinality(hasPart, 1);
                wholeClass.addSuperclass(qcr);
                // write the second model out to file
                Collection errors = new ArrayList();
                owlModel.save(new File(import2Path + ".owl").toURI(),
                                FileUtils.langXMLAbbrev, errors);

                // create a new model to hold the individuals importing our latest save
                try {
                        owlModel = ProtegeOWL.createJenaOWLModel();
                        /*
                         * I plan on accessing members of both the imp1 and imp2 ontologies.
                         * Both need to be added through the ProtegeOWLParser in order to
                         * maintain the prefixes
                         */
                        URI uri1 = URIUtilities.createURI(import1Path + ".owl");
                        URI uri2 = URIUtilities.createURI(import2Path + ".owl");
                        ProtegeOWLParser.addImport(owlModel, uri1, "imp1");
                        ProtegeOWLParser.addImport(owlModel, uri2, "imp2");
                        OWLOntology owl = owlModel.getDefaultOWLOntology();
                        /*
                         * Once again, these lines are needed so when the Individuals file
                         * is output it properly references the OWL types
                         */
                        owl.addImports(uri1);
                        owl.addImports(uri2);
                        String namespace = "file:///c:/_dev/test/Import2Individuals.owl#";
                        owlModel.getNamespaceManager().setDefaultNamespace(namespace);
                } catch (FileNotFoundException ex) {
                        System.out.println(ex.getMessage());
                        System.exit(-1);
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
                // create an individual on the second model
                OWLNamedClass imp2Class = owlModel.getOWLNamedClass("imp2:partClass");
                OWLIndividual imp2Individual = imp2Class.createOWLIndividual("part");
                OWLDatatypeProperty imp1Data = owlModel
                                .getOWLDatatypeProperty("imp1:import1DatatypeProperty");
                imp2Individual.setPropertyValue(imp1Data, "partData");
                // write the individual model out to file
                /*
                 * For some reason the Import1.owl file is altered here.  I get the following message:
                 * "Saving import file:///c:/_dev/test/Import1.owl to file:/c:/_dev/test/Import1.owl"
                 */
                errors = new ArrayList();

                owlModel.save(new File(import2Path + "Individuals.owl").toURI(),
                                FileUtils.langXMLAbbrev, errors);
        }

}

Import1.owl file after first run:

<?xml version="1.0"?>
<rdf:RDF
    xmlns="file:///c:/_dev/test/Import1.owl#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
  xml:base="file:///c:/_dev/test/Import1.owl">
  <owl:Ontology rdf:about=""/>
</rdf:RDF>

<!-- Created with Protege (with OWL Plugin 2.2, Build 322)  http://protege.stanford.edu -->

Reply | Threaded
Open this post in threaded view
|

[protege-owl] Re: OWL import file overwritten

James Wiley
Follow up to my original message... I have found a work-around.  It
seems like there may be a bug, but I'm not sure where the bug resides.  
It could be in a couple of places.  I found clues in Protege2Jena.java;
the file that performs the actual saving of the file.  I have found that
importing Import2.owl transitively imports Import1.owl.  However, the
transitively added repository for Import1.owl is marked "writable" for
reasons yet unknown.  What happens in the saveAll method of
Protege2Jena.java is that the Import1.owl file becomes marked for
updating (because it's writable) and, when updated, gets overwritten
with blank data.  I don't know how the transitively added repository
gets marked as writable.  I don't know why the updated data lacks the
original class and datatype properties.  My work-around, although
clumsy, uses the same algorithm that Protege2Jena.java uses.  Except,
when it finds the repository it removes the writable version of the
repository.  Since my code also adds a non-writable version of the
repsoitory, no data is lost in my final ontology

Here is the routine I use to remove the writable Import1.owl repository:

        try {
            Iterator ts =
owlModel.getTripleStoreModel().listUserTripleStores();
            ts.next();
            while (ts.hasNext()) {
                TripleStore tripleStore = (TripleStore) ts.next();
                String name = tripleStore.getName();
                URI ontologyName = new URI(name);
                Repository rep =
owlModel.getRepositoryManager().getRepository(ontologyName);
                if (rep != null) {
                    if (rep.isWritable(ontologyName)) {
                        owlModel.getRepositoryManager().remove(rep);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
which gets called right before I "save" the Import2Individuals.owl file.

Wiley, James J. (DMS/CISD) wrote:

>
> Hello All,
>
> I have been digging through the archives for the past few days and
> have come across a number of confusing examples of how to deal with
> imports.  My current project requires the use of namespace prefixes
> and imports in an extensive manner.  In all cases I will need to
> import an ontology, extend the import , save the extended import under
> a different ontology, and then create individuals from the extended
> import and save that under yet a different ontology.  I finally got an
> implementation to work... or so I thought.  As it turns out, it only
> works once.  If I run the code a second time, I run into a null
> pointer exception because the Import1.owl file has had its OWLClass
> and OWLDatatypeProperty removed.  I'm using Protege 3.2 Beta and OWL
> Plugin 2.2 Build 322.
>
> The following is the Import1.owl file and the source code for
> ImportTest.java.  Thanks in advance for your responses.
>
> Import1.owl:
>
> <?xml version="1.0"?>
> <rdf:RDF
>     xmlns="file:///c:/_dev/test/Import1.owl#"
>     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
>     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>     xmlns:owl="http://www.w3.org/2002/07/owl#"
>   xml:base="file:///c:/_dev/test/Import1.owl">
>   <owl:Ontology rdf:about=""/>
>   <owl:Class rdf:ID="Import1Class"/>
>   <owl:DatatypeProperty rdf:ID="import1DatatypeProperty">
>     <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
>   </owl:DatatypeProperty>
> </rdf:RDF>
>
> <!-- Created with Protege (with OWL Plugin 2.2, Build 322)  
> _http://protege.stanford.edu_ -->
>
> ImportTest.java:
>
> *import* java.io.File;
> *import* java.io.FileNotFoundException;
> *import* java.net.URI;
> *import* java.util.ArrayList;
> *import* java.util.Collection;
>
> *import* com.hp.hpl.jena.util.FileUtils;
>
> *import* edu.stanford.smi.protege.util.URIUtilities;
> *import* edu.stanford.smi.protegex.owl.ProtegeOWL;
> *import* edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
> *import* edu.stanford.smi.protegex.owl.jena.parser.ProtegeOWLParser;
> *import* edu.stanford.smi.protegex.owl.model.OWLCardinality;
> *import* edu.stanford.smi.protegex.owl.model.OWLDatatypeProperty;
> *import* edu.stanford.smi.protegex.owl.model.OWLIndividual;
> *import* edu.stanford.smi.protegex.owl.model.OWLNamedClass;
> *import* edu.stanford.smi.protegex.owl.model.OWLObjectProperty;
> *import* edu.stanford.smi.protegex.owl.model.OWLOntology;
>
> *public** class* ImportTest {
>
>         /**
>          ** @param* args
>          */
>        * public** static** void* main(String[] args) {
>                 JenaOWLModel owlModel =* null*;
>                 String import1Path = "C:\\_dev\\test\\Import1";
>                 String import2Path = "C:\\_dev\\test\\Import2";
>
>                 System./out/.println("Creating initial OWL Model");
>                 // create a new model importing an external model
>                * try* {
>                         owlModel = ProtegeOWL./createJenaOWLModel/();
>                         URI uri = URIUtilities./createURI/(import1Path
> + ".owl");
>                         /*
>                          * This step adds the import to the model so
> it can be accessed
>                          * programatically using the prefix "imp1"
> before any class or
>                          * property names.
>                          */
>                         ProtegeOWLParser./addImport/(owlModel, uri,
> "imp1");
>                         /*
>                          * This step adds information to the model so
> when it is written to
>                          * file and possibly imported later the proper
> OWL types are
>                          * preserved. Otherwise OWLDatatypeProperties
> are converted to
>                          * RDFDatatypeProperties, namespace prefixes
> are lost, and who knows
>                          * what else?
>                          */
>                         OWLOntology owl =
> owlModel.getDefaultOWLOntology();
>                         owl.addImports(uri);
>                         String namespace =
> "_file:///c:/_dev/test/Import2.owl#_";
>                        
> owlModel.getNamespaceManager().setDefaultNamespace(namespace);
>                 }* catch* (FileNotFoundException ex) {
>                         System./out/.println(ex.getMessage());
>                         System./exit/(-1);
>                 }* catch* (Exception e) {
>                         System./out/.println(e.getMessage());
>                 }
>                 // extend the imported model
>                 /*
>                  * This is where the null pointer exception is thrown
> on the second
>                  * run of this code because the Import1.owl file has
> been changed
>                  */
>                 OWLNamedClass imp1Class = owlModel
>                                 .getOWLNamedClass("imp1:Import1Class");
>                 OWLDatatypeProperty data = owlModel
>                                
> .getOWLDatatypeProperty("imp1:import1DatatypeProperty");
>                 OWLNamedClass wholeClass =
> owlModel.createOWLNamedSubclass(
>                                 "wholeClass", imp1Class);
>                 OWLNamedClass partClass =
> owlModel.createOWLNamedSubclass("partClass",
>                                 imp1Class);
>                 // create a cardinality restriction on a datatype
> property
>                 OWLCardinality propr =
> owlModel.createOWLCardinality(data, 1);
>                 partClass.addSuperclass(propr);
>                 // create an object property to represent a whole/part
> relationship
>                 OWLObjectProperty hasPart =
> owlModel.createOWLObjectProperty("hasPart");
>                 // restrict its range to part class
>                 hasPart.setRange(partClass);
>                 // set whole class as the domain
>                 hasPart.addUnionDomainClass(wholeClass);
>                 // remove owl:Thing as a Domain
>                
> hasPart.removeUnionDomainClass(owlModel.getOWLThingClass());
>                 // create a QCR
>                 OWLCardinality qcr =
> owlModel.createOWLCardinality(hasPart, 1);
>                 wholeClass.addSuperclass(qcr);
>                 // write the second model out to file
>                 Collection errors =* new* ArrayList();
>                 owlModel.save(*new* File(import2Path + ".owl").toURI(),
>                                 FileUtils./langXMLAbbrev/, errors);
>
>                 // create a new model to hold the individuals
> importing our latest save
>                * try* {
>                         owlModel = ProtegeOWL./createJenaOWLModel/();
>                         /*
>                          * I plan on accessing members of both the
> imp1 and imp2 ontologies.
>                          * Both need to be added through the
> ProtegeOWLParser in order to
>                          * maintain the prefixes
>                          */
>                         URI uri1 =
> URIUtilities./createURI/(import1Path + ".owl");
>                         URI uri2 =
> URIUtilities./createURI/(import2Path + ".owl");
>                         ProtegeOWLParser./addImport/(owlModel, uri1,
> "imp1");
>                         ProtegeOWLParser./addImport/(owlModel, uri2,
> "imp2");
>                         OWLOntology owl =
> owlModel.getDefaultOWLOntology();
>                         /*
>                          * Once again, these lines are needed so when
> the Individuals file
>                          * is output it properly references the OWL types
>                          */
>                         owl.addImports(uri1);
>                         owl.addImports(uri2);
>                         String namespace =
> "_file:///c:/_dev/test/Import2Individuals.owl#_";
>                        
> owlModel.getNamespaceManager().setDefaultNamespace(namespace);
>                 }* catch* (FileNotFoundException ex) {
>                         System./out/.println(ex.getMessage());
>                         System./exit/(-1);
>                 }* catch* (Exception e) {
>                         System./out/.println(e.getMessage());
>                 }
>                 // create an individual on the second model
>                 OWLNamedClass imp2Class =
> owlModel.getOWLNamedClass("imp2:partClass");
>                 OWLIndividual imp2Individual =
> imp2Class.createOWLIndividual("part");
>                 OWLDatatypeProperty imp1Data = owlModel
>                                
> .getOWLDatatypeProperty("imp1:import1DatatypeProperty");
>                 imp2Individual.setPropertyValue(imp1Data, "partData");
>                 // write the individual model out to file
>                 /*
>                  * For some reason the Import1.owl file is altered
> here.  I get the following message:
>                  * "Saving import _file:///c:/_dev/test/Import1.owl_
> to _file:/c:/_dev/test/Import1.owl_"
>                  */
>                 errors =* new* ArrayList();
>
>                 owlModel.save(*new* File(import2Path +
> "Individuals.owl").toURI(),
>                                 FileUtils./langXMLAbbrev/, errors);
>         }
>
> }
>
> Import1.owl file after first run:
>
> <?xml version="1.0"?>
> <rdf:RDF
>     xmlns="file:///c:/_dev/test/Import1.owl#"
>     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
>     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>     xmlns:owl="http://www.w3.org/2002/07/owl#"
>   xml:base="file:///c:/_dev/test/Import1.owl">
>   <owl:Ontology rdf:about=""/>
> </rdf:RDF>
>
> <!-- Created with Protege (with OWL Plugin 2.2, Build 322)  
> _http://protege.stanford.edu_ -->
>
-------------------------------------------------------------------------
To unsubscribe go to http://protege.stanford.edu/community/subscribe.html

Reply | Threaded
Open this post in threaded view
|

[protege-owl] OWL in web pages

sima soltani
Hi,
 
I want to use the OWL ontology I made in my home page But I don't know how to do this. Can you help me?
 
Best regards
Sima Soltani


Do you Yahoo!?
Next-gen email? Have it all with the all-new Yahoo! Mail Beta.