Search in sources :

Example 1 with Concepts

use of com.b2international.snowowl.snomed.common.SnomedConstants.Concepts in project snow-owl by b2ihealthcare.

the class SnomedRf2ExportRequest method getIdentifierConcepts.

private List<SnomedConcept> getIdentifierConcepts(final RepositoryContext context, final String currentVersion) {
    final Collection<String> refSetsToLoad;
    if (refSets == null) {
        // Retrieve all reference sets if refSets is null
        final Request<BranchContext, SnomedReferenceSets> refSetRequest = SnomedRequests.prepareSearchRefSet().all().build();
        final SnomedReferenceSets allReferenceSets = execute(context, currentVersion, refSetRequest);
        refSetsToLoad = allReferenceSets.stream().map(r -> r.getId()).collect(Collectors.toSet());
    } else {
        refSetsToLoad = refSets;
    }
    final SnomedConceptSearchRequestBuilder refSetRequestBuilder = SnomedRequests.prepareSearchConcept().all().filterByIds(refSetsToLoad).setExpand("pt(),referenceSet()").setLocales(locales());
    final Request<BranchContext, SnomedConcepts> request = refSetRequestBuilder.build();
    final SnomedConcepts referenceSets = execute(context, currentVersion, request);
    // Return only the identifier concepts which have an existing reference set on this branch
    return referenceSets.stream().filter(c -> c.getReferenceSet() != null).collect(Collectors.toList());
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) SnomedConceptSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) SnomedReferenceSets(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSets) RepositoryRequests(com.b2international.snowowl.core.repository.RepositoryRequests) Sets.newTreeSet(com.google.common.collect.Sets.newTreeSet) Collections.singleton(java.util.Collections.singleton) AccessControl(com.b2international.snowowl.core.authorization.AccessControl) LocalTime(java.time.LocalTime) RevisionIndex(com.b2international.index.revision.RevisionIndex) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) AttachmentRegistry(com.b2international.snowowl.core.attachments.AttachmentRegistry) Branches(com.b2international.snowowl.core.branch.Branches) Permission(com.b2international.snowowl.core.identity.Permission) CompareUtils(com.b2international.commons.CompareUtils) Path(java.nio.file.Path) com.google.common.collect(com.google.common.collect) Collectors.toSet(java.util.stream.Collectors.toSet) ResourceURI(com.b2international.snowowl.core.ResourceURI) com.b2international.snowowl.core.request(com.b2international.snowowl.core.request) Version(com.b2international.snowowl.core.version.Version) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Request(com.b2international.snowowl.core.events.Request) IEventBus(com.b2international.snowowl.eventbus.IEventBus) Instant(java.time.Instant) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Stream(java.util.stream.Stream) VersionDocument(com.b2international.snowowl.core.version.VersionDocument) LocalDate(java.time.LocalDate) Sort(com.b2international.snowowl.core.request.SearchResourceRequest.Sort) Entry(java.util.Map.Entry) CodeSystemRequests(com.b2international.snowowl.core.codesystem.CodeSystemRequests) SnomedDescriptionIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Builder(com.google.common.collect.ImmutableList.Builder) FileUtils(com.b2international.commons.FileUtils) CodeSystem(com.b2international.snowowl.core.codesystem.CodeSystem) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) java.util(java.util) TerminologyResource(com.b2international.snowowl.core.TerminologyResource) LocalDateTime(java.time.LocalDateTime) com.b2international.snowowl.snomed.datastore.request.rf2.exporter(com.b2international.snowowl.snomed.datastore.request.rf2.exporter) com.b2international.snowowl.snomed.core.domain(com.b2international.snowowl.snomed.core.domain) Branch(com.b2international.snowowl.core.branch.Branch) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Strings(com.google.common.base.Strings) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Attachment(com.b2international.snowowl.core.attachments.Attachment) SnomedRefSetMemberSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRefSetMemberSearchRequestBuilder) BadRequestException(com.b2international.commons.exceptions.BadRequestException) BranchPathUtils(com.b2international.snowowl.core.branch.BranchPathUtils) DateFormats(com.b2international.snowowl.core.date.DateFormats) Files(java.nio.file.Files) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SnomedTerminologyComponentConstants(com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) ChronoUnit(java.time.temporal.ChronoUnit) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) NotEmpty(org.hibernate.validator.constraints.NotEmpty) DateTimeFormatter(java.time.format.DateTimeFormatter) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) Versions(com.b2international.snowowl.core.version.Versions) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) BranchContext(com.b2international.snowowl.core.domain.BranchContext) SnomedReferenceSets(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSets) SnomedConceptSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedConceptSearchRequestBuilder)

