Search in sources :

Example 1 with SnomedConcepts

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

the class MapTypeRefSetDSVExporter method prepareLabelCache.

private Map<String, String> prepareLabelCache(SnomedReferenceSetMembers membersToExport) {
    final SnomedConcepts modelComponents = SnomedRequests.prepareSearchConcept().all().filterByActive(true).filterByEcl(String.format("< (%s OR %s) ", Concepts.MODULE_ROOT, Concepts.REFSET_ATTRIBUTE)).setExpand("fsn()").setLocales(exportSetting.getLocales()).build().execute(context);
    final Map<String, String> labels = newHashMapWithExpectedSize(membersToExport.getTotal() + modelComponents.getTotal());
    modelComponents.forEach(modelComponent -> {
        labels.put(modelComponent.getId(), getFsnOrId(modelComponent));
    });
    for (SnomedReferenceSetMember snomedReferenceSetMember : membersToExport) {
        SnomedCoreComponent referencedComponent = snomedReferenceSetMember.getReferencedComponent();
        String id = referencedComponent.getId();
        if (referencedComponent instanceof SnomedConcept) {
            labels.put(id, getFsnOrId((SnomedConcept) referencedComponent));
        } else if (referencedComponent instanceof SnomedDescription) {
            labels.put(id, ((SnomedDescription) referencedComponent).getTerm());
        } else if (referencedComponent instanceof SnomedRelationship) {
            SnomedRelationship relationship = (SnomedRelationship) referencedComponent;
            if (relationship.hasValue()) {
                labels.put(id, String.format("%s - %s - %s", relationship.getSourceId(), relationship.getTypeId(), relationship.getValue()));
            } else {
                labels.put(id, String.format("%s - %s - %s", relationship.getSourceId(), relationship.getTypeId(), relationship.getDestinationId()));
            }
        }
    }
    return labels;
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedCoreComponent(com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Example 2 with SnomedConcepts

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

the class SnomedMergePerformanceTest method testPerf.

@Test
public void testPerf() throws Exception {
    IBranchPath branch = BranchPathUtils.createPath(branchPath, "merge-test");
    branching.createBranch(branch).statusCode(201);
    BulkRequestBuilder<TransactionContext> bulk = BulkRequest.create();
    final int numberOfConceptsToWorkWith = 10_000;
    for (int i = 0; i < numberOfConceptsToWorkWith; i++) {
        bulk.add(SnomedRequests.prepareNewConcept().setIdFromNamespace(null).setActive(true).setModuleId(Concepts.MODULE_SCT_CORE).addDescription(SnomedRequests.prepareNewDescription().setIdFromNamespace(null).setTerm("MergeTest FSN " + i).setTypeId(Concepts.FULLY_SPECIFIED_NAME).setLanguageCode("en").preferredIn(Concepts.REFSET_LANGUAGE_TYPE_UK)).addDescription(SnomedRequests.prepareNewDescription().setIdFromNamespace(null).setTerm("MergeTest PT " + i).setTypeId(Concepts.SYNONYM).setLanguageCode("en").preferredIn(Concepts.REFSET_LANGUAGE_TYPE_UK)).addRelationship(SnomedRequests.prepareNewRelationship().setIdFromNamespace(null).setCharacteristicTypeId(Concepts.STATED_RELATIONSHIP).setTypeId(Concepts.IS_A).setDestinationId(Concepts.ROOT_CONCEPT)));
    }
    Stopwatch w = Stopwatch.createStarted();
    // commit large changeset
    CommitResult createCommitResult = SnomedRequests.prepareCommit().setBody(bulk).setCommitComment("Commit large bulk request").build(branch.getPath()).execute(getBus()).getSync();
    System.err.println("Bulk create 10_000 concepts commit took: " + w);
    w.reset().start();
    // extract the new IDs assigned to the concepts
    Set<String> idsOfCreatedConcepts = createCommitResult.getResultAs(BulkResponse.class).stream().filter(String.class::isInstance).map(String.class::cast).collect(Collectors.toSet());
    // retrieve the first 100 created concepts back, check the module
    SnomedConcepts first100 = SnomedRequests.prepareSearchConcept().setLimit(100).filterByIds(idsOfCreatedConcepts).build(branch.getPath()).execute(getBus()).getSync();
    assertThat(first100.getTotal()).isEqualTo(numberOfConceptsToWorkWith);
    first100.forEach(concept -> {
        assertThat(concept.getModuleId()).isEqualTo(Concepts.MODULE_SCT_CORE);
    });
    // check that SNOMED CT ROOT has 10_000
    int totalDescendants = SnomedRequests.prepareSearchConcept().setLimit(0).filterByStatedParent(Concepts.ROOT_CONCEPT).build(branch.getPath()).execute(getBus()).getSync().getTotal();
    assertThat(totalDescendants).isGreaterThanOrEqualTo(numberOfConceptsToWorkWith);
    // find the relationship IDs of the concepts
    final Set<String> conceptRelationshipIds = SnomedRequests.prepareSearchRelationship().all().filterByActive(true).filterBySources(idsOfCreatedConcepts).filterByDestination(Concepts.ROOT_CONCEPT).setFields(SnomedRelationshipIndexEntry.Fields.ID).build(branch.getPath()).execute(getBus()).getSync().stream().map(SnomedRelationship::getId).collect(Collectors.toSet());
    System.err.println("Verify creation of 10_000 concepts took: " + w);
    w.reset().start();
    // prepare module bulk update
    bulk = BulkRequest.create();
    for (String conceptIdToUpdate : idsOfCreatedConcepts) {
        bulk.add(SnomedRequests.prepareUpdateConcept(conceptIdToUpdate).setModuleId(Concepts.MODULE_SCT_MODEL_COMPONENT));
    }
    // commit module bulk update
    SnomedRequests.prepareCommit().setBody(bulk).setCommitComment("Commit update bulk request").build(branch.getPath()).execute(getBus()).getSync();
    System.err.println("Bulk update 10_000 concepts commit took: " + w);
    w.reset().start();
    // prepare move concepts in hierarchy to clinical finding update
    bulk = BulkRequest.create();
    // inactivate all current relationships
    for (String relationshipIdToInactive : conceptRelationshipIds) {
        bulk.add(SnomedRequests.prepareUpdateRelationship(relationshipIdToInactive).setActive(false));
    }
    // add new relationships to concepts
    for (String conceptIdToUpdate : idsOfCreatedConcepts) {
        bulk.add(SnomedRequests.prepareNewRelationship().setIdFromNamespace(null).setSourceId(conceptIdToUpdate).setTypeId(Concepts.IS_A).setDestinationId("404684003").setModuleId(Concepts.MODULE_SCT_MODEL_COMPONENT));
    }
    // commit move concepts in hierarchy to clinical finding update
    SnomedRequests.prepareCommit().setBody(bulk).setCommitComment("Commit update bulk request").build(branch.getPath()).execute(getBus()).getSync();
    System.err.println("Bulk move 10_000 concepts to Clinical Finding took: " + w);
    w.reset().start();
    SnomedRestFixtures.merge(branch, branchPath, "Promote changes from task...").body("status", CoreMatchers.equalTo(Merge.Status.COMPLETED.name()));
    System.err.println("Merge took: " + w);
}
Also used : CommitResult(com.b2international.snowowl.core.request.CommitResult) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext) Stopwatch(com.google.common.base.Stopwatch) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) IBranchPath(com.b2international.snowowl.core.api.IBranchPath) Test(org.junit.Test) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)

