Search in sources :

Example 11 with RelationshipValue

use of com.b2international.snowowl.snomed.core.domain.RelationshipValue 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);
}
Also used : SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) PolymorphicDispatcher(org.eclipse.xtext.util.PolymorphicDispatcher) Query(com.b2international.index.query.Query) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) Promise(com.b2international.snowowl.core.events.util.Promise) ID(com.b2international.index.revision.Revision.Fields.ID) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) SnomedDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument) SnomedRequests(com.b2international.snowowl.snomed.datastore.request.SnomedRequests) Options(com.b2international.commons.options.Options) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) com.b2international.snomed.ecl.ecl(com.b2international.snomed.ecl.ecl) RepositoryConfiguration(com.b2international.snowowl.core.config.RepositoryConfiguration) CompareUtils(com.b2international.commons.CompareUtils) com.google.common.collect(com.google.common.collect) ResourceURI(com.b2international.snowowl.core.ResourceURI) BadRequestException(com.b2international.commons.exceptions.BadRequestException) Function(com.google.common.base.Function) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) Collection(java.util.Collection) SnomedRelationshipSearchRequestBuilder(com.b2international.snowowl.snomed.datastore.request.SnomedRelationshipSearchRequestBuilder) DataType(com.b2international.snowowl.snomed.core.domain.refset.DataType) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) Trees(com.b2international.snowowl.snomed.core.tree.Trees) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Expressions(com.b2international.index.query.Expressions) Expressions.active(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active) Entry(java.util.Map.Entry) Expression(com.b2international.index.query.Expression) SnomedRf2Headers(com.b2international.snowowl.snomed.common.SnomedRf2Headers) BranchContext(com.b2international.snowowl.core.domain.BranchContext) Fields(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Fields) Expressions(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 12 with RelationshipValue

use of com.b2international.snowowl.snomed.core.domain.RelationshipValue in project snow-owl by b2ihealthcare.

the class DocumentBuilders method classAxiomsWithValue.

public static SnomedRefSetMemberIndexEntry.Builder classAxiomsWithValue(final String sourceId, final Object... axioms) {
    checkArgument(!CompareUtils.isEmpty(axioms), "At least one axiom must be provided");
    checkArgument(axioms.length % 3 == 0, "Each axiom should have 3 arguments [typeId:String, value:Integer|Double|String, group:Integer].");
    int numberOfAxioms = (int) axioms.length / 3;
    final List<SnomedOWLRelationshipDocument> classAxioms = new ArrayList<>(numberOfAxioms);
    for (int i = 0; i < numberOfAxioms; i++) {
        int offset = i * 3;
        final String typeId = (String) axioms[offset];
        final Object value = axioms[offset + 1];
        final int group = (int) axioms[offset + 2];
        final RelationshipValue relationshipValue;
        if (value instanceof Integer) {
            relationshipValue = new RelationshipValue((Integer) value);
        } else if (value instanceof BigDecimal) {
            relationshipValue = new RelationshipValue((BigDecimal) value);
        } else if (value instanceof String) {
            relationshipValue = new RelationshipValue((String) value);
        } else {
            throw new IllegalArgumentException("Unexpected value <" + value + ">.");
        }
        classAxioms.add(SnomedOWLRelationshipDocument.createValue(typeId, relationshipValue, group));
    }
    return classAxioms(sourceId, classAxioms);
}
Also used : ArrayList(java.util.ArrayList) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) BigDecimal(java.math.BigDecimal)

Example 13 with RelationshipValue

use of com.b2international.snowowl.snomed.core.domain.RelationshipValue in project snow-owl by b2ihealthcare.

the class SnomedEclRefinementEvaluator method evalStatementsWithValue.

private Promise<Collection<Property>> evalStatementsWithValue(BranchContext context, Set<String> focusConceptIds, Collection<String> typeIds, DataTypeComparison comparison) {
    final RelationshipValue value;
    final SearchResourceRequest.Operator operator = toSearchOperator(comparison.getOp());
    // XXX: no boolean comparison for relationships with value!
    if (comparison instanceof BooleanValueComparison) {
        return Promise.immediate(List.of());
    } else if (comparison instanceof StringValueComparison) {
        value = new RelationshipValue(((StringValueComparison) comparison).getValue());
    } else if (comparison instanceof IntegerValueComparison) {
        value = new RelationshipValue(((IntegerValueComparison) comparison).getValue());
    } else if (comparison instanceof DecimalValueComparison) {
        value = new RelationshipValue(((DecimalValueComparison) comparison).getValue());
    } else {
        return SnomedEclEvaluationRequest.throwUnsupported(comparison);
    }
    return evalStatementsWithValue(context, focusConceptIds, typeIds, value, operator);
}
Also used : RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest)

Example 14 with RelationshipValue

use of com.b2international.snowowl.snomed.core.domain.RelationshipValue in project snow-owl by b2ihealthcare.

the class SnomedRelationshipSearchRequest method prepareQuery.

