Search in sources :

Example 36 with Options

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>" + "&bull; referencedComponent(expand(pt(),...)) &ndash; 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());
}
Also used : Options(com.b2international.commons.options.Options) SnomedRefSetMemberSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRefSetMemberSearchRequestBuilder) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Example 37 with Options

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());
}
Also used : ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) LoadingCache(com.google.common.cache.LoadingCache) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Map(java.util.Map) QueryOptimizer(com.b2international.snowowl.core.request.QueryOptimizer) com.google.common.collect(com.google.common.collect) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Collection(java.util.Collection) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) Collectors(java.util.stream.Collectors) Ints(com.google.common.primitives.Ints) IDs(com.b2international.snowowl.core.id.IDs) CacheLoader(com.google.common.cache.CacheLoader) EclParser(com.b2international.snowowl.snomed.core.ecl.EclParser) List(java.util.List) Entry(java.util.Map.Entry) CacheBuilder(com.google.common.cache.CacheBuilder) EclConceptReference(com.b2international.snomed.ecl.ecl.EclConceptReference) ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) com.b2international.snowowl.core.domain(com.b2international.snowowl.core.domain) Collections(java.util.Collections) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) EclParser(com.b2international.snowowl.snomed.core.ecl.EclParser)

Example 38 with Options

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));
    }));
}
Also used : PolymorphicDispatcher(org.eclipse.xtext.util.PolymorphicDispatcher) Query(com.b2international.index.query.Query) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) Promise(com.b2international.snowowl.core.events.util.Promise) ID(com.b2international.index.revision.Revision.Fields.ID) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) SnomedDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) com.b2international.snomed.ecl.ecl(com.b2international.snomed.ecl.ecl) RepositoryConfiguration(com.b2international.snowowl.core.config.RepositoryConfiguration) CompareUtils(com.b2international.commons.CompareUtils) com.google.common.collect(com.google.common.collect) ResourceURI(com.b2international.snowowl.core.ResourceURI) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Function(com.google.common.base.Function) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) Collection(java.util.Collection) SnomedRelationshipSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRelationshipSearchRequestBuilder) DataType(com.b2international.snowowl.snomed.core.domain.refset.DataType) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Trees(com.b2international.snowowl.snomed.core.tree.Trees) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Expressions(com.b2international.index.query.Expressions) Expressions.active(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active) Entry(java.util.Map.Entry) Expression(com.b2international.index.query.Expression) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Fields(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Fields) Expressions(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) Options(com.b2international.commons.options.Options) DataType(com.b2international.snowowl.snomed.core.domain.refset.DataType) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest)

Example 39 with Options

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();
}
Also used : RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) Builder(com.b2international.snowowl.core.commit.CommitInfo.Builder) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Commit(com.b2international.index.revision.Commit) Collectors(java.util.stream.Collectors) Strings(com.google.common.base.Strings) List(java.util.List) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) Stream(java.util.stream.Stream) Options(com.b2international.commons.options.Options) Lists.newArrayListWithExpectedSize(com.google.common.collect.Lists.newArrayListWithExpectedSize) ChangeKind(com.b2international.commons.ChangeKind) CommitDetail(com.b2international.index.revision.CommitDetail) BaseResourceConverter(com.b2international.snowowl.core.request.BaseResourceConverter) Options(com.b2international.commons.options.Options) CommitDetail(com.b2international.index.revision.CommitDetail) Builder(com.b2international.snowowl.core.commit.CommitInfo.Builder)

Example 40 with Options

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);
}
Also used : List(java.util.List) Options(com.b2international.commons.options.Options) RepositoryManager(com.b2international.snowowl.core.RepositoryManager) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ValueSetMembers(com.b2international.snowowl.core.domain.ValueSetMembers) ResourceURI(com.b2international.snowowl.core.ResourceURI) Options(com.b2international.commons.options.Options) Set(java.util.Set) ValueSetMembers(com.b2international.snowowl.core.domain.ValueSetMembers)

Aggregations

Options (com.b2international.commons.options.Options)57 List (java.util.List)15 Collectors (java.util.stream.Collectors)14 Set (java.util.Set)13 ExtendedLocale (com.b2international.commons.http.ExtendedLocale)11 BranchContext (com.b2international.snowowl.core.domain.BranchContext)11 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)11 SnomedConceptRequestCache (com.b2international.snowowl.snomed.datastore.request.SnomedConceptRequestCache)11 Test (org.junit.Test)9 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)8 Map (java.util.Map)8 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)7 SnomedConcepts (com.b2international.snowowl.snomed.core.domain.SnomedConcepts)7 BadRequestException (com.b2international.commons.exceptions.BadRequestException)6 ResourceURI (com.b2international.snowowl.core.ResourceURI)6 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)6 SnomedRelationship (com.b2international.snowowl.snomed.core.domain.SnomedRelationship)6 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)6 Multimaps (com.google.common.collect.Multimaps)6 Collection (java.util.Collection)6