Search in sources :

Example 1 with ReasonerTaxonomyBuilder

use of com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomyBuilder in project snow-owl by b2ihealthcare.

the class ClassificationJobRequest method buildTaxonomy.

private ReasonerTaxonomy buildTaxonomy(final RevisionSearcher revisionSearcher, final Set<String> excludedModuleIds, final boolean concreteDomainSupported) {
    final ReasonerTaxonomyBuilder taxonomyBuilder = new ReasonerTaxonomyBuilder(excludedModuleIds);
    taxonomyBuilder.addActiveConceptIds(revisionSearcher);
    taxonomyBuilder.addActiveConceptIds(additionalConcepts.stream());
    taxonomyBuilder.finishConcepts();
    taxonomyBuilder.addConceptFlags(revisionSearcher);
    taxonomyBuilder.addActiveStatedEdges(revisionSearcher);
    taxonomyBuilder.addActiveStatedRelationships(revisionSearcher);
    taxonomyBuilder.addActiveInferredRelationships(revisionSearcher);
    taxonomyBuilder.addActiveAdditionalGroupedRelationships(revisionSearcher);
    taxonomyBuilder.addNeverGroupedTypeIds(revisionSearcher);
    taxonomyBuilder.addActiveAxioms(revisionSearcher);
    if (concreteDomainSupported) {
        taxonomyBuilder.addActiveConcreteDomainMembers(revisionSearcher);
    }
    // Add the extra definitions
    taxonomyBuilder.addConceptFlags(additionalConcepts.stream());
    final Supplier<Stream<SnomedRelationship>> relationshipSupplier = () -> additionalConcepts.stream().flatMap(c -> c.getRelationships().stream());
    taxonomyBuilder.addActiveStatedEdges(relationshipSupplier.get());
    taxonomyBuilder.addActiveStatedRelationships(relationshipSupplier.get());
    taxonomyBuilder.addActiveInferredRelationships(relationshipSupplier.get());
    taxonomyBuilder.addActiveAdditionalGroupedRelationships(relationshipSupplier.get());
    if (concreteDomainSupported) {
        final Stream<SnomedReferenceSetMember> conceptMembers = additionalConcepts.stream().flatMap(c -> c.getMembers().stream());
        taxonomyBuilder.addActiveConcreteDomainMembers(conceptMembers);
    }
    return taxonomyBuilder.build();
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) ReasonerTaxonomyBuilder(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomyBuilder) Stream(java.util.stream.Stream)

Example 2 with ReasonerTaxonomyBuilder

use of com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomyBuilder 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()));
        });
    }
}
Also used : ReasonerTaxonomy(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy) ReasonerTaxonomyBuilder(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomyBuilder) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) SnomedCoreConfiguration(com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration) DelegateOntologyFactory(com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntologyFactory) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) UUID(java.util.UUID) RevisionSearcher(com.b2international.index.revision.RevisionSearcher)

Aggregations

ReasonerTaxonomyBuilder (com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomyBuilder)2 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)1 AttachmentRegistry (com.b2international.snowowl.core.attachments.AttachmentRegistry)1 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)1 SnomedCoreConfiguration (com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration)1 ReasonerTaxonomy (com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy)1 DelegateOntologyFactory (com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntologyFactory)1 IOException (java.io.IOException)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 UUID (java.util.UUID)1 Stream (java.util.stream.Stream)1