@Override
protected Expression prepareQuery(BranchContext context) {
    final ExpressionBuilder queryBuilder = Expressions.builder();
    addActiveClause(queryBuilder);
    addReleasedClause(queryBuilder);
    addIdFilter(queryBuilder, RevisionDocument.Expressions::ids);
    addEclFilter(context, queryBuilder, SnomedSearchRequest.OptionKey.MODULE, SnomedDocument.Expressions::modules);
    addNamespaceFilter(queryBuilder);
    addNamespaceConceptIdFilter(context, queryBuilder);
    addEffectiveTimeClause(queryBuilder);
    addActiveMemberOfClause(context, queryBuilder);
    addMemberOfClause(context, queryBuilder);
    addEclFilter(context, queryBuilder, OptionKey.SOURCE, SnomedRelationshipIndexEntry.Expressions::sourceIds);
    addEclFilter(context, queryBuilder, OptionKey.TYPE, SnomedRelationshipIndexEntry.Expressions::typeIds);
    addEclFilter(context, queryBuilder, OptionKey.DESTINATION, SnomedRelationshipIndexEntry.Expressions::destinationIds);
    addEclFilter(context, queryBuilder, OptionKey.CHARACTERISTIC_TYPE, SnomedRelationshipIndexEntry.Expressions::characteristicTypeIds);
    addEclFilter(context, queryBuilder, OptionKey.MODIFIER, SnomedRelationshipIndexEntry.Expressions::modifierIds);
    if (containsKey(OptionKey.GROUP_MIN) || containsKey(OptionKey.GROUP_MAX)) {
        final int from = containsKey(OptionKey.GROUP_MIN) ? get(OptionKey.GROUP_MIN, Integer.class) : 0;
        final int to = containsKey(OptionKey.GROUP_MAX) ? get(OptionKey.GROUP_MAX, Integer.class) : Integer.MAX_VALUE;
        queryBuilder.filter(relationshipGroup(from, to));
    }
    if (containsKey(OptionKey.UNION_GROUP)) {
        queryBuilder.filter(unionGroup(get(OptionKey.UNION_GROUP, Integer.class)));
    }
    if (containsKey(OptionKey.HAS_DESTINATION_ID)) {
        // No need to check the value for the option key here, as it can only be set to "true" in the builder
        queryBuilder.filter(hasDestinationId());
    }
    if (containsKey(OptionKey.VALUE_TYPE)) {
        final Collection<RelationshipValueType> valueTypes = getCollection(OptionKey.VALUE_TYPE, RelationshipValueType.class);
        queryBuilder.filter(valueTypes(valueTypes));
    }
    if (containsKey(OptionKey.VALUE)) {
        final SearchResourceRequest.Operator op = get(OptionKey.OPERATOR, SearchResourceRequest.Operator.class);
        final Collection<RelationshipValue> values = getCollection(OptionKey.VALUE, RelationshipValue.class);
        switch(op) {
            case EQUALS:
                queryBuilder.filter(values(values));
                break;
            case NOT_EQUALS:
                queryBuilder.mustNot(values(values));
                break;
            case LESS_THAN:
                checkRangeValue(values);
                queryBuilder.filter(valueLessThan(Iterables.getOnlyElement(values), false));
                break;
            case LESS_THAN_EQUALS:
                checkRangeValue(values);
                queryBuilder.filter(valueLessThan(Iterables.getOnlyElement(values), true));
                break;
            case GREATER_THAN:
                checkRangeValue(values);
                queryBuilder.filter(valueGreaterThan(Iterables.getOnlyElement(values), false));
                break;
            case GREATER_THAN_EQUALS:
                checkRangeValue(values);
                queryBuilder.filter(valueGreaterThan(Iterables.getOnlyElement(values), true));
                break;
            default:
                throw new NotImplementedException("Unsupported concrete value operator %s", op);
        }
    }
    return queryBuilder.build();
}
Also used : RelationshipValueType(com.b2international.snowowl.snomed.core.domain.RelationshipValueType) NotImplementedException(com.b2international.commons.exceptions.NotImplementedException) Expressions(com.b2international.index.query.Expressions) Expressions(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SearchResourceRequest(com.b2international.snowowl.core.request.SearchResourceRequest)

Example 15 with RelationshipValue

use of com.b2international.snowowl.snomed.core.domain.RelationshipValue in project snow-owl by b2ihealthcare.

the class SnomedConcreteValueApiTest method createConcreteValueInvalidType.

@Test
public void createConcreteValueInvalidType() {
    Json requestBody = createConcreteValueRequestBody(Concepts.ROOT_CONCEPT, "11110000", new RelationshipValue(10)).with("commitComment", "Created new concrete value with invalid typeId");
    createComponent(branchPath, SnomedComponentType.RELATIONSHIP, requestBody).statusCode(400);
}
Also used : Json(com.b2international.commons.json.Json) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) Test(org.junit.Test) AbstractSnomedApiTest(com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)

Aggregations

RelationshipValue (com.b2international.snowowl.snomed.core.domain.RelationshipValue)20 Test (org.junit.Test)13 AbstractSnomedApiTest (com.b2international.snowowl.snomed.core.rest.AbstractSnomedApiTest)12 Json (com.b2international.commons.json.Json)11 BigDecimal (java.math.BigDecimal)4 SearchResourceRequest (com.b2international.snowowl.core.request.SearchResourceRequest)3 Expressions (com.b2international.index.query.Expressions)2 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)2 Expressions (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions)2 ArrayList (java.util.ArrayList)2 CompareUtils (com.b2international.commons.CompareUtils)1 BadRequestException (com.b2international.commons.exceptions.BadRequestException)1 NotImplementedException (com.b2international.commons.exceptions.NotImplementedException)1 Options (com.b2international.commons.options.Options)1 Expression (com.b2international.index.query.Expression)1 Query (com.b2international.index.query.Query)1 BaseRevisionIndexTest (com.b2international.index.revision.BaseRevisionIndexTest)1 ID (com.b2international.index.revision.Revision.Fields.ID)1 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)1 com.b2international.snomed.ecl.ecl (com.b2international.snomed.ecl.ecl)1