Example 3 with SnomedConcepts

use of com.b2international.snowowl.snomed.core.domain.SnomedConcepts 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 4 with SnomedConcepts

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

the class EquivalentConceptSetConverter method expand.

@Override
public void expand(final List<EquivalentConceptSet> results) {
    if (!expand().containsKey(EquivalentConceptSet.Expand.EQUIVALENT_CONCEPTS)) {
        return;
    }
    final Set<String> classificationTaskIds = results.stream().map(EquivalentConceptSet::getClassificationId).collect(Collectors.toSet());
    final Map<String, String> branchesByClassificationIdMap = ClassificationRequests.prepareSearchClassification().filterByIds(classificationTaskIds).all().build().execute(context()).stream().collect(Collectors.toMap(ClassificationTask::getId, ClassificationTask::getBranch));
    final Multimap<String, EquivalentConceptSet> itemsByBranch = Multimaps.index(results, r -> branchesByClassificationIdMap.get(r.getClassificationId()));
    final Options expandOptions = expand().get(EquivalentConceptSet.Expand.EQUIVALENT_CONCEPTS, Options.class);
    for (final String branch : itemsByBranch.keySet()) {
        final Collection<EquivalentConceptSet> itemsForCurrentBranch = itemsByBranch.get(branch);
        final Set<String> conceptIds = itemsForCurrentBranch.stream().flatMap(c -> c.getEquivalentConcepts().stream()).map(SnomedConcept::getId).collect(Collectors.toSet());
        final SnomedConcepts concepts = SnomedRequests.prepareSearchConcept().filterByIds(conceptIds).all().setExpand(expandOptions.get("expand", Options.class)).setLocales(locales()).build(branch).getRequest().execute(context());
        final Map<String, SnomedConcept> conceptsById = Maps.uniqueIndex(concepts, SnomedConcept::getId);
        for (final EquivalentConceptSet item : itemsForCurrentBranch) {
            final List<SnomedConcept> equivalentConcepts = item.getEquivalentConcepts().getItems();
            for (int i = 0; i < equivalentConcepts.size(); i++) {
                final SnomedConcept placeholderConcept = equivalentConcepts.get(i);
                final SnomedConcept expandedConcept = conceptsById.get(placeholderConcept.getId());
                equivalentConcepts.set(i, expandedConcept);
            }
        }
    }
}
Also used : Options(com.b2international.commons.options.Options) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) EquivalentConceptSet(com.b2international.snowowl.snomed.reasoner.domain.EquivalentConceptSet) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Example 5 with SnomedConcepts

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

