Search in sources :

Example 6 with BranchContext

use of com.b2international.snowowl.core.domain.BranchContext 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()));
            }
        }
    }
}
Also used : SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) BranchRequest(com.b2international.snowowl.core.request.BranchRequest) com.b2international.snowowl.snomed.reasoner.domain(com.b2international.snowowl.snomed.reasoner.domain) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) ExtendedLocale(com.b2international.commons.http.ExtendedLocale) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) DescriptionChangeDocument(com.b2international.snowowl.snomed.reasoner.index.DescriptionChangeDocument) SnomedDescriptions(com.b2international.snowowl.snomed.core.domain.SnomedDescriptions) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) ClassificationRequests(com.b2international.snowowl.snomed.reasoner.request.ClassificationRequests) Collection(java.util.Collection) Request(com.b2international.snowowl.core.events.Request) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) Set(java.util.Set) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) List(java.util.List) BranchContext(com.b2international.snowowl.core.domain.BranchContext) BaseResourceConverter(com.b2international.snowowl.core.request.BaseResourceConverter) Options(com.b2international.commons.options.Options) SnomedDescription(com.b2international.snowowl.snomed.core.domain.SnomedDescription) SnomedDescriptions(com.b2international.snowowl.snomed.core.domain.SnomedDescriptions) RevisionIndexReadRequest(com.b2international.snowowl.core.request.RevisionIndexReadRequest) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) BranchContext(com.b2international.snowowl.core.domain.BranchContext)

Example 7 with BranchContext

use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.

the class NamespaceIdProvider method extractNamespaceIds.

default Map<String, String> extractNamespaceIds(BranchContext context, final Collection<String> namespaceConceptIds, boolean ignoreInvalidValues) {
    final Set<String> mutableNamespaceConceptIds = Sets.newHashSet(namespaceConceptIds);
    final Map<String, String> namespacesByNamespaceConceptId = new HashMap<>(mutableNamespaceConceptIds.size());
    // Keep only valid SCTIDs passed in to the filter
    if (ignoreInvalidValues) {
        mutableNamespaceConceptIds.removeIf(id -> !SnomedIdentifiers.isValid(id));
    } else {
        final Set<String> invalidNamespaceConceptIds = mutableNamespaceConceptIds.stream().filter(id -> !SnomedIdentifiers.isValid(id)).collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder()));
        if (!invalidNamespaceConceptIds.isEmpty()) {
            throw new BadRequestException("The following namespaceConceptId values are invalid SNOMED CT Concept identifiers, %s", invalidNamespaceConceptIds.toString());
        }
    }
    /* 
		 * The International core namespace concept will not have an FSN matching the pattern,
		 * so remove it from the set, and convert it to the empty namespace directly. 
		 */
    if (mutableNamespaceConceptIds.remove(Concepts.CORE_NAMESPACE)) {
        namespacesByNamespaceConceptId.put(Concepts.CORE_NAMESPACE, "");
    }
    // Find the FSN of namespace SCTIDs
    SnomedRequests.prepareSearchDescription().filterByActive(true).filterByType(Concepts.FULLY_SPECIFIED_NAME).filterByConcepts(mutableNamespaceConceptIds).setFields(SnomedDescriptionIndexEntry.Fields.ID, SnomedDescriptionIndexEntry.Fields.CONCEPT_ID, SnomedDescriptionIndexEntry.Fields.TERM).setLimit(1000).stream(context).flatMap(SnomedDescriptions::stream).forEach(fsn -> {
        // Extract namespace from description terms
        final Matcher matcher = NAMESPACE_PATTERN.matcher(fsn.getTerm());
        if (matcher.matches()) {
            namespacesByNamespaceConceptId.put(fsn.getConceptId(), matcher.group(1));
        }
    });
    return Map.copyOf(namespacesByNamespaceConceptId);
}
Also used : BadRequestException(com.b2international.commons.exceptions.BadRequestException) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) java.util(java.util) Matcher(java.util.regex.Matcher) SnomedDescriptions(com.b2international.snowowl.snomed.core.domain.SnomedDescriptions) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) Pattern(java.util.regex.Pattern) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Sets(com.google.common.collect.Sets) Matcher(java.util.regex.Matcher) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 8 with BranchContext

