Search in sources :

Example 1 with SnomedOWLRelationshipDocument

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument in project snow-owl by b2ihealthcare.

the class Taxonomies method updateEdge.

private static void updateEdge(SnomedRefSetMemberIndexEntry member, TaxonomyGraph graphToUpdate, SnomedOWLExpressionConverter expressionConverter) {
    if (member.isActive()) {
        SnomedOWLExpressionConverterResult result = expressionConverter.toSnomedOWLRelationships(member.getReferencedComponentId(), member.getOwlExpression());
        if (!CompareUtils.isEmpty(result.getClassAxiomRelationships())) {
            /*
				 * XXX: IS A relationships are expected to have a destination ID, not a value,
				 * but we do not check this explicitly here -- Long#parseLong will throw a
				 * NumberFormatException if it encounters a null value.
				 */
            final long[] destinationIds = result.getClassAxiomRelationships().stream().filter(r -> Concepts.IS_A.equals(r.getTypeId())).map(SnomedOWLRelationshipDocument::getDestinationId).mapToLong(Long::parseLong).toArray();
            graphToUpdate.addEdge(member.getId(), Long.parseLong(member.getReferencedComponentId()), destinationIds);
        } else {
            graphToUpdate.removeEdge(member.getId());
        }
    } else {
        graphToUpdate.removeEdge(member.getId());
    }
}
Also used : Query(com.b2international.index.query.Query) Hits(com.b2international.index.Hits) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) LoggerFactory(org.slf4j.LoggerFactory) LongCollections(com.b2international.collections.longs.LongCollections) SnomedOWLExpressionConverterResult(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverterResult) Expressions.typeId(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.typeId) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) ImmutableList(com.google.common.collect.ImmutableList) LongSets(com.b2international.commons.collect.LongSets) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) CompareUtils(com.b2international.commons.CompareUtils) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) Logger(org.slf4j.Logger) LongIterator(com.b2international.collections.longs.LongIterator) LongCollection(com.b2international.collections.longs.LongCollection) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Collection(java.util.Collection) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) Expressions.characteristicTypeId(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.characteristicTypeId) Expressions.sourceIds(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.sourceIds) Set(java.util.Set) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) Expressions.destinationIds(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.destinationIds) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Expressions(com.b2international.index.query.Expressions) Expressions.active(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active) StagingArea(com.b2international.index.revision.StagingArea) IComponent(com.b2international.snowowl.core.domain.IComponent) Collections(java.util.Collections) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) SnomedOWLExpressionConverterResult(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverterResult)

Example 2 with SnomedOWLRelationshipDocument

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument in project snow-owl by b2ihealthcare.

the class Taxonomies method getStatements.

