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;
}
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();
}
}
}
}
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();
}
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);
}
Aggregations