Java Code Examples for org.semanticweb.owlapi.model.IRI#generateDocumentIRI()

The following examples show how to use org.semanticweb.owlapi.model.IRI#generateDocumentIRI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InferenceBuilder.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static OWLGraphWrapper enforceEL(OWLGraphWrapper graph) {
	Optional<IRI> originalIRI = graph.getSourceOntology().getOntologyID().getOntologyIRI();
	IRI newIRI;
	if(originalIRI.isPresent()) {
		String workIRI = originalIRI.get().toString();
		if (workIRI.endsWith(".owl")) {
			workIRI = workIRI.replace(".owl", "-el.owl");
		}
		else {
			workIRI = workIRI + "-el";
		}
		newIRI = IRI.create(workIRI);
	}
	else {
		newIRI = IRI.generateDocumentIRI();
	}
	return enforceEL(graph, newIRI);
}
 
Example 2
Source File: MirrorOperation.java    From robot with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Mirrors ontologies on local filesystem.
 *
 * @param rootOntology The ontology to mirror.
 * @param baseFolder The folder.
 * @param catalogFile The catalog file to write to.
 * @throws IOException on most problems.
 * @throws OWLOntologyStorageException if the ontology cannot be saved.
 */
public static void mirror(OWLOntology rootOntology, File baseFolder, File catalogFile)
    throws IOException, OWLOntologyStorageException {

  logger.info("Mirroring ontologies: " + rootOntology);
  Map<IRI, String> iriMap = new HashMap<>();
  for (OWLOntology ont : rootOntology.getImportsClosure()) {
    logger.info("Mirroring: " + ont);
    validateImports(ont);

    OWLOntologyID ontologyID = ont.getOntologyID();
    IRI ontologyIRI = ontologyID.getOntologyIRI().orNull();

    // Not really sure why this is here, but apparently we can get
    // an ontology without an IRI, in which case we'll generate one
    // that is 'sort of' unique (only fails if two different machines
    // run this tool at the exact same time).
    //
    if (ontologyIRI == null) {
      ontologyIRI = IRI.generateDocumentIRI();
    }
    // Always write the actualIRI
    String localFilePath = getMirrorPathOfOntologyIRI(ontologyIRI);
    IRI outputStream = IRI.create(new File(baseFolder, localFilePath));
    ont.getOWLOntologyManager().saveOntology(ont, outputStream);
    iriMap.put(ontologyIRI, localFilePath);

    //
    // In case there is a difference between the source document IRI
    // and the IRI of the resolved target (e.g., there is an HTTP
    // redirect from a legacy IRI to a newer IRI), then write an entry
    // in the catalog that points the legacy IRI to the newer, canonical
    // one.
    // Examples of this include:
    //  http://purl.obolibrary.org/obo/so.owl
    // which redirects to:
    //  http://purl.obolibrary.org/obo/so-xp.obo.owl
    //

    IRI documentIRI = ont.getOWLOntologyManager().getOntologyDocumentIRI(ont);
    if (!documentIRI.toString().startsWith("file:") && documentIRI != ontologyIRI) {
      String sourceLocalFile = getMirrorPathOfOntologyIRI(ontologyIRI);
      logger.info("Mirroring " + documentIRI + " in " + sourceLocalFile);
      iriMap.put(documentIRI, sourceLocalFile);
    }
  }
  writeCatalog(catalogFile, iriMap);
}
 
Example 3
Source File: ImportClosureSlurper.java    From owltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void save(File baseFolder, BufferedWriter w) throws IOException, OWLOntologyStorageException {
	w.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
	w.write("<catalog prefer=\"public\" xmlns=\"urn:oasis:names:tc:entity:xmlns:xml:catalog\">\n");
	
	for (OWLOntology ont : ontology.getImportsClosure()) {
		validateImports(ont);
		
		OWLOntologyID ontologyID = ont.getOntologyID();
		IRI actualIRI = null;
		Optional<IRI> optional = ontologyID.getOntologyIRI();
		if (optional.isPresent()) {
			actualIRI = optional.get();
		}

		// Not really sure why this is here, but apparently we can get
		// an ontology without an IRI, in which case we'll generate one
		// that is 'sort of' unique (only fails if two different machines run
		// this tool at the exact same time).
		//
		if (actualIRI == null) {
			IRI generatedIRI = IRI.generateDocumentIRI();
			actualIRI = generatedIRI;
		}
		// Always write the actualIRI
		String actualLocalFile = createLocalFileName(actualIRI);
		IRI outputStream = IRI.create(new File(baseFolder, actualLocalFile));
		ont.getOWLOntologyManager().saveOntology(ont, outputStream);
		
		if (LOGGER.isInfoEnabled()) {
			LOGGER.info("name: "+ actualIRI +" local: "+actualLocalFile);
		}
		w.write("  <uri name=\""+ actualIRI  +"\" uri=\""+ actualLocalFile +"\"/>\n");

		//
		// In case there is a difference between the source document IRI
		// and the IRI of the resolved target (e.g., there is an HTTP
		// redirect from a legacy IRI to a newer IRI), then write an entry
		// in the catalog that points the legacy IRI to the newer, canonical one.
		// Examples of this include:
		//  http://purl.obolibrary.org/obo/so.owl
		// which redirects to:
		//  http://purl.obolibrary.org/obo/so-xp.obo.owl
		//

		IRI 			documentIRI = ont.getOWLOntologyManager().getOntologyDocumentIRI(ont);
		if (documentIRI != actualIRI) {
			String sourceLocalFile = createLocalFileName(actualIRI);
			if (LOGGER.isInfoEnabled()) {
				LOGGER.info("alias: "+ documentIRI + " ==> " + actualIRI + " local: "+ sourceLocalFile);
			}
			w.write("  <uri name=\""+ documentIRI +"\" uri=\""+ sourceLocalFile +"\"/>\n");
		}
	}
	w.write("</catalog>\n");
	w.flush();
}