Search in sources :

Example 21 with SnomedRefSetMemberIndexEntry

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry in project snow-owl by b2ihealthcare.

the class SnomedConcreteDomainMemberCreateDelegate method execute.

@Override
public String execute(SnomedReferenceSet refSet, TransactionContext context) {
    checkRefSetType(refSet, SnomedRefSetType.CONCRETE_DATA_TYPE);
    checkReferencedComponent(refSet);
    checkNonEmptyProperty(SnomedRf2Headers.FIELD_VALUE);
    checkNonEmptyProperty(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP);
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_MODULE_ID, getModuleId());
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_REFERENCED_COMPONENT_ID, getReferencedComponentId());
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_TYPE_ID);
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID);
    DataType dataType = refSet.getDataType();
    String value = getProperty(SnomedRf2Headers.FIELD_VALUE);
    try {
        SnomedRefSetUtil.deserializeValue(dataType, value);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Couldn't deserialize value '%s' for data type '%s'.", value, dataType);
    }
    SnomedRefSetMemberIndexEntry member = SnomedComponents.newConcreteDomainReferenceSetMember().withId(getId()).withActive(isActive()).withCharacteristicTypeId(getComponentId(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)).withGroup(getProperty(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP, Integer.class)).withModuleId(getModuleId()).withReferencedComponent(getReferencedComponentId()).withRefSet(getReferenceSetId()).withSerializedValue(getProperty(SnomedRf2Headers.FIELD_VALUE)).withTypeId(getComponentId(SnomedRf2Headers.FIELD_TYPE_ID)).addTo(context);
    return member.getId();
}
Also used : SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) DataType(com.b2international.snowowl.snomed.core.domain.refset.DataType) BadRequestException(com.b2international.commons.exceptions.BadRequestException)

Example 22 with SnomedRefSetMemberIndexEntry

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry in project snow-owl by b2ihealthcare.

the class EvaluateQueryRefSetMemberRequest method execute.

@Override
public QueryRefSetMemberEvaluation execute(BranchContext context) {
    // TODO support pre-population???
    final boolean active;
    final String query;
    final String targetReferenceSet;
    if (context instanceof TransactionContext) {
        SnomedRefSetMemberIndexEntry member = ((TransactionContext) context).lookup(memberId, SnomedRefSetMemberIndexEntry.class);
        query = member.getQuery();
        targetReferenceSet = member.getReferencedComponentId();
        active = member.isActive();
    } else {
        final SnomedReferenceSetMember member = SnomedRequests.prepareGetMember(memberId).build().execute(context);
        query = (String) member.getProperties().get(SnomedRf2Headers.FIELD_QUERY);
        targetReferenceSet = member.getReferencedComponent().getId();
        active = member.isActive();
    }
    if (!active) {
        return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, Collections.emptyList());
    }
    if (Strings.isNullOrEmpty(query)) {
        return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, Collections.emptyList());
    }
    // add all matching first
    final Map<String, SnomedConcept> conceptsToAdd = newHashMap();
    // GET matching members of a query
    SnomedRequests.prepareSearchConcept().filterByEcl(query).setLimit(10_000).stream(context).flatMap(SnomedConcepts::stream).forEach(match -> conceptsToAdd.put(match.getId(), match));
    final Collection<SnomedReferenceSetMember> membersToRemove = newHashSet();
    final Collection<SnomedReferenceSetMember> conceptsToActivate = newHashSet();
    // then re-evaluate all current members of the target simple type reference set
    SnomedRequests.prepareSearchMember().filterByRefSet(targetReferenceSet).setLimit(10_000).stream(context).flatMap(SnomedReferenceSetMembers::stream).forEach(member -> {
        final String referencedComponentId = member.getReferencedComponent().getId();
        if (conceptsToAdd.containsKey(referencedComponentId)) {
            if (!member.isActive()) {
                conceptsToAdd.remove(referencedComponentId);
                conceptsToActivate.add(member);
            } else {
                conceptsToAdd.remove(referencedComponentId);
            }
        } else {
            if (member.isActive()) {
                membersToRemove.add(member);
            }
        }
    });
    // fetch all referenced components
    final Set<String> referencedConceptIds = newHashSet();
    referencedConceptIds.addAll(conceptsToAdd.keySet());
    referencedConceptIds.addAll(FluentIterable.from(membersToRemove).transform(SnomedReferenceSetMember::getReferencedComponent).transform(IComponent::getId).toSet());
    referencedConceptIds.addAll(FluentIterable.from(conceptsToActivate).transform(SnomedReferenceSetMember::getReferencedComponent).transform(IComponent::getId).toSet());
    final Map<String, SnomedConcept> concepts;
    if (expand().containsKey("referencedComponent")) {
        final Options expandOptions = expand().getOptions("referencedComponent");
        concepts = Maps.uniqueIndex(SnomedRequests.prepareSearchConcept().filterByIds(referencedConceptIds).setLimit(referencedConceptIds.size()).setExpand(expandOptions.getOptions("expand")).setLocales(locales()).build().execute(context), IComponent::getId);
    } else {
        // initialize with empty SnomedConcept resources
        concepts = newHashMap();
        for (String referencedConceptId : referencedConceptIds) {
            concepts.put(referencedConceptId, new SnomedConcept(referencedConceptId));
        }
    }
    final Collection<MemberChange> changes = newArrayList();
    for (String id : conceptsToAdd.keySet()) {
        changes.add(MemberChangeImpl.added(concepts.get(id)));
    }
    for (SnomedReferenceSetMember memberToRemove : membersToRemove) {
        changes.add(MemberChangeImpl.removed(concepts.get(memberToRemove.getReferencedComponent().getId()), memberToRemove.getId()));
    }
    for (SnomedReferenceSetMember conceptToActivate : conceptsToActivate) {
        changes.add(MemberChangeImpl.changed(concepts.get(conceptToActivate.getReferencedComponent().getId()), conceptToActivate.getId()));
    }
    return new QueryRefSetMemberEvaluationImpl(memberId, targetReferenceSet, changes);
}
Also used : Options(com.b2international.commons.options.Options) IComponent(com.b2international.snowowl.core.domain.IComponent) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext)

