Search in sources :

Example 1 with ReasonerApiException

use of com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException 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);
    }
}
Also used : ReasonerTaxonomy(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy) IRI(org.semanticweb.owlapi.model.IRI) LockedException(com.b2international.commons.exceptions.LockedException) ReasonerInterruptedException(org.semanticweb.owlapi.reasoner.ReasonerInterruptedException) SnomedCoreConfiguration(com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException) DelegateOntologyFactory(com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntologyFactory) Locks(com.b2international.snowowl.core.locks.Locks) NormalFormGenerator(com.b2international.snowowl.snomed.reasoner.normalform.NormalFormGenerator) OWLReasonerRuntimeException(org.semanticweb.owlapi.reasoner.OWLReasonerRuntimeException) DelegateOntology(com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntology) ReasonerTaxonomyInferrer(com.b2international.snowowl.snomed.reasoner.classification.ReasonerTaxonomyInferrer) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) RevisionSearcher(com.b2international.index.revision.RevisionSearcher)

Example 2 with ReasonerApiException

use of com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException in project snow-owl by b2ihealthcare.

the class SaveJobRequest method execute.

@Override
public Boolean execute(final BranchContext context) {
    final IProgressMonitor monitor = context.service(IProgressMonitor.class);
    final ClassificationTracker tracker = context.service(ClassificationTracker.class);
    final String user = !Strings.isNullOrEmpty(userId) ? userId : context.service(User.class).getUsername();
    try (Locks locks = Locks.on(context).user(user).lock(DatastoreLockContextDescriptions.SAVE_CLASSIFICATION_RESULTS, parentLockContext)) {
        return persistChanges(context, monitor);
    } catch (final LockedException e) {
        tracker.classificationFailed(classificationId);
        throw new ReasonerApiException("Couldn't acquire exclusive access to terminology store for persisting classification changes; %s", e.getMessage(), e);
    } catch (final Exception e) {
        LOG.error("Unexpected error while persisting classification changes.", e);
        tracker.classificationSaveFailed(classificationId);
        throw new ReasonerApiException("Error while persisting classification changes on '%s'.", context.path(), e);
    } finally {
        monitor.done();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) LockedException(com.b2international.commons.exceptions.LockedException) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException) Locks(com.b2international.snowowl.core.locks.Locks) ClassificationTracker(com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker) BadRequestException(com.b2international.commons.exceptions.BadRequestException) LockedException(com.b2international.commons.exceptions.LockedException) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException)

Example 3 with ReasonerApiException

use of com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException in project snow-owl by b2ihealthcare.

the class ClassificationJobRequest method execute.

@Override
public Boolean execute(final BranchContext context) {
    final RemoteJob job = context.service(RemoteJob.class);
    final String classificationId = job.getKey();
    final Branch branch = context.branch();
    final long headTimestamp = branch.headTimestamp();
    final ClassificationTracker tracker = context.service(ClassificationTracker.class);
    tracker.classificationRunning(classificationId, headTimestamp);
    try {
        executeClassification(context, classificationId, tracker);
    } catch (final ReasonerApiException e) {
        tracker.classificationFailed(classificationId);
        throw e;
    } catch (final Exception e) {
        LOGGER.error("Unexpected error encountered while running classification job.", e);
        tracker.classificationFailed(classificationId);
        throw new ReasonerApiException("Exception caught while running classification.", e);
    }
    return Boolean.TRUE;
}
Also used : RemoteJob(com.b2international.snowowl.core.jobs.RemoteJob) Branch(com.b2international.snowowl.core.branch.Branch) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException) ClassificationTracker(com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ReasonerInterruptedException(org.semanticweb.owlapi.reasoner.ReasonerInterruptedException) OWLReasonerRuntimeException(org.semanticweb.owlapi.reasoner.OWLReasonerRuntimeException) LockedException(com.b2international.commons.exceptions.LockedException) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException)

Example 4 with ReasonerApiException

use of com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyInferrer method createReasoner.

