Search in sources :

Example 1 with AxiomRepresentation

use of org.snomed.otf.owltoolkit.domain.AxiomRepresentation 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 2 with AxiomRepresentation

use of org.snomed.otf.owltoolkit.domain.AxiomRepresentation 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)2 List (java.util.List)2 AxiomRepresentation (org.snomed.otf.owltoolkit.domain.AxiomRepresentation)2 Relationship (org.snomed.otf.owltoolkit.domain.Relationship)2 ApiException (com.b2international.commons.exceptions.ApiException)1 Options (com.b2international.commons.options.Options)1 TimeUtil (com.b2international.commons.time.TimeUtil)1 BranchContext (com.b2international.snowowl.core.domain.BranchContext)1 RelationshipValue (com.b2international.snowowl.snomed.core.domain.RelationshipValue)1 RelationshipValueType (com.b2international.snowowl.snomed.core.domain.RelationshipValueType)1 SnomedCoreComponent (com.b2international.snowowl.snomed.core.domain.SnomedCoreComponent)1 SnomedRefSetType (com.b2international.snowowl.snomed.core.domain.refset.SnomedRefSetType)1 SnomedReferenceSetMember (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember)1 SnomedReferenceSetMembers (com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMembers)1 SnomedRefSetMemberIndexEntry (com.b2international.snowowl.snomed.datastore.index.entry.SnomedRefSetMemberIndexEntry)1 Stopwatch (com.google.common.base.Stopwatch)1 Strings (com.google.common.base.Strings)1 Supplier (com.google.common.base.Supplier)1 Suppliers (com.google.common.base.Suppliers)1 BigDecimal (java.math.BigDecimal)1