Search in sources :

Example 6 with ComponentCategory

use of com.b2international.snowowl.core.terminology.ComponentCategory in project snow-owl by b2ihealthcare.

the class SnomedReferenceSetMemberConverter method expandReferencedComponent.

private void expandReferencedComponent(List<SnomedReferenceSetMember> results) {
    if (expand().containsKey(SnomedReferenceSetMember.Expand.REFERENCED_COMPONENT)) {
        Options expandOptions = expand().get(SnomedReferenceSetMember.Expand.REFERENCED_COMPONENT, Options.class);
        final Multimap<String, SnomedReferenceSetMember> referencedComponentIdToMemberMap = collectReferencedComponentIds(results);
        final Multimap<ComponentCategory, String> componentCategoryToIdMap = collectReferencedComponentCategories(referencedComponentIdToMemberMap);
        for (ComponentCategory category : componentCategoryToIdMap.keySet()) {
            expandComponentCategory(expandOptions, referencedComponentIdToMemberMap, componentCategoryToIdMap, category);
        }
    }
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) Options(com.b2international.commons.options.Options) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory)

Example 7 with ComponentCategory

use of com.b2international.snowowl.core.terminology.ComponentCategory in project snow-owl by b2ihealthcare.

the class IdRequest method execute.

@Override
public R execute(final C context) {
    final IdActionRecorder recorder = new IdActionRecorder(context);
    try {
        final Multimap<ComponentCategory, SnomedComponentCreateRequest> componentCreateRequests = getComponentCreateRequests(next());
        if (!componentCreateRequests.isEmpty()) {
            try {
                for (final ComponentCategory category : componentCreateRequests.keySet()) {
                    final Class<? extends SnomedDocument> documentClass = getDocumentClass(category);
                    final Collection<SnomedComponentCreateRequest> categoryRequests = componentCreateRequests.get(category);
                    final Set<String> coreComponentIds = FluentIterable.from(categoryRequests).filter(SnomedCoreComponentCreateRequest.class).filter(request -> request.getIdGenerationStrategy() instanceof ConstantIdStrategy).transform(request -> ((ConstantIdStrategy) request.getIdGenerationStrategy()).getId()).toSet();
                    final Set<String> refsetMemberIds = FluentIterable.from(categoryRequests).filter(SnomedRefSetMemberCreateRequest.class).transform(request -> request.getId()).toSet();
                    final Set<String> userRequestedIds = Sets.newHashSet(Iterables.concat(coreComponentIds, refsetMemberIds));
                    final Set<String> existingIds = getExistingIds(context, userRequestedIds, documentClass);
                    if (!existingIds.isEmpty()) {
                        // TODO: Report all existing identifiers
                        throw new AlreadyExistsException(category.getDisplayName(), Iterables.getFirst(existingIds, null));
                    } else {
                        if (!coreComponentIds.isEmpty()) {
                            recorder.register(coreComponentIds);
                        }
                    }
                    final Multimap<String, BaseSnomedComponentCreateRequest> requestsByNamespace = HashMultimap.create();
                    // index requests where an explicit namespace identifier is specified (use it directly)
                    categoryRequests.stream().filter(BaseSnomedComponentCreateRequest.class::isInstance).map(BaseSnomedComponentCreateRequest.class::cast).filter(request -> request.getIdGenerationStrategy() instanceof NamespaceIdStrategy).forEach(request -> {
                        requestsByNamespace.put(getNamespaceKey(request), request);
                    });
                    // convert requests that define a namespaceConceptId by extracting the namespaceId from the namespaceConcept's FSN
                    final Multimap<String, BaseSnomedComponentCreateRequest> requestsByNamespaceConceptId = HashMultimap.create();
                    categoryRequests.stream().filter(BaseSnomedComponentCreateRequest.class::isInstance).map(BaseSnomedComponentCreateRequest.class::cast).filter(request -> request.getIdGenerationStrategy() instanceof NamespaceConceptIdStrategy).forEach(request -> {
                        requestsByNamespaceConceptId.put(request.getIdGenerationStrategy().getNamespace(), request);
                    });
                    if (!requestsByNamespaceConceptId.isEmpty()) {
                        final Map<String, String> namespacesByNamespaceConceptId = context.service(NamespaceIdProvider.class).extractNamespaceIds(context, requestsByNamespaceConceptId.keySet(), false);
                        for (String namespaceConceptId : Set.copyOf(requestsByNamespaceConceptId.keySet())) {
                            Collection<BaseSnomedComponentCreateRequest> requests = requestsByNamespaceConceptId.removeAll(namespaceConceptId);
                            if (!namespacesByNamespaceConceptId.containsKey(namespaceConceptId)) {
                                throw new BadRequestException("There is no namespace value associated with the '%s' namespaceConcept's FSN.", namespaceConceptId);
                            }
                            requestsByNamespace.putAll(namespacesByNamespaceConceptId.get(namespaceConceptId), requests);
                        }
                    }
                    for (final String namespace : requestsByNamespace.keySet()) {
                        final String convertedNamespace = SnomedIdentifiers.INT_NAMESPACE.equals(namespace) ? null : namespace;
                        final Collection<BaseSnomedComponentCreateRequest> namespaceRequests = requestsByNamespace.get(namespace);
                        final int count = namespaceRequests.size();
                        final Set<String> uniqueIds = getUniqueIds(context, recorder, category, documentClass, count, convertedNamespace);
                        final Iterator<String> idsToUse = Iterators.consumingIterator(uniqueIds.iterator());
                        for (final BaseSnomedComponentCreateRequest createRequest : namespaceRequests) {
                            createRequest.setIdGenerationStrategy(new ConstantIdStrategy(idsToUse.next()));
                        }
                        checkState(!idsToUse.hasNext(), "More SNOMED CT ids have been requested than used.");
                    }
                }
            } finally {
            // idGenerationSample.stop(registry.timer("idGenerationTime"));
            }
        }
        final R commitInfo = next(context);
        recorder.commit();
        return commitInfo;
    } catch (final Exception e) {
        recorder.rollback();
        throw e;
    }
}
Also used : Query(com.b2international.index.query.Query) AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) IdActionRecorder(com.b2international.snowowl.snomed.cis.action.IdActionRecorder) NamespaceIdStrategy(com.b2international.snowowl.snomed.core.domain.NamespaceIdStrategy) Map(java.util.Map) BulkRequest(com.b2international.snowowl.core.events.bulk.BulkRequest) com.b2international.snowowl.snomed.datastore.index.entry(com.b2international.snowowl.snomed.datastore.index.entry) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) CompareUtils(com.b2international.commons.CompareUtils) com.google.common.collect(com.google.common.collect) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Iterator(java.util.Iterator) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) RevisionDocument(com.b2international.snowowl.core.repository.RevisionDocument) Collection(java.util.Collection) Request(com.b2international.snowowl.core.events.Request) Set(java.util.Set) IOException(java.io.IOException) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ConstantIdStrategy(com.b2international.snowowl.snomed.core.domain.ConstantIdStrategy) TransactionalRequest(com.b2international.snowowl.core.request.TransactionalRequest) IdGenerationStrategy(com.b2international.snowowl.snomed.core.domain.IdGenerationStrategy) DelegatingRequest(com.b2international.snowowl.core.events.DelegatingRequest) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) NamespaceConceptIdStrategy(com.b2international.snowowl.snomed.core.domain.NamespaceConceptIdStrategy) BranchContext(com.b2international.snowowl.core.domain.BranchContext) NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory) IdActionRecorder(com.b2international.snowowl.snomed.cis.action.IdActionRecorder) AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory) AlreadyExistsException(com.b2international.commons.exceptions.AlreadyExistsException) BadRequestException(com.b2international.commons.exceptions.BadRequestException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) IOException(java.io.IOException) NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) ConstantIdStrategy(com.b2international.snowowl.snomed.core.domain.ConstantIdStrategy) BadRequestException(com.b2international.commons.exceptions.BadRequestException) NamespaceIdStrategy(com.b2international.snowowl.snomed.core.domain.NamespaceIdStrategy) NamespaceConceptIdStrategy(com.b2international.snowowl.snomed.core.domain.NamespaceConceptIdStrategy)

