Skip to content

Commit

Permalink
fix(classification): improve ClassifyOperation and EquivalencyChecker...
Browse files Browse the repository at this point in the history
...to be service context agnostic
  • Loading branch information
cmark committed Jun 25, 2024
1 parent 3da86f8 commit f049071
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;

import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.ResourceURI;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.api.SnowowlRuntimeException;
import com.b2international.snowowl.core.events.Notifications;
import com.b2international.snowowl.core.id.IDs;
Expand All @@ -36,7 +36,6 @@
import com.b2international.snowowl.core.jobs.RemoteJobEntry;
import com.b2international.snowowl.core.jobs.RemoteJobNotification;
import com.b2international.snowowl.core.jobs.RemoteJobs;
import com.b2international.snowowl.core.setup.Environment;
import com.b2international.snowowl.eventbus.IEventBus;
import com.b2international.snowowl.snomed.core.domain.SnomedConcept;
import com.b2international.snowowl.snomed.reasoner.request.ClassificationRequests;
Expand Down Expand Up @@ -89,11 +88,13 @@ public ClassifyOperation(final String reasonerId,

/**
* Allocates a reasoner instance, performs the requested operation, then releases the borrowed instance back to the pool.
*
* @param context the context where the operation should run
* @param monitor an {@link IProgressMonitor} to monitor operation progress
* @return the value returned by {@link #processResults(IProgressMonitor, long)}
* @throws OperationCanceledException
*/
public T run(final IProgressMonitor monitor) throws OperationCanceledException {
public final T run(final ServiceProvider context, final IProgressMonitor monitor) throws OperationCanceledException {

monitor.beginTask("Classification in progress...", IProgressMonitor.UNKNOWN);

Expand All @@ -110,7 +111,7 @@ public T run(final IProgressMonitor monitor) throws OperationCanceledException {
.one()
.filterById(jobId)
.buildAsync()
.execute(getEventBus()))
.execute(context.service(IEventBus.class)))
.map(RemoteJobs::first)
.map(Optional<RemoteJobEntry>::get)
.filter(RemoteJobEntry::isDone);
Expand Down Expand Up @@ -142,7 +143,7 @@ public void onNext(final RemoteJobEntry job) {
.addAllConcepts(additionalConcepts)
.setParentLockContext(parentLockContext)
.build(resourceUri)
.get(ApplicationContext.getServiceForClass(Environment.class));
.get(context);

while (true) {

Expand All @@ -164,15 +165,15 @@ public void onNext(final RemoteJobEntry job) {
break;
case FINISHED:
try {
return processResults(classificationId);
return processResults(context, classificationId);
} finally {
deleteEntry(jobId);
deleteEntry(context, jobId);
}
case CANCELED:
deleteEntry(jobId);
deleteEntry(context, jobId);
throw new OperationCanceledException();
case FAILED:
deleteEntry(jobId);
deleteEntry(context, jobId);
throw new SnowowlRuntimeException("Failed to retrieve the results of the classification.");
default:
throw new IllegalStateException("Unexpected state '" + jobEntry.getState() + "'.");
Expand All @@ -188,22 +189,19 @@ public void onNext(final RemoteJobEntry job) {
}
}

private void deleteEntry(final String classificationId) {
private void deleteEntry(final ServiceProvider context, final String classificationId) {
JobRequests.prepareDelete(classificationId)
.buildAsync()
.execute(getEventBus());
}

protected IEventBus getEventBus() {
return ApplicationContext.getServiceForClass(IEventBus.class);
.execute(context);
}

/**
* Performs an arbitrary operation using the reasoner. Subclasses should implement this method to perform any operation on the
* results of a classification run.
*
* @param context
* @param classificationId the classification's unique identifier
* @return the extracted results of the classification
*/
protected abstract T processResults(String classificationId);
protected abstract T processResults(final ServiceProvider context, String classificationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.b2international.collections.PrimitiveMaps;
import com.b2international.collections.longs.LongKeyLongMap;
import com.b2international.snowowl.core.ResourceURI;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.core.domain.SnomedConcept;
import com.b2international.snowowl.snomed.reasoner.classification.ClassifyOperation;
Expand Down Expand Up @@ -51,7 +52,7 @@ public EquivalencyChecker(final String reasonerId,
}

@Override
protected LongKeyLongMap processResults(final String classificationId) {
protected LongKeyLongMap processResults(final ServiceProvider context, final String classificationId) {

final Set<String> conceptIdsToCheck = additionalConcepts.stream()
.map(SnomedConcept::getId)
Expand All @@ -62,8 +63,7 @@ protected LongKeyLongMap processResults(final String classificationId) {
final ClassificationTask classificationTask = ClassificationRequests.prepareGetClassification(classificationId)
.setExpand("equivalentConceptSets()")
.build(SnomedTerminologyComponentConstants.TOOLING_ID)
.execute(getEventBus())
.getSync();
.get(context);

if (!ClassificationStatus.COMPLETED.equals(classificationTask.getStatus())) {
throw new ReasonerApiException("Selected reasoner could not start or failed to finish its job.");
Expand Down

0 comments on commit f049071

Please sign in to comment.