Example 23 with SnomedRefSetMemberIndexEntry

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry in project snow-owl by b2ihealthcare.

the class SnomedAssociationTargetUpdateRequest method doExecute.

@Override
protected void doExecute(TransactionContext context, SnomedComponentDocument componentToUpdate) {
    final List<SnomedReferenceSetMember> existingMembers = newArrayList(SnomedRequests.prepareSearchMember().all().filterByReferencedComponent(componentToUpdate.getId()).filterByRefSet("<" + Concepts.REFSET_ASSOCIATION_TYPE).build().execute(context));
    final Multimap<String, String> newAssociationTargetsToCreate = HashMultimap.create(newAssociationTargets);
    final ModuleIdProvider moduleIdFunction = context.service(ModuleIdProvider.class);
    final Iterator<SnomedReferenceSetMember> memberIterator = existingMembers.iterator();
    while (memberIterator.hasNext()) {
        final SnomedReferenceSetMember existingMember = memberIterator.next();
        // existing historical association member
        final String associationReferenceSetId = existingMember.getRefsetId();
        final String existingTargetId = ((String) existingMember.getProperties().get(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID));
        if (newAssociationTargetsToCreate.remove(associationReferenceSetId, existingTargetId)) {
            // Exact match, just make sure that the member is active and remove it from the working list
            final Builder updatedMember = SnomedRefSetMemberIndexEntry.builder(existingMember);
            final SnomedRefSetMemberIndexEntry oldRevision = updatedMember.build();
            ensureMemberActive(context, existingMember, updatedMember);
            context.update(oldRevision, updatedMember.build());
            memberIterator.remove();
        }
    }
    /* 
		 * Remaining entries in the multimap have no exact match, but there might be other members with the 
		 * same association type; attempt re-using them, changing the association target in the process 
		 */
    for (final SnomedReferenceSetMember existingMember : existingMembers) {
        final String associationReferenceSetId = existingMember.getRefsetId();
        final Builder updatedMember = SnomedRefSetMemberIndexEntry.builder(existingMember);
        if (newAssociationTargetsToCreate.containsKey(associationReferenceSetId)) {
            // We can re-use the member by changing the target component identifier, and checking that it is active
            final Iterator<String> targetIterator = newAssociationTargetsToCreate.get(associationReferenceSetId).iterator();
            final String newTargetId = targetIterator.next();
            targetIterator.remove();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Changing association member {} with type {} and target component identifier from {} to {}.", existingMember.getId(), associationReferenceSetId, ((String) existingMember.getProperties().get(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID)), newTargetId);
            }
            // Change target component, set status to active if needed, place it in the supplied module
            SnomedRefSetMemberIndexEntry oldRevision = updatedMember.build();
            updatedMember.field(SnomedRf2Headers.FIELD_TARGET_COMPONENT_ID, newTargetId);
            ensureMemberActive(context, existingMember, updatedMember);
            updateModule(context, existingMember, updatedMember, moduleIdFunction.apply(componentToUpdate));
            unsetEffectiveTime(existingMember, updatedMember);
            context.update(oldRevision, updatedMember.build());
        } else {
            // We have no use for this member -- remove or inactivate if already released
            SnomedRefSetMemberIndexEntry oldRevision = updatedMember.build();
            if (removeOrDeactivate(context, existingMember, updatedMember)) {
                context.update(oldRevision, updatedMember.build());
            }
        }
    }
    /* 
		 * With all existing members processed, the last set of entries in the multimap will need 
		 * to be added as new members; defaultModuleId is only used if there is at least a single
		 * new entry. 
		 */
    for (final Entry<String, String> newAssociationEntry : newAssociationTargetsToCreate.entries()) {
        SnomedComponents.newAssociationMember().withRefSet(newAssociationEntry.getKey()).withTargetComponentId(newAssociationEntry.getValue()).withReferencedComponent(componentToUpdate.getId()).withModuleId(moduleIdFunction.apply(componentToUpdate)).addTo(context);
    }
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) ModuleIdProvider(com.b2international.snowowl.snomed.datastore.request.ModuleRequest.ModuleIdProvider) Builder(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry.Builder)