Example 2 with Concepts

use of com.b2international.snowowl.snomed.common.SnomedConstants.Concepts in project snow-owl by b2ihealthcare.

the class Taxonomies method getStatements.

private static Collection<Object[]> getStatements(RevisionSearcher searcher, LongCollection conceptIds, String characteristicTypeId, boolean filterByConceptIds) throws IOException {
    // merge stated relationships and OWL axiom relationships into a single array
    ImmutableList.Builder<Object[]> isaStatementsBuilder = ImmutableList.builder();
    final Set<String> concepts = LongSets.toStringSet(conceptIds);
    ExpressionBuilder activeIsaRelationshipQuery = Expressions.builder().filter(active()).filter(typeId(Concepts.IS_A)).filter(characteristicTypeId(characteristicTypeId));
    if (filterByConceptIds) {
        activeIsaRelationshipQuery.filter(sourceIds(concepts)).filter(destinationIds(concepts));
    }
    final Query<String[]> activeStatedISARelationshipsQuery = Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.DESTINATION_ID).where(activeIsaRelationshipQuery.build()).limit(Integer.MAX_VALUE).build();
    Hits<String[]> activeIsaRelationships = searcher.search(activeStatedISARelationshipsQuery);
    activeIsaRelationships.forEach(activeIsaRelationship -> {
        isaStatementsBuilder.add(new Object[] { activeIsaRelationship[0], Long.parseLong(activeIsaRelationship[1]), new long[] { Long.parseLong(activeIsaRelationship[2]) } });
    });
    activeIsaRelationships = null;
    if (Concepts.STATED_RELATIONSHIP.equals(characteristicTypeId)) {
        // search existing axioms defined for the given set of conceptIds
        ExpressionBuilder activeOwlAxiomMemberQuery = Expressions.builder().filter(active());
        if (filterByConceptIds) {
            activeOwlAxiomMemberQuery.filter(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(concepts)).filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, Expressions.builder().filter(typeId(Concepts.IS_A)).filter(destinationIds(concepts)).build()));
        } else {
            activeOwlAxiomMemberQuery.filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, Expressions.builder().filter(typeId(Concepts.IS_A)).build()));
        }
        final Query<SnomedRefSetMemberIndexEntry> activeAxiomISARelationshipsQuery = Query.select(SnomedRefSetMemberIndexEntry.class).where(activeOwlAxiomMemberQuery.build()).limit(Integer.MAX_VALUE).build();
        Hits<SnomedRefSetMemberIndexEntry> activeAxiomISARelationships = searcher.search(activeAxiomISARelationshipsQuery);
        activeAxiomISARelationships.forEach(owlMember -> {
            if (!CompareUtils.isEmpty(owlMember.getClassAxiomRelationships())) {
                // XXX: breaks with a NumberFormatException if any of the IS A relationships has a value
                long[] destinationIds = owlMember.getClassAxiomRelationships().stream().filter(classAxiom -> Concepts.IS_A.equals(classAxiom.getTypeId())).map(SnomedOWLRelationshipDocument::getDestinationId).mapToLong(Long::parseLong).toArray();
                isaStatementsBuilder.add(new Object[] { owlMember.getId(), Long.parseLong(owlMember.getReferencedComponentId()), destinationIds });
            }
        });
        activeAxiomISARelationships = null;
    }
    return isaStatementsBuilder.build();
}
Also used : Query(com.b2international.index.query.Query) Hits(com.b2international.index.Hits) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) LoggerFactory(org.slf4j.LoggerFactory) LongCollections(com.b2international.collections.longs.LongCollections) SnomedOWLExpressionConverterResult(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverterResult) Expressions.typeId(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.typeId) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) ImmutableList(com.google.common.collect.ImmutableList) LongSets(com.b2international.commons.collect.LongSets) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) CompareUtils(com.b2international.commons.CompareUtils) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) Logger(org.slf4j.Logger) LongIterator(com.b2international.collections.longs.LongIterator) LongCollection(com.b2international.collections.longs.LongCollection) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Collection(java.util.Collection) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) Expressions.characteristicTypeId(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.characteristicTypeId) Expressions.sourceIds(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.sourceIds) Set(java.util.Set) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) Expressions.destinationIds(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.destinationIds) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Expressions(com.b2international.index.query.Expressions) Expressions.active(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active) StagingArea(com.b2international.index.revision.StagingArea) IComponent(com.b2international.snowowl.core.domain.IComponent) Collections(java.util.Collections) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) ImmutableList(com.google.common.collect.ImmutableList) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)