private static Collection<Object[]> getStatements(RevisionSearcher searcher, LongCollection conceptIds, String characteristicTypeId, boolean filterByConceptIds) throws IOException {
    // merge stated relationships and OWL axiom relationships into a single array
    ImmutableList.Builder<Object[]> isaStatementsBuilder = ImmutableList.builder();
    final Set<String> concepts = LongSets.toStringSet(conceptIds);
    ExpressionBuilder activeIsaRelationshipQuery = Expressions.builder().filter(active()).filter(typeId(Concepts.IS_A)).filter(characteristicTypeId(characteristicTypeId));
    if (filterByConceptIds) {
        activeIsaRelationshipQuery.filter(sourceIds(concepts)).filter(destinationIds(concepts));
    }
    final Query<String[]> activeStatedISARelationshipsQuery = Query.select(String[].class).from(SnomedRelationshipIndexEntry.class).fields(SnomedRelationshipIndexEntry.Fields.ID, SnomedRelationshipIndexEntry.Fields.SOURCE_ID, SnomedRelationshipIndexEntry.Fields.DESTINATION_ID).where(activeIsaRelationshipQuery.build()).limit(Integer.MAX_VALUE).build();
    Hits<String[]> activeIsaRelationships = searcher.search(activeStatedISARelationshipsQuery);
    activeIsaRelationships.forEach(activeIsaRelationship -> {
        isaStatementsBuilder.add(new Object[] { activeIsaRelationship[0], Long.parseLong(activeIsaRelationship[1]), new long[] { Long.parseLong(activeIsaRelationship[2]) } });
    });
    activeIsaRelationships = null;
    if (Concepts.STATED_RELATIONSHIP.equals(characteristicTypeId)) {
        // search existing axioms defined for the given set of conceptIds
        ExpressionBuilder activeOwlAxiomMemberQuery = Expressions.builder().filter(active());
        if (filterByConceptIds) {
            activeOwlAxiomMemberQuery.filter(SnomedRefSetMemberIndexEntry.Expressions.referencedComponentIds(concepts)).filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, Expressions.builder().filter(typeId(Concepts.IS_A)).filter(destinationIds(concepts)).build()));
        } else {
            activeOwlAxiomMemberQuery.filter(Expressions.nestedMatch(SnomedRefSetMemberIndexEntry.Fields.CLASS_AXIOM_RELATIONSHIP, Expressions.builder().filter(typeId(Concepts.IS_A)).build()));
        }
        final Query<SnomedRefSetMemberIndexEntry> activeAxiomISARelationshipsQuery = Query.select(SnomedRefSetMemberIndexEntry.class).where(activeOwlAxiomMemberQuery.build()).limit(Integer.MAX_VALUE).build();
        Hits<SnomedRefSetMemberIndexEntry> activeAxiomISARelationships = searcher.search(activeAxiomISARelationshipsQuery);
        activeAxiomISARelationships.forEach(owlMember -> {
            if (!CompareUtils.isEmpty(owlMember.getClassAxiomRelationships())) {
                // XXX: breaks with a NumberFormatException if any of the IS A relationships has a value
                long[] destinationIds = owlMember.getClassAxiomRelationships().stream().filter(classAxiom -> Concepts.IS_A.equals(classAxiom.getTypeId())).map(SnomedOWLRelationshipDocument::getDestinationId).mapToLong(Long::parseLong).toArray();
                isaStatementsBuilder.add(new Object[] { owlMember.getId(), Long.parseLong(owlMember.getReferencedComponentId()), destinationIds });
            }
        });
        activeAxiomISARelationships = null;
    }
    return isaStatementsBuilder.build();
}
Also used : Query(com.b2international.index.query.Query) Hits(com.b2international.index.Hits) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) SnomedRelationshipIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry) LoggerFactory(org.slf4j.LoggerFactory) LongCollections(com.b2international.collections.longs.LongCollections) SnomedOWLExpressionConverterResult(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverterResult) Expressions.typeId(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.typeId) Concepts(com.b2international.snowowl.snomed.common.SnomedConstants.Concepts) ImmutableList(com.google.common.collect.ImmutableList) LongSets(com.b2international.commons.collect.LongSets) RevisionSearcher(com.b2international.index.revision.RevisionSearcher) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) CompareUtils(com.b2international.commons.CompareUtils) RevisionPropertyDiff(com.b2international.index.revision.StagingArea.RevisionPropertyDiff) SnomedConceptDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) Logger(org.slf4j.Logger) LongIterator(com.b2international.collections.longs.LongIterator) LongCollection(com.b2international.collections.longs.LongCollection) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException) Collection(java.util.Collection) SnomedOWLExpressionConverter(com.b2international.snowowl.snomed.datastore.request.SnomedOWLExpressionConverter) Expressions.characteristicTypeId(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.characteristicTypeId) Expressions.sourceIds(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.sourceIds) Set(java.util.Set) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) Expressions.destinationIds(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry.Expressions.destinationIds) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Expressions(com.b2international.index.query.Expressions) Expressions.active(com.b2international.snowowl.snomed.datastore.index.entry.SnomedDocument.Expressions.active) StagingArea(com.b2international.index.revision.StagingArea) IComponent(com.b2international.snowowl.core.domain.IComponent) Collections(java.util.Collections) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) ImmutableList(com.google.common.collect.ImmutableList) ExpressionBuilder(com.b2international.index.query.Expressions.ExpressionBuilder) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)

