Search in sources :

Example 31 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class SnomedQueryValidationRuleEvaluator method eval.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<?> eval(BranchContext context, ValidationRule rule, Map<String, Object> params) throws Exception {
    checkArgument(type().equals(rule.getType()), "'%s' is not recognizable by this evaluator (accepts: %s)", rule, type());
    SnomedComponentValidationQuery<?, PageableCollectionResource<SnomedComponent>, SnomedComponent> validationQuery = context.service(ObjectMapper.class).<SnomedComponentValidationQuery<?, PageableCollectionResource<SnomedComponent>, SnomedComponent>>readValue(rule.getImplementation(), TYPE_REF);
    SnomedSearchRequestBuilder<?, PageableCollectionResource<SnomedComponent>> req = validationQuery.prepareSearch();
    String extensionModules = params != null && params.containsKey(ValidationConfiguration.MODULES) ? Strings.nullToEmpty((String) params.get(ValidationConfiguration.MODULES)) : "";
    String module = validationQuery.module;
    if (!Strings.isNullOrEmpty(module)) {
        // If there is a module given assume that it must be more specific and provided on purpose
        req.filterByModule(module);
    } else if (Boolean.TRUE.equals(validationQuery.extensionScope) && !Strings.isNullOrEmpty(extensionModules)) {
        req.filterByModule(extensionModules);
    }
    SearchIndexResourceRequest<BranchContext, ?, ? extends SnomedDocument> searchReq = (SearchIndexResourceRequest<BranchContext, ?, ? extends SnomedDocument>) req.build();
    final ExpressionBuilder expressionBuilder = Expressions.builder().filter(searchReq.toRawQuery(context));
    if (params != null && params.containsKey(ValidationConfiguration.IS_UNPUBLISHED_ONLY) && Boolean.TRUE.equals(params.get(ValidationConfiguration.IS_UNPUBLISHED_ONLY))) {
        expressionBuilder.filter(SnomedDocument.Expressions.effectiveTime(EffectiveTimes.UNSET_EFFECTIVE_TIME));
    }
    Expression where = expressionBuilder.build();
    // TODO check if the expression contains only the ID list, then skip scrolling and just report them
    List[] issues = { null };
    Query.select(String.class).from(validationQuery.getDocType()).fields(SnomedDocument.Fields.ID).where(where).limit(RULE_LIMIT).withScores(false).build().stream(context.service(RevisionSearcher.class)).forEachOrdered(page -> {
        if (issues[0] == null) {
            issues[0] = newArrayListWithExpectedSize(page.getTotal());
        }
        for (String affectedComponentId : page) {
            String affectedComponentType = SnomedComponent.getTypeSafe(affectedComponentId);
            if (TerminologyRegistry.UNKNOWN_COMPONENT_TYPE.equals(affectedComponentType)) {
                affectedComponentType = SnomedReferenceSetMember.TYPE;
            }
            issues[0].add(ComponentIdentifier.of(affectedComponentType, affectedComponentId));
        }
    });
    return issues[0] == null ? Collections.emptyList() : issues[0];
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SearchIndexResourceRequest(com.b2international.snowowl.core.request.SearchIndexResourceRequest) Expression(com.b2international.index.query.Expression) BranchContext(com.b2international.snowowl.core.domain.BranchContext) PageableCollectionResource(com.b2international.snowowl.core.domain.PageableCollectionResource) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 32 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyBuilder method addActiveInferredRelationships.

public ReasonerTaxonomyBuilder addActiveInferredRelationships(final RevisionSearcher searcher) {
    entering("Registering active inferred relationships using revision searcher");
    final ExpressionBuilder whereExpressionBuilder = Expressions.builder().filter(active()).filter(characteristicTypeId(Concepts.INFERRED_RELATIONSHIP));
    if (!excludedModuleIds.isEmpty()) {
        whereExpressionBuilder.mustNot(modules(excludedModuleIds));
    }
    addRelationships(searcher, whereExpressionBuilder, existingInferredRelationships);
    leaving("Registering active inferred relationships using revision searcher");
    return this;
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 33 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyBuilder method addFullySpecifiedNames.

public ReasonerTaxonomyBuilder addFullySpecifiedNames(final RevisionSearcher searcher) {
    entering("Registering fully specified names using revision searcher");
    checkState(fullySpecifiedNames == null, "Fully specified names should only be collected once");
    fullySpecifiedNames = PrimitiveMaps.newLongKeyOpenHashMapWithExpectedSize(conceptMap.size());
    final ExpressionBuilder whereExpressionBuilder = Expressions.builder().filter(SnomedDescriptionIndexEntry.Expressions.active()).filter(SnomedDescriptionIndexEntry.Expressions.type(Concepts.FULLY_SPECIFIED_NAME));
    if (!excludedModuleIds.isEmpty()) {
        whereExpressionBuilder.mustNot(modules(excludedModuleIds));
    }
    final List<String> conceptIds = new ArrayList<>(SCROLL_LIMIT);
    final List<String> terms = new ArrayList<>(SCROLL_LIMIT);
    Query.select(String[].class).from(SnomedDescriptionIndexEntry.class).fields(// 0
    SnomedDescriptionIndexEntry.Fields.CONCEPT_ID, // 1
    SnomedDescriptionIndexEntry.Fields.TERM).where(whereExpressionBuilder.build()).limit(SCROLL_LIMIT).build().stream(searcher).forEachOrdered(hits -> {
        for (final String[] description : hits) {
            if (conceptMap.containsKey(description[0])) {
                conceptIds.add(description[0]);
                terms.add(description[1]);
            } else {
                LOGGER.debug("Not registering FSN as its concept {} is inactive.", description[0]);
            }
        }
        for (int i = 0; i < conceptIds.size(); i++) {
            fullySpecifiedNames.put(Long.parseLong(conceptIds.get(i)), terms.get(i));
        }
        conceptIds.clear();
        terms.clear();
    });
    leaving("Registering fully specified names using revision searcher");
    return this;
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 34 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyBuilder method addActiveStatedEdges.

public ReasonerTaxonomyBuilder addActiveStatedEdges(final RevisionSearcher searcher) {
    entering("Registering active stated IS A graph edges using revision searcher");
    final ExpressionBuilder whereExpressionBuilder = Expressions.builder().filter(active()).filter(typeId(Concepts.IS_A)).filter(characteristicTypeId(Concepts.STATED_RELATIONSHIP));
    if (!excludedModuleIds.isEmpty()) {
        whereExpressionBuilder.mustNot(modules(excludedModuleIds));
    }
    final List<String> sourceIds = new ArrayList<>(SCROLL_LIMIT);
    final List<String> destinationIds = new ArrayList<>(SCROLL_LIMIT);
    Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(// 0
    SnomedRelationshipIndexEntry.Fields.SOURCE_ID, // 1
    SnomedRelationshipIndexEntry.Fields.DESTINATION_ID).where(whereExpressionBuilder.build()).limit(SCROLL_LIMIT).build().stream(searcher).forEachOrdered(hits -> {
        for (final String[] relationship : hits) {
            if (conceptMap.containsKey(relationship[0]) && conceptMap.containsKey(relationship[1])) {
                sourceIds.add(relationship[0]);
                destinationIds.add(relationship[1]);
            } else {
                LOGGER.debug("Not registering IS A relationship as its source {} and/or destination {} is inactive.", relationship[0], relationship[1]);
            }
        }
        statedAncestors.addEdges(sourceIds, destinationIds);
        statedDescendants.addEdges(destinationIds, sourceIds);
        sourceIds.clear();
        destinationIds.clear();
    });
    leaving("Registering active stated IS A graph edges using revision searcher");
    return this;
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 35 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyBuilder method addActiveConcreteDomainMembers.

public ReasonerTaxonomyBuilder addActiveConcreteDomainMembers(final RevisionSearcher searcher) {
    entering("Registering active concrete domain members using revision searcher");
    final ExpressionBuilder whereExpressionBuilder = Expressions.builder().filter(active()).filter(refSetTypes(Collections.singleton(SnomedRefSetType.CONCRETE_DATA_TYPE))).filter(characteristicTypeIds(CD_CHARACTERISTIC_TYPE_IDS));
    if (!excludedModuleIds.isEmpty()) {
        whereExpressionBuilder.mustNot(modules(excludedModuleIds));
    }
    final List<ConcreteDomainFragment> statedFragments = new ArrayList<>(SCROLL_LIMIT);
    final List<ConcreteDomainFragment> inferredFragments = new ArrayList<>(SCROLL_LIMIT);
    final List<ConcreteDomainFragment> additionalGroupedFragments = new ArrayList<>(SCROLL_LIMIT);
    final String[] lastReferencedComponentId = { "" };
    Query.select(SnomedRefSetMemberIndexEntry.class).where(whereExpressionBuilder.build()).sortBy(SortBy.builder().sortByField(SnomedRefSetMemberIndexEntry.Fields.REFERENCED_COMPONENT_ID, Order.ASC).sortByField(SnomedRefSetMemberIndexEntry.Fields.ID, Order.ASC).build()).limit(SCROLL_LIMIT).build().stream(searcher).forEachOrdered(hits -> {
        for (final SnomedRefSetMemberIndexEntry member : hits) {
            final String referencedComponentId = member.getReferencedComponentId();
            if (lastReferencedComponentId[0].isEmpty()) {
                lastReferencedComponentId[0] = referencedComponentId;
            } else if (!lastReferencedComponentId[0].equals(referencedComponentId)) {
                if (conceptMap.containsKey(lastReferencedComponentId[0])) {
                    statedConcreteDomainMembers.putAll(lastReferencedComponentId[0], statedFragments);
                    inferredConcreteDomainMembers.putAll(lastReferencedComponentId[0], inferredFragments);
                    additionalGroupedConcreteDomainMembers.putAll(lastReferencedComponentId[0], additionalGroupedFragments);
                } else {
                    LOGGER.debug("Not registering CD members for concept {} as it is inactive.", lastReferencedComponentId[0]);
                }
                statedFragments.clear();
                inferredFragments.clear();
                additionalGroupedFragments.clear();
                lastReferencedComponentId[0] = referencedComponentId;
            }
            final String memberId = member.getId();
            final long refsetId = Long.parseLong(member.getRefsetId());
            final String serializedValue = SnomedRefSetUtil.serializeValue(member.getDataType(), member.getValue());
            final Integer group = member.getRelationshipGroup();
            final long typeId = Long.parseLong(member.getTypeId());
            final boolean released = member.isReleased();
            final ConcreteDomainFragment fragment = new ConcreteDomainFragment(memberId, refsetId, group, serializedValue, typeId, released);
            if (Concepts.STATED_RELATIONSHIP.equals(member.getCharacteristicTypeId())) {
                statedFragments.add(fragment);
            } else if (Concepts.ADDITIONAL_RELATIONSHIP.equals(member.getCharacteristicTypeId()) && member.getRelationshipGroup() > 0) {
                additionalGroupedFragments.add(fragment);
            } else if (Concepts.INFERRED_RELATIONSHIP.equals(member.getCharacteristicTypeId())) {
                inferredFragments.add(fragment);
            }
        }
    });
    if (!lastReferencedComponentId[0].isEmpty()) {
        if (conceptMap.containsKey(lastReferencedComponentId[0])) {
            statedConcreteDomainMembers.putAll(lastReferencedComponentId[0], statedFragments);
            inferredConcreteDomainMembers.putAll(lastReferencedComponentId[0], inferredFragments);
            additionalGroupedConcreteDomainMembers.putAll(lastReferencedComponentId[0], additionalGroupedFragments);
        } else {
            LOGGER.debug("Not registering CD members for concept {} as it is inactive.", lastReferencedComponentId[0]);
        }
        statedFragments.clear();
        inferredFragments.clear();
        additionalGroupedFragments.clear();
    }
    leaving("Registering active concrete domain members using revision searcher");
    return this;
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Aggregations

ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)47 Expressions (com.b2international.index.query.Expressions)17 Expression (com.b2international.index.query.Expression)6 List (java.util.List)6 Set (java.util.Set)6 BadRequestException (com.b2international.commons.exceptions.BadRequestException)5 NotImplementedException (com.b2international.commons.exceptions.NotImplementedException)5 Options (com.b2international.commons.options.Options)5 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)5 SearchResourceRequest (com.b2international.snowowl.core.request.SearchResourceRequest)5 Collection (java.util.Collection)5 Collectors (java.util.stream.Collectors)5 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)4 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)4 IOException (java.io.IOException)4 CompareUtils (com.b2international.commons.CompareUtils)3 Query (com.b2international.index.query.Query)3 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)3 BranchContext (com.b2international.snowowl.core.domain.BranchContext)3 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)3