Example 3 with Concepts

use of com.b2international.snowowl.snomed.common.SnomedConstants.Concepts in project snow-owl by b2ihealthcare.

the class NormalFormGenerator method computeChanges.

@Override
public final void computeChanges(final IProgressMonitor monitor, final OntologyChangeProcessor<StatementFragment> statementProcessor, final OntologyChangeProcessor<ConcreteDomainFragment> concreteDomainProcessor) {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    LOGGER.info(">>> Distribution normal form generation");
    final LongList entries = reasonerTaxonomy.getIterationOrder();
    final SubMonitor subMonitor = SubMonitor.convert(monitor, "Generating distribution normal form...", entries.size() * 2);
    try {
        LongSet previousLayer = null;
        LongSet currentLayer = PrimitiveSets.newLongOpenHashSet();
        final Set<Long> graphTypeIds = reasonerTaxonomy.getPropertyChains().stream().map(PropertyChain::getDestinationType).collect(Collectors.toSet());
        // The first round can be skipped entirely, if no type IDs participate in a property chain
        final boolean propertyChainsPresent = !graphTypeIds.isEmpty();
        if (propertyChainsPresent) {
            // Initialize node graphs for properties we need to traverse
            LOGGER.info("--- Initializing node graphs for types: {}", graphTypeIds);
            graphTypeIds.forEach(id -> transitiveNodeGraphs.put(id, new NodeGraph()));
            // Round 1: build alternative hierarchies
            for (final LongIterator itr = entries.iterator(); itr.hasNext(); ) /* empty */
            {
                final long conceptId = itr.next();
                if (conceptId == ReasonerTaxonomyInferrer.DEPTH_CHANGE) {
                    if (previousLayer != null) {
                        invalidate(previousLayer);
                    }
                    previousLayer = currentLayer;
                    currentLayer = PrimitiveSets.newLongOpenHashSet();
                    continue;
                }
                precomputeProperties(conceptId, false);
                final Collection<StatementFragment> inferredNonIsAFragments = statementCache.get(conceptId);
                inferredNonIsAFragments.stream().filter(r -> transitiveNodeGraphs.keySet().contains(r.getTypeId())).filter(StatementFragmentWithDestination.class::isInstance).map(StatementFragmentWithDestination.class::cast).forEachOrdered(r -> transitiveNodeGraphs.get(r.getTypeId()).addParent(conceptId, r.getDestinationId()));
            }
            // Clear the last layer of concepts
            previousLayer = null;
            currentLayer = PrimitiveSets.newLongOpenHashSet();
            statementCache.clear();
            concreteDomainCache.clear();
        } else {
            LOGGER.info("--- Node graphs computation skipped, no types used for property chaining");
        }
        LOGGER.info("--- Use node graphs for hierarchy computation");
        // Round 2: record changes using the hierarchies
        for (final LongIterator itr = entries.iterator(); itr.hasNext(); ) /* empty */
        {
            final long conceptId = itr.next();
            if (conceptId == ReasonerTaxonomyInferrer.DEPTH_CHANGE) {
                if (previousLayer != null) {
                    invalidate(previousLayer);
                }
                previousLayer = currentLayer;
                currentLayer = PrimitiveSets.newLongOpenHashSet();
                continue;
            }
            // Run costly comparison of property chain hierarchies only if there are any
            precomputeProperties(conceptId, propertyChainsPresent);
            final Collection<StatementFragment> existingStatements = reasonerTaxonomy.getExistingInferredRelationships().get(conceptId);
            final Collection<StatementFragment> targetStatements = getTargetRelationships(conceptId);
            statementProcessor.apply(conceptId, existingStatements, targetStatements, StatementFragmentOrdering.INSTANCE, subMonitor.newChild(1));
            final Collection<ConcreteDomainFragment> existingMembers = reasonerTaxonomy.getInferredConcreteDomainMembers().get(Long.toString(conceptId));
            final Collection<ConcreteDomainFragment> targetMembers = getTargetMembers(conceptId);
            concreteDomainProcessor.apply(conceptId, existingMembers, targetMembers, ConcreteDomainChangeOrdering.INSTANCE, subMonitor.newChild(1));
        }
    } finally {
        subMonitor.done();
        LOGGER.info("<<< Distribution normal form generation [{}]", stopwatch.toString());
    }
}
Also used : LongKeyMap(com.b2international.collections.longs.LongKeyMap) Iterables(com.google.common.collect.Iterables) PrimitiveMaps(com.b2international.collections.PrimitiveMaps) SubMonitor(org.eclipse.core.runtime.SubMonitor) OntologyChangeProcessor(com.b2international.snowowl.snomed.reasoner.diff.OntologyChangeProcessor) Stopwatch(com.google.common.base.Stopwatch) LoggerFactory(org.slf4j.LoggerFactory) StatementFragmentWithDestination(com.b2international.snowowl.snomed.datastore.StatementFragmentWithDestination) ConcreteDomainChangeOrdering(com.b2international.snowowl.snomed.reasoner.diff.concretedomain.ConcreteDomainChangeOrdering) Multimap(com.google.common.collect.Multimap) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) Multimaps(com.google.common.collect.Multimaps) PrimitiveSets(com.b2international.collections.PrimitiveSets) LongSet(com.b2international.collections.longs.LongSet) ConcreteDomainFragment(com.b2international.snowowl.snomed.datastore.ConcreteDomainFragment) ImmutableList(com.google.common.collect.ImmutableList) LongSets(com.b2international.commons.collect.LongSets) ReasonerTaxonomyInferrer(com.b2international.snowowl.snomed.reasoner.classification.ReasonerTaxonomyInferrer) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Predicates(com.google.common.base.Predicates) PropertyChain(com.b2international.snowowl.snomed.datastore.index.taxonomy.PropertyChain) StatementFragmentOrdering(com.b2international.snowowl.snomed.reasoner.diff.relationship.StatementFragmentOrdering) ReasonerTaxonomy(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomy) Logger(org.slf4j.Logger) LongIterator(com.b2international.collections.longs.LongIterator) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) INormalFormGenerator(com.b2international.snowowl.snomed.reasoner.classification.INormalFormGenerator) Collection(java.util.Collection) Set(java.util.Set) StatementFragmentWithValue(com.b2international.snowowl.snomed.datastore.StatementFragmentWithValue) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) LongList(com.b2international.collections.longs.LongList) List(java.util.List) StatementFragment(com.b2international.snowowl.snomed.datastore.StatementFragment) LongSet(com.b2international.collections.longs.LongSet) Stopwatch(com.google.common.base.Stopwatch) SubMonitor(org.eclipse.core.runtime.SubMonitor) LongList(com.b2international.collections.longs.LongList) StatementFragment(com.b2international.snowowl.snomed.datastore.StatementFragment) ConcreteDomainFragment(com.b2international.snowowl.snomed.datastore.ConcreteDomainFragment) StatementFragmentWithDestination(com.b2international.snowowl.snomed.datastore.StatementFragmentWithDestination) LongIterator(com.b2international.collections.longs.LongIterator)

