Search in sources :

Example 1 with StagingArea

use of com.b2international.index.revision.StagingArea in project snow-owl by b2ihealthcare.

the class SnomedEclEvaluationRequestTest method generateHierarchy.

/**
 * Generates the following test fixtures:
 * <ul>
 * 	<li>Substances (children of SUBSTANCE):
 * 		<ul>
 * 			<li>INGREDIENT1 (ingredient with three inbound from PANADOL and ABACAVIR TABLET)</li>
 * 			<li>INGREDIENT2 (ingredient with five inbound from TRIPHASIL AND PANADOL AND ABACAVIR TABLET)</li>
 * 		</ul>
 * 	</li>
 * 	<li>Drugs (children of DRUG_ROOT):
 * 		<ul>
 * 			<li>ABACAVIR_TABLET (drug with two outgoing inferred relationships, one HAS_BOSS and one HAS_TRADE_NAME relationship to INGREDIENT1)</li>
 * 			<li>PANADOL_TABLET (drug with three outgoing inferred relationships, one HAI to INGREDIENT1, one HAS_BOSS to INGREDIENT 2 and one HAS_TRADE_NAME to INGREDIENT2)</li>
 * 			<li>TRIPHASIL_TABLET (drug with three outgoing inferred relationships, one HAI, one HAS_BOSS and one HAS_TRADE_NAME to INGREDIENT2)</li>
 * 		</ul>
 * 	</li>
 * </ul>
 */
private void generateHierarchy() {
    StagingArea staging = index().prepareCommit(MAIN).stageNew(concept(INGREDIENT1).parents(SUBSTANCEL).statedParents(SUBSTANCEL).build()).stageNew(concept(INGREDIENT2).parents(SUBSTANCEL).statedParents(SUBSTANCEL).build()).stageNew(concept(ABACAVIR_TABLET).parents(DRUG_ROOTL).statedParents(DRUG_ROOTL).build()).stageNew(concept(PANADOL_TABLET).parents(DRUG_ROOTL).statedParents(DRUG_ROOTL).build()).stageNew(concept(TRIPHASIL_TABLET).parents(DRUG_ROOTL).statedParents(DRUG_ROOTL).build());
    if (isAxiom()) {
        staging.stageNew(classAxioms(PANADOL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT1, 1, HAS_BOSS, INGREDIENT2, 1, HAS_TRADE_NAME, INGREDIENT2, 1).build());
        staging.stageNew(classAxioms(ABACAVIR_TABLET, HAS_BOSS, INGREDIENT1, 1, HAS_TRADE_NAME, INGREDIENT1, 1).build());
        staging.stageNew(classAxioms(TRIPHASIL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT2, 2, HAS_BOSS, INGREDIENT2, 2, HAS_TRADE_NAME, INGREDIENT2, 1).build());
    } else {
        staging.stageNew(relationship(PANADOL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT1, getCharacteristicType()).relationshipGroup(1).build()).stageNew(relationship(PANADOL_TABLET, HAS_BOSS, INGREDIENT2, getCharacteristicType()).relationshipGroup(1).build()).stageNew(relationship(PANADOL_TABLET, HAS_TRADE_NAME, INGREDIENT2, getCharacteristicType()).relationshipGroup(1).build()).stageNew(relationship(ABACAVIR_TABLET, HAS_BOSS, INGREDIENT1, getCharacteristicType()).relationshipGroup(1).build()).stageNew(relationship(ABACAVIR_TABLET, HAS_TRADE_NAME, INGREDIENT1, getCharacteristicType()).relationshipGroup(1).build());
        staging.stageNew(relationship(TRIPHASIL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT2, getCharacteristicType()).relationshipGroup(2).build()).stageNew(relationship(TRIPHASIL_TABLET, HAS_BOSS, INGREDIENT2, getCharacteristicType()).relationshipGroup(2).build()).stageNew(relationship(TRIPHASIL_TABLET, HAS_TRADE_NAME, INGREDIENT2, getCharacteristicType()).relationshipGroup(1).build());
    }
    staging.commit(currentTime(), "test", "Generate hierarchy");
}
Also used : StagingArea(com.b2international.index.revision.StagingArea)

Example 2 with StagingArea

use of com.b2international.index.revision.StagingArea in project snow-owl by b2ihealthcare.

