Search in sources :

Example 1 with RevisionIndexReadRequest

use of com.b2international.snowowl.core.request.RevisionIndexReadRequest in project snow-owl by b2ihealthcare.

the class DescriptionChangeConverter method expand.

@Override
public void expand(final List<DescriptionChange> results) {
    if (!expand().containsKey(DescriptionChange.Expand.DESCRIPTION)) {
        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, DescriptionChange> itemsByBranch = getItemsByBranch(results);
    // Check if we only need to load inferred CD members in their entirety
    final Options expandOptions = expand().getOptions(DescriptionChange.Expand.DESCRIPTION);
    final boolean inferredOnly = expandOptions.getBoolean("inferredOnly");
    final Options descriptionExpandOptions = expandOptions.getOptions("expand");
    final Options conceptOptions = descriptionExpandOptions.getOptions("concept");
    final boolean needsConcept = descriptionExpandOptions.keySet().contains("concept");
    for (final String branch : itemsByBranch.keySet()) {
        final Collection<DescriptionChange> itemsForCurrentBranch = itemsByBranch.get(branch);
        /*
			 * Expand concept on "new" descriptions via a separate search request, they will
			 * be different from the concept on the "origin" description.
			 */
        if (needsConcept) {
            final List<ReasonerDescription> blankDescriptions = itemsForCurrentBranch.stream().filter(c -> ChangeNature.NEW.equals(c.getChangeNature())).map(DescriptionChange::getDescription).collect(Collectors.toList());
            final Multimap<String, ReasonerDescription> descriptionsByConceptId = FluentIterable.from(blankDescriptions).index(ReasonerDescription::getConceptId);
            final Set<String> conceptIds = descriptionsByConceptId.keySet();
            final Request<BranchContext, SnomedConcepts> conceptSearchRequest = SnomedRequests.prepareSearchConcept().filterByIds(conceptIds).setLimit(conceptIds.size()).setExpand(conceptOptions.get("expand", Options.class)).setLocales(locales()).build();
            final SnomedConcepts concepts = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(conceptSearchRequest)).execute(context());
            for (final SnomedConcept concept : concepts) {
                final String conceptId = concept.getId();
                final Collection<ReasonerDescription> descriptionsForConcept = descriptionsByConceptId.get(conceptId);
                for (final ReasonerDescription description : descriptionsForConcept) {
                    description.setConcept(concept);
                }
            }
        }
        /*
			 * Then fetch all the required descriptions. Note that the same "origin"
			 * description might be used for multiple eg. "new" counterparts.
			 */
        final Set<String> descriptionIds = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(c -> c.getDescription().getOriginDescriptionId()).collect(Collectors.toSet());
        final Request<BranchContext, SnomedDescriptions> descriptionSearchRequest = SnomedRequests.prepareSearchDescription().filterByIds(descriptionIds).setLimit(descriptionIds.size()).setExpand(descriptionExpandOptions).setLocales(locales()).build();
        final SnomedDescriptions descriptions = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(descriptionSearchRequest)).execute(context());
        final Map<String, SnomedDescription> descriptionsById = Maps.uniqueIndex(descriptions, SnomedDescription::getId);
        for (final DescriptionChange item : itemsForCurrentBranch) {
            final ReasonerDescription reasonerDescription = item.getDescription();
            final String descriptionId = reasonerDescription.getOriginDescriptionId();
            switch(item.getChangeNature()) {
                case NEW:
                    {
                        final SnomedDescription expandedDescription = descriptionsById.get(descriptionId);
                        reasonerDescription.setAcceptabilityMap(expandedDescription.getAcceptabilityMap());
                        reasonerDescription.setCaseSignificanceId(expandedDescription.getCaseSignificanceId());
                        // reasonerDescription.setConcept(...) is already set earlier (or expanded)
                        reasonerDescription.setLanguageCode(expandedDescription.getLanguageCode());
                        // reasonerMember.setReleased(...) is already set
                        reasonerDescription.setTerm(expandedDescription.getTerm());
                        reasonerDescription.setType(expandedDescription.getType());
                    }
                    break;
                case REDUNDANT:
                    if (!inferredOnly) {
                        final SnomedDescription expandedDescription = descriptionsById.get(descriptionId);
                        reasonerDescription.setAcceptabilityMap(expandedDescription.getAcceptabilityMap());
                        reasonerDescription.setCaseSignificanceId(expandedDescription.getCaseSignificanceId());
                        reasonerDescription.setConcept(expandedDescription.getConcept());
                        reasonerDescription.setLanguageCode(expandedDescription.getLanguageCode());
                        // reasonerMember.setReleased(...) is already set
                        reasonerDescription.setTerm(expandedDescription.getTerm());
                        reasonerDescription.setType(expandedDescription.getType());
                    }
                    break;
                default:
                    throw new IllegalStateException(String.format("Unexpected description change '%s' found with SCTID '%s'.", item.getChangeNature(), item.getDescription().getOriginDescriptionId()));
            }
        }
    }
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) 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) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) DescriptionChangeDocument(com.b2international.snowowl.snomed.reasoner.index.DescriptionChangeDocument) SnomedDescriptions(com.b2international.snowowl.snomed.core.domain.SnomedDescriptions) 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) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) Set(java.util.Set) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) List(java.util.List) BranchContext(com.b2international.snowowl.core.domain.BranchContext) BaseResourceConverter(com.b2international.snowowl.core.request.BaseResourceConverter) Options(com.b2international.commons.options.Options) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) SnomedDescriptions(com.b2international.snowowl.snomed.core.domain.SnomedDescriptions) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) BranchContext(com.b2international.snowowl.core.domain.BranchContext)

