Search in sources :

Example 16 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyBuilder method addActiveConceptIds.

public ReasonerTaxonomyBuilder addActiveConceptIds(final RevisionSearcher searcher) {
    entering("Registering active concept IDs using revision searcher");
    final ExpressionBuilder whereExpressionBuilder = Expressions.builder().filter(active());
    if (!excludedModuleIds.isEmpty()) {
        whereExpressionBuilder.mustNot(modules(excludedModuleIds));
    }
    final List<String> conceptIds = new ArrayList<>(SCROLL_LIMIT);
    Query.select(String[].class).from(SnomedConceptDocument.class).fields(SnomedConceptDocument.Fields.ID).where(whereExpressionBuilder.build()).limit(SCROLL_LIMIT).build().stream(searcher).forEachOrdered(hits -> {
        for (String[] hit : hits) {
            conceptIds.add(hit[0]);
        }
        conceptMapBuilder.addAll(conceptIds);
        conceptIds.clear();
    });
    leaving("Registering active concept IDs using revision searcher");
    return this;
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 17 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyBuilder method addActiveStatedRelationships.

public ReasonerTaxonomyBuilder addActiveStatedRelationships(final RevisionSearcher searcher) {
    entering("Registering active stated relationships using revision searcher");
    final ExpressionBuilder whereExpressionBuilder = Expressions.builder().filter(active()).filter(characteristicTypeId(Concepts.STATED_RELATIONSHIP));
    if (!excludedModuleIds.isEmpty()) {
        whereExpressionBuilder.mustNot(modules(excludedModuleIds));
    }
    addRelationships(searcher, whereExpressionBuilder, statedRelationships);
    leaving("Registering active stated relationships using revision searcher");
    return this;
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 18 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyBuilder method addActiveAxioms.

public ReasonerTaxonomyBuilder addActiveAxioms(final RevisionSearcher searcher) {
    entering("Registering active stated OWL axioms using revision searcher");
    final ExpressionBuilder whereExpressionBuilder = Expressions.builder().filter(SnomedRefSetMemberIndexEntry.Expressions.active()).filter(SnomedRefSetMemberIndexEntry.Expressions.refSetTypes(Collections.singleton(SnomedRefSetType.OWL_AXIOM)));
    if (!excludedModuleIds.isEmpty()) {
        whereExpressionBuilder.mustNot(modules(excludedModuleIds));
    }
    // XXX: we can only guess the lower limit here (1 relationship for each OWL axiom)
    final List<StatementFragment> nonIsAFragments = new ArrayList<>(SCROLL_LIMIT);
    final List<String> axioms = new ArrayList<>(SCROLL_LIMIT);
    final List<String> sourceIds = new ArrayList<>(SCROLL_LIMIT);
    final List<String> destinationIds = new ArrayList<>(SCROLL_LIMIT);
    final String[] lastReferencedComponentId = { "" };
    final int[] groupOffset = { AXIOM_GROUP_BASE };
    Query.select(SnomedRefSetMemberIndexEntry.class).from(SnomedRefSetMemberIndexEntry.class).where(whereExpressionBuilder.build()).sortBy(SortBy.builder().sortByField(SnomedRefSetMemberIndexEntry.Fields.REFERENCED_COMPONENT_ID, Order.ASC).sortByField(SnomedRefSetMemberIndexEntry.Fields.ID, Order.ASC).build()).limit(SCROLL_LIMIT).build().stream(searcher).forEachOrdered(hits -> {
        for (final SnomedRefSetMemberIndexEntry member : hits) {
            final String referencedComponentId = member.getReferencedComponentId();
            if (lastReferencedComponentId[0].isEmpty()) {
                lastReferencedComponentId[0] = referencedComponentId;
            } else if (!lastReferencedComponentId[0].equals(referencedComponentId)) {
                if (conceptMap.containsKey(lastReferencedComponentId[0])) {
                    axiomNonIsaRelationships.putAll(lastReferencedComponentId[0], nonIsAFragments);
                    statedAxioms.putAll(lastReferencedComponentId[0], axioms);
                    statedAncestors.addEdges(sourceIds, destinationIds);
                    statedDescendants.addEdges(destinationIds, sourceIds);
                } else {
                    LOGGER.debug("Not registering OWL axioms for concept {} as it is inactive.", lastReferencedComponentId[0]);
                }
                nonIsAFragments.clear();
                axioms.clear();
                sourceIds.clear();
                destinationIds.clear();
                lastReferencedComponentId[0] = referencedComponentId;
                groupOffset[0] = AXIOM_GROUP_BASE;
            }
            if (!conceptMap.containsKey(referencedComponentId)) {
                LOGGER.debug("Not registering OWL axiom member for concept {} as the source is inactive.", referencedComponentId);
                continue;
            }
            final String expression = member.getOwlExpression();
            final StringTokenizer tok = new StringTokenizer(expression.toLowerCase(Locale.ENGLISH), "(): ");
            boolean isSubPropertyOf = false;
            // [ ] ReflexiveObjectProperty(...)
            try {
                final String firstToken = tok.nextToken();
                if ("transitiveobjectproperty".equals(firstToken)) {
                    long propertyId = Long.parseLong(tok.nextToken());
                    propertyChains.add(new PropertyChain(propertyId, propertyId, propertyId));
                } else if ("subobjectpropertyof".equals(firstToken)) {
                    String nextToken = tok.nextToken();
                    if ("objectpropertychain".equals(nextToken)) {
                        long sourceType = Long.parseLong(tok.nextToken());
                        long destinationType = Long.parseLong(tok.nextToken());
                        long inferredType = Long.parseLong(tok.nextToken());
                        propertyChains.add(new PropertyChain(sourceType, destinationType, inferredType));
                    } else {
                        isSubPropertyOf = true;
                    }
                } else if ("subdatapropertyof".equals(firstToken)) {
                    isSubPropertyOf = true;
                }
                // Collect the OWL axiom only if it is not of type "Sub<Object|Data>PropertyOf"
                if (!isSubPropertyOf) {
                    axioms.add(expression);
                }
            } catch (NoSuchElementException | NumberFormatException e) {
            // skip
            }
            if (!CompareUtils.isEmpty(member.getClassAxiomRelationships())) {
                for (SnomedOWLRelationshipDocument relationship : member.getClassAxiomRelationships()) {
                    if (relationship.getRelationshipGroup() >= AXIOM_GROUP_BASE) {
                        throw new IllegalStateException("OWL member has too many groups");
                    }
                    if (relationship.hasValue()) {
                        // Add relationship with value
                        nonIsAFragments.add(relationship.toStatementFragment(groupOffset[0]));
                        continue;
                    }
                    if (!conceptMap.containsKey(relationship.getDestinationId())) {
                        LOGGER.debug("Not registering OWL axiom relationship for concept {} as destination concept {} is inactive.", referencedComponentId, relationship.getDestinationId());
                        continue;
                    }
                    if (!relationship.getTypeId().equals(Concepts.IS_A)) {
                        // Add non-IS_A relationships with destination
                        nonIsAFragments.add(relationship.toStatementFragment(groupOffset[0]));
                        continue;
                    }
                    if (isSubPropertyOf) {
                        /*
								 * XXX: Register "Sub<Object|Data>PropertyOf" axioms as "stated parents", so that we
								 * can create both the original axiom _and_ a SubClassOf axiom for a (punted)
								 * OWL class representing the property concept.
								 */
                        sourceIds.add(referencedComponentId);
                        destinationIds.add(relationship.getDestinationId());
                        continue;
                    }
                }
            }
            /*
					 * The next OWL member's group numbers will be shifted (it should not have group
					 * numbers greater than AXIOM_GROUP_BASE)
					 */
            groupOffset[0] += AXIOM_GROUP_BASE;
        }
    });
    if (!lastReferencedComponentId[0].isEmpty()) {
        if (conceptMap.containsKey(lastReferencedComponentId[0])) {
            axiomNonIsaRelationships.putAll(lastReferencedComponentId[0], nonIsAFragments);
            statedAxioms.putAll(lastReferencedComponentId[0], axioms);
            statedAncestors.addEdges(sourceIds, destinationIds);
            statedDescendants.addEdges(destinationIds, sourceIds);
        } else {
            LOGGER.debug("Not registering OWL axioms for concept {} as it is inactive.", lastReferencedComponentId[0]);
        }
        nonIsAFragments.clear();
        axioms.clear();
        sourceIds.clear();
        destinationIds.clear();
    }
    leaving("Registering active stated OWL axioms using revision searcher");
    return this;
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 19 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class ReasonerTaxonomyBuilder method addConceptFlags.

private void addConceptFlags(final RevisionSearcher searcher, final Expression expression, final InternalSctIdSet.Builder sctIdSet) {
    final ExpressionBuilder whereExpressionBuilder = Expressions.builder().filter(active()).filter(expression);
    if (!excludedModuleIds.isEmpty()) {
        whereExpressionBuilder.mustNot(modules(excludedModuleIds));
    }
    /*
		 * XXX: For HashSets, Guava's factory method over-allocates by the expected
		 * amount so that the expected number of elements can be inserted without
		 * expanding the backing data structure.
		 */
    final Set<String> sctIds = newHashSetWithExpectedSize(SCROLL_LIMIT);
    Query.select(String[].class).from(SnomedConceptDocument.class).fields(SnomedConceptDocument.Fields.ID).where(whereExpressionBuilder.build()).limit(SCROLL_LIMIT).build().stream(searcher).forEachOrdered(hits -> {
        for (final String[] concept : hits) {
            sctIds.add(concept[0]);
        }
        sctIdSet.addAll(sctIds);
        sctIds.clear();
    });
}
Also used : ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Example 20 with ExpressionBuilder

use of com.b2international.index.query.Expressions.ExpressionBuilder in project snow-owl by b2ihealthcare.

the class DetachedContainerChangeProcessor method process.

@Override
public void process(StagingArea staging, RevisionSearcher searcher) throws IOException {
    final Set<String> deletedCoreComponentIds = newHashSet();
    final Set<String> deletedConceptIds = newHashSet();
    staging.getRemovedObjects().forEach(detachedObject -> {
        if (detachedObject instanceof SnomedComponentDocument) {
            String id = ((SnomedComponentDocument) detachedObject).getId();
            deletedCoreComponentIds.add(id);
            if (detachedObject instanceof SnomedConceptDocument) {
                deletedConceptIds.add(id);
            }
        }
    });
    if (deletedCoreComponentIds.isEmpty() && deletedConceptIds.isEmpty()) {
        return;
    }
    // deleting concepts should delete all of its descriptions, relationships, and inbound relationships
    Query.select(SnomedDescriptionIndexEntry.class).where(SnomedDescriptionIndexEntry.Expressions.concepts(deletedConceptIds)).limit(PAGE_SIZE).build().stream(searcher).flatMap(Hits::stream).forEachOrdered(description -> {
        deletedCoreComponentIds.add(description.getId());
        stageRemove(description);
    });
    Query.select(SnomedRelationshipIndexEntry.class).where(Expressions.builder().should(SnomedRelationshipIndexEntry.Expressions.sourceIds(deletedConceptIds)).should(SnomedRelationshipIndexEntry.Expressions.destinationIds(deletedConceptIds)).build()).limit(PAGE_SIZE).build().stream(searcher).flatMap(Hits::stream).forEachOrdered(relationship -> {
        deletedCoreComponentIds.add(relationship.getId());
        stageRemove(relationship);
    });
    // deleting core components should delete all referring members as well
    ExpressionBuilder referringMembersQuery = Expressions.builder().should(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(deletedCoreComponentIds)).should(SnomedRefSetMemberIndexEntry.Expressions.refsetIds(deletedConceptIds));
    SnomedRf2Headers.MEMBER_FIELDS_WITH_COMPONENT_ID.forEach(memberField -> {
        referringMembersQuery.should(Expressions.matchAny(memberField, deletedCoreComponentIds));
    });
    Query.select(SnomedRefSetMemberIndexEntry.class).where(referringMembersQuery.build()).limit(PAGE_SIZE).build().stream(searcher).flatMap(Hits::stream).forEachOrdered(member -> stageRemove(member));
}
Also used : SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) SnomedComponentDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedComponentDocument) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder)

Aggregations

ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)47 Expressions (com.b2international.index.query.Expressions)17 Expression (com.b2international.index.query.Expression)6 List (java.util.List)6 Set (java.util.Set)6 BadRequestException (com.b2international.commons.exceptions.BadRequestException)5 NotImplementedException (com.b2international.commons.exceptions.NotImplementedException)5 Options (com.b2international.commons.options.Options)5 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)5 SearchResourceRequest (com.b2international.snowowl.core.request.SearchResourceRequest)5 Collection (java.util.Collection)5 Collectors (java.util.stream.Collectors)5 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)4 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)4 IOException (java.io.IOException)4 CompareUtils (com.b2international.commons.CompareUtils)3 Query (com.b2international.index.query.Query)3 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)3 BranchContext (com.b2international.snowowl.core.domain.BranchContext)3 RepositoryContext (com.b2international.snowowl.core.domain.RepositoryContext)3