the class Rf2TransactionContext method add.

void add(Collection<SnomedComponent> componentChanges, Multimap<Class<? extends SnomedDocument>, String> dependenciesByType) {
    final Multimap<Class<? extends SnomedDocument>, SnomedComponent> componentChangesByType = Multimaps.index(componentChanges, this::getDocType);
    for (Class<? extends SnomedDocument> type : IMPORT_ORDER) {
        final Collection<SnomedComponent> rf2Components = componentChangesByType.get(type);
        final Set<String> componentsToLookup = rf2Components.stream().map(IComponent::getId).collect(Collectors.toSet());
        // add all dependencies with the same type
        componentsToLookup.addAll(dependenciesByType.get(type));
        final Map<String, ? extends SnomedDocument> existingComponents = lookup(componentsToLookup, type);
        final Map<String, SnomedConceptDocument> existingRefSets;
        if (SnomedRefSetMemberIndexEntry.class == type) {
            existingRefSets = lookup(rf2Components.stream().map(member -> ((SnomedReferenceSetMember) member).getRefsetId()).collect(Collectors.toSet()), SnomedConceptDocument.class);
        } else {
            existingRefSets = Collections.emptyMap();
        }
        final Set<String> newRefSetIds = newHashSet();
        // seed missing component before applying row changes
        // and check for existing components with the same or greater effective time and skip them
        final Collection<SnomedComponent> componentsToImport = newArrayList();
        for (SnomedComponent rf2Component : rf2Components) {
            SnomedDocument existingObject = existingComponents.get(rf2Component.getId());
            if (existingObject == null) {
                // new component, add to new components and register row for import
                newComponents.put(rf2Component.getId(), createIdOnlyDoc(rf2Component.getId(), type));
                componentsToImport.add(rf2Component);
            } else if (existingObject instanceof SnomedDocument && rf2Component instanceof SnomedComponent) {
                final SnomedComponent rf2Row = (SnomedComponent) rf2Component;
                final SnomedDocument existingRow = (SnomedDocument) existingObject;
                if (rf2Row.getEffectiveTime() == null || EffectiveTimes.getEffectiveTime(rf2Row.getEffectiveTime()) > existingRow.getEffectiveTime()) {
                    componentsToImport.add(rf2Component);
                }
            }
            // check and register refset props on concept docs
            if (rf2Component instanceof SnomedReferenceSetMember) {
                final SnomedReferenceSetMember member = (SnomedReferenceSetMember) rf2Component;
                // seed the refset if missing
                final String refSetId = member.getRefsetId();
                SnomedConceptDocument conceptDocToUpdate = existingRefSets.get(refSetId);
                if (conceptDocToUpdate == null || newComponents.containsKey(refSetId)) {
                    conceptDocToUpdate = (SnomedConceptDocument) newComponents.get(refSetId);
                }
                if (conceptDocToUpdate.getRefSetType() == null) {
                    final String referencedComponentType = SnomedComponent.getType(member.getReferencedComponentId());
                    String mapTargetComponentType = TerminologyRegistry.UNKNOWN_COMPONENT_TYPE;
                    try {
                        mapTargetComponentType = SnomedComponent.getType((String) member.getProperties().get(SnomedRf2Headers.FIELD_MAP_TARGET));
                    } catch (IllegalArgumentException e) {
                    // ignored
                    }
                    final SnomedReferenceSet refSet = new SnomedReferenceSet();
                    refSet.setType(member.type());
                    refSet.setReferencedComponentType(referencedComponentType);
                    refSet.setMapTargetComponentType(mapTargetComponentType);
                    final SnomedConceptDocument updatedConcept = SnomedConceptDocument.builder(conceptDocToUpdate).refSet(refSet).build();
                    if (newComponents.containsKey(refSetId)) {
                        newComponents.put(refSetId, updatedConcept);
                        newRefSetIds.add(refSetId);
                    } else {
                        update(conceptDocToUpdate, updatedConcept);
                    }
                }
            }
        }
        // apply row changes
        for (SnomedComponent rf2Component : componentsToImport) {
            final String id = rf2Component.getId();
            SnomedDocument existingRevision = null;
            SnomedDocument.Builder<?, ?> newRevision;
            if (newComponents.containsKey(id)) {
                newRevision = createDocBuilder(id, type, newComponents.get(id));
            } else if (existingComponents.containsKey(id)) {
                existingRevision = existingComponents.get(id);
                newRevision = createDocBuilder(id, type, existingRevision);
            } else {
                throw new IllegalStateException(String.format("Current revision is null for %s", id));
            }
            final SnomedComponentBuilder builder;
            if (rf2Component instanceof SnomedCoreComponent) {
                builder = prepareCoreComponent(rf2Component);
            } else if (rf2Component instanceof SnomedReferenceSetMember) {
                builder = prepareMember((SnomedReferenceSetMember) rf2Component);
            } else {
                throw new UnsupportedOperationException("Unsupported component: " + rf2Component);
            }
            // apply row changes
            builder.init(newRevision, this);
            if (existingRevision == null) {
                // in this case the component is new, and the default values are okay to use
                add(newRevision.build());
            } else {
                // in this case, recalculate the released flag based on the currently available revision
                if (existingRevision.isReleased()) {
                    update(existingRevision, newRevision.released(existingRevision.isReleased()).build());
                } else {
                    update(existingRevision, newRevision.build());
                }
            }
        }
        // make sure we always attach refset properties to identifier concepts
        final StagingArea staging = service(StagingArea.class);
        for (String newRefSetId : newRefSetIds) {
            SnomedConceptDocument newRefSet = (SnomedConceptDocument) newComponents.get(newRefSetId);
            SnomedConceptDocument stagedNewRefSet = (SnomedConceptDocument) staging.getNewObject(SnomedConceptDocument.class, newRefSetId);
            if (newRefSet != null && stagedNewRefSet != null) {
                if (stagedNewRefSet.getRefSetType() == null) {
                    add(SnomedConceptDocument.builder(stagedNewRefSet).refSetType(newRefSet.getRefSetType()).referencedComponentType(newRefSet.getReferencedComponentType()).mapTargetComponentType(newRefSet.getMapTargetComponentType()).build());
                }
            }
        }
    }
}
Also used : EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) SnomedComponentBuilder(com.b2international.snowowl.snomed.core.store.SnomedComponentBuilder) com.b2international.snowowl.snomed.core.domain(com.b2international.snowowl.snomed.core.domain) Multimap(com.google.common.collect.Multimap) Multimaps(com.google.common.collect.Multimaps) ImmutableList(com.google.common.collect.ImmutableList) com.b2international.snowowl.snomed.datastore.index.entry(com.b2international.snowowl.snomed.datastore.index.entry) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) ComponentNotFoundException(com.b2international.snowowl.core.exceptions.ComponentNotFoundException) ISnomedIdentifierService(com.b2international.snowowl.snomed.cis.ISnomedIdentifierService) SnomedComponents(com.b2international.snowowl.snomed.core.store.SnomedComponents) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Commit(com.b2international.index.revision.Commit) SnomedMemberBuilder(com.b2international.snowowl.snomed.core.store.SnomedMemberBuilder) Collectors(java.util.stream.Collectors) TerminologyRegistry(com.b2international.snowowl.core.terminology.TerminologyRegistry) RepositoryTransactionContext(com.b2international.snowowl.core.repository.RepositoryTransactionContext) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) LocalDate(java.time.LocalDate) StagingArea(com.b2international.index.revision.StagingArea) DelegatingTransactionContext(com.b2international.snowowl.core.domain.DelegatingTransactionContext) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet) StagingArea(com.b2international.index.revision.StagingArea) SnomedComponentBuilder(com.b2international.snowowl.snomed.core.store.SnomedComponentBuilder) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedReferenceSet(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSet)

