Search in sources :

Example 46 with SnomedConcept

use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.

the class SnomedCompareRestRequestTest method testCompareWithChangedComponents.

@Test
public void testCompareWithChangedComponents() throws IOException {
    final String newConceptId = createNewConcept(parentBranch);
    final SnomedConcept concept = getComponent(parentBranch, SnomedComponentType.CONCEPT, newConceptId).extract().as(SnomedConcept.class);
    final String parentBranchPath = parentBranch.toString();
    final String childBranchPath = createBranch(parentBranchPath, "SNOMEDCT-CHANGED");
    SnomedRequests.prepareUpdateConcept(concept.getId()).setModuleId(Concepts.MODULE_SCT_MODEL_COMPONENT).build(childBranchPath, RestExtensions.USER, "Change module ID").execute(bus).getSync();
    final BranchCompareResult compareResult = getCompareResult(parentBranchPath, childBranchPath);
    // compare child branch with it's parent
    assertThat(compareResult.getNewComponents()).isEmpty();
    assertThat(compareResult.getChangedComponents()).contains(ComponentIdentifier.of(SnomedConcept.TYPE, newConceptId));
    assertThat(compareResult.getDeletedComponents()).isEmpty();
}
Also used : BranchCompareResult(com.b2international.snowowl.core.branch.compare.BranchCompareResult) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) Test(org.junit.Test)

Example 47 with SnomedConcept

use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.

the class SnomedConceptMapSearchRequestEvaluator method toCollectionResource.

private ConceptMapMappings toCollectionResource(SnomedReferenceSetMembers referenceSetMembers, ResourceURI uri, ServiceProvider context, Options search, SnomedDisplayTermType snomedDisplayTermType) {
    final Set<String> refSetsToFetch = referenceSetMembers.stream().map(SnomedReferenceSetMember::getRefsetId).collect(Collectors.toSet());
    final Map<String, SnomedConcept> refSetsById = SnomedRequests.prepareSearchConcept().all().filterByIds(refSetsToFetch).setLocales(search.getList(OptionKey.LOCALES, ExtendedLocale.class)).setExpand("pt(),referenceSet()").build(uri).execute(context.service(IEventBus.class)).getSync(1, TimeUnit.MINUTES).stream().collect(Collectors.toMap(SnomedConcept::getId, concept -> concept));
    final Map<String, ComponentURI> targetComponentsByRefSetId = getTargetComponentsByRefSetId(context, refSetsById);
    List<ConceptMapMapping> mappings = referenceSetMembers.stream().filter(m -> SnomedConcept.TYPE.equals(m.getReferencedComponent().getComponentType())).map(m -> {
        return toMapping(m, uri, targetComponentsByRefSetId.get(m.getRefsetId()), snomedDisplayTermType, refSetsById);
    }).collect(Collectors.toList());
    if (!mappings.isEmpty()) {
        final Map<String, Concept> conceptsById = Multimaps.index(mappings, mapping -> mapping.getTargetComponentURI().resourceUri()).asMap().entrySet().stream().filter(entry -> !TerminologyRegistry.UNSPECIFIED.equals(entry.getKey().getResourceId())).map(entry -> {
            final Set<String> idsToFetch = entry.getValue().stream().map(map -> map.getTargetComponentURI().identifier()).collect(Collectors.toSet());
            return CodeSystemRequests.prepareSearchConcepts().all().filterByCodeSystemUri(entry.getKey()).filterByIds(idsToFetch).buildAsync().execute(context.service(IEventBus.class)).getSync(5, TimeUnit.MINUTES).stream().collect(Collectors.toMap(Concept::getId, c -> c));
        }).flatMap(map -> map.entrySet().stream()).collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (concept1, concept2) -> concept1));
        mappings = mappings.stream().map(mapping -> {
            final String mapTargetId = mapping.getTargetComponentURI().identifier();
            if (conceptsById.containsKey(mapTargetId) && !mapping.getTargetComponentURI().isUnspecified()) {
                final Concept concept = conceptsById.get(mapTargetId);
                return mapping.toBuilder().targetTerm(concept.getTerm()).targetIconId(concept.getIconId()).build();
            } else {
                return mapping;
            }
        }).collect(Collectors.toList());
    }
    return new ConceptMapMappings(mappings, referenceSetMembers.getSearchAfter(), referenceSetMembers.getLimit(), referenceSetMembers.getTotal());
}
Also used : CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) SnomedRefSetUtil(com.b2international.snowowl.snomed.datastore.SnomedRefSetUtil) ConceptMapMappingSearchRequestEvaluator(com.b2international.snowowl.core.request.ConceptMapMappingSearchRequestEvaluator) SnomedDisplayTermType(com.b2international.snowowl.snomed.core.SnomedDisplayTermType) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Multimaps(com.google.common.collect.Multimaps) Strings(com.google.common.base.Strings) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) Options(com.b2international.commons.options.Options) ConceptMapMappings(com.b2international.snowowl.core.domain.ConceptMapMappings) Map(java.util.Map) ResourceURI(com.b2international.snowowl.core.ResourceURI) SnomedReferenceSetMembers(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers) ConceptMapMapping(com.b2international.snowowl.core.domain.ConceptMapMapping) Collection(java.util.Collection) MappingCorrelation(com.b2international.snowowl.core.request.MappingCorrelation) Set(java.util.Set) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) Builder(com.b2international.snowowl.core.domain.ConceptMapMapping.Builder) IEventBus(com.b2international.snowowl.eventbus.IEventBus) Collectors(java.util.stream.Collectors) TerminologyRegistry(com.b2international.snowowl.core.terminology.TerminologyRegistry) TimeUnit(java.util.concurrent.TimeUnit) ResourceDocument(com.b2international.snowowl.core.internal.ResourceDocument) List(java.util.List) Concept(com.b2international.snowowl.core.domain.Concept) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) OptionsBuilder(com.b2international.commons.options.OptionsBuilder) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) Concept(com.b2international.snowowl.core.domain.Concept) ConceptMapMapping(com.b2international.snowowl.core.domain.ConceptMapMapping) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) ConceptMapMappings(com.b2international.snowowl.core.domain.ConceptMapMappings)

