Search in sources :

Example 1 with Concept

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

the class ConceptSearchRequestSnomedTest method setPreferreDisplayToFsn.

@Test
public void setPreferreDisplayToFsn() throws Exception {
    Concepts matches = CodeSystemRequests.prepareSearchConcepts().filterByCodeSystemUri(CODESYSTEM).filterById(ID).setPreferredDisplay("FSN").setLocales("en").buildAsync().execute(Services.bus()).getSync();
    assertThat(matches.getTotal()).isEqualTo(1);
    final Concept concept = matches.first().get();
    assertThat(concept.getTerm()).isEqualTo(FSN);
}
Also used : Concept(com.b2international.snowowl.core.domain.Concept) Concepts(com.b2international.snowowl.core.domain.Concepts) Test(org.junit.Test)

Example 2 with Concept

use of com.b2international.snowowl.core.domain.Concept 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 3 with Concept

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

the class FhirValidateCodeRequest method doExecute.

@Override
public ValidateCodeResult doExecute(ServiceProvider context, CodeSystem codeSystem) {
    Set<Coding> codings = collectCodingsToValidate(request);
    Map<String, Coding> codingsById = codings.stream().collect(Collectors.toMap(Coding::getCodeValue, c -> c));
    // extract locales from the request
    Map<String, Concept> conceptsById = CodeSystemRequests.prepareSearchConcepts().setLimit(codingsById.keySet().size()).filterByCodeSystemUri(codeSystem.getResourceURI()).filterByIds(codingsById.keySet()).setLocales(extractLocales(request.getDisplayLanguage())).buildAsync().execute(context).stream().collect(Collectors.toMap(Concept::getId, c -> c));
    // check if both Maps have the same keys and report if not
    Set<String> missingConceptIds = Sets.difference(codingsById.keySet(), conceptsById.keySet());
    if (!missingConceptIds.isEmpty()) {
        return ValidateCodeResult.builder().result(false).message(String.format("Could not find code%s '%s'.", missingConceptIds.size() == 1 ? "" : "s", ImmutableSortedSet.copyOf(missingConceptIds))).build();
    }
    // XXX it would be great to have support for multiple messages/validation results in a single request
    for (String id : codingsById.keySet()) {
        // check display if provided
        Coding providedCoding = codingsById.get(id);
        if (providedCoding.getDisplay() != null) {
            Concept concept = conceptsById.get(id);
            if (!providedCoding.getDisplay().equals(concept.getTerm())) {
                return ValidateCodeResult.builder().result(false).display(concept.getTerm()).message(String.format("Incorrect display '%s' for code '%s'.", providedCoding.getDisplay(), providedCoding.getCodeValue())).build();
            }
        }
    }
    return ValidateCodeResult.builder().result(true).build();
}
Also used : ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ValidateCodeRequest(com.b2international.snowowl.fhir.core.model.codesystem.ValidateCodeRequest) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) HashSet(java.util.HashSet) Valid(javax.validation.Valid) CodeSystem(com.b2international.snowowl.fhir.core.model.codesystem.CodeSystem) Concept(com.b2international.snowowl.core.domain.Concept) Map(java.util.Map) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) JsonUnwrapped(com.fasterxml.jackson.annotation.JsonUnwrapped) ValidateCodeResult(com.b2international.snowowl.fhir.core.model.ValidateCodeResult) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding) CodeableConcept(com.b2international.snowowl.fhir.core.model.dt.CodeableConcept) Concept(com.b2international.snowowl.core.domain.Concept) CodeableConcept(com.b2international.snowowl.fhir.core.model.dt.CodeableConcept) Coding(com.b2international.snowowl.fhir.core.model.dt.Coding)

Example 4 with Concept

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

the class FhirValueSetExpandRequest method computeFhirValueSetUsingUrl.