Example 3 with StagingArea

use of com.b2international.index.revision.StagingArea in project snow-owl by b2ihealthcare.

the class Taxonomies method updateTaxonomy.

private static TaxonomyGraphStatus updateTaxonomy(RevisionSearcher searcher, SnomedOWLExpressionConverter expressionConverter, StagingArea staging, TaxonomyGraph graphToUpdate, String characteristicTypeId) throws IOException {
    LOGGER.trace("Processing changes taxonomic information.");
    staging.getNewObjects(SnomedRelationshipIndexEntry.class).filter(relationship -> characteristicTypeId.equals(relationship.getCharacteristicTypeId())).forEach(newRelationship -> updateEdge(newRelationship, graphToUpdate));
    final Set<String> relationshipsToExcludeFromReactivatedConcepts = newHashSet();
    staging.getChangedRevisions(SnomedRelationshipIndexEntry.class).map(diff -> (SnomedRelationshipIndexEntry) diff.newRevision).filter(relationship -> characteristicTypeId.equals(relationship.getCharacteristicTypeId())).forEach(dirtyRelationship -> {
        relationshipsToExcludeFromReactivatedConcepts.add(dirtyRelationship.getId());
        updateEdge(dirtyRelationship, graphToUpdate);
    });
    staging.getRemovedObjects(SnomedRelationshipIndexEntry.class).filter(relationship -> characteristicTypeId.equals(relationship.getCharacteristicTypeId())).forEach(relationship -> {
        relationshipsToExcludeFromReactivatedConcepts.add(relationship.getId());
        graphToUpdate.removeEdge(relationship.getId());
    });
    if (Concepts.STATED_RELATIONSHIP.equals(characteristicTypeId)) {
        staging.getNewObjects(SnomedRefSetMemberIndexEntry.class).filter(member -> SnomedRefSetType.OWL_AXIOM == member.getReferenceSetType()).forEach(member -> updateEdge(member, graphToUpdate, expressionConverter));
        staging.getChangedRevisions(SnomedRefSetMemberIndexEntry.class).map(diff -> (SnomedRefSetMemberIndexEntry) diff.newRevision).filter(member -> SnomedRefSetType.OWL_AXIOM == member.getReferenceSetType()).forEach(member -> updateEdge(member, graphToUpdate, expressionConverter));
        staging.getRemovedObjects(SnomedRefSetMemberIndexEntry.class).filter(member -> SnomedRefSetType.OWL_AXIOM == member.getReferenceSetType()).map(SnomedRefSetMemberIndexEntry::getId).forEach(graphToUpdate::removeEdge);
    }
    staging.getNewObjects(SnomedConceptDocument.class).forEach(newConcept -> updateConcept(newConcept, graphToUpdate));
    staging.getRemovedObjects(SnomedConceptDocument.class).forEach(concept -> graphToUpdate.removeNode(concept.getId()));
    final Set<String> conceptWithPossibleMissingRelationships = newHashSet();
    staging.getChangedRevisions(SnomedConceptDocument.class, Collections.singleton(SnomedConceptDocument.Fields.ACTIVE)).forEach(diff -> {
        final RevisionPropertyDiff propDiff = diff.getRevisionPropertyDiff(SnomedConceptDocument.Fields.ACTIVE);
        final boolean oldValue = Boolean.parseBoolean(propDiff.getOldValue());
        final boolean newValue = Boolean.parseBoolean(propDiff.getNewValue());
        final String conceptId = diff.newRevision.getId();
        if (!oldValue && newValue) {
            // make sure the node is part of the new tree
            graphToUpdate.addNode(conceptId);
            conceptWithPossibleMissingRelationships.add(conceptId);
        }
    });
    if (!conceptWithPossibleMissingRelationships.isEmpty()) {
        Hits<String[]> possibleMissingRelationships = searcher.search(Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.DESTINATION_ID).where(Expressions.builder().filter(SnomedRelationshipIndexEntry.Expressions.active()).filter(SnomedRelationshipIndexEntry.Expressions.characteristicTypeId(characteristicTypeId)).filter(SnomedRelationshipIndexEntry.Expressions.typeId(Concepts.IS_A)).filter(SnomedRelationshipIndexEntry.Expressions.sourceIds(conceptWithPossibleMissingRelationships)).mustNot(SnomedRelationshipIndexEntry.Expressions.ids(relationshipsToExcludeFromReactivatedConcepts)).build()).limit(Integer.MAX_VALUE).build());
        for (String[] relationship : possibleMissingRelationships) {
            graphToUpdate.addNode(relationship[2]);
            graphToUpdate.addEdge(relationship[0], Long.parseLong(relationship[1]), new long[] { Long.parseLong(relationship[2]) });
        }
    }
    LOGGER.trace("Rebuilding taxonomic information based on the changes.");
    return graphToUpdate.update();
}
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) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff)

