use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class SnomedReferenceSetMemberRestService method searchByGet.
@Operation(summary = "Retrieve reference set members from a path", description = "Returns a list with all reference set members from a path." + "<p>The following properties can be expanded:" + "<p>" + "• referencedComponent(expand(pt(),...)) – the referenced component, and any applicable nested expansions<br>")
@ApiResponses({ @ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "Branch not found") })
@GetMapping(produces = { AbstractRestService.JSON_MEDIA_TYPE })
@ResponseBody
public Promise<SnomedReferenceSetMembers> searchByGet(@Parameter(description = "The resource path", required = true) @PathVariable(value = "path") final String path, @ParameterObject final SnomedReferenceSetMemberRestSearch params, @Parameter(description = "Accepted language tags, in order of preference", example = "en-US;q=0.8,en-GB;q=0.6") @RequestHeader(value = "Accept-Language", defaultValue = "en-US;q=0.8,en-GB;q=0.6", required = false) final String acceptLanguage) {
final SnomedRefSetMemberSearchRequestBuilder req = SnomedRequests.prepareSearchMember().setLimit(params.getLimit()).setSearchAfter(params.getSearchAfter()).filterByIds(params.getId()).filterByActive(params.getActive()).filterByModules(params.getModule()).filterByEffectiveTime(params.getEffectiveTime()).filterByRefSet(params.getRefsetId()).filterByReferencedComponent(params.getReferencedComponentId()).setExpand(params.getExpand()).setFields(params.getField()).setLocales(acceptLanguage).sortBy(extractSortFields(params.getSort()));
Options propFilters = params.toPropsFilter();
if (!propFilters.isEmpty()) {
req.filterByProps(propFilters);
}
return req.build(path).execute(getBus());
}
use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class SnomedQueryOptimizer method optimize.
@Override
public QueryExpressionDiffs optimize(BranchContext context, Options params) {
final Collection<QueryExpression> inclusions = params.getCollection(QueryOptimizer.OptionKey.INCLUSIONS, QueryExpression.class);
final List<ExtendedLocale> locales = params.getList(QueryOptimizer.OptionKey.LOCALES, ExtendedLocale.class);
final EclParser eclParser = context.service(EclParser.class);
final LoadingCache<String, ExpressionConstraint> eclCache = CacheBuilder.newBuilder().build(CacheLoader.from(eclParser::parse));
final Multimap<String, QueryExpression> singleConceptInclusions = FluentIterable.from(inclusions).filter(ex -> isSingleConceptExpression(eclCache, ex.getQuery())).index(ex -> toSingleConceptId(eclCache, ex.getQuery()));
// if there are no single concept inclusions to optimize, exit early
if (singleConceptInclusions.isEmpty()) {
return new QueryExpressionDiffs(Collections.emptyList());
}
// Record the ancestors (both direct and indirect) of each single concept inclusion
final Multimap<String, QueryExpression> membersByAncestor = HashMultimap.create();
SnomedRequests.prepareSearchConcept().filterByIds(singleConceptInclusions.keySet()).setLimit(singleConceptInclusions.keySet().size()).stream(context).flatMap(SnomedConcepts::stream).forEach(child -> {
final Collection<QueryExpression> childExpressions = singleConceptInclusions.get(child.getId());
final List<String> parentIds = child.getParentIdsAsString();
final List<String> ancestorIds = child.getAncestorIdsAsString();
parentIds.forEach(parentId -> {
if (!IComponent.ROOT_ID.equals(parentId) && !Concepts.ROOT_CONCEPT.equals(parentId)) {
membersByAncestor.putAll(parentId, childExpressions);
}
});
ancestorIds.forEach(ancestorId -> {
if (!IComponent.ROOT_ID.equals(ancestorId) && !Concepts.ROOT_CONCEPT.equals(ancestorId)) {
membersByAncestor.putAll(ancestorId, childExpressions);
}
});
});
// Get number of referenced descendants (taking possible duplicates into account)
final Map<String, Long> uniqueDescendantsByParent = ImmutableMap.copyOf(Maps.transformValues(membersByAncestor.asMap(), descendants -> descendants.stream().map(QueryExpression::getQuery).distinct().count()));
final ImmutableList.Builder<QueryExpressionDiff> diffs = ImmutableList.builder();
// references can be replaced with a single << expression.
for (Entry<String, Long> uniqueDescendantsByParentEntry : uniqueDescendantsByParent.entrySet()) {
SnomedConcept parent = SnomedRequests.prepareGetConcept(uniqueDescendantsByParentEntry.getKey()).setLocales(locales).setExpand("pt(),descendants(direct:false,limit:0)").build().execute(context);
final String parentId = parent.getId();
final int referencedDescendants = Ints.checkedCast(uniqueDescendantsByParent.get(parentId));
final int totalDescendants = parent.getDescendants().getTotal();
if (totalDescendants == referencedDescendants) {
final List<QueryExpression> remove = List.copyOf(membersByAncestor.get(parentId).stream().filter(ex -> !ex.isPinned()).collect(Collectors.toList()));
// The optimization is a "net win" if we can remove at least two clauses from the original
if (remove.size() > 1) {
final QueryExpression replacement = new QueryExpression(IDs.base64UUID(), String.format("<%s%s", parent.getId(), getTerm(parent)), false);
final List<QueryExpression> addToInclusion = List.of(replacement);
final List<QueryExpression> addToExclusion = List.of();
final QueryExpressionDiff diff = new QueryExpressionDiff(addToInclusion, addToExclusion, remove);
diffs.add(diff);
}
}
}
return new QueryExpressionDiffs(diffs.build());
}
use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class SnomedEclRefinementEvaluator method evalMembers.
private Promise<Collection<Property>> evalMembers(BranchContext context, Set<String> focusConceptIds, Collection<String> typeIds, DataTypeComparison comparison) {
final Object value;
final DataType type;
if (comparison instanceof BooleanValueComparison) {
value = ((BooleanValueComparison) comparison).isValue();
type = DataType.BOOLEAN;
} else if (comparison instanceof StringValueComparison) {
value = ((StringValueComparison) comparison).getValue();
type = DataType.STRING;
} else if (comparison instanceof IntegerValueComparison) {
value = ((IntegerValueComparison) comparison).getValue();
type = DataType.INTEGER;
} else if (comparison instanceof DecimalValueComparison) {
value = ((DecimalValueComparison) comparison).getValue();
type = DataType.DECIMAL;
} else {
return SnomedEclEvaluationRequest.throwUnsupported(comparison);
}
final SearchResourceRequest.Operator operator = toSearchOperator(comparison.getOp());
final Options propFilter = Options.builder().put(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID, getCharacteristicTypes(expressionForm)).put(SnomedRf2Headers.FIELD_TYPE_ID, typeIds).put(SnomedRefSetMemberIndexEntry.Fields.DATA_TYPE, type).put(SnomedRf2Headers.FIELD_VALUE, value).put(SearchResourceRequest.operator(SnomedRf2Headers.FIELD_VALUE), operator).build();
return SnomedRequests.prepareSearchMember().filterByActive(true).filterByRefSetType(SnomedRefSetType.CONCRETE_DATA_TYPE).filterByReferencedComponent(focusConceptIds).filterByProps(propFilter).setEclExpressionForm(expressionForm).setLimit(context.service(RepositoryConfiguration.class).getIndexConfiguration().getResultWindow()).<Property>transformAsync(context, req -> req.build(context.path()), members -> members.stream().map(input -> {
return new Property(input.getReferencedComponent().getId(), (String) input.getProperties().get(SnomedRf2Headers.FIELD_TYPE_ID), input.getProperties().get(SnomedRf2Headers.FIELD_VALUE), (Integer) input.getProperties().get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP));
}));
}
use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class CommitInfoConverter method toResource.
@Override
protected CommitInfo toResource(final Commit doc) {
final Builder builder = CommitInfo.builder(doc);
// expand details if requested
if (expand().containsKey(CommitInfo.Expand.DETAILS)) {
final Options detailsExpandOptions = expand().get(CommitInfo.Expand.DETAILS, Options.class);
final Collection<CommitDetail> commitDetails = getCommitDetails(doc, detailsExpandOptions);
final List<CommitInfoDetail> commitInfoDetails = commitDetails.stream().flatMap(info -> toCommitInfoDetail(info)).collect(Collectors.toList());
builder.details(new CommitInfoDetails(commitInfoDetails, null, commitInfoDetails.size(), commitInfoDetails.size()));
}
return builder.build();
}
use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class ValueSetMemberSearchRequest method doExecute.
@Override
protected ValueSetMembers doExecute(ServiceProvider context) throws IOException {
final int limit = limit();
Options options = Options.builder().putAll(options()).put(MemberSearchRequestEvaluator.OptionKey.AFTER, searchAfter()).put(MemberSearchRequestEvaluator.OptionKey.LIMIT, limit).put(MemberSearchRequestEvaluator.OptionKey.LOCALES, locales()).put(SearchResourceRequest.OptionKey.SORT_BY, sortBy()).build();
// extract all ValueSetMemberSearchRequestEvaluator from all connected toolings and determine which ones can handle this request
List<ValueSetMembers> evaluatedMembers = context.service(RepositoryManager.class).repositories().stream().flatMap(repository -> {
ValueSetMemberSearchRequestEvaluator evaluator = repository.service(ValueSetMemberSearchRequestEvaluator.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 (ValueSetMembers evaluatedMember : evaluatedMembers) {
total += evaluatedMember.getTotal();
}
return new ValueSetMembers(// TODO add manual sorting here if multiple resources have been fetched
evaluatedMembers.stream().flatMap(ValueSetMembers::stream).limit(limit).collect(Collectors.toList()), null, /* not supported across resources, TODO support it when a single ValueSet is being fetched */
limit, total);
}
Aggregations