Example 24 with SnomedRefSetMemberIndexEntry

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry in project snow-owl by b2ihealthcare.

the class SnomedLanguageMemberCreateDelegate method execute.

@Override
public String execute(SnomedReferenceSet refSet, TransactionContext context) {
    checkRefSetType(refSet, SnomedRefSetType.LANGUAGE);
    checkReferencedComponent(refSet);
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_MODULE_ID, getModuleId());
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_REFERENCED_COMPONENT_ID, getReferencedComponentId());
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_ACCEPTABILITY_ID);
    SnomedRefSetMemberIndexEntry member = SnomedComponents.newLanguageMember().withId(getId()).withActive(isActive()).withReferencedComponent(getReferencedComponentId()).withModuleId(getModuleId()).withRefSet(getReferenceSetId()).withAcceptability(Acceptability.getByConceptId(getComponentId(SnomedRf2Headers.FIELD_ACCEPTABILITY_ID))).addTo(context);
    return member.getId();
}
Also used : SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)

Example 25 with SnomedRefSetMemberIndexEntry

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry in project snow-owl by b2ihealthcare.

the class SnomedMRCMAttributeRangeMemberCreateDelegate method execute.

@Override
public String execute(final SnomedReferenceSet refSet, final TransactionContext context) {
    checkRefSetType(refSet, SnomedRefSetType.MRCM_ATTRIBUTE_RANGE);
    checkReferencedComponent(refSet);
    checkNonEmptyProperty(SnomedRf2Headers.FIELD_MRCM_RANGE_CONSTRAINT);
    checkNonEmptyProperty(SnomedRf2Headers.FIELD_MRCM_ATTRIBUTE_RULE);
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_MODULE_ID, getModuleId());
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_REFERENCED_COMPONENT_ID, getReferencedComponentId());
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID, getProperty(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID));
    checkComponentExists(refSet, context, SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID, getProperty(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID));
    final SnomedRefSetMemberIndexEntry member = SnomedComponents.newMRCMAttributeRangeReferenceSetMember().withId(getId()).withActive(isActive()).withModuleId(getModuleId()).withRefSet(getReferenceSetId()).withReferencedComponent(getReferencedComponentId()).withRangeConstraint(getProperty(SnomedRf2Headers.FIELD_MRCM_RANGE_CONSTRAINT)).withAttributeRule(getProperty(SnomedRf2Headers.FIELD_MRCM_ATTRIBUTE_RULE)).withRuleStrengthId(getProperty(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID)).withContentTypeId(getProperty(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID)).addTo(context);
    return member.getId();
}
Also used : SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)

Aggregations

SnomedRefSetMemberIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)59 Test (org.junit.Test)29 Revision (com.b2international.index.revision.Revision)28 SnomedConceptDocument (com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument)19 SnomedDescriptionIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionIndexEntry)15 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)8 SnomedRelationshipIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry)8 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)7 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)7 IOException (java.io.IOException)7 CompareUtils (com.b2international.commons.CompareUtils)5 Expressions (com.b2international.index.query.Expressions)5 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)5 Query (com.b2international.index.query.Query)5 StagingArea (com.b2international.index.revision.StagingArea)5 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)5 SnomedDescriptionFragment (com.b2international.snowowl.snomed.datastore.index.entry.SnomedDescriptionFragment)5 Expressions.active (com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active)5 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)5 Collection (java.util.Collection)5