Example 4 with StagingArea

use of com.b2international.index.revision.StagingArea 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)

Example 5 with StagingArea

use of com.b2international.index.revision.StagingArea in project snow-owl by b2ihealthcare.

the class SnomedEclEvaluationRequestTest method generateDrugHierarchy.

/**
 * Generates the following test fixtures:
 * <ul>
 * 	<li>Substances (children of SUBSTANCE):
 * 		<ul>
 * 			<li>INGREDIENT1 (ingredient with two inbound from PANADOL and TRIPHASIL TABLET)</li>
 * 			<li>INGREDIENT2 (ingredient with one inbound from TRIPHASIL TABLET)</li>
 * 			<li>INGREDIENT3 (ingredient without any inbound HAI relationships)</li>
 * 		</ul>
 * 	</li>
 * 	<li>Drugs (children of DRUG_ROOT):
 * 		<ul>
 * 			<li>ABACAVIR_TABLET (drug without any outgoing HAI relationships)</li>
 * 			<li>PANADOL_TABLET (drug with a single outgoing inferred HAI relationship to INGREDIENT1)</li>
 * 			<li>TRIPHASIL_TABLET (drug with two outgoing inferred HAI relationships, to INGREDIENT1 and 2, and one HAS_BOSS to INGREDIENT2)</li>
 * 			<li>AMOXICILLIN_TABLET (drug with one outgoing stated HAI relationship to INGREDIENT1)</li>
 * 		</ul>
 * 	</li>
 * </ul>
 */