Example 2 with RevisionIndexReadRequest

use of com.b2international.snowowl.core.request.RevisionIndexReadRequest in project snow-owl by b2ihealthcare.

the class Rf2Exporter method exportBranch.

public final void exportBranch(final Path releaseDirectory, final RepositoryContext context, final String branch, final long effectiveTimeStart, final long effectiveTimeEnd, final Set<String> visitedComponentEffectiveTimes) throws IOException {
    LOG.info("Exporting {} branch to '{}'", branch, getFileName());
    // Ensure that the path leading to the export file exists
    final Path exportFileDirectory = releaseDirectory.resolve(getRelativeDirectory());
    Files.createDirectories(exportFileDirectory);
    final Path exportFile = exportFileDirectory.resolve(getFileName());
    try (RandomAccessFile randomAccessFile = new RandomAccessFile(exportFile.toFile(), "rw")) {
        try (FileChannel fileChannel = randomAccessFile.getChannel()) {
            // Add a header if the file is empty
            if (randomAccessFile.length() == 0L) {
                fileChannel.write(toByteBuffer(TAB_JOINER.join(getHeader())));
                fileChannel.write(toByteBuffer(CR_LF));
            }
            // We want to append rows, if the file already exists, so jump to the end
            fileChannel.position(fileChannel.size());
            /*
				 * XXX: createSearchRequestBuilder() should handle namespace/language code
				 * filtering, if applicable; we will only handle the effective time and module
				 * filters here.
				 * 
				 * An effective time filter is always set, even if not in delta mode, to prevent
				 * exporting unpublished content twice.
				 */
            new BranchRequest<R>(branch, new RevisionIndexReadRequest<>(inner -> {
                createSearchRequestBuilder().filterByModules(// null value will be ignored
                modules).filterByEffectiveTime(effectiveTimeStart, effectiveTimeEnd).setLimit(BATCH_SIZE).setFields(Arrays.asList(getHeader())).stream(inner).flatMap(hits -> getMappedStream(hits, context, branch)).forEachOrdered(row -> {
                    String id = row.get(0);
                    String effectiveTime = row.get(1);
                    if (!visitedComponentEffectiveTimes.add(String.join("_", id, effectiveTime))) {
                        return;
                    }
                    try {
                        fileChannel.write(toByteBuffer(TAB_JOINER.join(row)));
                        fileChannel.write(toByteBuffer(CR_LF));
                    } catch (final IOException e) {
                        throw new SnowowlRuntimeException("Failed to write contents for file '" + exportFile.getFileName() + "'.");
                    }
                });
                return null;
            })).execute(context);
        }
    }
}
Also used : Path(java.nio.file.Path) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) RandomAccessFile(java.io.RandomAccessFile) BranchRequest(com.b2international.snowowl.core.request.BranchRequest) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) SnomedSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedSearchRequestBuilder) ByteBuffer(java.nio.ByteBuffer) BooleanUtils(com.b2international.commons.BooleanUtils) Path(java.nio.file.Path) Rf2ReleaseType(com.b2international.snowowl.snomed.core.domain.Rf2ReleaseType) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) DateFormats(com.b2international.snowowl.core.date.DateFormats) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) Files(java.nio.file.Files) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) PageableCollectionResource(com.b2international.snowowl.core.domain.PageableCollectionResource) List(java.util.List) Stream(java.util.stream.Stream) LocalDate(java.time.LocalDate) FileChannel(java.nio.channels.FileChannel) SnomedComponent(com.b2international.snowowl.snomed.core.domain.SnomedComponent) Joiner(com.google.common.base.Joiner) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 3 with RevisionIndexReadRequest