Example 48 with SnomedConcept

use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.

the class EvaluateQueryRefSetMemberRequest method execute.

@Override
public QueryRefSetMemberEvaluation execute(BranchContext context) {
    // TODO support pre-population???
    final boolean active;
    final String query;
    final String targetReferenceSet;
    if (context instanceof TransactionContext) {
        SnomedRefSetMemberIndexEntry member = ((TransactionContext) context).lookup(memberId, SnomedRefSetMemberIndexEntry.class);
        query = member.getQuery();
        targetReferenceSet = member.getReferencedComponentId();
        active = member.isActive();
    } else {
        final SnomedReferenceSetMember member = SnomedRequests.prepareGetMember(memberId).build().execute(context);
        query = (String) member.getProperties().get(SnomedRf2Headers.FIELD_QUERY);
        targetReferenceSet = member.getReferencedComponent().getId();
        active = member.isActive();
    }
    if (!active) {
        return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, Collections.emptyList());
    }
    if (Strings.isNullOrEmpty(query)) {
        return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, Collections.emptyList());
    }
    // add all matching first
    final Map<String, SnomedConcept> conceptsToAdd = newHashMap();
    // GET matching members of a query
    SnomedRequests.prepareSearchConcept().filterByEcl(query).setLimit(10_000).stream(context).flatMap(SnomedConcepts::stream).forEach(match -> conceptsToAdd.put(match.getId(), match));
    final Collection<SnomedReferenceSetMember> membersToRemove = newHashSet();
    final Collection<SnomedReferenceSetMember> conceptsToActivate = newHashSet();
    // then re-evaluate all current members of the target simple type reference set
    SnomedRequests.prepareSearchMember().filterByRefSet(targetReferenceSet).setLimit(10_000).stream(context).flatMap(SnomedReferenceSetMembers::stream).forEach(member -> {
        final String referencedComponentId = member.getReferencedComponent().getId();
        if (conceptsToAdd.containsKey(referencedComponentId)) {
            if (!member.isActive()) {
                conceptsToAdd.remove(referencedComponentId);
                conceptsToActivate.add(member);
            } else {
                conceptsToAdd.remove(referencedComponentId);
            }
        } else {
            if (member.isActive()) {
                membersToRemove.add(member);
            }
        }
    });
    // fetch all referenced components
    final Set<String> referencedConceptIds = newHashSet();
    referencedConceptIds.addAll(conceptsToAdd.keySet());
    referencedConceptIds.addAll(FluentIterable.from(membersToRemove).transform(SnomedReferenceSetMember::getReferencedComponent).transform(IComponent::getId).toSet());
    referencedConceptIds.addAll(FluentIterable.from(conceptsToActivate).transform(SnomedReferenceSetMember::getReferencedComponent).transform(IComponent::getId).toSet());
    final Map<String, SnomedConcept> concepts;
    if (expand().containsKey("referencedComponent")) {
        final Options expandOptions = expand().getOptions("referencedComponent");
        concepts = Maps.uniqueIndex(SnomedRequests.prepareSearchConcept().filterByIds(referencedConceptIds).setLimit(referencedConceptIds.size()).setExpand(expandOptions.getOptions("expand")).setLocales(locales()).build().execute(context), IComponent::getId);
    } else {
        // initialize with empty SnomedConcept resources
        concepts = newHashMap();
        for (String referencedConceptId : referencedConceptIds) {
            concepts.put(referencedConceptId, new SnomedConcept(referencedConceptId));
        }
    }
    final Collection<MemberChange> changes = newArrayList();
    for (String id : conceptsToAdd.keySet()) {
        changes.add(MemberChangeImpl.added(concepts.get(id)));
    }
    for (SnomedReferenceSetMember memberToRemove : membersToRemove) {
        changes.add(MemberChangeImpl.removed(concepts.get(memberToRemove.getReferencedComponent().getId()), memberToRemove.getId()));
    }
    for (SnomedReferenceSetMember conceptToActivate : conceptsToActivate) {
        changes.add(MemberChangeImpl.changed(concepts.get(conceptToActivate.getReferencedComponent().getId()), conceptToActivate.getId()));
    }
    return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, changes);
}
Also used : Options(com.b2international.commons.options.Options) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext)