private ValueSet computeFhirValueSetUsingUrl(ServiceProvider context, String urlValue) {
    // only URLs with query parts are supported, every other case is rejected for now
    if (urlValue.contains("#")) {
        return null;
    }
    // extract the non-query part from the URL value
    String baseUrl = urlValue.split("\\?")[0];
    String query = "";
    if (urlValue.contains("?")) {
        query = urlValue.split("\\?")[1];
    }
    // if this is the base URI string, then always append the core module to represent the International Edition properly
    if (Uri.SNOMED_BASE_URI_STRING.equals(baseUrl)) {
        baseUrl = baseUrl.concat("/900000000000207008");
    }
    // try to lookup the CodeSystem using the baseUrl
    CodeSystem codeSystem = FhirRequests.codeSystems().prepareSearch().one().filterByUrl(baseUrl).buildAsync().execute(context).first().map(ResourceResponseEntry.class::cast).map(ResourceResponseEntry::getResponseResource).map(CodeSystem.class::cast).orElse(null);
    // if no CodeSystem stored to use as Value Set source, return NotFound response
    if (codeSystem == null) {
        return null;
    }
    // return the content of the CodeSystem as Value Set
    String id = Hashing.goodFastHash(8).hashString(urlValue, Charsets.UTF_8).toString();
    Builder valueSet = ValueSet.builder(id).url(urlValue).status(PublicationStatus.ACTIVE);
    final Expansion.Builder expansion = Expansion.builder().identifier(id).timestamp(new Date());
    ConceptSearchRequestBuilder req = CodeSystemRequests.prepareSearchConcepts().filterByCodeSystemUri(codeSystem.getResourceURI()).filterByActive(request.getActiveOnly()).filterByTerm(request.getFilter()).setLimit(request.getCount() == null ? 10 : request.getCount()).setSearchAfter(request.getAfter()).setPreferredDisplay("FSN").sortBy(!CompareUtils.isEmpty(request.getFilter()) ? SearchIndexResourceRequest.SCORE : SearchResourceRequest.Sort.fieldAsc("id"));
    Compose compose = null;
    // configure query based on fhir_vs query parameter and also build the compose declaration for this implicit Value Set
    if (Strings.isNullOrEmpty(query) || "fhir_vs".equals(query)) {
    // do nothing, search all concepts
    } else if (query.startsWith("fhir_vs=")) {
        String fhirVsValue = query.replace("fhir_vs=", "");
        if (fhirVsValue.startsWith("ecl/")) {
            String ecl = fhirVsValue.replace("ecl/", "");
            req.filterByQuery(ecl);
            // configure Value Set for ECL
            valueSet.name(String.format("%s Concepts matching %s", codeSystem.getName(), ecl)).description(String.format("All SNOMED CT concepts that match the expression constraint %s", ecl));
            // configure compose for ECL
            compose = Compose.builder().addInclude(Include.builder().addFilters(ValueSetFilter.builder().property("constraint").operator(FilterOperator.EQUALS).value(ecl).build()).build()).build();
        } else if (fhirVsValue.startsWith("isa/")) {
            String parent = fhirVsValue.replace("isa/", "");
            req.filterByAncestor(parent);
            // configure Value Set for IS A
            valueSet.name(String.format("%s Concept %s and descendants", codeSystem.getName(), parent)).description(String.format("All SNOMED CT concepts for %s", parent));
            // configure compose for IS A
            compose = Compose.builder().addInclude(Include.builder().system(baseUrl).addFilters(ValueSetFilter.builder().property("concept").operator(FilterOperator.IS_A).value(parent).build()).build()).build();
        } else if (fhirVsValue.startsWith("refset/")) {
            String refsetId = fhirVsValue.replace("refset/", "");
            if (Strings.isNullOrEmpty(refsetId)) {
                // TODO support refset identifier concept search
                return null;
            } else {
                req.filterByQuery("^" + refsetId);
                // configure Value Set for REFSET
                valueSet.name(String.format("%s Reference Set %s", codeSystem.getName(), refsetId)).description(String.format("All SNOMED CT concepts in the reference set %s", refsetId));
                // configure compose for REFSET
                compose = Compose.builder().addInclude(Include.builder().addFilters(ValueSetFilter.builder().property("concept").operator(FilterOperator.IN).value(refsetId).build()).build()).build();
            }
        } else {
            // TODO check against declared filter values in CodeSystem
            return null;
        }
    }
    Concepts concepts = req.buildAsync().execute(context);
    expansion.total(concepts.getTotal()).after(concepts.getSearchAfter());
    for (Concept concept : concepts) {
        expansion.addContains(Contains.builder().code(concept.getId()).system(baseUrl).display(concept.getTerm()).build());
    }
    return valueSet.compose(compose).expansion(expansion.build()).build();
}
Also used : Concept(com.b2international.snowowl.core.domain.Concept) Concepts(com.b2international.snowowl.core.domain.Concepts) ResourceResponseEntry(com.b2international.snowowl.fhir.core.model.ResourceResponseEntry) ConceptSearchRequestBuilder(com.b2international.snowowl.core.request.ConceptSearchRequestBuilder) Builder(com.b2international.snowowl.fhir.core.model.valueset.ValueSet.Builder) ConceptSearchRequestBuilder(com.b2international.snowowl.core.request.ConceptSearchRequestBuilder) CodeSystem(com.b2international.snowowl.fhir.core.model.codesystem.CodeSystem) Expansion(com.b2international.snowowl.fhir.core.model.valueset.expansion.Expansion) Date(java.util.Date)

Example 5 with Concept

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

the class ConceptSearchRequestSnomedTest method useDefaultDisplay.

@Test
public void useDefaultDisplay() throws Exception {
    Concepts matches = CodeSystemRequests.prepareSearchConcepts().filterByCodeSystemUri(CODESYSTEM).filterById(ID).setLocales("en").buildAsync().execute(Services.bus()).getSync();
    assertThat(matches.getTotal()).isEqualTo(1);
    final Concept concept = matches.first().get();
    assertThat(concept.getTerm()).isEqualTo(PT);
}
Also used : Concept(com.b2international.snowowl.core.domain.Concept) Concepts(com.b2international.snowowl.core.domain.Concepts) Test(org.junit.Test)

Aggregations

Concept (com.b2international.snowowl.core.domain.Concept)9 Concepts (com.b2international.snowowl.core.domain.Concepts)4 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 ResourceURI (com.b2international.snowowl.core.ResourceURI)2 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)2 CodeSystemRequests (com.b2international.snowowl.core.codesystem.CodeSystemRequests)2 CodeSystem (com.b2international.snowowl.fhir.core.model.codesystem.CodeSystem)2 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)2 List (java.util.List)2 Map (java.util.Map)2 NotFoundException (com.b2international.commons.exceptions.NotFoundException)1 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)1 Options (com.b2international.commons.options.Options)1 OptionsBuilder (com.b2international.commons.options.OptionsBuilder)1 TextConstants (com.b2international.index.compat.TextConstants)1 RepositoryManager (com.b2international.snowowl.core.RepositoryManager)1 CodeSystem (com.b2international.snowowl.core.codesystem.CodeSystem)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 ConceptMapMapping (com.b2international.snowowl.core.domain.ConceptMapMapping)1