private static OWLReasoner createReasoner(final String reasonerId, final OWLOntology owlOntology) {
    final IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID);
    final IExtension[] extensions = extensionPoint.getExtensions();
    for (final IExtension extension : extensions) {
        final IConfigurationElement[] configurationElements = extension.getConfigurationElements();
        final String extensionId = extension.getUniqueIdentifier();
        if (reasonerId.equals(extensionId)) {
            final Optional<IConfigurationElement> classElement = Arrays.asList(configurationElements).stream().filter(e -> CLASS_ELEMENT.equals(e.getName())).findFirst();
            if (!classElement.isPresent()) {
                throw new ReasonerApiException("Couldn't create reasoner info instance for extension '%s'.", reasonerId);
            }
            final ProtegeOWLReasonerInfo reasonerInfo;
            try {
                reasonerInfo = (ProtegeOWLReasonerInfo) classElement.get().createExecutableExtension(VALUE_ATTRIBUTE);
                reasonerInfo.initialise();
            } catch (final Exception e) {
                throw new ReasonerApiException("Couldn't create reasoner info instance for extension '%s'.", reasonerId, e);
            }
            final OWLReasonerFactory reasonerFactory = reasonerInfo.getReasonerFactory();
            final OWLReasonerConfiguration reasonerConfiguration = reasonerInfo.getConfiguration(new LoggingProgressMonitor(LOGGER));
            return reasonerFactory.createNonBufferingReasoner(owlOntology, reasonerConfiguration);
        }
    }
    throw new ReasonerApiException("Couldn't create reasoner info instance for extension '%s'.", reasonerId);
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) java.util(java.util) Stopwatch(com.google.common.base.Stopwatch) LoggerFactory(org.slf4j.LoggerFactory) RateLimiter(com.google.common.util.concurrent.RateLimiter) com.b2international.snowowl.snomed.datastore.index.taxonomy(com.b2international.snowowl.snomed.datastore.index.taxonomy) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) PrimitiveSets(com.b2international.collections.PrimitiveSets) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) LongSet(com.b2international.collections.longs.LongSet) LongSets(com.b2international.commons.collect.LongSets) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) ProtegeOWLReasonerInfo(org.protege.editor.owl.model.inference.ProtegeOWLReasonerInfo) org.semanticweb.owlapi.reasoner(org.semanticweb.owlapi.reasoner) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) PrimitiveLists(com.b2international.collections.PrimitiveLists) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) DelegateOntology(com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntology) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) IExtension(org.eclipse.core.runtime.IExtension) Logger(org.slf4j.Logger) RevisionDocument(com.b2international.snowowl.core.repository.RevisionDocument) LongList(com.b2international.collections.longs.LongList) Ordering(com.google.common.collect.Ordering) OWLClassNodeSet(org.semanticweb.owlapi.reasoner.impl.OWLClassNodeSet) Sort(com.b2international.snowowl.core.request.SearchResourceRequest.Sort) Platform(org.eclipse.core.runtime.Platform) BranchContext(com.b2international.snowowl.core.domain.BranchContext) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException) OWLClass(org.semanticweb.owlapi.model.OWLClass) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) IExtension(org.eclipse.core.runtime.IExtension) ProtegeOWLReasonerInfo(org.protege.editor.owl.model.inference.ProtegeOWLReasonerInfo)

Example 5 with ReasonerApiException

use of com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException in project snow-owl by b2ihealthcare.

the class EquivalencyChecker method processResults.

@Override
protected LongKeyLongMap processResults(final String classificationId) {
    final Set<String> conceptIdsToCheck = additionalConcepts.stream().map(SnomedConcept::getId).collect(Collectors.toSet());
    final LongKeyLongMap equivalentConceptMap = PrimitiveMaps.newLongKeyLongOpenHashMap();
    final ClassificationTask classificationTask = ClassificationRequests.prepareGetClassification(classificationId).setExpand("equivalentConceptSets()").build(repositoryId).execute(getEventBus()).getSync();
    if (!ClassificationStatus.COMPLETED.equals(classificationTask.getStatus())) {
        throw new ReasonerApiException("Selected reasoner could not start or failed to finish its job.");
    }
    if (!classificationTask.getEquivalentConceptsFound()) {
        return equivalentConceptMap;
    }
    final EquivalentConceptSets equivalentConceptSets = classificationTask.getEquivalentConceptSets();
    registerEquivalentConcepts(equivalentConceptSets, conceptIdsToCheck, equivalentConceptMap);
    return equivalentConceptMap;
}
Also used : ClassificationTask(com.b2international.snowowl.snomed.reasoner.domain.ClassificationTask) EquivalentConceptSets(com.b2international.snowowl.snomed.reasoner.domain.EquivalentConceptSets) LongKeyLongMap(com.b2international.collections.longs.LongKeyLongMap) ReasonerApiException(com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException)

Aggregations

ReasonerApiException (com.b2international.snowowl.snomed.reasoner.exceptions.ReasonerApiException)5 LockedException (com.b2international.commons.exceptions.LockedException)3 Locks (com.b2international.snowowl.core.locks.Locks)2 ClassificationTracker (com.b2international.snowowl.snomed.reasoner.classification.ClassificationTracker)2 DelegateOntology (com.b2international.snowowl.snomed.reasoner.ontology.DelegateOntology)2 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)2 OWLReasonerRuntimeException (org.semanticweb.owlapi.reasoner.OWLReasonerRuntimeException)2 ReasonerInterruptedException (org.semanticweb.owlapi.reasoner.ReasonerInterruptedException)2 PrimitiveLists (com.b2international.collections.PrimitiveLists)1 PrimitiveSets (com.b2international.collections.PrimitiveSets)1 LongKeyLongMap (com.b2international.collections.longs.LongKeyLongMap)1 LongList (com.b2international.collections.longs.LongList)1 LongSet (com.b2international.collections.longs.LongSet)1 LongSets (com.b2international.commons.collect.LongSets)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 TerminologyResource (com.b2international.snowowl.core.TerminologyResource)1 Branch (com.b2international.snowowl.core.branch.Branch)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 RemoteJob (com.b2international.snowowl.core.jobs.RemoteJob)1