Example 49 with SnomedConcept

use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.

the class SnomedDescendantsExpander method expand.

@Override
protected void expand(List<SnomedConcept> results, final Set<String> conceptIds, Options descendantExpandOptions, boolean direct) {
    try {
        final int limit = getLimit(descendantExpandOptions);
        final ExpressionBuilder expression = Expressions.builder();
        expression.filter(active());
        final ExpressionBuilder descendantFilter = Expressions.builder();
        if (stated) {
            descendantFilter.should(statedParents(conceptIds));
            if (!direct) {
                descendantFilter.should(statedAncestors(conceptIds));
            }
        } else {
            descendantFilter.should(parents(conceptIds));
            if (!direct) {
                descendantFilter.should(ancestors(conceptIds));
            }
        }
        expression.filter(descendantFilter.build());
        final Query<SnomedConceptDocument> query = Query.select(SnomedConceptDocument.class).where(expression.build()).limit((conceptIds.size() == 1 && limit == 0) ? limit : Integer.MAX_VALUE).build();
        final RevisionSearcher searcher = context().service(RevisionSearcher.class);
        final Hits<SnomedConceptDocument> hits = searcher.search(query);
        if (hits.getTotal() < 1) {
            final SnomedConcepts descendants = new SnomedConcepts(0, 0);
            for (SnomedConcept concept : results) {
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
            return;
        }
        // XXX won't work if number of results is greater than one, either use custom ConceptSearch or figure out how to expand descendants effectively
        if (conceptIds.size() == 1 && limit == 0) {
            for (SnomedConcept concept : results) {
                final SnomedConcepts descendants = new SnomedConcepts(0, hits.getTotal());
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
            return;
        }
        final Multimap<String, String> descendantsByAncestor = TreeMultimap.create();
        for (SnomedConceptDocument hit : hits) {
            final Set<String> parentsAndAncestors = newHashSet();
            if (stated) {
                parentsAndAncestors.addAll(LongSets.toStringSet(hit.getStatedParents()));
                if (!direct) {
                    parentsAndAncestors.addAll(LongSets.toStringSet(hit.getStatedAncestors()));
                }
            } else {
                parentsAndAncestors.addAll(LongSets.toStringSet(hit.getParents()));
                if (!direct) {
                    parentsAndAncestors.addAll(LongSets.toStringSet(hit.getAncestors()));
                }
            }
            parentsAndAncestors.retainAll(conceptIds);
            for (String ancestor : parentsAndAncestors) {
                descendantsByAncestor.put(ancestor, hit.getId());
            }
        }
        final Collection<String> componentIds = newHashSet(descendantsByAncestor.values());
        if (limit > 0 && !componentIds.isEmpty()) {
            // query descendants again
            final SnomedConcepts descendants = SnomedRequests.prepareSearchConcept().all().filterByIds(componentIds).setLocales(locales()).setExpand(descendantExpandOptions.get("expand", Options.class)).build().execute(context());
            final Map<String, SnomedConcept> descendantsById = newHashMap();
            descendantsById.putAll(Maps.uniqueIndex(descendants, SnomedConcept::getId));
            for (SnomedConcept concept : results) {
                final Collection<String> descendantIds = descendantsByAncestor.get(concept.getId());
                final List<SnomedConcept> currentDescendants = FluentIterable.from(descendantIds).limit(limit).transform(Functions.forMap(descendantsById)).toList();
                final SnomedConcepts descendantConcepts = new SnomedConcepts(currentDescendants, null, limit, descendantIds.size());
                if (stated) {
                    concept.setStatedDescendants(descendantConcepts);
                } else {
                    concept.setDescendants(descendantConcepts);
                }
            }
        } else {
            for (SnomedConcept concept : results) {
                final Collection<String> descendantIds = descendantsByAncestor.get(concept.getId());
                final SnomedConcepts descendants = new SnomedConcepts(limit, descendantIds.size());
                if (stated) {
                    concept.setStatedDescendants(descendants);
                } else {
                    concept.setDescendants(descendants);
                }
            }
        }
    } catch (IOException e) {
        throw SnowowlRuntimeException.wrap(e);
    }
}
Also used : SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) Options(com.b2international.commons.options.Options) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) IOException(java.io.IOException) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) RevisionSearcher(com.b2international.index.revision.RevisionSearcher)