use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.

the class ValidateRequest method doValidate.

private ValidationResult doValidate(BranchContext context, Writer index) throws IOException {
    final String branchPath = context.path();
    ValidationRuleSearchRequestBuilder req = ValidationRequests.rules().prepareSearch();
    TerminologyResource resource = context.service(TerminologyResource.class);
    ResourceURI resourceURI = resource.getResourceURI(branchPath);
    if (!CompareUtils.isEmpty(ruleIds)) {
        req.filterByIds(ruleIds);
    }
    final ValidationRules rules = req.all().build().execute(context);
    final ValidationThreadPool pool = context.service(ValidationThreadPool.class);
    final BlockingQueue<IssuesToPersist> issuesToPersistQueue = Queues.newLinkedBlockingDeque();
    final List<Promise<Object>> validationPromises = Lists.newArrayList();
    // evaluate selected rules
    for (ValidationRule rule : rules) {
        checkArgument(rule.getCheckType() != null, "CheckType is missing for rule " + rule.getId());
        final ValidationRuleEvaluator evaluator = ValidationRuleEvaluator.Registry.get(rule.getType());
        if (evaluator != null) {
            validationPromises.add(pool.submit(rule.getCheckType(), () -> {
                Stopwatch w = Stopwatch.createStarted();
                try {
                    LOG.info("Executing rule '{}'...", rule.getId());
                    final List<?> evaluationResponse = evaluator.eval(context, rule, ruleParameters);
                    issuesToPersistQueue.offer(new IssuesToPersist(rule.getId(), evaluationResponse));
                    LOG.info("Execution of rule '{}' successfully completed in '{}'.", rule.getId(), w);
                // TODO report successfully executed validation rule
                } catch (Exception e) {
                    // TODO report failed validation rule
                    LOG.error("Execution of rule '{}' failed after '{}'.", rule.getId(), w, e);
                }
            }));
        }
    }
    final Set<String> ruleIds = rules.stream().map(ValidationRule::getId).collect(Collectors.toSet());
    final Multimap<String, ComponentIdentifier> whiteListedEntries = fetchWhiteListEntries(context, ruleIds);
    final Promise<List<Object>> promise = Promise.all(validationPromises);
    while (!promise.isDone() || !issuesToPersistQueue.isEmpty()) {
        if (!issuesToPersistQueue.isEmpty()) {
            final Collection<IssuesToPersist> issuesToPersist = newArrayList();
            issuesToPersistQueue.drainTo(issuesToPersist);
            if (!issuesToPersist.isEmpty()) {
                final List<String> rulesToPersist = issuesToPersist.stream().map(itp -> itp.ruleId).collect(Collectors.toList());
                LOG.info("Persisting issues generated by rules '{}'...", rulesToPersist);
                // persist new issues generated by rules so far, extending them using the Issue Extension API
                int persistedIssues = 0;
                final Multimap<String, ValidationIssue> issuesToExtendWithDetailsByToolingId = HashMultimap.create();
                for (IssuesToPersist ruleIssues : Iterables.consumingIterable(issuesToPersist)) {
                    final String ruleId = ruleIssues.ruleId;
                    final List<ValidationIssue> existingRuleIssues = ValidationRequests.issues().prepareSearch().all().filterByResourceUri(resourceURI).filterByRule(ruleId).build().execute(context).getItems();
                    final Set<String> issueIdsToDelete = Sets.newHashSet();
                    final Map<ComponentIdentifier, ValidationIssue> existingIsssuesByComponentIdentifier = new HashMap<>();
                    for (ValidationIssue issue : existingRuleIssues) {
                        if (existingIsssuesByComponentIdentifier.containsKey(issue.getAffectedComponent())) {
                            issueIdsToDelete.add(issue.getId());
                        } else {
                            existingIsssuesByComponentIdentifier.put(issue.getAffectedComponent(), issue);
                        }
                    }
                    // remove all processed whitelist entries
                    final Collection<ComponentIdentifier> ruleWhiteListEntries = whiteListedEntries.removeAll(ruleId);
                    final String toolingId = rules.stream().filter(rule -> ruleId.equals(rule.getId())).findFirst().get().getToolingId();
                    for (ValidationIssueDetails issueDetails : ruleIssues.issueDetails) {
                        final ValidationIssue validationIssue;
                        ComponentIdentifier componentIdentifier = issueDetails.affectedComponentId;
                        if (!existingIsssuesByComponentIdentifier.containsKey(componentIdentifier)) {
                            validationIssue = new ValidationIssue(UUID.randomUUID().toString(), ruleId, ComponentURI.of(resourceURI, componentIdentifier), ruleWhiteListEntries.contains(componentIdentifier));
                        } else {
                            final ValidationIssue issueToCopy = existingIsssuesByComponentIdentifier.get(componentIdentifier);
                            validationIssue = new ValidationIssue(issueToCopy.getId(), issueToCopy.getRuleId(), ComponentURI.of(resourceURI, issueToCopy.getAffectedComponent()), ruleWhiteListEntries.contains(issueToCopy.getAffectedComponent()));
                            existingIsssuesByComponentIdentifier.remove(componentIdentifier);
                        }
                        validationIssue.setDetails(ValidationIssueDetails.HIGHLIGHT_DETAILS, issueDetails.stylingDetails);
                        issuesToExtendWithDetailsByToolingId.put(toolingId, validationIssue);
                        persistedIssues++;
                    }
                    existingRuleIssues.stream().filter(issue -> existingIsssuesByComponentIdentifier.containsKey(issue.getAffectedComponent())).forEach(issue -> issueIdsToDelete.add(issue.getId()));
                    if (!issueIdsToDelete.isEmpty()) {
                        index.removeAll(Collections.singletonMap(ValidationIssue.class, issueIdsToDelete));
                    }
                }
                for (String toolingId : issuesToExtendWithDetailsByToolingId.keySet()) {
                    final ValidationIssueDetailExtension extensions = context.service(ValidationIssueDetailExtensionProvider.class).getExtensions(toolingId);
                    final Collection<ValidationIssue> issues = issuesToExtendWithDetailsByToolingId.removeAll(toolingId);
                    extensions.extendIssues(context, issues, ruleParameters);
                    for (ValidationIssue issue : issues) {
                        index.put(issue);
                    }
                }
                index.commit();
                LOG.info("Persisted '{}' issues generated by rules '{}'.", persistedIssues, rulesToPersist);
            }
        } else {
            try {
                // wait at least number of rules * 50ms for the next responses
                Thread.sleep(Math.min(((long) ruleIds.size()) * 100, POLL_INTERVAL_MAX));
            } catch (InterruptedException e) {
                throw new SnowowlRuntimeException(e);
            }
        }
    }
    // TODO return ValidationResult object with status and new issue IDs as set
    return new ValidationResult(context.info().id(), context.path());
}
Also used : ValidationIssueDetailExtensionProvider(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider) java.util(java.util) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Stopwatch(com.google.common.base.Stopwatch) Promise(com.b2international.snowowl.core.events.util.Promise) LoggerFactory(org.slf4j.LoggerFactory) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) ValidationIssueDetailExtension(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtension) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) Permission(com.b2international.snowowl.core.identity.Permission) CompareUtils(com.b2international.commons.CompareUtils) ValidationRuleSearchRequestBuilder(com.b2international.snowowl.core.validation.rule.ValidationRuleSearchRequestBuilder) com.google.common.collect(com.google.common.collect) ValidationRepository(com.b2international.snowowl.core.internal.validation.ValidationRepository) ResourceURI(com.b2international.snowowl.core.ResourceURI) ValidationRule(com.b2international.snowowl.core.validation.rule.ValidationRule) ValidationWhiteListSearchRequestBuilder(com.b2international.snowowl.core.validation.whitelist.ValidationWhiteListSearchRequestBuilder) ValidationThreadPool(com.b2international.snowowl.core.internal.validation.ValidationThreadPool) Logger(org.slf4j.Logger) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Request(com.b2international.snowowl.core.events.Request) ComponentURI(com.b2international.snowowl.core.uri.ComponentURI) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) Collectors(java.util.stream.Collectors) Writer(com.b2international.index.Writer) ValidationRuleEvaluator(com.b2international.snowowl.core.validation.eval.ValidationRuleEvaluator) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ValidationRules(com.b2international.snowowl.core.validation.rule.ValidationRules) BranchContext(com.b2international.snowowl.core.domain.BranchContext) ValidationRuleSearchRequestBuilder(com.b2international.snowowl.core.validation.rule.ValidationRuleSearchRequestBuilder) ValidationThreadPool(com.b2international.snowowl.core.internal.validation.ValidationThreadPool) Stopwatch(com.google.common.base.Stopwatch) ComponentIdentifier(com.b2international.snowowl.core.ComponentIdentifier) ValidationIssueDetailExtension(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtension) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ResourceURI(com.b2international.snowowl.core.ResourceURI) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) IOException(java.io.IOException) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) ValidationRule(com.b2international.snowowl.core.validation.rule.ValidationRule) Promise(com.b2international.snowowl.core.events.util.Promise) ValidationRules(com.b2international.snowowl.core.validation.rule.ValidationRules) ValidationRuleEvaluator(com.b2international.snowowl.core.validation.eval.ValidationRuleEvaluator) ValidationIssueDetailExtensionProvider(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider)