use of com.b2international.snowowl.core.request.RevisionIndexReadRequest in project snow-owl by b2ihealthcare.

the class ConcreteDomainChangeConverter method expandComponentCategory.

// Copied from SnomedReferenceSetMemberConverter
private void expandComponentCategory(final String branch, final ComponentCategory category, final Collection<String> componentIds, final Options componentOptions, final Multimap<String, ReasonerConcreteDomainMember> membersByReferencedComponent) {
    final SearchResourceRequestBuilder<?, BranchContext, ? extends CollectionResource<? extends SnomedCoreComponent>> searchRequestBuilder = createSearchRequestBuilder(category);
    searchRequestBuilder.filterByIds(componentIds).setLimit(componentIds.size()).setLocales(locales()).setExpand(componentOptions.get("expand", Options.class));
    final CollectionResource<? extends SnomedCoreComponent> referencedComponents = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(searchRequestBuilder.build())).execute(context());
    for (final SnomedCoreComponent referencedComponent : referencedComponents) {
        for (final ReasonerConcreteDomainMember member : membersByReferencedComponent.get(referencedComponent.getId())) {
            member.setReferencedComponent(referencedComponent);
        }
    }
}
Also used : Options(com.b2international.commons.options.Options) SnomedCoreComponent(com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent) BranchContext(com.b2international.snowowl.core.domain.BranchContext) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest)

Example 4 with RevisionIndexReadRequest

use of com.b2international.snowowl.core.request.RevisionIndexReadRequest 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)

Example 5 with RevisionIndexReadRequest

use of com.b2international.snowowl.core.request.RevisionIndexReadRequest in project snow-owl by b2ihealthcare.

the class RelationshipChangeConverter method expand.