Example 8 with ComponentCategory

use of com.b2international.snowowl.core.terminology.ComponentCategory in project snow-owl by b2ihealthcare.

the class ConcreteDomainChangeConverter method expand.

@Override
public void expand(final List<ConcreteDomainChange> results) {
    if (!expand().containsKey(ConcreteDomainChange.Expand.CONCRETE_DOMAIN_MEMBER)) {
        return;
    }
    /*
		 * Depending on the CD member change search request, we might need to issue
		 * SNOMED CT searches against multiple branches; find out which ones we have.
		 */
    final Multimap<String, ConcreteDomainChange> itemsByBranch = getItemsByBranch(results);
    // Check if we only need to load inferred CD members in their entirety
    final Options expandOptions = expand().getOptions(ConcreteDomainChange.Expand.CONCRETE_DOMAIN_MEMBER);
    final boolean inferredOnly = expandOptions.getBoolean("inferredOnly");
    final Options cdMemberExpandOptions = expandOptions.getOptions("expand");
    final Options referencedComponentOptions = cdMemberExpandOptions.getOptions(SnomedReferenceSetMember.Expand.REFERENCED_COMPONENT);
    /*
		 * Remove this option from the member expand options map, so that member search
		 * does not expand the referenced component again
		 */
    final boolean needsReferencedComponent = cdMemberExpandOptions.keySet().remove(SnomedReferenceSetMember.Expand.REFERENCED_COMPONENT);
    for (final String branch : itemsByBranch.keySet()) {
        final Collection<ConcreteDomainChange> itemsForCurrentBranch = itemsByBranch.get(branch);
        /*
			 * Expand referenced component on members via a separate search request, as they
			 * can be different from the referenced component on the "origin" member
			 */
        if (needsReferencedComponent) {
            final List<ReasonerConcreteDomainMember> blankMembers = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(ConcreteDomainChange::getConcreteDomainMember).collect(Collectors.toList());
            final Multimap<String, ReasonerConcreteDomainMember> membersByReferencedComponent = Multimaps.index(blankMembers, ReasonerConcreteDomainMember::getReferencedComponentId);
            final Multimap<ComponentCategory, String> referencedComponentsByCategory = Multimaps.index(membersByReferencedComponent.keySet(), SnomedIdentifiers::getComponentCategory);
            for (final Entry<ComponentCategory, Collection<String>> categoryEntry : referencedComponentsByCategory.asMap().entrySet()) {
                expandComponentCategory(branch, categoryEntry.getKey(), categoryEntry.getValue(), referencedComponentOptions, membersByReferencedComponent);
            }
        }
        /*
			 * Then fetch all the required members (these will have a referenced component
			 * ID that should no longer be copied on inferred members). Note that the same "origin"
			 * member might be used for multiple eg. "new" counterparts.
			 */
        final Set<String> cdMemberUuids = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(c -> c.getConcreteDomainMember().getOriginMemberId()).collect(Collectors.toSet());
        final Request<BranchContext, SnomedReferenceSetMembers> cdMemberSearchRequest = SnomedRequests.prepareSearchMember().filterByIds(cdMemberUuids).setLimit(cdMemberUuids.size()).setExpand(cdMemberExpandOptions).setLocales(locales()).build();
        final SnomedReferenceSetMembers cdMembers = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(cdMemberSearchRequest)).execute(context());
        final Map<String, SnomedReferenceSetMember> cdMembersByUuid = Maps.uniqueIndex(cdMembers, SnomedReferenceSetMember::getId);
        /*
			 * Finally, set the member on the change item, but preserve the properties that
			 * were already set in "toResource"
			 */
        for (final ConcreteDomainChange item : itemsForCurrentBranch) {
            final ReasonerConcreteDomainMember reasonerMember = item.getConcreteDomainMember();
            final String memberUuid = reasonerMember.getOriginMemberId();
            switch(item.getChangeNature()) {
                case NEW:
                    {
                        final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
                        final Map<String, Object> expandedProperties = expandedMember.getProperties();
                        // reasonerMember.setCharacteristicTypeId(...) is already set
                        // reasonerMember.setGroup(...) is already set
                        // reasonerMember.setReferencedComponent(...) is already set (or expanded)
                        reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
                        // reasonerMember.setReleased(...) is already set
                        reasonerMember.setSerializedValue((String) expandedProperties.get(SnomedRf2Headers.FIELD_VALUE));
                        reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
                    }
                    break;
                case UPDATED:
                    if (!inferredOnly) {
                        final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
                        final Map<String, Object> expandedProperties = expandedMember.getProperties();
                        reasonerMember.setCharacteristicTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID));
                        reasonerMember.setGroup((Integer) expandedProperties.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP));
                        // reasonerMember.setReferencedComponent(...) is already set (or expanded)
                        reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
                        // reasonerMember.setReleased(...) is already set
                        // reasonerMember.setSerializedValue(...) is already set
                        reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
                    }
                    break;
                case REDUNDANT:
                    if (!inferredOnly) {
                        final SnomedReferenceSetMember expandedMember = cdMembersByUuid.get(memberUuid);
                        final Map<String, Object> expandedProperties = expandedMember.getProperties();
                        reasonerMember.setCharacteristicTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID));
                        reasonerMember.setGroup((Integer) expandedProperties.get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP));
                        // reasonerMember.setReferencedComponent(...) is already set (or expanded)
                        reasonerMember.setReferenceSetId(expandedMember.getRefsetId());
                        // reasonerMember.setReleased(...) is already set
                        reasonerMember.setSerializedValue((String) expandedProperties.get(SnomedRf2Headers.FIELD_VALUE));
                        reasonerMember.setTypeId((String) expandedProperties.get(SnomedRf2Headers.FIELD_TYPE_ID));
                    }
                    break;
                default:
                    throw new IllegalStateException(String.format("Unexpected CD member change '%s' found with UUID '%s'.", item.getChangeNature(), item.getConcreteDomainMember().getOriginMemberId()));
            }
        }
    }
}
Also used : BranchRequest(com.b2international.snowowl.core.request.BranchRequest) com.b2international.snowowl.snomed.reasoner.domain(com.b2international.snowowl.snomed.reasoner.domain) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) Map(java.util.Map) CollectionResource(com.b2international.snowowl.core.domain.CollectionResource) SnomedReferenceSetMembers(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ClassificationRequests(com.b2international.snowowl.snomed.reasoner.request.ClassificationRequests) Collection(java.util.Collection) Request(com.b2international.snowowl.core.events.Request) Set(java.util.Set) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) ConcreteDomainChangeDocument(com.b2international.snowowl.snomed.reasoner.index.ConcreteDomainChangeDocument) List(java.util.List) SnomedCoreComponent(com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) Entry(java.util.Map.Entry) SearchResourceRequestBuilder(com.b2international.snowowl.core.request.SearchResourceRequestBuilder) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) BranchContext(com.b2international.snowowl.core.domain.BranchContext) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory) BaseResourceConverter(com.b2international.snowowl.core.request.BaseResourceConverter) Options(com.b2international.commons.options.Options) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) ComponentCategory(com.b2international.snowowl.core.terminology.ComponentCategory) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) SnomedReferenceSetMembers(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Collection(java.util.Collection) Map(java.util.Map)

Aggregations

ComponentCategory (com.b2international.snowowl.core.terminology.ComponentCategory)8 Options (com.b2international.commons.options.Options)3 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)3 Set (java.util.Set)3 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)2 BranchContext (com.b2international.snowowl.core.domain.BranchContext)2 DelegatingRequest (com.b2international.snowowl.core.events.DelegatingRequest)2 Request (com.b2international.snowowl.core.events.Request)2 BulkRequest (com.b2international.snowowl.core.events.bulk.BulkRequest)2 TransactionalRequest (com.b2international.snowowl.core.request.TransactionalRequest)2 SnomedIdentifiers (com.b2international.snowowl.snomed.cis.SnomedIdentifiers)2 SnomedCoreComponent (com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent)2 Collection (java.util.Collection)2 Map (java.util.Map)2 CompareUtils (com.b2international.commons.CompareUtils)1 AlreadyExistsException (com.b2international.commons.exceptions.AlreadyExistsException)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 NotImplementedException (com.b2international.commons.exceptions.NotImplementedException)1 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)1 Index (com.b2international.index.Index)1