Example 9 with BranchContext

use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.

the class EclExpression method resolveToGroupedOnly.

private Promise<Multimap<String, Integer>> resolveToGroupedOnly(BranchContext context, Set<String> sourceIds) {
    final Set<String> characteristicTypes = isInferred() ? SnomedEclRefinementEvaluator.INFERRED_CHARACTERISTIC_TYPES : SnomedEclRefinementEvaluator.STATED_CHARACTERISTIC_TYPES;
    List<Promise<Multimap<String, Integer>>> promises = newArrayListWithCapacity(3);
    // search relationships
    promises.add(SnomedRequests.prepareSearchRelationship().all().filterByActive(true).filterByCharacteristicTypes(characteristicTypes).filterBySources(sourceIds).filterByGroup(1, Integer.MAX_VALUE).setEclExpressionForm(expressionForm).setFields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.RELATIONSHIP_GROUP).build(context.service(ResourceURI.class)).execute(context.service(IEventBus.class)).then(new Function<SnomedRelationships, Multimap<String, Integer>>() {

        @Override
        public Multimap<String, Integer> apply(SnomedRelationships input) {
            final Multimap<String, SnomedRelationship> relationshipsBySource = Multimaps.index(input, SnomedRelationship::getSourceId);
            final Multimap<String, Integer> groupsByRelationshipId = Multimaps.transformValues(relationshipsBySource, SnomedRelationship::getRelationshipGroup);
            return ImmutableSetMultimap.copyOf(groupsByRelationshipId);
        }
    }));
    // search concrete domain members
    if (context.service(SnomedCoreConfiguration.class).isConcreteDomainSupported()) {
        final Options propFilter = Options.builder().put(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID, characteristicTypes).put(SearchResourceRequest.operator(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP), SearchResourceRequest.Operator.NOT_EQUALS).put(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP, 0).build();
        promises.add(SnomedRequests.prepareSearchMember().all().filterByActive(true).filterByReferencedComponent(sourceIds).filterByRefSetType(SnomedRefSetType.CONCRETE_DATA_TYPE).filterByProps(propFilter).setEclExpressionForm(expressionForm).build(context.service(ResourceURI.class)).execute(context.service(IEventBus.class)).then(members -> {
            final Multimap<String, SnomedReferenceSetMember> relationshipsBySource = Multimaps.index(members, m -> m.getReferencedComponent().getId());
            return Multimaps.transformValues(relationshipsBySource, m -> (Integer) m.getProperties().get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP));
        }));
    } else {
        promises.add(Promise.immediate(ImmutableSetMultimap.of()));
    }
    // search owl axiom members
    if (isStated()) {
        ImmutableSetMultimap.Builder<String, Integer> groupedAxioms = ImmutableSetMultimap.builder();
        SnomedEclRefinementEvaluator.evalAxiomStatements(context, true, sourceIds, null, null).forEach(property -> groupedAxioms.put(property.getObjectId(), property.getGroup()));
        promises.add(Promise.immediate(groupedAxioms.build()));
    } else {
        promises.add(Promise.immediate(ImmutableSetMultimap.of()));
    }
    return Promise.all(promises).then(statements -> {
        Multimap<String, Integer> relationshipStatements = (Multimap<String, Integer>) statements.get(0);
        Multimap<String, Integer> concreteDomainStatements = (Multimap<String, Integer>) statements.get(1);
        Multimap<String, Integer> axiomStatements = (Multimap<String, Integer>) statements.get(2);
        return ImmutableSetMultimap.<String, Integer>builder().putAll(relationshipStatements).putAll(concreteDomainStatements).putAll(axiomStatements).build();
    });
}
Also used : Query(com.b2international.index.query.Query) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) Promise(com.b2international.snowowl.core.events.util.Promise) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) NestedExpression(com.b2international.snomed.ecl.ecl.NestedExpression) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) Nullable(javax.annotation.Nullable) ResourceURI(com.b2international.snowowl.core.ResourceURI) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) Lists.newArrayListWithCapacity(com.google.common.collect.Lists.newArrayListWithCapacity) Function(com.google.common.base.Function) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) RevisionDocument(com.b2international.snowowl.core.repository.RevisionDocument) SnomedConcepts(com.b2international.snowowl.snomed.core.domain.SnomedConcepts) Set(java.util.Set) IOException(java.io.IOException) IEventBus(com.b2international.snowowl.eventbus.IEventBus) NotNull(javax.validation.constraints.NotNull) PathWithVersion(com.b2international.snowowl.core.uri.ResourceURIPathResolver.PathWithVersion) SnomedRelationships(com.b2international.snowowl.snomed.core.domain.SnomedRelationships) Any(com.b2international.snomed.ecl.ecl.Any) Trees(com.b2international.snowowl.snomed.core.tree.Trees) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) List(java.util.List) Expressions(com.b2international.index.query.Expressions) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) Expression(com.b2international.index.query.Expression) Preconditions(com.google.common.base.Preconditions) Ecl(com.b2international.snomed.ecl.Ecl) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) EclConceptReference(com.b2international.snomed.ecl.ecl.EclConceptReference) ExpressionConstraint(com.b2international.snomed.ecl.ecl.ExpressionConstraint) BranchContext(com.b2international.snowowl.core.domain.BranchContext) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedCoreConfiguration(com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration) Options(com.b2international.commons.options.Options) SnomedRelationship(com.b2international.snowowl.snomed.core.domain.SnomedRelationship) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) SnomedCoreConfiguration(com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration) SnomedRelationships(com.b2international.snowowl.snomed.core.domain.SnomedRelationships) Promise(com.b2international.snowowl.core.events.util.Promise) Function(com.google.common.base.Function) Multimap(com.google.common.collect.Multimap) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) IEventBus(com.b2international.snowowl.eventbus.IEventBus)

