Search in sources :

Example 21 with SnomedReferenceSetMember

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.

the class EclExpression method resolveToGroupedOnly.

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

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

Example 22 with SnomedReferenceSetMember

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.

the class SnomedVersioningRequest method adjustDependencyRefSetMembers.

private void adjustDependencyRefSetMembers(TransactionContext context, Multimap<String, String> moduleDependencies, Map<String, Long> moduleToLatestEffectiveTime, long effectiveTime) {
    // Update existing, add new members to moduleDependencyRefSet
    if (!CompareUtils.isEmpty(moduleDependencies)) {
        moduleDependencies.entries().forEach((entry) -> {
            final String source = entry.getKey();
            final String target = entry.getValue();
            final SnomedReferenceSetMember existingLatestMember = SnomedRequests.prepareSearchMember().all().filterByModule(source).filterByReferencedComponent(target).filterByRefSet(Concepts.REFSET_MODULE_DEPENDENCY_TYPE).build().execute(context).stream().sorted((o1, o2) -> {
                if (null == o1.getEffectiveTime() && null == o2.getEffectiveTime()) {
                    return 0;
                } else if (null == o1.getEffectiveTime() && null != o2.getEffectiveTime()) {
                    return 1;
                } else if (null != o1.getEffectiveTime() && null == o2.getEffectiveTime()) {
                    return -1;
                }
                return o1.getEffectiveTime().compareTo(o2.getEffectiveTime());
            }).reduce((first, second) -> second).orElse(null);
            final SnomedRefSetMemberIndexEntry.Builder updatedMember;
            if (existingLatestMember == null) {
                updatedMember = SnomedRefSetMemberIndexEntry.builder().id(UUID.randomUUID().toString()).active(true).refsetId(Concepts.REFSET_MODULE_DEPENDENCY_TYPE).referenceSetType(SnomedRefSetType.MODULE_DEPENDENCY).moduleId(source).referencedComponentId(target);
            } else {
                updatedMember = SnomedRefSetMemberIndexEntry.builder(existingLatestMember);
            }
            final long targetEffectiveTime = moduleToLatestEffectiveTime.get(target) == EffectiveTimes.UNSET_EFFECTIVE_TIME ? effectiveTime : moduleToLatestEffectiveTime.get(target);
            updatedMember.released(true).effectiveTime(effectiveTime).field(SnomedRf2Headers.FIELD_SOURCE_EFFECTIVE_TIME, effectiveTime).field(SnomedRf2Headers.FIELD_TARGET_EFFECTIVE_TIME, targetEffectiveTime);
            if (existingLatestMember == null) {
                context.add(updatedMember.build());
            } else {
                context.update(SnomedRefSetMemberIndexEntry.builder(existingLatestMember).build(), updatedMember.build());
            }
        });
    }
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) EffectiveTimes(com.b2international.snowowl.core.date.EffectiveTimes) java.util(java.util) Logger(org.slf4j.Logger) Query(com.b2international.index.query.Query) Hits(com.b2international.index.Hits) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) VersioningConfiguration(com.b2international.snowowl.core.request.version.VersioningConfiguration) VersioningRequest(com.b2international.snowowl.core.request.version.VersioningRequest) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Stream(java.util.stream.Stream) TransactionContext(com.b2international.snowowl.core.domain.TransactionContext) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) com.b2international.snowowl.snomed.datastore.index.entry(com.b2international.snowowl.snomed.datastore.index.entry) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) SnomedIdentifiers(com.b2international.snowowl.snomed.cis.SnomedIdentifiers) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) CompareUtils(com.b2international.commons.CompareUtils) com.google.common.collect(com.google.common.collect) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)

Example 23 with SnomedReferenceSetMember

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.

the class ClassificationJobRequest method buildTaxonomy.

private ReasonerTaxonomy buildTaxonomy(final RevisionSearcher revisionSearcher, final Set<String> excludedModuleIds, final boolean concreteDomainSupported) {
    final ReasonerTaxonomyBuilder taxonomyBuilder = new ReasonerTaxonomyBuilder(excludedModuleIds);
    taxonomyBuilder.addActiveConceptIds(revisionSearcher);
    taxonomyBuilder.addActiveConceptIds(additionalConcepts.stream());
    taxonomyBuilder.finishConcepts();
    taxonomyBuilder.addConceptFlags(revisionSearcher);
    taxonomyBuilder.addActiveStatedEdges(revisionSearcher);
    taxonomyBuilder.addActiveStatedRelationships(revisionSearcher);
    taxonomyBuilder.addActiveInferredRelationships(revisionSearcher);
    taxonomyBuilder.addActiveAdditionalGroupedRelationships(revisionSearcher);
    taxonomyBuilder.addNeverGroupedTypeIds(revisionSearcher);
    taxonomyBuilder.addActiveAxioms(revisionSearcher);
    if (concreteDomainSupported) {
        taxonomyBuilder.addActiveConcreteDomainMembers(revisionSearcher);
    }
    // Add the extra definitions
    taxonomyBuilder.addConceptFlags(additionalConcepts.stream());
    final Supplier<Stream<SnomedRelationship>> relationshipSupplier = () -> additionalConcepts.stream().flatMap(c -> c.getRelationships().stream());
    taxonomyBuilder.addActiveStatedEdges(relationshipSupplier.get());
    taxonomyBuilder.addActiveStatedRelationships(relationshipSupplier.get());
    taxonomyBuilder.addActiveInferredRelationships(relationshipSupplier.get());
    taxonomyBuilder.addActiveAdditionalGroupedRelationships(relationshipSupplier.get());
    if (concreteDomainSupported) {
        final Stream<SnomedReferenceSetMember> conceptMembers = additionalConcepts.stream().flatMap(c -> c.getMembers().stream());
        taxonomyBuilder.addActiveConcreteDomainMembers(conceptMembers);
    }
    return taxonomyBuilder.build();
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) ReasonerTaxonomyBuilder(com.b2international.snowowl.snomed.datastore.index.taxonomy.ReasonerTaxonomyBuilder) Stream(java.util.stream.Stream)

