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());
}
}
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();
}
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;
}
}
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);
}
Aggregations