Example 4 with Concepts

use of com.b2international.snowowl.snomed.common.SnomedConstants.Concepts 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)

Example 5 with Concepts

use of com.b2international.snowowl.snomed.common.SnomedConstants.Concepts in project snow-owl by b2ihealthcare.

the class SnomedRepositoryPreCommitHook method getChangeSetProcessors.

@Override
protected Collection<ChangeSetProcessor> getChangeSetProcessors(StagingArea staging, RevisionSearcher index) throws IOException {
    final RepositoryContext context = ClassUtils.checkAndCast(staging.getContext(), RepositoryContext.class);
    // initialize OWL Expression converter on the current branch
    final SnomedOWLExpressionConverter expressionConverter = new BranchRequest<>(staging.getBranchPath(), branchContext -> {
        return new SnomedOWLExpressionConverter(branchContext.inject().bind(RevisionSearcher.class, index).build());
    }).execute(context);
    final Set<String> statedSourceIds = Sets.newHashSet();
    final Set<String> statedDestinationIds = Sets.newHashSet();
    final Set<String> inferredSourceIds = Sets.newHashSet();
    final Set<String> inferredDestinationIds = Sets.newHashSet();
    collectIds(statedSourceIds, statedDestinationIds, staging.getNewObjects(SnomedRelationshipIndexEntry.class), Concepts.STATED_RELATIONSHIP);
    collectIds(statedSourceIds, statedDestinationIds, staging.getChangedRevisions(SnomedRelationshipIndexEntry.class).map(diff -> (SnomedRelationshipIndexEntry) diff.newRevision), Concepts.STATED_RELATIONSHIP);
    collectIds(inferredSourceIds, inferredDestinationIds, staging.getNewObjects(SnomedRelationshipIndexEntry.class), Concepts.INFERRED_RELATIONSHIP);
    collectIds(inferredSourceIds, inferredDestinationIds, staging.getChangedRevisions(SnomedRelationshipIndexEntry.class).map(diff -> (SnomedRelationshipIndexEntry) diff.newRevision), Concepts.INFERRED_RELATIONSHIP);
    collectIds(statedSourceIds, statedDestinationIds, staging.getNewObjects(SnomedRefSetMemberIndexEntry.class), expressionConverter);
    collectIds(statedSourceIds, statedDestinationIds, staging.getChangedRevisions(SnomedRefSetMemberIndexEntry.class).map(diff -> (SnomedRefSetMemberIndexEntry) diff.newRevision), expressionConverter);
    staging.getRemovedObjects(SnomedRelationshipIndexEntry.class).filter(detachedRelationship -> Concepts.IS_A.equals(detachedRelationship.getTypeId())).forEach(detachedRelationship -> {
        // XXX: IS A relationships are expected to have a destination ID, not a value
        checkState(!detachedRelationship.hasValue(), "IS A relationship found with value: %s", detachedRelationship.getId());
        if (Concepts.STATED_RELATIONSHIP.equals(detachedRelationship.getCharacteristicTypeId())) {
            statedSourceIds.add(detachedRelationship.getSourceId());
            statedDestinationIds.add(detachedRelationship.getDestinationId());
        } else if (Concepts.INFERRED_RELATIONSHIP.equals(detachedRelationship.getCharacteristicTypeId())) {
            inferredSourceIds.add(detachedRelationship.getSourceId());
            inferredDestinationIds.add(detachedRelationship.getDestinationId());
        }
    });
    staging.getRemovedObjects(SnomedRefSetMemberIndexEntry.class).filter(detachedMember -> SnomedRefSetType.OWL_AXIOM == detachedMember.getReferenceSetType()).forEach(detachedOwlMember -> {
        collectIds(statedSourceIds, statedDestinationIds, detachedOwlMember, expressionConverter);
    });
    final LongSet statedConceptIds = PrimitiveSets.newLongOpenHashSet();
    final LongSet inferredConceptIds = PrimitiveSets.newLongOpenHashSet();
    if (!statedDestinationIds.isEmpty()) {
        for (SnomedConceptDocument statedDestinationConcept : index.get(SnomedConceptDocument.class, statedDestinationIds)) {
            statedConceptIds.add(Long.parseLong(statedDestinationConcept.getId()));
            if (statedDestinationConcept.getStatedParents() != null) {
                statedConceptIds.addAll(statedDestinationConcept.getStatedParents());
            }
            if (statedDestinationConcept.getStatedAncestors() != null) {
                statedConceptIds.addAll(statedDestinationConcept.getStatedAncestors());
            }
        }
    }
    if (!inferredDestinationIds.isEmpty()) {
        for (SnomedConceptDocument inferredDestinationConcept : index.get(SnomedConceptDocument.class, inferredDestinationIds)) {
            inferredConceptIds.add(Long.parseLong(inferredDestinationConcept.getId()));
            if (inferredDestinationConcept.getParents() != null) {
                inferredConceptIds.addAll(inferredDestinationConcept.getParents());
            }
            if (inferredDestinationConcept.getAncestors() != null) {
                inferredConceptIds.addAll(inferredDestinationConcept.getAncestors());
            }
        }
    }
    staging.getRemovedObjects(SnomedDescriptionIndexEntry.class).forEach(removedDescription -> {
        if (removedDescription.isFsn() && removedDescription.isActive()) {
            statedSourceIds.add(removedDescription.getConceptId());
            inferredSourceIds.add(removedDescription.getConceptId());
        }
    });
    staging.getChangedRevisions(SnomedDescriptionIndexEntry.class).filter(diff -> ((SnomedDescriptionIndexEntry) diff.newRevision).isFsn()).filter(diff -> diff.hasRevisionPropertyChanges(ACTIVE_AND_TERM_FIELDS)).forEach(diff -> {
        SnomedDescriptionIndexEntry newRevision = (SnomedDescriptionIndexEntry) diff.newRevision;
        statedSourceIds.add(newRevision.getConceptId());
        inferredSourceIds.add(newRevision.getConceptId());
    });
    staging.getNewObjects(SnomedDescriptionIndexEntry.class).filter(newDescription -> newDescription.isFsn() && newDescription.isActive()).forEach(newDescription -> {
        statedSourceIds.add(newDescription.getConceptId());
        inferredSourceIds.add(newDescription.getConceptId());
    });
    if (!statedSourceIds.isEmpty()) {
        final Query<SnomedConceptDocument> statedSourceConceptsQuery = Query.select(SnomedConceptDocument.class).where(Expressions.builder().should(SnomedConceptDocument.Expressions.ids(statedSourceIds)).should(SnomedConceptDocument.Expressions.statedParents(statedSourceIds)).should(SnomedConceptDocument.Expressions.statedAncestors(statedSourceIds)).build()).limit(Integer.MAX_VALUE).build();
        for (SnomedConceptDocument statedSourceConcept : index.search(statedSourceConceptsQuery)) {
            statedConceptIds.add(Long.parseLong(statedSourceConcept.getId()));
            if (statedSourceConcept.getStatedParents() != null) {
                statedConceptIds.addAll(statedSourceConcept.getStatedParents());
            }
            if (statedSourceConcept.getStatedAncestors() != null) {
                statedConceptIds.addAll(statedSourceConcept.getStatedAncestors());
            }
        }
    }
    if (!inferredSourceIds.isEmpty()) {
        final Query<SnomedConceptDocument> inferredSourceConceptsQuery = Query.select(SnomedConceptDocument.class).where(Expressions.builder().should(SnomedConceptDocument.Expressions.ids(inferredSourceIds)).should(SnomedConceptDocument.Expressions.parents(inferredSourceIds)).should(SnomedConceptDocument.Expressions.ancestors(inferredSourceIds)).build()).limit(Integer.MAX_VALUE).build();
        for (SnomedConceptDocument inferredSourceConcept : index.search(inferredSourceConceptsQuery)) {
            inferredConceptIds.add(Long.parseLong(inferredSourceConcept.getId()));
            if (inferredSourceConcept.getParents() != null) {
                inferredConceptIds.addAll(inferredSourceConcept.getParents());
            }
            if (inferredSourceConcept.getAncestors() != null) {
                inferredConceptIds.addAll(inferredSourceConcept.getAncestors());
            }
        }
    }
    staging.getNewObjects(SnomedConceptDocument.class).forEach(newConcept -> {
        long longId = Long.parseLong(newConcept.getId());
        statedConceptIds.add(longId);
        inferredConceptIds.add(longId);
    });
    // collect all reactivated concepts for the taxonomy to properly re-register them in the tree even if they don't carry stated/inferred information in this commit, but they have something in the index
    staging.getChangedRevisions(SnomedConceptDocument.class, Set.of(SnomedRf2Headers.FIELD_ACTIVE)).forEach(diff -> {
        RevisionPropertyDiff propertyDiff = diff.getRevisionPropertyDiff(SnomedRf2Headers.FIELD_ACTIVE);
        if ("false".equals(propertyDiff.getOldValue()) && "true".equals(propertyDiff.getNewValue())) {
            long longId = Long.parseLong(diff.newRevision.getId());
            statedConceptIds.add(longId);
            inferredConceptIds.add(longId);
        }
    });
    log.trace("Retrieving taxonomic information from store...");
    final boolean checkCycles = !(context instanceof Rf2TransactionContext);
    final Taxonomy inferredTaxonomy = Taxonomies.inferred(index, expressionConverter, staging, inferredConceptIds, checkCycles);
    final Taxonomy statedTaxonomy = Taxonomies.stated(index, expressionConverter, staging, statedConceptIds, checkCycles);
    // XXX change processor execution order is important!!!
    return List.of(// those values will be used in the ConceptChangeProcessor for example to properly compute the preferredDescriptions derived field
    new DescriptionChangeProcessor(), new ConceptChangeProcessor(DoiDataProvider.INSTANCE, SnomedIconProvider.INSTANCE.getAvailableIconIds(), statedTaxonomy, inferredTaxonomy), new RelationshipChangeProcessor());
}
Also used : BranchRequest(com.b2international.snowowl.core.request.BranchRequest) SnomedIconProvider(com.b2international.snowowl.snomed.icons.SnomedIconProvider) Query(com.b2international.index.query.Query) ClassUtils(com.b2international.commons.ClassUtils) RevisionBranch(com.b2international.index.revision.RevisionBranch) BaseRepositoryPreCommitHook(com.b2international.snowowl.core.repository.BaseRepositoryPreCommitHook) SnomedOWLExpressionConverterResult(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverterResult) Rf2ImportConfiguration(com.b2international.snowowl.snomed.datastore.request.rf2.importer.Rf2ImportConfiguration) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) PrimitiveSets(com.b2international.collections.PrimitiveSets) LongSet(com.b2international.collections.longs.LongSet) ImmutableList(com.google.common.collect.ImmutableList) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) com.b2international.snowowl.snomed.datastore.index.entry(com.b2international.snowowl.snomed.datastore.index.entry) CompareUtils(com.b2international.commons.CompareUtils) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff) Rf2ReleaseType(com.b2international.snowowl.snomed.core.domain.Rf2ReleaseType) Logger(org.slf4j.Logger) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) Collection(java.util.Collection) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) Set(java.util.Set) IOException(java.io.IOException) Taxonomy(com.b2international.snowowl.snomed.datastore.taxonomy.Taxonomy) Sets(com.google.common.collect.Sets) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) Stream(java.util.stream.Stream) Expressions(com.b2international.index.query.Expressions) Taxonomies(com.b2international.snowowl.snomed.datastore.taxonomy.Taxonomies) Rf2TransactionContext(com.b2international.snowowl.snomed.datastore.request.rf2.importer.Rf2TransactionContext) StagingArea(com.b2international.index.revision.StagingArea) ChangeSetProcessor(com.b2international.snowowl.core.repository.ChangeSetProcessor) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) Collections(java.util.Collections) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) RepositoryContext(com.b2international.snowowl.core.domain.RepositoryContext) Taxonomy(com.b2international.snowowl.snomed.datastore.taxonomy.Taxonomy) LongSet(com.b2international.collections.longs.LongSet) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff) Rf2TransactionContext(com.b2international.snowowl.snomed.datastore.request.rf2.importer.Rf2TransactionContext) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) RevisionSearcher(com.b2international.index.revision.RevisionSearcher)

Aggregations

Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)10 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)8 Collectors (java.util.stream.Collectors)8 IOException (java.io.IOException)7 LongSets (com.b2international.commons.collect.LongSets)6 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)6 StagingArea (com.b2international.index.revision.StagingArea)5 SnomedConceptDocument (com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument)5 SnomedDescriptionIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry)5 com.google.common.collect (com.google.common.collect)5 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)5 java.util (java.util)5 Collection (java.util.Collection)5 Set (java.util.Set)5 CompareUtils (com.b2international.commons.CompareUtils)4 PrimitiveSets (com.b2international.collections.PrimitiveSets)3 LongIterator (com.b2international.collections.longs.LongIterator)3 ObjectId (com.b2international.index.revision.ObjectId)3 Revision (com.b2international.index.revision.Revision)3 RevisionDiff (com.b2international.index.revision.StagingArea.RevisionDiff)3