Example 3 with SnomedOWLRelationshipDocument

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument in project snow-owl by b2ihealthcare.

the class SnomedOWLExpressionConverter method toSnomedOWLRelationships.

public SnomedOWLExpressionConverterResult toSnomedOWLRelationships(String conceptId, String axiomExpression) {
    // Only attempt to convert axioms which the OWL toolkit supports
    if (Strings.isNullOrEmpty(axiomExpression) || !isAxiomSupported(axiomExpression)) {
        return SnomedOWLExpressionConverterResult.EMPTY;
    }
    try {
        final Long conceptIdLong = Long.valueOf(conceptId);
        final AxiomRepresentation axiomRepresentation = convertAxiom(conceptId, axiomExpression);
        if (axiomRepresentation == null) {
            return SnomedOWLExpressionConverterResult.EMPTY;
        }
        boolean gci = false;
        Map<Integer, List<Relationship>> relationships = null;
        if (conceptIdLong.equals(axiomRepresentation.getLeftHandSideNamedConcept())) {
            relationships = axiomRepresentation.getRightHandSideRelationships();
        } else if (conceptIdLong.equals(axiomRepresentation.getRightHandSideNamedConcept())) {
            /*
				 * XXX: EquivalentClasses axioms are not ordered in any meaningful way, so it
				 * can happen that the class expression representing relationships falls on the left-hand side
				 */
            gci = axiomRepresentation.isPrimitive();
            relationships = axiomRepresentation.getLeftHandSideRelationships();
        } else {
            LOG.warn("Illegal assignment of referenced component id ('{}') was detected for the OWL expression: '{}'", conceptId, axiomExpression);
        }
        if (relationships == null) {
            return SnomedOWLExpressionConverterResult.EMPTY;
        }
        final List<SnomedOWLRelationshipDocument> convertedRelationships = relationships.values().stream().flatMap(List::stream).map(relationship -> {
            if (relationship.isConcrete()) {
                return SnomedOWLRelationshipDocument.createValue(Long.toString(relationship.getTypeId()), toRelationshipValue(relationship.getValue()), relationship.getGroup());
            } else {
                return SnomedOWLRelationshipDocument.create(Long.toString(relationship.getTypeId()), Long.toString(relationship.getDestinationId()), relationship.getGroup());
            }
        }).collect(Collectors.toList());
        return new SnomedOWLExpressionConverterResult(gci ? null : convertedRelationships, gci ? convertedRelationships : null);
    } catch (ApiException e) {
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to convert OWL axiom '{}' to relationship representations for concept '{}'", axiomExpression, conceptId, e);
        return SnomedOWLExpressionConverterResult.EMPTY;
    }
}
Also used : RelationshipValueType(com.b2international.snowowl.snomed.core.domain.RelationshipValueType) SnomedRefSetMemberIndexEntry(com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry) Stopwatch(com.google.common.base.Stopwatch) ConversionException(org.snomed.otf.owltoolkit.conversion.ConversionException) LoggerFactory(org.slf4j.LoggerFactory) Supplier(com.google.common.base.Supplier) Callable(java.util.concurrent.Callable) TimeUtil(com.b2international.commons.time.TimeUtil) BigDecimal(java.math.BigDecimal) AxiomRepresentation(org.snomed.otf.owltoolkit.domain.AxiomRepresentation) Strings(com.google.common.base.Strings) Options(com.b2international.commons.options.Options) Map(java.util.Map) Suppliers(com.google.common.base.Suppliers) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) ConcreteValue(org.snomed.otf.owltoolkit.domain.Relationship.ConcreteValue) ApiException(com.b2international.commons.exceptions.ApiException) SnomedReferenceSetMembers(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers) AxiomRelationshipConversionService(org.snomed.otf.owltoolkit.conversion.AxiomRelationshipConversionService) Logger(org.slf4j.Logger) RelationshipValue(com.b2international.snowowl.snomed.core.domain.RelationshipValue) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) SnomedCoreComponent(com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent) SnomedReferenceSetMember(com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember) Relationship(org.snomed.otf.owltoolkit.domain.Relationship) BranchContext(com.b2international.snowowl.core.domain.BranchContext) SnomedRefSetType(com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType) SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) List(java.util.List) AxiomRepresentation(org.snomed.otf.owltoolkit.domain.AxiomRepresentation) ConversionException(org.snomed.otf.owltoolkit.conversion.ConversionException) ApiException(com.b2international.commons.exceptions.ApiException) ApiException(com.b2international.commons.exceptions.ApiException)

