use of com.b2international.snowowl.core.ApplicationContext.getServiceForClass in project snow-owl by b2ihealthcare.
the class SnomedDescriptionApiTest method testDuplicateMemberCleanupEmptiesAcceptabilityMap.
@Test
public void testDuplicateMemberCleanupEmptiesAcceptabilityMap() throws Exception {
String descriptionId = createNewDescription(branchPath);
// Inject inactive language member with different acceptability (API won't allow it)
String memberIdToUpdate = UUID.randomUUID().toString();
SnomedRefSetMemberIndexEntry member = SnomedRefSetMemberIndexEntry.builder().id(memberIdToUpdate).active(false).released(false).moduleId(Concepts.MODULE_SCT_CORE).refsetId(Concepts.REFSET_LANGUAGE_TYPE_UK).referencedComponentId(descriptionId).referenceSetType(SnomedRefSetType.LANGUAGE).field(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID, Concepts.REFSET_DESCRIPTION_ACCEPTABILITY_PREFERRED).build();
new RepositoryRequest<>(SnomedTerminologyComponentConstants.TOOLING_ID, context -> {
ApplicationContext.getServiceForClass(RepositoryManager.class).get(SnomedTerminologyComponentConstants.TOOLING_ID).service(RevisionIndex.class).prepareCommit(branchPath.getPath()).stageNew(member).withContext(context.inject().bind(ModuleIdProvider.class, c -> c.getModuleId()).build()).commit(ApplicationContext.getServiceForClass(TimestampProvider.class).getTimestamp(), "test", "Added duplicate language reference set member to " + descriptionId);
return null;
}).execute(ApplicationContext.getServiceForClass(Environment.class));
// Check the acceptability map; the description should be acceptable in the UK reference set
SnomedDescription description = getComponent(branchPath, SnomedComponentType.DESCRIPTION, descriptionId, "members()").statusCode(200).extract().as(SnomedDescription.class);
assertEquals(Acceptability.ACCEPTABLE, description.getAcceptabilityMap().get(Concepts.REFSET_LANGUAGE_TYPE_UK));
assertEquals(2, description.getMembers().getTotal());
String memberIdToDelete = null;
for (SnomedReferenceSetMember descriptionMember : description.getMembers()) {
if (Concepts.REFSET_DESCRIPTION_ACCEPTABILITY_ACCEPTABLE.equals(descriptionMember.getProperties().get(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID))) {
memberIdToDelete = descriptionMember.getId();
break;
}
}
assertNotNull(memberIdToDelete);
// Using bulk update, remove the currently active member and activate the inactive one, also changing its acceptability
Json bulkRequest = Json.object("requests", Json.array(Json.object("action", "delete", "memberId", memberIdToDelete), Json.object("action", "update", "memberId", memberIdToUpdate, "active", true, "acceptabilityId", Concepts.REFSET_DESCRIPTION_ACCEPTABILITY_ACCEPTABLE)), "commitComment", "Consolidated language reference set members");
bulkUpdateMembers(branchPath, Concepts.REFSET_LANGUAGE_TYPE_UK, bulkRequest).statusCode(204);
// Verify that description acceptability is still acceptable, but only one member remains
SnomedDescription newDescription = getComponent(branchPath, SnomedComponentType.DESCRIPTION, descriptionId, "members()").statusCode(200).extract().as(SnomedDescription.class);
assertEquals(Acceptability.ACCEPTABLE, newDescription.getAcceptabilityMap().get(Concepts.REFSET_LANGUAGE_TYPE_UK));
assertEquals(1, newDescription.getMembers().getTotal());
SnomedReferenceSetMember languageMember = Iterables.getOnlyElement(newDescription.getMembers());
assertEquals(memberIdToUpdate, languageMember.getId());
assertEquals(true, languageMember.isActive());
}
use of com.b2international.snowowl.core.ApplicationContext.getServiceForClass in project snow-owl by b2ihealthcare.
the class ClassifyOperation method run.
/**
* Allocates a reasoner instance, performs the requested operation, then releases the borrowed instance back to the pool.
* @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 {
monitor.beginTask("Classification in progress...", IProgressMonitor.UNKNOWN);
try {
final String classificationId = UUID.randomUUID().toString();
final String jobId = IDs.sha1(classificationId);
final Notifications notifications = getServiceForClass(Notifications.class);
final BlockingQueue<RemoteJobEntry> jobQueue = Queues.newArrayBlockingQueue(1);
final Observable<RemoteJobEntry> jobObservable = notifications.ofType(RemoteJobNotification.class).filter(RemoteJobNotification::isChanged).filter(notification -> notification.getJobIds().contains(jobId)).concatMap(notification -> JobRequests.prepareSearch().one().filterById(jobId).buildAsync().execute(getEventBus())).map(RemoteJobs::first).map(Optional<RemoteJobEntry>::get).filter(RemoteJobEntry::isDone);
// "One-shot" subscription; it should self-destruct after the first notification
jobObservable.subscribe(new DisposableObserver<RemoteJobEntry>() {
@Override
public void onComplete() {
dispose();
}
@Override
public void onError(final Throwable t) {
dispose();
}
@Override
public void onNext(final RemoteJobEntry job) {
try {
jobQueue.put(job);
} catch (InterruptedException e) {
throw new SnowowlRuntimeException("Interrupted while trying to add a remote job entry to the queue.", e);
} finally {
dispose();
}
}
});
ClassificationRequests.prepareCreateClassification().setClassificationId(classificationId).setReasonerId(reasonerId).setUserId(userId).addAllConcepts(additionalConcepts).setParentLockContext(parentLockContext).build(branch).get(ApplicationContext.getServiceForClass(Environment.class));
while (true) {
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
try {
final RemoteJobEntry jobEntry = jobQueue.poll(CHECK_JOB_INTERVAL_SECONDS, TimeUnit.SECONDS);
if (jobEntry == null) {
continue;
}
switch(jobEntry.getState()) {
// $FALL-THROUGH$
case SCHEDULED:
case RUNNING:
case CANCEL_REQUESTED:
break;
case FINISHED:
try {
return processResults(classificationId);
} finally {
deleteEntry(jobId);
}
case CANCELED:
deleteEntry(jobId);
throw new OperationCanceledException();
case FAILED:
deleteEntry(jobId);
throw new SnowowlRuntimeException("Failed to retrieve the results of the classification.");
default:
throw new IllegalStateException("Unexpected state '" + jobEntry.getState() + "'.");
}
} catch (final InterruptedException e) {
// Nothing to do
}
}
} finally {
monitor.done();
}
}
Aggregations