the class SnomedConceptSearchRequestEvaluator method evaluate.

@Override
public Concepts evaluate(ResourceURI uri, ServiceProvider context, Options search) {
    final String preferredDisplay = search.getString(OptionKey.DISPLAY);
    SnomedDisplayTermType displayTermType;
    if (preferredDisplay != null) {
        displayTermType = SnomedDisplayTermType.getEnum(preferredDisplay);
    } else {
        displayTermType = SnomedDisplayTermType.PT;
    }
    final SnomedConceptSearchRequestBuilder req = SnomedRequests.prepareSearchConcept();
    evaluateTermFilterOptions(req, search);
    if (search.containsKey(OptionKey.ID)) {
        req.filterByIds(search.getCollection(OptionKey.ID, String.class));
    }
    if (search.containsKey(OptionKey.ACTIVE)) {
        req.filterByActive(search.getBoolean(OptionKey.ACTIVE));
    }
    if (search.containsKey(OptionKey.PARENT)) {
        req.filterByParents(search.getCollection(OptionKey.PARENT, String.class));
    }
    if (search.containsKey(OptionKey.ANCESTOR)) {
        req.filterByAncestors(search.getCollection(OptionKey.ANCESTOR, String.class));
    }
    if (search.containsKey(OptionKey.TERM_TYPE)) {
        req.filterByDescriptionType(search.getString(OptionKey.TERM_TYPE));
    }
    if (search.containsKey(OptionKey.QUERY) || search.containsKey(OptionKey.MUST_NOT_QUERY)) {
        StringBuilder query = new StringBuilder();
        if (search.containsKey(OptionKey.QUERY)) {
            query.append("(").append(Ecl.or(search.getCollection(OptionKey.QUERY, String.class))).append(")");
        } else {
            query.append(Ecl.ANY);
        }
        if (search.containsKey(OptionKey.MUST_NOT_QUERY)) {
            query.append(" MINUS (").append(Ecl.or(search.getCollection(OptionKey.MUST_NOT_QUERY, String.class))).append(")");
        }
        req.filterByEcl(query.toString());
    }
    boolean requestedExpand = search.containsKey(OptionKey.EXPAND);
    // make sure preferredDescriptions() and displayTermType expansion data are always loaded
    Options expand = ExpandParser.parse("preferredDescriptions()").merge(requestedExpand ? search.getOptions(OptionKey.EXPAND) : Options.empty());
    if (!Strings.isNullOrEmpty(displayTermType.getExpand())) {
        expand = ExpandParser.parse(displayTermType.getExpand()).merge(expand);
    }
    SnomedConcepts matches = req.setLocales(search.getList(OptionKey.LOCALES, ExtendedLocale.class)).setSearchAfter(search.getString(OptionKey.AFTER)).setLimit(search.get(OptionKey.LIMIT, Integer.class)).setFields(search.getList(OptionKey.FIELDS, String.class)).setExpand(expand).sortBy(search.containsKey(SearchResourceRequest.OptionKey.SORT_BY) ? search.getList(SearchResourceRequest.OptionKey.SORT_BY, SearchResourceRequest.Sort.class) : null).build(uri).execute(context);
    return new Concepts(matches.stream().map(concept -> toConcept(uri, concept, displayTermType.getLabel(concept), requestedExpand)).collect(Collectors.toList()), matches.getSearchAfter(), matches.getLimit(), matches.getTotal());
}
Also used : Options(com.b2international.commons.options.Options) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) Concepts(com.b2international.snowowl.core.domain.Concepts) SnomedDisplayTermType(com.b2international.snowowl.snomed.core.SnomedDisplayTermType) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) SnomedConceptSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest)

Aggregations

SnomedConcepts (com.b2international.snowowl.snomed.core.domain.SnomedConcepts)20 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)12 AbstractSnomedApiTest (com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)7 Options (com.b2international.commons.options.Options)6 Test (org.junit.Test)6 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)5 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)5 BranchContext (com.b2international.snowowl.core.domain.BranchContext)4 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)4 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)4 SnomedConceptSearchRequestBuilder (com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder)4 Collection (java.util.Collection)4 List (java.util.List)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 SnomedRf2Headers (com.b2international.snowowl.snomed.common.SnomedRf2Headers)3 SnomedRelationship (com.b2international.snowowl.snomed.core.domain.SnomedRelationship)3 SnomedConceptDocument (com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument)3 com.b2international.snowowl.snomed.reasoner.domain (com.b2international.snowowl.snomed.reasoner.domain)3 Multimap (com.google.common.collect.Multimap)3