Example 4 with SnomedOWLRelationshipDocument

use of com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument in project snow-owl by b2ihealthcare.

the class SnomedOWLRelationshipConverter method fromSnomedOwlRelationships.

public String fromSnomedOwlRelationships(final boolean gci, final boolean isPrimitive, final String conceptId, final List<SnomedOWLRelationshipDocument> owlRelationships) {
    final AxiomRepresentation axiomRepresentation = new AxiomRepresentation();
    final Long conceptIdLong = Long.valueOf(conceptId);
    final ListMultimap<Integer, Relationship> relationships = ArrayListMultimap.create();
    for (final SnomedOWLRelationshipDocument owlRelationship : owlRelationships) {
        final Relationship relationship;
        if (owlRelationship.hasValue()) {
            relationship = new Relationship(owlRelationship.getRelationshipGroup(), Long.valueOf(owlRelationship.getTypeId()), toConcreteValue(owlRelationship.getValueAsObject()));
        } else {
            relationship = new Relationship(owlRelationship.getRelationshipGroup(), Long.valueOf(owlRelationship.getTypeId()), Long.valueOf(owlRelationship.getDestinationId()));
        }
        relationships.put(relationship.getGroup(), relationship);
    }
    final Map<Integer, List<Relationship>> relationshipsMap = Multimaps.asMap(relationships);
    if (gci) {
        axiomRepresentation.setLeftHandSideRelationships(relationshipsMap);
        axiomRepresentation.setRightHandSideNamedConcept(conceptIdLong);
    } else {
        axiomRepresentation.setLeftHandSideNamedConcept(conceptIdLong);
        axiomRepresentation.setRightHandSideRelationships(relationshipsMap);
    }
    axiomRepresentation.setPrimitive(isPrimitive);
    return convertRelationshipToAxiom(conceptId, axiomRepresentation);
}
Also used : SnomedOWLRelationshipDocument(com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument) Relationship(org.snomed.otf.owltoolkit.domain.Relationship) List(java.util.List) AxiomRepresentation(org.snomed.otf.owltoolkit.domain.AxiomRepresentation)

Aggregations

SnomedOWLRelationshipDocument (com.b2international.snowowl.snomed.datastore.index.entry.SnomedOWLRelationshipDocument)4 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)3 SnomedRefSetMemberIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)3 Set (java.util.Set)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 LongCollection (com.b2international.collections.longs.LongCollection)2 LongCollections (com.b2international.collections.longs.LongCollections)2 LongIterator (com.b2international.collections.longs.LongIterator)2 CompareUtils (com.b2international.commons.CompareUtils)2 LongSets (com.b2international.commons.collect.LongSets)2 Hits (com.b2international.index.Hits)2 Expressions (com.b2international.index.query.Expressions)2 ExpressionBuilder (com.b2international.index.query.Expressions.ExpressionBuilder)2 Query (com.b2international.index.query.Query)2 RevisionSearcher (com.b2international.index.revision.RevisionSearcher)2 StagingArea (com.b2international.index.revision.StagingArea)2 RevisionPropertyDiff (com.b2international.index.revision.StagingArea.RevisionPropertyDiff)2 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)2 IComponent (com.b2international.snowowl.core.domain.IComponent)2