private OWLOntology loadOwlOntology() throws OWLOntologyCreationException { FileDocumentSource fileDocumentSource = new FileDocumentSource(ontologyFile); // use silent import handling strategy to enable ontology loading without internet access // see: https://github.com/molgenis/molgenis/issues/5301 OWLOntologyLoaderConfiguration owlOntologyLoaderConfiguration = new OWLOntologyLoaderConfiguration() .setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT); return manager.loadOntologyFromOntologyDocument( fileDocumentSource, owlOntologyLoaderConfiguration); }
/** * Specifies whether or not annotation axioms (instances of {@code OWLAnnotationAxiom}) should * be loaded or whether they should be discarded on loading. By default, the loading of * annotation axioms is enabled. * * @param b {@code true} if annotation axioms should be loaded, or {@code false} if annotation * axioms should not be loaded and should be discarded on loading. * @return An {@code OWLOntologyLoaderConfiguration} object with the option set. */ public OWLOntologyLoaderConfiguration setLoadAnnotationAxioms(boolean b) { // do not make copies if setting the same value if (isLoadAnnotationAxioms() == b) { return this; } OWLOntologyLoaderConfiguration copy = copyConfiguration(); copy.overrides.put(LOAD_ANNOTATIONS, Boolean.valueOf(b)); return copy; }
/** * @param missingOntologyHeaderStrategy new value * @return a copy of this configuration object with a different strategy */ public OWLOntologyLoaderConfiguration setMissingOntologyHeaderStrategy( MissingOntologyHeaderStrategy missingOntologyHeaderStrategy) { // do not make copies if setting the same value if (getMissingOntologyHeaderStrategy() == missingOntologyHeaderStrategy) { return this; } OWLOntologyLoaderConfiguration copy = copyConfiguration(); copy.overrides.put(MISSING_ONTOLOGY_HEADER_STRATEGY, missingOntologyHeaderStrategy); return copy; }
@Nonnull private static OWLOntologyLoaderConfiguration createLoaderConfig() { return new OWLOntologyLoaderConfiguration() .setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT) .setReportStackTraces(true) // It is safe to turn of illegal punning fixing as we've already parsed // (and saved) the ontology using a manager with this turned on. .setRepairIllegalPunnings(false); }
/** * @param strict new value for strict * @return copy of the configuration with new strict value */ public OWLOntologyLoaderConfiguration setStrict(boolean strict) { // do not make copies if setting the same value if (isStrict() == strict) { return this; } OWLOntologyLoaderConfiguration copy = copyConfiguration(); copy.overrides.put(PARSE_WITH_STRICT_CONFIGURATION, Boolean.valueOf(strict)); return copy; }
@Override public OWLOntology parsePremiseOntology(SerializationFormat format) throws OntologyParseException { try { final OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); manager.setOntologyLoaderConfiguration(manager.getOntologyLoaderConfiguration().setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT)); manager.getIRIMappers().clear(); ImportsHelper.loadImports(manager, this, format); OWLOntology o = parsedPremise.get(format); if (o == null) { final String l = getPremiseOntology(format); if (l == null) { return null; } final StringDocumentSource source = new StringDocumentSource(l); o = manager.loadOntologyFromOntologyDocument(source); parsedPremise.put(format, o); } return o; } catch (final OWLOntologyCreationException e) { throw new OntologyParseException(e); } } }
@Override public Set<URI> getSuggestions(final File f) { try { final IRI iri = IRI.create(f); OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); manager.addIRIMapper(ontologyIRI -> { if (ontologyIRI.equals(iri)) { return IRI.create(f); OWLOntologyLoaderConfiguration configuration = new OWLOntologyLoaderConfiguration(); configuration = configuration.setLoadAnnotationAxioms(false); configuration = configuration.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT); OWLOntology ontology = manager.loadOntology(iri); Set<URI> suggestions = new TreeSet<>(); OWLOntologyID id = ontology.getOntologyID(); if (id.getOntologyIRI().isPresent()) { suggestions.add(id.getOntologyIRI().get().toURI());
PriorityCollection<OWLOntologyIRIMapper> iriMappers = loadingManager.getIRIMappers(); iriMappers.clear(); iriMappers.add(userResolvedIRIMapper); iriMappers.add(new AutoMappedRepositoryIRIMapper(modelManager.getOntologyCatalogManager())); loadingManager.addOntologyLoaderListener(new ProgressDialogOntologyLoaderListener(dlg, logger)); OWLOntologyLoaderConfiguration configuration = new OWLOntologyLoaderConfiguration(); configuration = configuration.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT); IRIDocumentSource documentSource = new IRIDocumentSource(IRI.create(documentURI)); OWLOntology ontology = loadingManager.loadOntologyFromOntologyDocument(documentSource, configuration); Set<OWLOntology> alreadyLoadedOntologies = new HashSet<>(); for (OWLOntology loadedOntology : loadingManager.getOntologies()) { OWLOntologyID id = ontology.getOntologyID(); if (!id.isAnonymous()) { getOntologyManager().getIRIMappers().add(new SimpleIRIMapper(id.getDefaultDocumentIRI().get(), IRI.create(documentURI)));
private void setupSynonymMap() { // We can load legacy ontologies by providing synonyms for built in // vocabulary // where the vocabulary has simply changed (e.g. DAML+OIL -> OWL) synonymMap.clear(); // Legacy protege-owlapi representation of QCRs synonymMap.put(IRI.create(OWL.getPrefixIRI(), "valuesFrom"), OWL_ON_CLASS.getIRI()); if (!configuration.isStrict()) { addDAMLOILVocabulary(); addIntermediateOWLSpecVocabulary(); } }
getConsumer().addOntology(subject); getConsumer().addOntology(object); OWLImportsDeclaration importsDeclaration = getDataFactory().getOWLImportsDeclaration(object); getConsumer().addImport(importsDeclaration); if (!getConsumer().getConfiguration().isIgnoredImport(object)) { OWLOntologyManager man = getConsumer().getOWLOntologyManager(); man.makeLoadImportRequest(importsDeclaration, getConsumer().getConfiguration()); OWLOntology importedOntology = man.getImportedOntology(importsDeclaration); if (importedOntology != null) { OWLOntologyFormat importedOntologyFormat = man.getOntologyFormat(importedOntology); if (importedOntologyFormat instanceof RDFOntologyFormat) { if (importedOntology.isAnonymous()) { OWLOntologyLoaderConfiguration.MissingOntologyHeaderStrategy missingOntologyHeaderStrategy = getConsumer().getConfiguration().getMissingOntologyHeaderStrategy(); boolean includeGraph = missingOntologyHeaderStrategy.equals(OWLOntologyLoaderConfiguration.MissingOntologyHeaderStrategy.INCLUDE_GRAPH); man.applyChange(new RemoveImport(getConsumer().getOntology(), importsDeclaration)); for (OWLImportsDeclaration decl : importedOntology.getImportsDeclarations()) { man.applyChange(new AddImport(getConsumer().getOntology(), decl)); for (OWLAnnotation anno : importedOntology.getAnnotations()) { man.applyChange(new AddOntologyAnnotation(getConsumer().getOntology(), anno));
/** * Returns the ontology for the entry. * @param entry the entry * @return the OWL ontology */ public OWLOntology getOntology(OntologyRepositoryEntry entry) { try(InputStream is = getInputStream(entry.getPhysicalURI().toURL())) { OWLOntologyManager man = OWLManager.createOWLOntologyManager(); man.addMissingImportListener(e -> { System.out.println("Missing import: " + e.getImportedOntologyURI()); }); OWLOntologyLoaderConfiguration conf = new OWLOntologyLoaderConfiguration(); conf.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT); conf.addIgnoredImport(IRI.create("http://www.co-ode.org/ontologies/lists/2008/09/11/list.owl")); man.setOntologyLoaderConfiguration(conf); OWLOntology ont = man.loadOntologyFromOntologyDocument(is); return ont; } catch (Exception e) { e.printStackTrace(); } return null; }
protected void handleImportingRDFGraphRatherThanOntology(OWLImportsDeclaration id, OWLOntologyManager man, @Nullable OWLOntology io) { if (io != null) { OWLDocumentFormat importedOntologyFormat = io.getFormat(); if (importedOntologyFormat instanceof AbstractRDFPrefixDocumentFormat && io.isAnonymous() && consumer.getConfiguration() .getMissingOntologyHeaderStrategy() == INCLUDE_GRAPH) { // We should have just included the triples rather // than imported them. So, // we remove the imports statement, add the axioms // from the imported ontology to // out importing ontology and remove the imported // ontology. // WHO EVER THOUGHT THAT THIS WAS A GOOD IDEA? man.applyChange(new RemoveImport(consumer.getOntology(), id)); io.importsDeclarations().forEach(d -> addImport(man, d)); io.annotations().forEach(ann -> addOntAnn(man, ann)); io.axioms().forEach(this::add); man.removeOntology(io); } } } }
private <E extends OWLEntity> E getErrorEntity(EntityType<E> entityType) { IRI iri = IRI.create("http://org.semanticweb.owlapi/error#", "Error" + ERRORCOUNTER.incrementAndGet()); LOGGER.error("Entity not properly recognized, missing triples in input? {} for type {}", iri, entityType); if (configuration.isStrict()) { throw new OWLParserException( "Entity not properly recognized, missing triples in input? " + iri + " for type " + entityType); } return df.getOWLEntity(entityType, iri); }
@Override public OWLOntologyFormat parse(OWLOntologyDocumentSource documentSource, OWLOntology ontology, OWLOntologyLoaderConfiguration configuration) throws OWLParserException, IOException, OWLOntologyChangeException, UnloadableImportException { try { KRSSOntologyFormat format = new KRSSOntologyFormat(); KRSSParser parser; if(documentSource.isReaderAvailable()) { parser = new KRSSParser(documentSource.getReader()); } else if(documentSource.isInputStreamAvailable()) { parser = new KRSSParser(documentSource.getInputStream()); } else { parser = new KRSSParser(getInputStream(documentSource.getDocumentIRI(), configuration)); } parser.setOntology(ontology, ontology.getOWLOntologyManager().getOWLDataFactory()); parser.parse(); return format; } catch (ParseException e) { throw new KRSSOWLParserException(e); } }
/** * Determines if a change is applicable. A change may not be applicable for a number of reasons. * * @param change The change to be tested. * @return {@code true} if the change is applicable, otherwise, {@code false}. */ private boolean isChangeApplicable(OWLOntologyChange change) { OWLOntologyLoaderConfiguration ontologyConfig = ontologyConfigurationsByOntologyID.get(change.getOntology().getOntologyID()); if (ontologyConfig != null && !ontologyConfig.isLoadAnnotationAxioms() && change.isAddAxiom() && change.getAxiom() instanceof OWLAnnotationAxiom) { return false; } return true; }
public static Optional<OWLOntology> load(final File file, final OWLOntologyManager manager, final Optional<OWLOntologyID> ontologyId) { final List<OWLAxiom> axioms = new ArrayList<>(); final OWLOntology ontology; try (final BufferedReader in = new BufferedReader(new FileReader(file))) { ontology = (ontologyId.isPresent()) ? manager.createOntology(ontologyId.get()) : manager.createOntology(); final OWLFunctionalSyntaxParser parser = new OWLFunctionalSyntaxParser(in); parser.setUp(ontology, new OWLOntologyLoaderConfiguration()); try { while (true) axioms.add(parser.Axiom()); } catch (@SuppressWarnings("unused") final Exception e) { // The exception is the way we detected the end. } } catch (final Exception exception) { Log.error(_logger, "Can't load the ontology from file " + file + ".", exception); return Optional.empty(); } ontology.addAxioms(axioms); return Optional.of(ontology); } }
@Override public void makeLoadImportRequest(OWLImportsDeclaration declaration, OWLOntologyLoaderConfiguration configuration) { writeLock.lock(); try { IRI iri = declaration.getIRI(); if (!configuration.isIgnoredImport(iri) && !importedIRIs.containsKey(iri)) { // insert temporary value - we do not know the actual ID yet importedIRIs.put(iri, new Object()); try { OWLOntology ont = loadImports(declaration, configuration); if (ont != null) { ontologyIDsByImportsDeclaration.put(declaration, ont.getOntologyID()); importedIRIs.put(iri, ont.getOntologyID()); } } catch (OWLOntologyCreationException e) { // Wrap as UnloadableImportException and throw throw new UnloadableImportException(e, declaration); } } } finally { writeLock.unlock(); } }
@Override public OWLObjectExactCardinality translate(IRI mainNode) { getConsumer().consumeTriple(mainNode, RDF_TYPE.getIRI(), OWL_RESTRICTION.getIRI()); int cardi = translateInteger(mainNode, OWL_CARDINALITY); IRI propertyIRI = getConsumer().getResourceObject(mainNode, OWL_ON_PROPERTY, true); OWLObjectPropertyExpression property = getConsumer() .translateObjectPropertyExpression(verifyNotNull(propertyIRI)); IRI fillerIRI = getConsumer().getResourceObject(mainNode, OWL_ON_CLASS, true); if (fillerIRI != null && !getConsumer().getConfiguration().isStrict()) { // Be tolerant OWLClassExpression filler = accessor.translateClassExpression(fillerIRI); return getDataFactory().getOWLObjectExactCardinality(cardi, property, filler); } else { return getDataFactory().getOWLObjectExactCardinality(cardi, property); } } }
@Override public OWLDataMinCardinality translate(IRI mainNode) { getConsumer().consumeTriple(mainNode, RDF_TYPE.getIRI(), OWL_RESTRICTION.getIRI()); int cardi = translateInteger(mainNode, OWL_MIN_CARDINALITY); IRI propertyIRI = getConsumer().getResourceObject(mainNode, OWL_ON_PROPERTY, true); OWLDataPropertyExpression property = getConsumer() .translateDataPropertyExpression(verifyNotNull(propertyIRI)); IRI fillerIRI = getConsumer().getResourceObject(mainNode, OWL_ON_DATA_RANGE, true); if (fillerIRI != null && !getConsumer().getConfiguration().isStrict()) { // Be tolerant OWLDataRange filler = getConsumer().translateDataRange(fillerIRI); return getDataFactory().getOWLDataMinCardinality(cardi, property, filler); } else { return getDataFactory().getOWLDataMinCardinality(cardi, property); } } }
/** * @return a new OWLOntologyLoaderConfiguration from the builder current settings */ public OWLOntologyLoaderConfiguration buildLoaderConfiguration() { return new OWLOntologyLoaderConfiguration() .setAcceptingHTTPCompression(shouldAcceptHTTPCompression()) .setConnectionTimeout(getConnectionTimeout()) .setFollowRedirects(shouldFollowRedirects()) .setLoadAnnotationAxioms(shouldLoadAnnotations()) .setMissingImportHandlingStrategy(getMissingImportHandlingStrategy()) .setMissingOntologyHeaderStrategy(getMissingOntologyHeaderStrategy()) .setPriorityCollectionSorting(getPriorityCollectionSorting()) .setReportStackTraces(shouldReportStackTraces()) .setRetriesToAttempt(getRetriesToAttempt()) .setStrict(shouldParseWithStrictConfiguration()) .setTreatDublinCoreAsBuiltIn(shouldTreatDublinCoreAsBuiltin()) .setBannedParsers(getBannedParsers()) .setRepairIllegalPunnings(shouldRepairIllegalPunnings()); }