use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.
the class SnomedEclRefinementEvaluator method evalAxiomStatements.
static Set<Property> evalAxiomStatements(final BranchContext context, final boolean groupedRelationshipsOnly, final Collection<String> sourceIds, final Collection<String> typeIds, final Collection<String> destinationIds) {
try {
// search existing axioms (no values!) defined for the given set of conceptIds
ExpressionBuilder axiomFilter = Expressions.builder().filter(hasDestinationId());
if (typeIds != null) {
axiomFilter.filter(typeIds(typeIds));
}
if (destinationIds != null) {
axiomFilter.filter(destinationIds(destinationIds));
}
if (groupedRelationshipsOnly) {
axiomFilter.filter(relationshipGroup(1, Integer.MAX_VALUE));
}
ExpressionBuilder activeOwlAxiomMemberQuery = Expressions.builder().filter(active()).filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, axiomFilter.build()));
if (sourceIds != null) {
activeOwlAxiomMemberQuery.filter(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(sourceIds));
}
final Query<SnomedRefSetMemberIndexEntry> activeAxiomStatementsQuery = Query.select(SnomedRefSetMemberIndexEntry.class).where(activeOwlAxiomMemberQuery.build()).limit(Integer.MAX_VALUE).build();
return context.service(RevisionSearcher.class).search(activeAxiomStatementsQuery).stream().filter(owlMember -> !CompareUtils.isEmpty(owlMember.getClassAxiomRelationships())).flatMap(owlMember -> {
return owlMember.getClassAxiomRelationships().stream().filter(classAxiom -> {
return (typeIds == null || typeIds.contains(classAxiom.getTypeId())) && (destinationIds == null || destinationIds.contains(classAxiom.getDestinationId())) && (!groupedRelationshipsOnly || classAxiom.getRelationshipGroup() >= 1);
}).map(classAxiom -> {
return new Property(owlMember.getReferencedComponentId(), classAxiom.getTypeId(), classAxiom.getDestinationId(), classAxiom.getRelationshipGroup());
});
}).collect(Collectors.toSet());
} catch (IOException e) {
throw new SnowowlRuntimeException(e);
}
}
use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.
the class SnomedEclRefinementEvaluator method evalStatements.
/**
* Executes a SNOMED CT Relationship search request using the given source, type, destination filters.
* If the groupedRelationshipsOnly boolean flag is <code>true</code>, then the search will match relationships that are grouped (their groupId is greater than or equals to <code>1</code>).
* @param context - the context where the search should happen
* @param sourceFilter - filter for relationship sources
* @param typeFilter - filter for relationship types
* @param destinationFilter - filter for relationship destinations
* @param groupedRelationshipsOnly - whether the search should consider grouped relationships only or not
* @return a {@link Promise} of {@link Collection} of {@link Property} objects that match the criteria
* @see SnomedRelationshipSearchRequestBuilder
*/
/*package*/
static Promise<Collection<Property>> evalStatements(final BranchContext context, final Collection<String> sourceFilter, final Collection<String> typeFilter, final Collection<String> destinationFilter, final boolean groupedRelationshipsOnly, final String expressionForm) {
final ImmutableList.Builder<String> fieldsToLoad = ImmutableList.builder();
fieldsToLoad.add(SnomedDocument.Fields.ID, SOURCE_ID, TYPE_ID, DESTINATION_ID);
if (groupedRelationshipsOnly) {
fieldsToLoad.add(RELATIONSHIP_GROUP);
}
SnomedRelationshipSearchRequestBuilder searchRelationships = SnomedRequests.prepareSearchRelationship().filterByActive(true).filterBySources(sourceFilter).filterByTypes(typeFilter).filterByDestinations(destinationFilter).filterByCharacteristicTypes(getCharacteristicTypes(expressionForm)).setEclExpressionForm(expressionForm).setFields(fieldsToLoad.build()).setLimit(context.service(RepositoryConfiguration.class).getIndexConfiguration().getResultWindow());
// if a grouping refinement, then filter relationships with group >= 1
if (groupedRelationshipsOnly) {
searchRelationships.filterByGroup(1, Integer.MAX_VALUE);
}
Promise<Collection<Property>> relationshipSearch = searchRelationships.transformAsync(context, req -> req.build(context.service(ResourceURI.class)), relationships -> relationships.stream().map(r -> new Property(r.getSourceId(), r.getTypeId(), r.getDestinationId(), r.getRelationshipGroup())));
if (Trees.STATED_FORM.equals(expressionForm)) {
final Set<Property> axiomStatements = evalAxiomStatements(context, groupedRelationshipsOnly, sourceFilter, typeFilter, destinationFilter);
return relationshipSearch.then(relationshipStatements -> ImmutableSet.<Property>builder().addAll(relationshipStatements).addAll(axiomStatements).build());
} else {
return relationshipSearch;
}
}
use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.
the class SnomedEclRefinementEvaluator method evalAxiomsWithValue.
private Promise<Collection<Property>> evalAxiomsWithValue(BranchContext context, Set<String> focusConceptIds, Collection<String> typeIds, RelationshipValue value, SearchResourceRequest.Operator operator) {
// search existing axioms defined for the given set of conceptIds
ExpressionBuilder axiomFilter = Expressions.builder();
if (typeIds != null) {
axiomFilter.filter(typeIds(typeIds));
}
switch(operator) {
case EQUALS:
axiomFilter.filter(values(List.of(value)));
break;
case GREATER_THAN:
axiomFilter.filter(valueGreaterThan(value, false));
break;
case GREATER_THAN_EQUALS:
axiomFilter.filter(valueGreaterThan(value, true));
break;
case LESS_THAN:
axiomFilter.filter(valueLessThan(value, false));
break;
case LESS_THAN_EQUALS:
axiomFilter.filter(valueLessThan(value, true));
break;
case NOT_EQUALS:
axiomFilter.mustNot(values(List.of(value)));
break;
default:
throw new IllegalStateException("Unexpected operator '" + operator + "'.");
}
ExpressionBuilder activeOwlAxiomMemberQuery = Expressions.builder().filter(active()).filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, axiomFilter.build()));
if (focusConceptIds != null) {
activeOwlAxiomMemberQuery.filter(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(focusConceptIds));
}
final Query<SnomedRefSetMemberIndexEntry> activeAxiomStatementsQuery = Query.select(SnomedRefSetMemberIndexEntry.class).where(activeOwlAxiomMemberQuery.build()).limit(context.service(RepositoryConfiguration.class).getIndexConfiguration().getResultWindow()).build();
final Set<Property> axiomProperties = newHashSet();
context.service(RevisionSearcher.class).stream(activeAxiomStatementsQuery).forEach(chunk -> {
chunk.stream().filter(owlMember -> !CompareUtils.isEmpty(owlMember.getClassAxiomRelationships())).forEachOrdered(owlMember -> {
owlMember.getClassAxiomRelationships().stream().filter(r -> typeIds == null || typeIds.contains(r.getTypeId())).filter(r -> r.getValueAsObject().matches(operator, value)).map(r -> new Property(owlMember.getReferencedComponentId(), r.getTypeId(), r.getValueAsObject().toObject(), r.getRelationshipGroup())).forEachOrdered(axiomProperties::add);
});
});
return Promise.immediate(axiomProperties);
}
use of com.b2international.snowowl.core.domain.BranchContext in project snow-owl by b2ihealthcare.
the class SnomedEclRefinementEvaluator method evalRefinement.
/**
* Evaluates an {@link AttributeConstraint} refinement on the given focusConceptId set on the given {@link BranchContext}.
* Grouped parameter can
*/
private Promise<Collection<Property>> evalRefinement(final BranchContext context, final AttributeConstraint refinement, final boolean grouped, final Set<String> focusConceptIds) {
final Comparison comparison = refinement.getComparison();
final Collection<String> typeConceptFilter = evalToConceptIds(context, refinement.getAttribute(), expressionForm).getSync(1, TimeUnit.MINUTES);
if (comparison instanceof AttributeComparison) {
// resolve non-* focusConcept ECLs to IDs, so we can filter relationships by source/destination
// filterByType and filterByDestination accepts ECL expressions as well, so serialize them into ECL and pass as String when required
// if reversed refinement, then we are interested in the destinationIds otherwise we need the sourceIds
final Collection<String> destinationConceptFilter = evalToConceptIds(context, ((AttributeComparison) comparison).getValue(), expressionForm).getSync(1, TimeUnit.MINUTES);
final Collection<String> focusConceptFilter = refinement.isReversed() ? destinationConceptFilter : focusConceptIds;
final Collection<String> valueConceptFilter = refinement.isReversed() ? focusConceptIds : destinationConceptFilter;
return evalStatements(context, focusConceptFilter, typeConceptFilter, valueConceptFilter, grouped, expressionForm);
} else if (comparison instanceof DataTypeComparison) {
if (refinement.isReversed()) {
throw new BadRequestException("Reversed flag is not supported in data type based comparison (string/numeric)");
} else {
final Promise<Collection<Property>> statementsWithValue = evalStatementsWithValue(context, focusConceptIds, typeConceptFilter, (DataTypeComparison) comparison);
final Promise<Collection<Property>> members = evalMembers(context, focusConceptIds, typeConceptFilter, (DataTypeComparison) comparison);
return Promise.all(statementsWithValue, members).then(results -> {
final Collection<Property> s = (Collection<Property>) results.get(0);
final Collection<Property> m = (Collection<Property>) results.get(1);
return FluentIterable.concat(s, m).toSet();
});
}
} else {
return SnomedEclEvaluationRequest.throwUnsupported(comparison);
}
}
use of com.b2international.snowowl.core.domain.BranchContext 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);
}
}
}
Aggregations