@Override
public void expand(final List<RelationshipChange> results) {
    if (!expand().containsKey(RelationshipChange.Expand.RELATIONSHIP)) {
        return;
    }
    /*
		 * Depending on the relationship change search request, we might need to issue
		 * SNOMED CT searches against multiple branches; find out which ones we have.
		 */
    final Multimap<String, RelationshipChange> itemsByBranch = getItemsByBranch(results);
    // Check if we only need to load inferred relationships in their entirety
    final Options expandOptions = expand().getOptions(RelationshipChange.Expand.RELATIONSHIP);
    final boolean inferredOnly = expandOptions.getBoolean("inferredOnly");
    final Options relationshipExpandOptions = expandOptions.getOptions("expand");
    final Options sourceOptions = relationshipExpandOptions.getOptions(SnomedRelationship.Expand.SOURCE);
    final Options typeOptions = relationshipExpandOptions.getOptions(SnomedRelationship.Expand.TYPE);
    final Options destinationOptions = relationshipExpandOptions.getOptions(SnomedRelationship.Expand.DESTINATION);
    final boolean needsSource = relationshipExpandOptions.keySet().contains(SnomedRelationship.Expand.SOURCE);
    final boolean needsType = relationshipExpandOptions.keySet().contains(SnomedRelationship.Expand.TYPE);
    final boolean needsDestination = relationshipExpandOptions.keySet().contains(SnomedRelationship.Expand.DESTINATION);
    // Do not allow expansion of members
    final boolean needsMembers = relationshipExpandOptions.keySet().contains(MEMBERS);
    if (needsMembers) {
        throw new BadRequestException("Members can not be expanded on reasoner relationship changes.");
    }
    for (final String branch : itemsByBranch.keySet()) {
        final Collection<RelationshipChange> itemsForCurrentBranch = itemsByBranch.get(branch);
        /*
			 * Expand concepts on the relationship currently set on each item first, as they
			 * might have changed when compared to the "origin" relationship.
			 */
        if (needsSource) {
            expandConcepts(branch, itemsForCurrentBranch, sourceOptions, ReasonerRelationship::getSourceId, ReasonerRelationship::setSource);
        }
        if (needsType) {
            expandConcepts(branch, itemsForCurrentBranch, typeOptions, ReasonerRelationship::getTypeId, ReasonerRelationship::setType);
        }
        if (needsDestination) {
            expandConcepts(branch, itemsForCurrentBranch, destinationOptions, ReasonerRelationship::getDestinationId, ReasonerRelationship::setDestination);
        }
        // Now fetch the rest of the properties for the relationships (except IS As where no ID is recorded)
        final Set<String> relationshipIds = itemsForCurrentBranch.stream().filter(c -> !inferredOnly || ChangeNature.NEW.equals(c.getChangeNature())).map(c -> c.getRelationship().getOriginId()).filter(id -> id != null).collect(Collectors.toSet());
        final Request<BranchContext, SnomedRelationships> relationshipSearchRequest = SnomedRequests.prepareSearchRelationship().filterByIds(relationshipIds).setLimit(relationshipIds.size()).setExpand(relationshipExpandOptions).setLocales(locales()).build();
        final SnomedRelationships relationships = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(relationshipSearchRequest)).execute(context());
        final Map<String, SnomedRelationship> relationshipsById = Maps.uniqueIndex(relationships, SnomedRelationship::getId);
        for (final RelationshipChange item : itemsForCurrentBranch) {
            final ReasonerRelationship reasonerRelationship = item.getRelationship();
            final String originId = reasonerRelationship.getOriginId();
            switch(item.getChangeNature()) {
                case NEW:
                    if (originId == null) {
                        // reasonerRelationship.setCharacteristicType(...) is already set
                        // reasonerRelationship.setDestination(...) is already set
                        reasonerRelationship.setDestinationNegated(false);
                        // reasonerRelationship.setGroup(...) is already set
                        reasonerRelationship.setModifierId(Concepts.EXISTENTIAL_RESTRICTION_MODIFIER);
                    // reasonerRelationship.setReleased(...) is already set
                    // reasonerRelationship.setSource(...) is already set
                    // reasonerRelationship.setType(...) is already set
                    // reasonerRelationship.setUnionGroup(...) is already set
                    // reasonerRelationship.setValue(...) is already set
                    } else {
                        final SnomedRelationship expandedRelationship = relationshipsById.get(originId);
                        // reasonerRelationship.setCharacteristicType(...) is already set
                        reasonerRelationship.setDestination(expandedRelationship.getDestination());
                        reasonerRelationship.setDestinationNegated(expandedRelationship.isDestinationNegated());
                        // reasonerRelationship.setGroup(...) is already set
                        reasonerRelationship.setModifierId(expandedRelationship.getModifierId());
                        // reasonerRelationship.setReleased(...) is already set
                        // reasonerRelationship.setSource(...) is already set
                        reasonerRelationship.setType(expandedRelationship.getType());
                        // reasonerRelationship.setUnionGroup(...) is already set
                        reasonerRelationship.setValueAsObject(expandedRelationship.getValueAsObject());
                    }
                    break;
                case UPDATED:
                    if (!inferredOnly) {
                        final SnomedRelationship expandedRelationship = relationshipsById.get(originId);
                        reasonerRelationship.setCharacteristicTypeId(expandedRelationship.getCharacteristicTypeId());
                        reasonerRelationship.setDestination(expandedRelationship.getDestination());
                        reasonerRelationship.setDestinationNegated(expandedRelationship.isDestinationNegated());
                        // reasonerRelationship.setGroup(...) is already set
                        reasonerRelationship.setModifierId(expandedRelationship.getModifierId());
                        // reasonerRelationship.setReleased(...) is already set
                        reasonerRelationship.setSource(expandedRelationship.getSource());
                        reasonerRelationship.setType(expandedRelationship.getType());
                        reasonerRelationship.setUnionGroup(expandedRelationship.getUnionGroup());
                        reasonerRelationship.setValueAsObject(expandedRelationship.getValueAsObject());
                    }
                    break;
                case REDUNDANT:
                    if (!inferredOnly) {
                        final SnomedRelationship expandedRelationship = relationshipsById.get(originId);
                        reasonerRelationship.setCharacteristicTypeId(expandedRelationship.getCharacteristicTypeId());
                        reasonerRelationship.setDestination(expandedRelationship.getDestination());
                        reasonerRelationship.setDestinationNegated(expandedRelationship.isDestinationNegated());
                        reasonerRelationship.setGroup(expandedRelationship.getRelationshipGroup());
                        reasonerRelationship.setModifierId(expandedRelationship.getModifierId());
                        // reasonerRelationship.setReleased(...) is already set
                        reasonerRelationship.setSource(expandedRelationship.getSource());
                        reasonerRelationship.setType(expandedRelationship.getType());
                        reasonerRelationship.setUnionGroup(expandedRelationship.getUnionGroup());
                        reasonerRelationship.setValueAsObject(expandedRelationship.getValueAsObject());
                    }
                    break;
                default:
                    throw new IllegalStateException(String.format("Unexpected relationship change '%s' found with SCTID '%s'.", item.getChangeNature(), item.getRelationship().getOriginId()));
            }
        }
    }
}
Also used : BranchRequest(com.b2international.snowowl.core.request.BranchRequest) com.b2international.snowowl.snomed.reasoner.domain(com.b2international.snowowl.snomed.reasoner.domain) com.b2international.snowowl.snomed.core.domain(com.b2international.snowowl.snomed.core.domain) Multimap(com.google.common.collect.Multimap) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) RelationshipChangeDocument(com.b2international.snowowl.snomed.reasoner.index.RelationshipChangeDocument) Multimaps(com.google.common.collect.Multimaps) BigDecimal(java.math.BigDecimal) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) BadRequestException(com.b2international.commons.exceptions.BadRequestException) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) Function(com.google.common.base.Function) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) 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) List(java.util.List) BranchContext(com.b2international.snowowl.core.domain.BranchContext) BaseResourceConverter(com.b2international.snowowl.core.request.BaseResourceConverter) Options(com.b2international.commons.options.Options) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) BranchContext(com.b2international.snowowl.core.domain.BranchContext) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Aggregations

RevisionIndexReadRequest (com.b2international.snowowl.core.request.RevisionIndexReadRequest)6 Options (com.b2international.commons.options.Options)5 BranchContext (com.b2international.snowowl.core.domain.BranchContext)5 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)5 BranchRequest (com.b2international.snowowl.core.request.BranchRequest)5 Collection (java.util.Collection)5 List (java.util.List)5 Set (java.util.Set)5 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)4 Request (com.b2international.snowowl.core.events.Request)4 BaseResourceConverter (com.b2international.snowowl.core.request.BaseResourceConverter)4 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)4 com.b2international.snowowl.snomed.reasoner.domain (com.b2international.snowowl.snomed.reasoner.domain)4 ClassificationRequests (com.b2international.snowowl.snomed.reasoner.request.ClassificationRequests)4 Maps (com.google.common.collect.Maps)4 Multimap (com.google.common.collect.Multimap)4 Multimaps (com.google.common.collect.Multimaps)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)2