Search in sources :

Example 56 with Options

use of com.b2international.commons.options.Options 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)

Example 57 with Options

use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.

the class ConceptChangeConverter method expand.

@Override
public void expand(final List<ConceptChange> results) {
    if (!expand().containsKey(ConceptChange.Expand.CONCEPT)) {
        return;
    }
    /*
		 * Depending on the concept change search request, we might need to issue
		 * SNOMED CT searches against multiple branches; find out which ones we have.
		 */
    final Multimap<String, ConceptChange> itemsByBranch = getItemsByBranch(results);
    // Check if we only need to load inferred concepts in their entirety
    final Options expandOptions = expand().getOptions(ConceptChange.Expand.CONCEPT);
    // pt() and fsn() are the only useful options here
    final Options conceptExpandOptions = expandOptions.getOptions("expand");
    conceptExpandOptions.keySet().retainAll(CONCEPT_EXPAND_OPTIONS);
    for (final String branch : itemsByBranch.keySet()) {
        final Collection<ConceptChange> itemsForCurrentBranch = itemsByBranch.get(branch);
        final Set<String> conceptIds = itemsForCurrentBranch.stream().map(c -> c.getConcept().getOriginConceptId()).collect(Collectors.toSet());
        final Request<BranchContext, SnomedConcepts> conceptSearchRequest = SnomedRequests.prepareSearchConcept().filterByIds(conceptIds).setLimit(conceptIds.size()).setExpand(conceptExpandOptions).setLocales(locales()).build();
        final SnomedConcepts concepts = new BranchRequest<>(branch, new RevisionIndexReadRequest<>(conceptSearchRequest)).execute(context());
        final Map<String, SnomedConcept> conceptsById = Maps.uniqueIndex(concepts, SnomedConcept::getId);
        for (final ConceptChange item : itemsForCurrentBranch) {
            final ReasonerConcept reasonerConcept = item.getConcept();
            final String conceptId = reasonerConcept.getOriginConceptId();
            final SnomedConcept expandedConcept = conceptsById.get(conceptId);
            reasonerConcept.setDefinitionStatusId(expandedConcept.getDefinitionStatusId());
            reasonerConcept.setPt(expandedConcept.getPt());
            reasonerConcept.setFsn(expandedConcept.getFsn());
        // reasonerConcept.setReleased(...) is already set
        }
    }
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) BranchRequest(com.b2international.snowowl.core.request.BranchRequest) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ImmutableSet(com.google.common.collect.ImmutableSet) 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) com.b2international.snowowl.snomed.reasoner.domain(com.b2international.snowowl.snomed.reasoner.domain) Set(java.util.Set) Multimap(com.google.common.collect.Multimap) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) Multimaps(com.google.common.collect.Multimaps) List(java.util.List) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) Map(java.util.Map) ConceptChangeDocument(com.b2international.snowowl.snomed.reasoner.index.ConceptChangeDocument) 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) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) BranchContext(com.b2international.snowowl.core.domain.BranchContext)

Aggregations

Options (com.b2international.commons.options.Options)57 List (java.util.List)15 Collectors (java.util.stream.Collectors)14 Set (java.util.Set)13 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)11 BranchContext (com.b2international.snowowl.core.domain.BranchContext)11 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)11 SnomedConceptRequestCache (com.b2international.snowowl.snomed.datastore.request.SnomedConceptRequestCache)11 Test (org.junit.Test)9 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)8 Map (java.util.Map)8 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)7 SnomedConcepts (com.b2international.snowowl.snomed.core.domain.SnomedConcepts)7 BadRequestException (com.b2international.commons.exceptions.BadRequestException)6 ResourceURI (com.b2international.snowowl.core.ResourceURI)6 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)6 SnomedRelationship (com.b2international.snowowl.snomed.core.domain.SnomedRelationship)6 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)6 Multimaps (com.google.common.collect.Multimaps)6 Collection (java.util.Collection)6