Search in sources :

Example 1 with LongCollection

use of com.b2international.collections.longs.LongCollection in project snow-owl by b2ihealthcare.

the class LongSets method transform.

/**
 * Transforms the given {@link LongCollection} into a collection of object based on the {@link InverseLongFunction function} argument.
 * @param fromCollection the collection of primitive long values to transform.
 * @param function the function for the transformation.
 * @return the transformed collection of long values.
 */
public static <T> Collection<T> transform(final LongCollection fromCollection, final InverseLongFunction<? extends T> function) {
    checkNotNull(fromCollection, "fromCollection");
    checkNotNull(function, "function");
    @SuppressWarnings("unchecked") final Collection<T> toCollection = (Collection<T>) (fromCollection instanceof LongSet ? Sets.newHashSetWithExpectedSize(fromCollection.size()) : Lists.newArrayListWithExpectedSize(fromCollection.size()));
    for (final LongIterator itr = fromCollection.iterator(); itr.hasNext(); ) /**/
    {
        toCollection.add(function.apply(itr.next()));
    }
    return toCollection;
}
Also used : LongSet(com.b2international.collections.longs.LongSet) LongCollection(com.b2international.collections.longs.LongCollection) Collection(java.util.Collection) AbstractLongIterator(com.b2international.collections.longs.AbstractLongIterator) LongIterator(com.b2international.collections.longs.LongIterator)

Example 2 with LongCollection

use of com.b2international.collections.longs.LongCollection in project snow-owl by b2ihealthcare.

the class LongTarjan method visit.

private void visit(final long currentId, final LongCollection ids) {
    indexMap.put(currentId, index);
    lowLinkMap.put(currentId, index);
    index++;
    idStack.add(currentId);
    final LongCollection followers = getFollowers.apply(currentId);
    for (final LongIterator itr = followers.iterator(); itr.hasNext(); ) /* empty */
    {
        final long currentFollowerId = itr.next();
        if (!indexMap.containsKey(currentFollowerId)) {
            continue;
        }
        if (indexMap.get(currentFollowerId) == -1) {
            visit(currentFollowerId, ids);
            final int newLowLink = Math.min(lowLinkMap.get(currentId), lowLinkMap.get(currentFollowerId));
            lowLinkMap.put(currentId, newLowLink);
        } else if (idStack.contains(currentFollowerId)) {
            final int newLowLink = Math.min(lowLinkMap.get(currentId), indexMap.get(currentFollowerId));
            lowLinkMap.put(currentId, newLowLink);
        }
    }
    if (lowLinkMap.get(currentId) == indexMap.get(currentId)) {
        long sccMember = removeLast();
        if (currentId == sccMember) {
            addToCurrent(sccMember);
            if (currentSize() >= batchSize) {
                flushBatch();
            }
        } else {
            addToCurrent(sccMember);
            do {
                sccMember = removeLast();
                addToCurrent(sccMember);
            } while (currentId != sccMember);
            if (currentSize() >= batchSize) {
                flushBatch();
            }
        }
    }
}
Also used : LongCollection(com.b2international.collections.longs.LongCollection) LongIterator(com.b2international.collections.longs.LongIterator)

Example 3 with LongCollection

use of com.b2international.collections.longs.LongCollection 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 4 with LongCollection

use of com.b2international.collections.longs.LongCollection in project snow-owl by b2ihealthcare.

the class TreeBuilderImpl method build.

