Java 类org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException 实例源码

项目:minerva    文件:CoreMolecularModelManager.java   
/**
 * Try to load (or replace) a model with the given ontology. It is expected
 * that the content is an A-Box ontology, which imports the T-BOX. Also the
 * ontology ID is used to extract the modelId.<br>
 * <br>
 * This method will currently <b>NOT<b> work due to a bug in the OWL-API.
 * The functional syntax parser does not properly report the exceptions and
 * will return an ontology with an wrong ontology ID!
 * 
 * @param modelData
 * @return modelId
 * @throws OWLOntologyCreationException
 */
public ModelContainer importModel(String modelData) throws OWLOntologyCreationException {
    // load data from String
    final OWLOntologyManager manager = graph.getManager();
    final OWLOntologyDocumentSource documentSource = new StringDocumentSource(modelData);
    OWLOntology modelOntology;
    final Set<OWLParserFactory> originalFactories = removeOBOParserFactories(manager);
    try {
        modelOntology = manager.loadOntologyFromOntologyDocument(documentSource);
    }
    catch (OWLOntologyAlreadyExistsException e) {
        // exception is thrown if there is an ontology with the same ID already in memory 
        OWLOntologyID id = e.getOntologyID();
        IRI existingModelId = id.getOntologyIRI().orNull();

        // remove the existing memory model
        unlinkModel(existingModelId);

        // try loading the import version (again)
        modelOntology = manager.loadOntologyFromOntologyDocument(documentSource);
    }
    finally {
        resetOBOParserFactories(manager, originalFactories);
    }

    // try to extract modelId
    IRI modelId = null;
    Optional<IRI> ontologyIRI = modelOntology.getOntologyID().getOntologyIRI();
    if (ontologyIRI.isPresent()) {
        modelId = ontologyIRI.get();
    }
    if (modelId == null) {
        throw new OWLOntologyCreationException("Could not extract the modelId from the given model");
    }
    // paranoia check
    ModelContainer existingModel = modelMap.get(modelId);
    if (existingModel != null) {
        unlinkModel(modelId);
    }

    // add to internal model
    ModelContainer newModel = addModel(modelId, modelOntology);

    // update imports
    updateImports(newModel);

    return newModel;
}
项目:align-api-project    文件:OWLAPI3OntologyFactory.java   
@Override
   public HeavyLoadedOntology loadOntology( URI uri ) throws OntowrapException {
OWLAPI3Ontology onto = null;
//System.err.println( " Loading ontology "+uri );
// Cache seems to be implemented in API 3.0 anyway
// and it seems to not work well with this one
onto = cache.getOntologyFromURI( uri );
//System.err.println( "   cache1: "+onto );
if ( onto != null ) return onto;
onto = cache.getOntology( uri );
//System.err.println( "   cache2: "+onto );
if ( onto != null ) return onto;
// OWLAPI's own cache
IRI ontoIRI = IRI.create( uri );
OWLOntology ontology = manager.getOntology( ontoIRI );
//System.err.println( "   cache3: "+ontology );

try {
    // This below does not seem to work!
    //ontology = manager.loadOntologyFromOntologyDocument( IRI.create( uri ) );
    if ( ontology == null ) ontology = manager.loadOntology( ontoIRI );
    //System.err.println( "   loaded: "+ontology );
    // I must retrieve it from cache and return it!
} catch ( OWLOntologyDocumentAlreadyExistsException oodaeex ) { // should never happen
    // This is a cache failure
    throw new OntowrapException("Already loaded [doc cache failure] " + uri, oodaeex );
} catch ( OWLOntologyAlreadyExistsException ooaeex ) {
    // This happens when the ontology has been loaded from a different URIs
    ontology = manager.getOntology( ooaeex.getOntologyID() );
    if ( ontology == null )
    throw new OntowrapException("Already loaded [owl cache failure] " + uri, ooaeex );
} catch ( OWLOntologyCreationException oocex ) {
    oocex.printStackTrace();
    throw new OntowrapException("Cannot load " + uri, oocex );
}
onto = new OWLAPI3Ontology();
onto.setFormalism( formalismId );
onto.setFormURI( formalismUri );
onto.setOntology( ontology );
onto.setFile( uri );
try {
    onto.setURI( ontology.getOntologyID().getOntologyIRI().toURI() );
} catch ( Exception e ) { // Should be a NullPointerException
    // Better put in the OntowrapException of loaded
    // The ontology has no URI. In principle, it is not valid
    // It may be possible to put the uri instead (now it is void)
    e.printStackTrace();
}
cache.recordOntology( uri, onto );
//System.err.println( "   after-cache: "+cache.getOntology( uri ) );
return onto;
   }