Example 50 with SnomedConcept

use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.

the class SnomedRelationshipConverter method expandDestination.

private void expandDestination(List<SnomedRelationship> results) {
    if (expand().containsKey(SnomedRelationship.Expand.DESTINATION)) {
        final Options destinationOptions = expand().get(SnomedRelationship.Expand.DESTINATION, Options.class);
        final Iterable<String> destinationConceptIds = FluentIterable.from(results).filter(// skip expand on relationships with value
        r -> !r.hasValue()).transform(SnomedRelationship::getDestinationId);
        context().service(SnomedConceptRequestCache.class).request(destinationConceptIds, destinationOptions.getOptions("expand"), locales(), destinationConceptsById -> {
            for (SnomedRelationship relationship : results) {
                final String destinationId = relationship.getDestinationId();
                // containsKey handles any null values here
                if (destinationConceptsById.containsKey(destinationId)) {
                    final SnomedConcept destinationConcept = destinationConceptsById.get(destinationId);
                    ((SnomedRelationship) relationship).setDestination(destinationConcept);
                }
            }
        });
    }
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) Set(java.util.Set) SnomedRelationships(com.b2international.snowowl.snomed.core.domain.SnomedRelationships) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) BaseRevisionResourceConverter(com.b2international.snowowl.core.request.BaseRevisionResourceConverter) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) List(java.util.List) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) Options(com.b2international.commons.options.Options) FluentIterable(com.google.common.collect.FluentIterable) BranchContext(com.b2international.snowowl.core.domain.BranchContext) SnomedConceptRequestCache(com.b2international.snowowl.snomed.datastore.request.SnomedConceptRequestCache) Options(com.b2international.commons.options.Options) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) SnomedConceptRequestCache(com.b2international.snowowl.snomed.datastore.request.SnomedConceptRequestCache) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Aggregations

SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)90 Test (org.junit.Test)42 AbstractSnomedApiTest (com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)17 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)15 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)15 LocalDate (java.time.LocalDate)15 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)12 SnomedConcepts (com.b2international.snowowl.snomed.core.domain.SnomedConcepts)12 Options (com.b2international.commons.options.Options)10 SnomedRelationship (com.b2international.snowowl.snomed.core.domain.SnomedRelationship)10 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)9 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)8 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)8 CodeSystemRestRequests.createCodeSystem (com.b2international.snowowl.test.commons.codesystem.CodeSystemRestRequests.createCodeSystem)8 List (java.util.List)8 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 Json (com.b2international.commons.json.Json)7 SnomedRf2Headers (com.b2international.snowowl.snomed.common.SnomedRf2Headers)7 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)5