use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class SnomedConceptSearchRequestEvaluator method evaluate.
@Override
public Concepts evaluate(ResourceURI uri, ServiceProvider context, Options search) {
final String preferredDisplay = search.getString(OptionKey.DISPLAY);
SnomedDisplayTermType displayTermType;
if (preferredDisplay != null) {
displayTermType = SnomedDisplayTermType.getEnum(preferredDisplay);
} else {
displayTermType = SnomedDisplayTermType.PT;
}
final SnomedConceptSearchRequestBuilder req = SnomedRequests.prepareSearchConcept();
evaluateTermFilterOptions(req, search);
if (search.containsKey(OptionKey.ID)) {
req.filterByIds(search.getCollection(OptionKey.ID, String.class));
}
if (search.containsKey(OptionKey.ACTIVE)) {
req.filterByActive(search.getBoolean(OptionKey.ACTIVE));
}
if (search.containsKey(OptionKey.PARENT)) {
req.filterByParents(search.getCollection(OptionKey.PARENT, String.class));
}
if (search.containsKey(OptionKey.ANCESTOR)) {
req.filterByAncestors(search.getCollection(OptionKey.ANCESTOR, String.class));
}
if (search.containsKey(OptionKey.TERM_TYPE)) {
req.filterByDescriptionType(search.getString(OptionKey.TERM_TYPE));
}
if (search.containsKey(OptionKey.QUERY) || search.containsKey(OptionKey.MUST_NOT_QUERY)) {
StringBuilder query = new StringBuilder();
if (search.containsKey(OptionKey.QUERY)) {
query.append("(").append(Ecl.or(search.getCollection(OptionKey.QUERY, String.class))).append(")");
} else {
query.append(Ecl.ANY);
}
if (search.containsKey(OptionKey.MUST_NOT_QUERY)) {
query.append(" MINUS (").append(Ecl.or(search.getCollection(OptionKey.MUST_NOT_QUERY, String.class))).append(")");
}
req.filterByEcl(query.toString());
}
boolean requestedExpand = search.containsKey(OptionKey.EXPAND);
// make sure preferredDescriptions() and displayTermType expansion data are always loaded
Options expand = ExpandParser.parse("preferredDescriptions()").merge(requestedExpand ? search.getOptions(OptionKey.EXPAND) : Options.empty());
if (!Strings.isNullOrEmpty(displayTermType.getExpand())) {
expand = ExpandParser.parse(displayTermType.getExpand()).merge(expand);
}
SnomedConcepts matches = req.setLocales(search.getList(OptionKey.LOCALES, ExtendedLocale.class)).setSearchAfter(search.getString(OptionKey.AFTER)).setLimit(search.get(OptionKey.LIMIT, Integer.class)).setFields(search.getList(OptionKey.FIELDS, String.class)).setExpand(expand).sortBy(search.containsKey(SearchResourceRequest.OptionKey.SORT_BY) ? search.getList(SearchResourceRequest.OptionKey.SORT_BY, SearchResourceRequest.Sort.class) : null).build(uri).execute(context);
return new Concepts(matches.stream().map(concept -> toConcept(uri, concept, displayTermType.getLabel(concept), requestedExpand)).collect(Collectors.toList()), matches.getSearchAfter(), matches.getLimit(), matches.getTotal());
}
use of com.b2international.commons.options.Options 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.commons.options.Options in project snow-owl by b2ihealthcare.
the class SnomedReferenceSetConverter method expandMembers.
private void expandMembers(List<SnomedReferenceSet> results) {
if (expand().containsKey("members")) {
Options expandOptions = expand().get("members", Options.class);
for (SnomedReferenceSet refSet : results) {
SnomedRefSetMemberSearchRequestBuilder req = SnomedRequests.prepareSearchMember().filterByRefSet(refSet.getId()).setLocales(locales()).setExpand(expandOptions.get("expand", Options.class));
if (expandOptions.containsKey("limit")) {
req.setLimit(expandOptions.get("limit", Integer.class));
}
refSet.setMembers(req.build().execute(context()));
}
}
}
use of com.b2international.commons.options.Options in project snow-owl by b2ihealthcare.
the class SnomedRelationshipConverter method expandModifier.
private void expandModifier(List<SnomedRelationship> results) {
if (expand().containsKey(SnomedRelationship.Expand.MODIFIER)) {
final Options modifierOptions = expand().get(SnomedRelationship.Expand.MODIFIER, Options.class);
final Iterable<String> modifierIds = FluentIterable.from(results).transform(SnomedRelationship::getModifierId);
context().service(SnomedConceptRequestCache.class).request(modifierIds, modifierOptions.getOptions("expand"), locales(), modifiersById -> {
for (SnomedRelationship relationship : results) {
((SnomedRelationship) relationship).setModifier(modifiersById.get(relationship.getModifierId()));
}
});
}
}
use of com.b2international.commons.options.Options 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