use of com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy in project snow-owl by b2ihealthcare.
the class ClassificationJobRequest method executeClassification.
private void executeClassification(final BranchContext context, final String classificationId, final ClassificationTracker tracker) {
final RevisionSearcher revisionSearcher = context.service(RevisionSearcher.class);
TerminologyResource resource = context.service(TerminologyResource.class);
@SuppressWarnings("unchecked") final Set<String> reasonerExcludedModuleIds = Collections3.toImmutableSet((Iterable) resource.getSettings().getOrDefault(REASONER_EXCLUDE_MODULE_IDS, Collections.emptySet()));
final SnomedCoreConfiguration configuration = context.service(SnomedCoreConfiguration.class);
final boolean concreteDomainSupported = configuration.isConcreteDomainSupported();
final ReasonerTaxonomy taxonomy;
try (Locks locks = Locks.on(context).lock(DatastoreLockContextDescriptions.CLASSIFY, parentLockContext)) {
taxonomy = buildTaxonomy(revisionSearcher, reasonerExcludedModuleIds, concreteDomainSupported);
} catch (final LockedException e) {
throw new ReasonerApiException("Couldn't acquire exclusive access to terminology store for classification; %s", e.getMessage(), e);
}
final OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
ontologyManager.getOntologyFactories().add(new DelegateOntologyFactory(taxonomy));
// TODO: custom moduleId in ontology IRI?
final IRI ontologyIRI = IRI.create(DelegateOntology.NAMESPACE_SCTM + Concepts.MODULE_SCT_CORE);
try {
final DelegateOntology ontology = (DelegateOntology) ontologyManager.createOntology(ontologyIRI);
final ReasonerTaxonomyInferrer inferrer = new ReasonerTaxonomyInferrer(reasonerId, ontology, context);
final ReasonerTaxonomy inferredTaxonomy = inferrer.addInferences(taxonomy);
final NormalFormGenerator normalFormGenerator = new NormalFormGenerator(inferredTaxonomy);
tracker.classificationCompleted(classificationId, inferredTaxonomy, normalFormGenerator);
} catch (final OWLOntologyCreationException e) {
throw new ReasonerApiException("Exception caught while creating ontology instance.", e);
} catch (final ReasonerInterruptedException | OWLReasonerRuntimeException e) {
throw new ReasonerApiException("Exception caught while classifying the ontology.", e);
}
}
use of com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy in project snow-owl by b2ihealthcare.
the class OntologyExportRequest method execute.
@Override
public String execute(final BranchContext context) {
final RevisionSearcher revisionSearcher = context.service(RevisionSearcher.class);
TerminologyResource resource = context.service(TerminologyResource.class);
@SuppressWarnings("unchecked") final Set<String> reasonerExcludedModuleIds = Collections3.toImmutableSet((Iterable) resource.getSettings().getOrDefault(REASONER_EXCLUDE_MODULE_IDS, Collections.emptySet()));
final SnomedCoreConfiguration configuration = context.service(SnomedCoreConfiguration.class);
final boolean concreteDomainSupportEnabled = configuration.isConcreteDomainSupported();
final ReasonerTaxonomyBuilder taxonomyBuilder = new ReasonerTaxonomyBuilder(reasonerExcludedModuleIds);
taxonomyBuilder.addActiveConceptIds(revisionSearcher);
taxonomyBuilder.finishConcepts();
taxonomyBuilder.addFullySpecifiedNames(revisionSearcher);
taxonomyBuilder.addConceptFlags(revisionSearcher);
taxonomyBuilder.addActiveStatedEdges(revisionSearcher);
taxonomyBuilder.addActiveStatedRelationships(revisionSearcher);
taxonomyBuilder.addNeverGroupedTypeIds(revisionSearcher);
taxonomyBuilder.addActiveAxioms(revisionSearcher);
if (concreteDomainSupportEnabled) {
taxonomyBuilder.addActiveConcreteDomainMembers(revisionSearcher);
}
final ReasonerTaxonomy taxonomy = taxonomyBuilder.build();
final OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
ontologyManager.getOntologyFactories().add(new DelegateOntologyFactory(taxonomy));
final IRI ontologyIRI = IRI.create(DelegateOntology.NAMESPACE_SCTM + ontologyModuleId);
try {
final OWLOntology ontology = ontologyManager.createOntology(ontologyIRI);
OWLOntology ontologyToExport = ontologyManager.createOntology();
ontology.getAxioms().forEach(axiom -> {
ontologyManager.addAxiom(ontologyToExport, axiom);
});
final OWLDocumentFormat documentFormat = getOWLDocumentFormat();
final AttachmentRegistry fileRegistry = context.service(AttachmentRegistry.class);
final UUID id = UUID.randomUUID();
final PipedOutputStream os = new PipedOutputStream();
final PipedInputStream is = new PipedInputStream(os, PIPE_SIZE);
final ForkJoinTask<?> uploadTask = ForkJoinTask.adapt(() -> fileRegistry.upload(id, is));
final ForkJoinTask<?> saveTask = ForkJoinTask.adapt(() -> {
try {
ontologyManager.saveOntology(ontologyToExport, documentFormat, os);
} catch (final OWLOntologyStorageException e) {
throw createExportFailedException(context, e);
} finally {
try {
os.close();
} catch (final IOException e) {
throw createExportFailedException(context, e);
}
}
});
ForkJoinTask.invokeAll(saveTask, uploadTask);
return id.toString();
} catch (final OWLOntologyCreationException e) {
throw createExportFailedException(context, e);
} catch (final IOException e) {
throw createExportFailedException(context, e);
} finally {
// invalidate static cache entries :'(
ontologyManager.getOntologies().forEach(o -> {
ontologyManager.applyChange(new SetOntologyID(o, new OWLOntologyID()));
});
}
}
Aggregations