private void generateDrugHierarchy() {
    StagingArea staging = index().prepareCommit(MAIN).stageNew(concept(INGREDIENT1).parents(SUBSTANCEL).statedParents(SUBSTANCEL).build()).stageNew(concept(INGREDIENT2).parents(SUBSTANCEL).statedParents(SUBSTANCEL).build()).stageNew(concept(INGREDIENT3).parents(SUBSTANCEL).statedParents(SUBSTANCEL).build()).stageNew(concept(ABACAVIR_TABLET).parents(DRUG_ROOTL).statedParents(DRUG_ROOTL).build()).stageNew(concept(PANADOL_TABLET).parents(DRUG_ROOTL).statedParents(DRUG_ROOTL).build()).stageNew(concept(TRIPHASIL_TABLET).parents(DRUG_ROOTL).statedParents(DRUG_ROOTL).build()).stageNew(concept(AMOXICILLIN_TABLET).parents(DRUG_ROOTL).statedParents(DRUG_ROOTL).build());
    // has active ingredient relationships
    if (isAxiom()) {
        staging.stageNew(classAxioms(PANADOL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT1, 0).build());
        staging.stageNew(classAxioms(TRIPHASIL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT1, 0, HAS_ACTIVE_INGREDIENT, INGREDIENT2, 0, HAS_BOSS, INGREDIENT2, 0).build());
    } else {
        staging.stageNew(relationship(PANADOL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT1, getCharacteristicType()).relationshipGroup(0).build()).stageNew(relationship(TRIPHASIL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT1, getCharacteristicType()).relationshipGroup(0).build()).stageNew(relationship(TRIPHASIL_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT2, getCharacteristicType()).relationshipGroup(0).build()).stageNew(relationship(TRIPHASIL_TABLET, HAS_BOSS, INGREDIENT2, getCharacteristicType()).relationshipGroup(0).build());
    }
    // XXX: This relationship's characteristicType setting is here for a reason so in ecl searches we won't find this
    staging.stageNew(relationship(AMOXICILLIN_TABLET, HAS_ACTIVE_INGREDIENT, INGREDIENT1, getCharacteristicType()).relationshipGroup(0).characteristicTypeId(// inverse!
    isInferred() ? Concepts.STATED_RELATIONSHIP : Concepts.INFERRED_RELATIONSHIP).build());
    if (isStatementsWithValue()) {
        if (isAxiom()) {
            staging.stageNew(classAxiomsWithValue(PANADOL_TABLET, HAS_TRADE_NAME, "PANADOL", 0, PREFERRED_STRENGTH, 500, 0).build()).stageNew(classAxiomsWithValue(TRIPHASIL_TABLET, HAS_TRADE_NAME, "TRIPHASIL", 0, PREFERRED_STRENGTH, -500, 0).build()).stageNew(classAxiomsWithValue(AMOXICILLIN_TABLET, HAS_TRADE_NAME, "AMOXICILLIN", 0, PREFERRED_STRENGTH, BigDecimal.valueOf(5.5d), 0).build()).stageNew(classAxiomsWithValue(ABACAVIR_TABLET, PREFERRED_STRENGTH, BigDecimal.valueOf(-5.5d), 0).build());
        } else {
            staging.stageNew(stringValue(PANADOL_TABLET, HAS_TRADE_NAME, "PANADOL", getCharacteristicType()).build()).stageNew(stringValue(TRIPHASIL_TABLET, HAS_TRADE_NAME, "TRIPHASIL", getCharacteristicType()).build()).stageNew(stringValue(AMOXICILLIN_TABLET, HAS_TRADE_NAME, "AMOXICILLIN", getCharacteristicType()).build()).stageNew(integerValue(PANADOL_TABLET, PREFERRED_STRENGTH, 500, getCharacteristicType()).build()).stageNew(integerValue(TRIPHASIL_TABLET, PREFERRED_STRENGTH, -500, getCharacteristicType()).build()).stageNew(decimalValue(AMOXICILLIN_TABLET, PREFERRED_STRENGTH, BigDecimal.valueOf(5.5d), getCharacteristicType()).build()).stageNew(decimalValue(ABACAVIR_TABLET, PREFERRED_STRENGTH, BigDecimal.valueOf(-5.5d), getCharacteristicType()).build());
        // XXX: manufactured flags are not indexed as relationships do not support boolean values
        }
    } else {
        staging.stageNew(stringMember(PANADOL_TABLET, HAS_TRADE_NAME, "PANADOL", getCharacteristicType()).build()).stageNew(stringMember(TRIPHASIL_TABLET, HAS_TRADE_NAME, "TRIPHASIL", getCharacteristicType()).build()).stageNew(stringMember(AMOXICILLIN_TABLET, HAS_TRADE_NAME, "AMOXICILLIN", getCharacteristicType()).build()).stageNew(integerMember(PANADOL_TABLET, PREFERRED_STRENGTH, 500, getCharacteristicType()).build()).stageNew(integerMember(TRIPHASIL_TABLET, PREFERRED_STRENGTH, -500, getCharacteristicType()).build()).stageNew(decimalMember(AMOXICILLIN_TABLET, PREFERRED_STRENGTH, BigDecimal.valueOf(5.5d), getCharacteristicType()).build()).stageNew(decimalMember(ABACAVIR_TABLET, PREFERRED_STRENGTH, BigDecimal.valueOf(-5.5d), getCharacteristicType()).build()).stageNew(booleanMember(PANADOL_TABLET, MANUFACTURED, true, getCharacteristicType()).build()).stageNew(booleanMember(TRIPHASIL_TABLET, MANUFACTURED, true, getCharacteristicType()).build()).stageNew(booleanMember(AMOXICILLIN_TABLET, MANUFACTURED, false, getCharacteristicType()).build());
    }
    staging.commit(currentTime(), UUID.randomUUID().toString(), "Initialize generated drugs");
}
Also used : StagingArea(com.b2international.index.revision.StagingArea)

Aggregations

StagingArea (com.b2international.index.revision.StagingArea)15 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)10 IOException (java.io.IOException)10 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)7 Collectors (java.util.stream.Collectors)7 ChangeSetProcessorBase (com.b2international.snowowl.core.repository.ChangeSetProcessorBase)6 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)6 RefSetMemberChange (com.b2international.snowowl.snomed.datastore.index.refset.RefSetMemberChange)6 Set (java.util.Set)6 Collection (java.util.Collection)5 Revision (com.b2international.index.revision.Revision)4 RevisionPropertyDiff (com.b2international.index.revision.StagingArea.RevisionPropertyDiff)4 Acceptability (com.b2international.snowowl.snomed.core.domain.Acceptability)4 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)4 com.b2international.snowowl.snomed.datastore.index.entry (com.b2international.snowowl.snomed.datastore.index.entry)4 SnomedRefSetMemberIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)4 ReferenceSetMembershipUpdater (com.b2international.snowowl.snomed.datastore.index.update.ReferenceSetMembershipUpdater)4 com.google.common.collect (com.google.common.collect)4 Multimap (com.google.common.collect.Multimap)4 Map (java.util.Map)4