Example 10 with BranchContext

use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.

the class SnomedEclEvaluationRequest method eval.

/**
 * Handles (possibly) filtered expression constraints by evaluating them along
 * with the primary ECL expression, and adding the resulting query expressions as
 * extra required clauses.
 *
 * @param context
 * @param filtered
 * @return
 */
protected Promise<Expression> eval(BranchContext context, final FilteredExpressionConstraint filtered) {
    final ExpressionConstraint constraint = filtered.getConstraint();
    final FilterConstraint filterConstraint = filtered.getFilter();
    final Domain filterDomain = Ecl.getDomain(filterConstraint);
    final Filter filter = filterConstraint.getFilter();
    final Promise<Expression> evaluatedConstraint = evaluate(context, constraint);
    Promise<Expression> evaluatedFilter = evaluate(context, filter);
    if (Domain.DESCRIPTION.equals(filterDomain)) {
        // Find concepts that match the description expression, then use the resulting concept IDs as the expression
        evaluatedFilter = evaluatedFilter.then(ex -> executeDescriptionSearch(context, ex));
    }
    if (isAnyExpression(constraint)) {
        // No need to combine "match all" with the filter query expression, return it directly
        return evaluatedFilter;
    }
    return Promise.all(evaluatedConstraint, evaluatedFilter).then(results -> {
        final Expressions.ExpressionBuilder builder = Expressions.builder();
        results.forEach(f -> builder.filter((Expression) f));
        return builder.build();
    });
}
Also used : PolymorphicDispatcher(org.eclipse.xtext.util.PolymorphicDispatcher) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) java.util(java.util) Hits(com.b2international.index.Hits) Promise(com.b2international.snowowl.core.events.util.Promise) ID(com.b2international.index.revision.Revision.Fields.ID) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) SnomedDescriptionUtils(com.b2international.snowowl.snomed.datastore.SnomedDescriptionUtils) SnomedDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument) Expressions.ids(com.b2international.index.revision.Revision.Expressions.ids) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Predicates(com.google.common.base.Predicates) com.b2international.snomed.ecl.ecl(com.b2international.snomed.ecl.ecl) CompareUtils(com.b2international.commons.CompareUtils) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) TreeIterator(org.eclipse.emf.common.util.TreeIterator) com.google.common.collect(com.google.common.collect) Nullable(javax.annotation.Nullable) DateFormats(com.b2international.snowowl.core.date.DateFormats) Function(com.google.common.base.Function) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Request(com.b2international.snowowl.core.events.Request) EclExpression.isEclConceptReference(com.b2international.snowowl.snomed.core.ecl.EclExpression.isEclConceptReference) IOException(java.io.IOException) EObject(org.eclipse.emf.ecore.EObject) EclExpression.isAnyExpression(com.b2international.snowowl.snomed.core.ecl.EclExpression.isAnyExpression) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Trees(com.b2international.snowowl.snomed.core.tree.Trees) ACTIVE_MEMBER_OF(com.b2international.snowowl.snomed.datastore.index.entry.SnomedComponentDocument.Fields.ACTIVE_MEMBER_OF) TimeUnit(java.util.concurrent.TimeUnit) Expressions.id(com.b2international.index.revision.Revision.Expressions.id) IComponent(com.b2international.snowowl.core.domain.IComponent) Expressions.activeMemberOf(com.b2international.snowowl.snomed.datastore.index.entry.SnomedComponentDocument.Expressions.activeMemberOf) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) Ecl(com.b2international.snomed.ecl.Ecl) BranchContext(com.b2international.snowowl.core.domain.BranchContext) NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) com.b2international.index.query(com.b2international.index.query) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) EclExpression.isAnyExpression(com.b2international.snowowl.snomed.core.ecl.EclExpression.isAnyExpression)

Aggregations

BranchContext (com.b2international.snowowl.core.domain.BranchContext)27 Collectors (java.util.stream.Collectors)18 SnomedRequests (com.b2international.snowowl.snomed.datastore.request.SnomedRequests)16 Options (com.b2international.commons.options.Options)15 List (java.util.List)15 Set (java.util.Set)14 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)13 IOException (java.io.IOException)13 BadRequestException (com.b2international.commons.exceptions.BadRequestException)12 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)12 Query (com.b2international.index.query.Query)11 ResourceURI (com.b2international.snowowl.core.ResourceURI)11 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)11 Request (com.b2international.snowowl.core.events.Request)11 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)11 Expression (com.b2international.index.query.Expression)10 SnomedRf2Headers (com.b2international.snowowl.snomed.common.SnomedRf2Headers)10 Expressions (com.b2international.index.query.Expressions)9 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)9 Function (com.google.common.base.Function)9