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;
}
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);
}
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()));
}
}
}
}
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);
}
}
}
}
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());
}
Aggregations