use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class ResourceConverter method expandCommits.
private void expandCommits(List<Resource> results) {
if (expand().containsKey(TerminologyResource.Expand.COMMITS)) {
Options expandOptions = expand().getOptions(TerminologyResource.Expand.COMMITS);
// commit searches must be performed individually on each resource to provide correct results
var commitSearchRequests = results.stream().filter(TerminologyResource.class::isInstance).map(TerminologyResource.class::cast).map(res -> {
return RepositoryRequests.commitInfos().prepareSearchCommitInfo().filterByBranch(res.getBranchPath()).setLimit(getLimit(expandOptions)).setFields(expandOptions.containsKey("fields") ? expandOptions.getList("fields", String.class) : CommitInfo.Fields.DEAFULT_FIELD_SELECTION).sortBy(expandOptions.containsKey("sort") ? expandOptions.getString("sort") : null).setLocales(locales()).build(res.getToolingId()).execute(context().service(IEventBus.class)).then(commits -> {
res.setCommits(commits);
return commits;
});
}).collect(Collectors.toList());
// wait until all search requests resolve, or timeout of 3 minutes reached
Promise.all(commitSearchRequests).getSync(3, TimeUnit.MINUTES);
}
}
use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class ConceptMapMappingSearchRequest method doExecute.
@Override
protected ConceptMapMappings doExecute(ServiceProvider context) throws IOException {
final int limit = limit();
Options options = Options.builder().putAll(options()).put(ConceptMapMappingSearchRequestEvaluator.OptionKey.AFTER, searchAfter()).put(ConceptMapMappingSearchRequestEvaluator.OptionKey.LIMIT, limit()).put(ConceptMapMappingSearchRequestEvaluator.OptionKey.LOCALES, locales()).put(SearchResourceRequest.OptionKey.SORT_BY, sortBy()).build();
List<ConceptMapMappings> evaluatedMappings = context.service(RepositoryManager.class).repositories().stream().flatMap(repository -> {
ConceptMapMappingSearchRequestEvaluator evaluator = repository.service(ConceptMapMappingSearchRequestEvaluator.class);
Set<ResourceURI> targets = evaluator.evaluateSearchTargetResources(context, options);
return targets.stream().map(uri -> {
return evaluator.evaluate(uri, context, options);
});
}).collect(Collectors.toList());
// calculate grand total
int total = 0;
for (ConceptMapMappings evaluatedMember : evaluatedMappings) {
total += evaluatedMember.getTotal();
}
return new ConceptMapMappings(// TODO add manual sorting here if multiple resources have been fetched
evaluatedMappings.stream().flatMap(ConceptMapMappings::stream).limit(limit).collect(Collectors.toList()), null, /* not supported across resources, TODO support it when a single Conceptmap is being fetched */
limit, total);
}
use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class ConceptSearchRequest method doExecute.
@Override
protected Concepts doExecute(ServiceProvider context) throws IOException {
final int limit = limit();
Options conceptSearchOptions = Options.builder().putAll(options()).put(ConceptSearchRequestEvaluator.OptionKey.ID, componentIds()).put(ConceptSearchRequestEvaluator.OptionKey.AFTER, searchAfter()).put(ConceptSearchRequestEvaluator.OptionKey.LIMIT, limit).put(ConceptSearchRequestEvaluator.OptionKey.LOCALES, locales()).put(ConceptSearchRequestEvaluator.OptionKey.FIELDS, fields()).put(ConceptSearchRequestEvaluator.OptionKey.EXPAND, expand()).put(SearchResourceRequest.OptionKey.SORT_BY, sortBy()).build();
final CodeSystemSearchRequestBuilder codeSystemSearchReq = CodeSystemRequests.prepareSearchCodeSystem().all();
final Map<ResourceURI, ResourceURI> codeSystemResourceFiltersByResource;
if (containsKey(OptionKey.CODESYSTEM)) {
// remove path so we can use the code resource URI as key
codeSystemResourceFiltersByResource = Maps.uniqueIndex(getCollection(OptionKey.CODESYSTEM, ResourceURI.class), uri -> uri.withoutPath());
// for filtering use the keys
codeSystemSearchReq.filterByIds(codeSystemResourceFiltersByResource.keySet().stream().map(ResourceURI::getResourceId).collect(Collectors.toSet()));
} else {
codeSystemResourceFiltersByResource = Collections.emptyMap();
}
// .filterByToolingIds(toolingIds) TODO perform TOOLING filtering
// .filterByUrls(urls) TODO perform URL filtering
List<Concepts> concepts = codeSystemSearchReq.buildAsync().execute(context).stream().map(codeSystem -> {
final ResourceURI uriToEvaluateOn = codeSystemResourceFiltersByResource.getOrDefault(codeSystem.getResourceURI(), codeSystem.getResourceURI());
return context.service(RepositoryManager.class).get(codeSystem.getToolingId()).service(ConceptSearchRequestEvaluator.class).evaluate(uriToEvaluateOn, context, conceptSearchOptions);
}).collect(Collectors.toList());
// for single CodeSystem searches, sorting, paging works as it should
if (concepts.size() == 1) {
return Iterables.getOnlyElement(concepts);
}
// otherwise, check if searchAfter was used, as it would return bogus results; it can not be applied across code systems
if (searchAfter() != null) {
throw new BadRequestException("searchAfter is not supported in Concept Search API for multiple code systems.");
}
// calculate grand total
int total = 0;
for (Concepts conceptsToAdd : concepts) {
total += conceptsToAdd.getTotal();
}
return new Concepts(// TODO add manual sorting here if multiple resources have been fetched
concepts.stream().flatMap(Concepts::stream).limit(limit).collect(Collectors.toList()), null, /* not supported across codesystems */
limit, total);
}
use of com.b2international.commons.options.Options 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.commons.options.Options 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);
}
}
}
}
Aggregations