@Override
public TerminologyTree build(final ResourceURI resource, final Iterable<SnomedConcept> nodes, final String snomedDescriptionExpand) {
    final Collection<SnomedConcept> topLevelConcepts = this.topLevelConcepts == null ? getDefaultTopLevelConcepts(resource, snomedDescriptionExpand) : this.topLevelConcepts;
    final Map<String, SnomedConcept> treeItemsById = newHashMap();
    // all matching concepts should be in the componentMap
    treeItemsById.putAll(FluentIterable.from(nodes).uniqueIndex(IComponent::getId));
    final Collection<String> requiredTopLevelConceptIds = topLevelConcepts.stream().map(IComponent::getId).collect(Collectors.toSet());
    // compute subType and superType maps for the tree
    final SetMultimap<String, String> superTypeMap = HashMultimap.create();
    final SetMultimap<String, String> subTypeMap = HashMultimap.create();
    for (SnomedConcept entry : nodes) {
        final LongCollection parentIds = getParents(entry);
        final LongCollection ancestorIds = getAncestors(entry);
        if (parentIds != null) {
            final Collection<String> parents = LongSets.toStringSet(parentIds);
            final Collection<String> selectedParents = newHashSet();
            // if the parent is not a match or TOP level
            for (String parent : parents) {
                if (treeItemsById.containsKey(parent) || requiredTopLevelConceptIds.contains(parent)) {
                    selectedParents.add(parent);
                }
            }
            if (selectedParents.isEmpty()) {
                findParentInAncestors(entry, treeItemsById, requiredTopLevelConceptIds, subTypeMap, superTypeMap);
            } else {
                for (String parent : selectedParents) {
                    subTypeMap.put(parent, entry.getId());
                    superTypeMap.put(entry.getId(), parent);
                }
            }
        } else if (ancestorIds != null) {
            findParentInAncestors(entry, treeItemsById, requiredTopLevelConceptIds, subTypeMap, superTypeMap);
        } else {
            // no parents or ancestors, root element
            subTypeMap.put(null, entry.getId());
        }
    }
    // add TOP levels
    for (SnomedConcept entry : topLevelConcepts) {
        if (!Concepts.ROOT_CONCEPT.equals(entry.getId()) && !treeItemsById.containsKey(entry.getId())) {
            if (subTypeMap.containsKey(entry.getId())) {
                treeItemsById.put(entry.getId(), entry);
            }
        }
    }
    for (SnomedConcept entry : topLevelConcepts) {
        if (Concepts.ROOT_CONCEPT.equals(entry.getId())) {
            // find all top level child and connect them with the root
            for (SnomedConcept tl : topLevelConcepts) {
                if (!Concepts.ROOT_CONCEPT.equals(tl.getId()) && treeItemsById.containsKey(tl.getId())) {
                    subTypeMap.put(entry.getId(), tl.getId());
                    superTypeMap.put(tl.getId(), entry.getId());
                }
            }
            // only add root concept if the tree contains top level concepts
            if (subTypeMap.containsKey(Concepts.ROOT_CONCEPT)) {
                treeItemsById.put(entry.getId(), entry);
                subTypeMap.put(null, entry.getId());
            }
            break;
        }
    }
    // fetch all missing components to build the remaining part of the FULL tree
    final Set<String> allRequiredComponents = newHashSet();
    allRequiredComponents.addAll(superTypeMap.keySet());
    allRequiredComponents.addAll(subTypeMap.keySet());
    allRequiredComponents.removeAll(treeItemsById.keySet());
    allRequiredComponents.remove(null);
    // fetch required data for all unknown items
    for (SnomedConcept entry : getComponents(resource, allRequiredComponents, snomedDescriptionExpand)) {
        treeItemsById.put(entry.getId(), entry);
    }
    return new TerminologyTree(treeItemsById, subTypeMap, superTypeMap);
}
Also used : LongCollection(com.b2international.collections.longs.LongCollection) SnomedConcept(com.b2international.snowowl.snomed.core.domain.SnomedConcept)

Aggregations

LongCollection (com.b2international.collections.longs.LongCollection)4 LongIterator (com.b2international.collections.longs.LongIterator)3 Collection (java.util.Collection)2 AbstractLongIterator (com.b2international.collections.longs.AbstractLongIterator)1 LongCollections (com.b2international.collections.longs.LongCollections)1 LongSet (com.b2international.collections.longs.LongSet)1 CompareUtils (com.b2international.commons.CompareUtils)1 LongSets (com.b2international.commons.collect.LongSets)1 Hits (com.b2international.index.Hits)1 Expressions (com.b2international.index.query.Expressions)1 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)1 Query (com.b2international.index.query.Query)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 StagingArea (com.b2international.index.revision.StagingArea)1 RevisionPropertyDiff (com.b2international.index.revision.StagingArea.RevisionPropertyDiff)1 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)1 IComponent (com.b2international.snowowl.core.domain.IComponent)1 Concepts (com.b2international.snowowl.snomed.common.SnomedConstants.Concepts)1 SnomedConcept (com.b2international.snowowl.snomed.core.domain.SnomedConcept)1 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)1