Example 24 with SnomedReferenceSetMember

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.

the class SnomedReferenceSetMemberConverter method toResource.

@Override
protected SnomedReferenceSetMember toResource(SnomedRefSetMemberIndexEntry entry) {
    final SnomedReferenceSetMember member = new SnomedReferenceSetMember();
    member.setId(entry.getId());
    member.setEffectiveTime(toEffectiveTime(entry.getEffectiveTime()));
    member.setReleased(entry.isReleased());
    member.setActive(entry.isActive());
    member.setModuleId(entry.getModuleId());
    member.setIconId(entry.getIconId());
    member.setRefsetId(entry.getRefsetId());
    member.setType(entry.getReferenceSetType());
    member.setScore(entry.getScore());
    final Map<String, Object> props = newHashMap(entry.getAdditionalFields());
    // convert stored long values to short date format
    props.computeIfPresent(SnomedRf2Headers.FIELD_SOURCE_EFFECTIVE_TIME, (key, currentValue) -> toEffectiveTime((long) currentValue));
    props.computeIfPresent(SnomedRf2Headers.FIELD_TARGET_EFFECTIVE_TIME, (key, currentValue) -> toEffectiveTime((long) currentValue));
    // convert concrete domain value to serialized String format
    if (entry.getValue() != null) {
        props.put(SnomedRf2Headers.FIELD_VALUE, SnomedRefSetUtil.serializeValue(entry.getDataType(), entry.getValue()));
    }
    member.setProperties(props);
    String owlExpression = entry.getOwlExpression();
    if (Concepts.REFSET_OWL_AXIOM.equals(entry.getRefsetId()) && expand().containsKey("owlRelationships") && !Strings.isNullOrEmpty(owlExpression)) {
        if (!CompareUtils.isEmpty(entry.getClassAxiomRelationships())) {
            if (owlExpression.startsWith("EquivalentClasses")) {
                member.setEquivalentOWLRelationships(entry.getClassAxiomRelationships());
            } else {
                member.setClassOWLRelationships(entry.getClassAxiomRelationships());
            }
        } else if (!CompareUtils.isEmpty(entry.getGciAxiomRelationships())) {
            member.setGciOWLRelationships(entry.getGciAxiomRelationships());
        }
    }
    setReferencedComponent(member, entry.getReferencedComponentId(), entry.getReferencedComponentType());
    return member;
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)

Example 25 with SnomedReferenceSetMember

use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.

the class SnomedReferenceSetMemberConverter method expandComponentCategory.

private void expandComponentCategory(Options expandOptions, Multimap<String, SnomedReferenceSetMember> referencedComponentIdToMemberMap, Multimap<ComponentCategory, String> componentCategoryToIdMap, ComponentCategory category) {
    final Collection<String> componentIds = componentCategoryToIdMap.get(category);
    final SearchResourceRequestBuilder<?, BranchContext, ? extends CollectionResource<? extends SnomedCoreComponent>> search;
    switch(category) {
        case CONCEPT:
            search = SnomedRequests.prepareSearchConcept();
            break;
        case DESCRIPTION:
            search = SnomedRequests.prepareSearchDescription();
            break;
        case RELATIONSHIP:
            search = SnomedRequests.prepareSearchRelationship();
            break;
        default:
            throw new UnsupportedOperationException("Category is not supported in referenced component expansion");
    }
    search.filterByIds(componentIds).setLimit(componentIds.size()).setLocales(locales()).setExpand(expandOptions.get("expand", Options.class));
    CollectionResource<? extends SnomedCoreComponent> components = search.build().execute(context());
    for (SnomedCoreComponent component : components) {
        for (SnomedReferenceSetMember member : referencedComponentIdToMemberMap.get(component.getId())) {
            ((SnomedReferenceSetMember) member).setReferencedComponent(component);
        }
    }
}
Also used : SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) Options(com.b2international.commons.options.Options) SnomedCoreComponent(com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent) BranchContext(com.b2international.snowowl.core.domain.BranchContext)

Aggregations

SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)41 Test (org.junit.Test)17 AbstractSnomedApiTest (com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)13 Json (com.b2international.commons.json.Json)12 SnomedRf2Headers (com.b2international.snowowl.snomed.common.SnomedRf2Headers)11 SnomedReferenceSetMembers (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers)11 Options (com.b2international.commons.options.Options)8 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)8 SnomedDescription (com.b2international.snowowl.snomed.core.domain.SnomedDescription)8 List (java.util.List)8 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)7 Map (java.util.Map)7 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)6 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)5 BranchContext (com.b2international.snowowl.core.domain.BranchContext)5 TransactionContext (com.b2international.snowowl.core.domain.TransactionContext)5 ComponentCategory (com.b2international.snowowl.core.terminology.ComponentCategory)5 Collectors (java.util.stream.Collectors)5 BranchPathUtils (com.b2international.snowowl.core.branch.BranchPathUtils)4 EffectiveTimes (com.b2international.snowowl.core.date.EffectiveTimes)4