use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class SnomedCompareRestRequestTest method testCompareWithChangedComponents.
@Test
public void testCompareWithChangedComponents() throws IOException {
final String newConceptId = createNewConcept(parentBranch);
final SnomedConcept concept = getComponent(parentBranch, SnomedComponentType.CONCEPT, newConceptId).extract().as(SnomedConcept.class);
final String parentBranchPath = parentBranch.toString();
final String childBranchPath = createBranch(parentBranchPath, "SNOMEDCT-CHANGED");
SnomedRequests.prepareUpdateConcept(concept.getId()).setModuleId(Concepts.MODULE_SCT_MODEL_COMPONENT).build(childBranchPath, RestExtensions.USER, "Change module ID").execute(bus).getSync();
final BranchCompareResult compareResult = getCompareResult(parentBranchPath, childBranchPath);
// compare child branch with it's parent
assertThat(compareResult.getNewComponents()).isEmpty();
assertThat(compareResult.getChangedComponents()).contains(ComponentIdentifier.of(SnomedConcept.TYPE, newConceptId));
assertThat(compareResult.getDeletedComponents()).isEmpty();
}
use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class SnomedConceptMapSearchRequestEvaluator method toCollectionResource.
private ConceptMapMappings toCollectionResource(SnomedReferenceSetMembers referenceSetMembers, ResourceURI uri, ServiceProvider context, Options search, SnomedDisplayTermType snomedDisplayTermType) {
final Set<String> refSetsToFetch = referenceSetMembers.stream().map(SnomedReferenceSetMember::getRefsetId).collect(Collectors.toSet());
final Map<String, SnomedConcept> refSetsById = SnomedRequests.prepareSearchConcept().all().filterByIds(refSetsToFetch).setLocales(search.getList(OptionKey.LOCALES, ExtendedLocale.class)).setExpand("pt(),referenceSet()").build(uri).execute(context.service(IEventBus.class)).getSync(1, TimeUnit.MINUTES).stream().collect(Collectors.toMap(SnomedConcept::getId, concept -> concept));
final Map<String, ComponentURI> targetComponentsByRefSetId = getTargetComponentsByRefSetId(context, refSetsById);
List<ConceptMapMapping> mappings = referenceSetMembers.stream().filter(m -> SnomedConcept.TYPE.equals(m.getReferencedComponent().getComponentType())).map(m -> {
return toMapping(m, uri, targetComponentsByRefSetId.get(m.getRefsetId()), snomedDisplayTermType, refSetsById);
}).collect(Collectors.toList());
if (!mappings.isEmpty()) {
final Map<String, Concept> conceptsById = Multimaps.index(mappings, mapping -> mapping.getTargetComponentURI().resourceUri()).asMap().entrySet().stream().filter(entry -> !TerminologyRegistry.UNSPECIFIED.equals(entry.getKey().getResourceId())).map(entry -> {
final Set<String> idsToFetch = entry.getValue().stream().map(map -> map.getTargetComponentURI().identifier()).collect(Collectors.toSet());
return CodeSystemRequests.prepareSearchConcepts().all().filterByCodeSystemUri(entry.getKey()).filterByIds(idsToFetch).buildAsync().execute(context.service(IEventBus.class)).getSync(5, TimeUnit.MINUTES).stream().collect(Collectors.toMap(Concept::getId, c -> c));
}).flatMap(map -> map.entrySet().stream()).collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (concept1, concept2) -> concept1));
mappings = mappings.stream().map(mapping -> {
final String mapTargetId = mapping.getTargetComponentURI().identifier();
if (conceptsById.containsKey(mapTargetId) && !mapping.getTargetComponentURI().isUnspecified()) {
final Concept concept = conceptsById.get(mapTargetId);
return mapping.toBuilder().targetTerm(concept.getTerm()).targetIconId(concept.getIconId()).build();
} else {
return mapping;
}
}).collect(Collectors.toList());
}
return new ConceptMapMappings(mappings, referenceSetMembers.getSearchAfter(), referenceSetMembers.getLimit(), referenceSetMembers.getTotal());
}
use of com.b2international.snowowl.snomed.core.domain.SnomedConcept 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);
}
use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class SnomedDescendantsExpander method expand.
@Override
protected void expand(List<SnomedConcept> results, final Set<String> conceptIds, Options descendantExpandOptions, boolean direct) {
try {
final int limit = getLimit(descendantExpandOptions);
final ExpressionBuilder expression = Expressions.builder();
expression.filter(active());
final ExpressionBuilder descendantFilter = Expressions.builder();
if (stated) {
descendantFilter.should(statedParents(conceptIds));
if (!direct) {
descendantFilter.should(statedAncestors(conceptIds));
}
} else {
descendantFilter.should(parents(conceptIds));
if (!direct) {
descendantFilter.should(ancestors(conceptIds));
}
}
expression.filter(descendantFilter.build());
final Query<SnomedConceptDocument> query = Query.select(SnomedConceptDocument.class).where(expression.build()).limit((conceptIds.size() == 1 && limit == 0) ? limit : Integer.MAX_VALUE).build();
final RevisionSearcher searcher = context().service(RevisionSearcher.class);
final Hits<SnomedConceptDocument> hits = searcher.search(query);
if (hits.getTotal() < 1) {
final SnomedConcepts descendants = new SnomedConcepts(0, 0);
for (SnomedConcept concept : results) {
if (stated) {
concept.setStatedDescendants(descendants);
} else {
concept.setDescendants(descendants);
}
}
return;
}
// XXX won't work if number of results is greater than one, either use custom ConceptSearch or figure out how to expand descendants effectively
if (conceptIds.size() == 1 && limit == 0) {
for (SnomedConcept concept : results) {
final SnomedConcepts descendants = new SnomedConcepts(0, hits.getTotal());
if (stated) {
concept.setStatedDescendants(descendants);
} else {
concept.setDescendants(descendants);
}
}
return;
}
final Multimap<String, String> descendantsByAncestor = TreeMultimap.create();
for (SnomedConceptDocument hit : hits) {
final Set<String> parentsAndAncestors = newHashSet();
if (stated) {
parentsAndAncestors.addAll(LongSets.toStringSet(hit.getStatedParents()));
if (!direct) {
parentsAndAncestors.addAll(LongSets.toStringSet(hit.getStatedAncestors()));
}
} else {
parentsAndAncestors.addAll(LongSets.toStringSet(hit.getParents()));
if (!direct) {
parentsAndAncestors.addAll(LongSets.toStringSet(hit.getAncestors()));
}
}
parentsAndAncestors.retainAll(conceptIds);
for (String ancestor : parentsAndAncestors) {
descendantsByAncestor.put(ancestor, hit.getId());
}
}
final Collection<String> componentIds = newHashSet(descendantsByAncestor.values());
if (limit > 0 && !componentIds.isEmpty()) {
// query descendants again
final SnomedConcepts descendants = SnomedRequests.prepareSearchConcept().all().filterByIds(componentIds).setLocales(locales()).setExpand(descendantExpandOptions.get("expand", Options.class)).build().execute(context());
final Map<String, SnomedConcept> descendantsById = newHashMap();
descendantsById.putAll(Maps.uniqueIndex(descendants, SnomedConcept::getId));
for (SnomedConcept concept : results) {
final Collection<String> descendantIds = descendantsByAncestor.get(concept.getId());
final List<SnomedConcept> currentDescendants = FluentIterable.from(descendantIds).limit(limit).transform(Functions.forMap(descendantsById)).toList();
final SnomedConcepts descendantConcepts = new SnomedConcepts(currentDescendants, null, limit, descendantIds.size());
if (stated) {
concept.setStatedDescendants(descendantConcepts);
} else {
concept.setDescendants(descendantConcepts);
}
}
} else {
for (SnomedConcept concept : results) {
final Collection<String> descendantIds = descendantsByAncestor.get(concept.getId());
final SnomedConcepts descendants = new SnomedConcepts(limit, descendantIds.size());
if (stated) {
concept.setStatedDescendants(descendants);
} else {
concept.setDescendants(descendants);
}
}
}
} catch (IOException e) {
throw SnowowlRuntimeException.wrap(e);
}
}
use of com.b2international.snowowl.snomed.core.domain.SnomedConcept in project snow-owl by b2ihealthcare.
the class SnomedRelationshipConverter method expandDestination.
private void expandDestination(List<SnomedRelationship> results) {
if (expand().containsKey(SnomedRelationship.Expand.DESTINATION)) {
final Options destinationOptions = expand().get(SnomedRelationship.Expand.DESTINATION, Options.class);
final Iterable<String> destinationConceptIds = FluentIterable.from(results).filter(// skip expand on relationships with value
r -> !r.hasValue()).transform(SnomedRelationship::getDestinationId);
context().service(SnomedConceptRequestCache.class).request(destinationConceptIds, destinationOptions.getOptions("expand"), locales(), destinationConceptsById -> {
for (SnomedRelationship relationship : results) {
final String destinationId = relationship.getDestinationId();
// containsKey handles any null values here
if (destinationConceptsById.containsKey(destinationId)) {
final SnomedConcept destinationConcept = destinationConceptsById.get(destinationId);
((SnomedRelationship) relationship).setDestination(destinationConcept);
}
}
});
}
}
Aggregations