use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.
the class MapDBRf2EffectiveTimeSlice method doImport.
@Override
public void doImport(final BranchContext context, final ResourceURI codeSystemUri, final Rf2ImportConfiguration importConfig, final ImmutableSet.Builder<ComponentURI> visitedComponents) throws Exception {
final Stopwatch w = Stopwatch.createStarted();
final String importingMessage = isUnpublishedSlice() ? "Importing unpublished components" : String.format("Importing components from %s", getEffectiveTime());
final String commitMessage = isUnpublishedSlice() ? "Imported unpublished components" : String.format("Imported components from %s", getEffectiveTime());
final boolean doCreateVersion = !isUnpublishedSlice() && !isSnapshotSlice() && importConfig.isCreateVersions();
// Collect the type ID for all integer relationship values
final LongKeyLongMap integerTypeIdsByValueId = PrimitiveMaps.newLongKeyLongOpenHashMap();
context.log().info(importingMessage);
try (Rf2TransactionContext tx = new Rf2TransactionContext(context.openTransaction(context, DatastoreLockContextDescriptions.IMPORT), loadOnDemand, importConfig)) {
final Iterator<LongSet> importPlan = getImportPlan().iterator();
while (importPlan.hasNext()) {
LongSet componentsToImportInBatch = importPlan.next();
LongIterator it = componentsToImportInBatch.iterator();
final Collection<SnomedComponent> componentsToImport = newArrayListWithExpectedSize(componentsToImportInBatch.size());
while (it.hasNext()) {
long componentToImportL = it.next();
String componentToImport = Long.toString(componentToImportL);
final SnomedComponent component = getComponent(componentToImport);
if (component != null) {
componentsToImport.add(component);
// Record value types across the entire effective time slice
if (component instanceof SnomedRelationship) {
final SnomedRelationship relationship = (SnomedRelationship) component;
final RelationshipValue relationshipValue = relationship.getValueAsObject();
if (relationshipValue != null) {
if (RelationshipValueType.INTEGER.equals(relationshipValue.type())) {
integerTypeIdsByValueId.put(componentToImportL, Long.parseLong(relationship.getTypeId()));
}
}
}
// Register container concept as visited component
final String conceptId = getConceptId(component);
visitedComponents.add(ComponentURI.of(codeSystemUri, SnomedConcept.TYPE, conceptId));
}
// add all members of this component to this batch as well
final Set<String> containerComponents = membersByReferencedComponent.remove(componentToImportL);
if (containerComponents != null) {
for (String containedComponentId : containerComponents) {
SnomedReferenceSetMember containedComponent = getComponent(containedComponentId);
if (containedComponent != null) {
componentsToImport.add(containedComponent);
// Register reference set as visited component
final String refSetId = containedComponent.getRefsetId();
visitedComponents.add(ComponentURI.of(codeSystemUri, SnomedConcept.REFSET_TYPE, refSetId));
}
}
}
}
tx.add(componentsToImport, getDependencies(componentsToImport));
tx.commit(commitMessage);
}
// Check if any integer values should actually be decimals, indicated by the range constraint on MRCM members
final LongSet decimalTypeIds = collectAttributesWithRangeConstraint(context, "dec(>#0..)");
final LongSet decimalValueIds = collectValueTypeChanges(integerTypeIdsByValueId, decimalTypeIds);
swapValueType(context, decimalValueIds);
if (doCreateVersion) {
ResourceRequests.prepareNewVersion().setResource(codeSystemUri).setVersion(getEffectiveTime()).setDescription("").setEffectiveTime(getEffectiveDate()).buildAsync().getRequest().execute(context);
}
}
context.log().info("{} in {}", commitMessage, w);
}
use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.
the class SnomedImportApiTest method import26OWLExpressionReferenceSetMembers.
@Test
public void import26OWLExpressionReferenceSetMembers() throws Exception {
SnomedConcept oldRoot = getComponent(branchPath, SnomedComponentType.CONCEPT, Concepts.ROOT_CONCEPT, "members()").extract().as(SnomedConcept.class);
assertTrue(oldRoot.getMembers().getItems().stream().noneMatch(m -> m.getRefsetId().equals(Concepts.REFSET_OWL_AXIOM) || m.getRefsetId().equals(Concepts.REFSET_OWL_ONTOLOGY)));
importArchive("SnomedCT_Release_INT_20170731_new_owl_expression_members.zip");
SnomedConcept root = getComponent(branchPath, SnomedComponentType.CONCEPT, Concepts.ROOT_CONCEPT, "members()").extract().as(SnomedConcept.class);
Optional<SnomedReferenceSetMember> axiomMember = root.getMembers().getItems().stream().filter(m -> m.getRefsetId().equals(Concepts.REFSET_OWL_AXIOM)).findFirst();
assertTrue(axiomMember.isPresent());
assertEquals("ec2cc6be-a10b-44b1-a2cc-42a3f11d406e", axiomMember.get().getId());
assertEquals(Concepts.MODULE_SCT_CORE, axiomMember.get().getModuleId());
assertEquals(Concepts.REFSET_OWL_AXIOM, axiomMember.get().getRefsetId());
assertEquals(Concepts.ROOT_CONCEPT, axiomMember.get().getReferencedComponent().getId());
assertEquals(OWL_EXPRESSION, axiomMember.get().getProperties().get(SnomedRf2Headers.FIELD_OWL_EXPRESSION));
Optional<SnomedReferenceSetMember> ontologyMember = root.getMembers().getItems().stream().filter(m -> m.getRefsetId().equals(Concepts.REFSET_OWL_ONTOLOGY)).findFirst();
assertTrue(ontologyMember.isPresent());
assertEquals("f81c24fb-c40a-4b28-9adb-85f748f71395", ontologyMember.get().getId());
assertEquals(Concepts.MODULE_SCT_CORE, ontologyMember.get().getModuleId());
assertEquals(Concepts.REFSET_OWL_ONTOLOGY, ontologyMember.get().getRefsetId());
assertEquals(Concepts.ROOT_CONCEPT, ontologyMember.get().getReferencedComponent().getId());
assertEquals("Ontology(<http://snomed.info/sct/900000000000207008>)", ontologyMember.get().getProperties().get(SnomedRf2Headers.FIELD_OWL_EXPRESSION));
}
use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.
the class SnomedImportApiTest method import27MRCMReferenceSetMembers.
@Test
public void import27MRCMReferenceSetMembers() throws Exception {
SnomedConcept rootConcept = getComponent(branchPath, SnomedComponentType.CONCEPT, Concepts.ROOT_CONCEPT, "members()").extract().as(SnomedConcept.class);
assertTrue(StreamSupport.stream(rootConcept.getMembers().spliterator(), false).noneMatch(m -> {
return m.getRefsetId().equals(Concepts.REFSET_MRCM_DOMAIN_INTERNATIONAL) || m.getRefsetId().equals(Concepts.REFSET_MRCM_ATTRIBUTE_DOMAIN_INTERNATIONAL) || m.getRefsetId().equals(Concepts.REFSET_MRCM_ATTRIBUTE_RANGE_INTERNATIONAL) || m.getRefsetId().equals(Concepts.REFSET_MRCM_MODULE_SCOPE);
}));
importArchive("SnomedCT_Release_INT_20170731_new_mrcm_members.zip");
SnomedConcept newRootConcept = getComponent(branchPath, SnomedComponentType.CONCEPT, Concepts.ROOT_CONCEPT, "members()").extract().as(SnomedConcept.class);
Optional<SnomedReferenceSetMember> mrcmDomainMemberCandidate = StreamSupport.stream(newRootConcept.getMembers().spliterator(), false).filter(m -> m.getRefsetId().equals(Concepts.REFSET_MRCM_DOMAIN_INTERNATIONAL)).findFirst();
assertTrue(mrcmDomainMemberCandidate.isPresent());
SnomedReferenceSetMember mrcmDomainMember = mrcmDomainMemberCandidate.get();
assertEquals("28ecaa32-8f0e-4ff8-b6b1-b642e40519d8", mrcmDomainMember.getId());
assertEquals(Concepts.MODULE_SCT_MODEL_COMPONENT, mrcmDomainMember.getModuleId());
assertEquals(Concepts.REFSET_MRCM_DOMAIN_INTERNATIONAL, mrcmDomainMember.getRefsetId());
assertEquals(Concepts.ROOT_CONCEPT, mrcmDomainMember.getReferencedComponent().getId());
Map<String, Object> domainMemberProps = mrcmDomainMember.getProperties();
assertEquals("domainConstraint", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_DOMAIN_CONSTRAINT));
assertEquals("parentDomain", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_PARENT_DOMAIN));
assertEquals("proximalPrimitiveConstraint", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_PROXIMAL_PRIMITIVE_CONSTRAINT));
assertEquals("proximalPrimitiveRefinement", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_PROXIMAL_PRIMITIVE_REFINEMENT));
assertEquals("domainTemplateForPrecoordination", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_DOMAIN_TEMPLATE_FOR_PRECOORDINATION));
assertEquals("domainTemplateForPostcoordination", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_DOMAIN_TEMPLATE_FOR_POSTCOORDINATION));
assertEquals("guideURL", domainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_GUIDEURL));
Optional<SnomedReferenceSetMember> mrcmAttributeDomainMemberCandidate = StreamSupport.stream(newRootConcept.getMembers().spliterator(), false).filter(m -> m.getRefsetId().equals(Concepts.REFSET_MRCM_ATTRIBUTE_DOMAIN_INTERNATIONAL)).findFirst();
assertTrue(mrcmAttributeDomainMemberCandidate.isPresent());
SnomedReferenceSetMember mrcmAttributeDomainMember = mrcmAttributeDomainMemberCandidate.get();
assertEquals("126bf3f1-4f34-439d-ba0a-a832824d072a", mrcmAttributeDomainMember.getId());
assertEquals(Concepts.MODULE_SCT_MODEL_COMPONENT, mrcmAttributeDomainMember.getModuleId());
assertEquals(Concepts.REFSET_MRCM_ATTRIBUTE_DOMAIN_INTERNATIONAL, mrcmAttributeDomainMember.getRefsetId());
assertEquals(Concepts.ROOT_CONCEPT, mrcmAttributeDomainMember.getReferencedComponent().getId());
Map<String, Object> attributeDomainMemberProps = mrcmAttributeDomainMember.getProperties();
assertEquals(Concepts.ROOT_CONCEPT, attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_DOMAIN_ID));
assertEquals(Boolean.TRUE, attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_GROUPED));
assertEquals("attributeCardinality", attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_ATTRIBUTE_CARDINALITY));
assertEquals("attributeInGroupCardinality", attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_ATTRIBUTE_IN_GROUP_CARDINALITY));
assertEquals(Concepts.ROOT_CONCEPT, attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID));
assertEquals(Concepts.ROOT_CONCEPT, attributeDomainMemberProps.get(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID));
Optional<SnomedReferenceSetMember> mrcmAttributeRangeMemberCandidate = StreamSupport.stream(newRootConcept.getMembers().spliterator(), false).filter(m -> m.getRefsetId().equals(Concepts.REFSET_MRCM_ATTRIBUTE_RANGE_INTERNATIONAL)).findFirst();
assertTrue(mrcmAttributeRangeMemberCandidate.isPresent());
SnomedReferenceSetMember mrcmAttributeRangeMember = mrcmAttributeRangeMemberCandidate.get();
assertEquals("ae090cc3-2827-4e39-80c6-364435d30c17", mrcmAttributeRangeMember.getId());
assertEquals(Concepts.MODULE_SCT_MODEL_COMPONENT, mrcmAttributeRangeMember.getModuleId());
assertEquals(Concepts.REFSET_MRCM_ATTRIBUTE_RANGE_INTERNATIONAL, mrcmAttributeRangeMember.getRefsetId());
assertEquals(Concepts.ROOT_CONCEPT, mrcmAttributeRangeMember.getReferencedComponent().getId());
Map<String, Object> attributeRangeMemberProps = mrcmAttributeRangeMember.getProperties();
assertEquals("rangeConstraint", attributeRangeMemberProps.get(SnomedRf2Headers.FIELD_MRCM_RANGE_CONSTRAINT));
assertEquals("attributeRule", attributeRangeMemberProps.get(SnomedRf2Headers.FIELD_MRCM_ATTRIBUTE_RULE));
assertEquals(Concepts.ROOT_CONCEPT, attributeRangeMemberProps.get(SnomedRf2Headers.FIELD_MRCM_RULE_STRENGTH_ID));
assertEquals(Concepts.ROOT_CONCEPT, attributeRangeMemberProps.get(SnomedRf2Headers.FIELD_MRCM_CONTENT_TYPE_ID));
Optional<SnomedReferenceSetMember> mrcmModuleScopeMemberCandidate = StreamSupport.stream(newRootConcept.getMembers().spliterator(), false).filter(m -> m.getRefsetId().equals(Concepts.REFSET_MRCM_MODULE_SCOPE)).findFirst();
assertTrue(mrcmModuleScopeMemberCandidate.isPresent());
SnomedReferenceSetMember mrmcModuleScopeMember = mrcmModuleScopeMemberCandidate.get();
assertEquals("52d29f1b-f7a3-4a0f-828c-383c6259c3f5", mrmcModuleScopeMember.getId());
assertEquals(Concepts.MODULE_SCT_MODEL_COMPONENT, mrmcModuleScopeMember.getModuleId());
assertEquals(Concepts.REFSET_MRCM_MODULE_SCOPE, mrmcModuleScopeMember.getRefsetId());
assertEquals(Concepts.ROOT_CONCEPT, mrmcModuleScopeMember.getReferencedComponent().getId());
assertEquals(Concepts.REFSET_MRCM_DOMAIN_INTERNATIONAL, mrmcModuleScopeMember.getProperties().get(SnomedRf2Headers.FIELD_MRCM_RULE_REFSET_ID));
}
use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.
the class Rf2TransactionContext method add.
void add(Collection<SnomedComponent> componentChanges, Multimap<Class<? extends SnomedDocument>, String> dependenciesByType) {
final Multimap<Class<? extends SnomedDocument>, SnomedComponent> componentChangesByType = Multimaps.index(componentChanges, this::getDocType);
for (Class<? extends SnomedDocument> type : IMPORT_ORDER) {
final Collection<SnomedComponent> rf2Components = componentChangesByType.get(type);
final Set<String> componentsToLookup = rf2Components.stream().map(IComponent::getId).collect(Collectors.toSet());
// add all dependencies with the same type
componentsToLookup.addAll(dependenciesByType.get(type));
final Map<String, ? extends SnomedDocument> existingComponents = lookup(componentsToLookup, type);
final Map<String, SnomedConceptDocument> existingRefSets;
if (SnomedRefSetMemberIndexEntry.class == type) {
existingRefSets = lookup(rf2Components.stream().map(member -> ((SnomedReferenceSetMember) member).getRefsetId()).collect(Collectors.toSet()), SnomedConceptDocument.class);
} else {
existingRefSets = Collections.emptyMap();
}
final Set<String> newRefSetIds = newHashSet();
// seed missing component before applying row changes
// and check for existing components with the same or greater effective time and skip them
final Collection<SnomedComponent> componentsToImport = newArrayList();
for (SnomedComponent rf2Component : rf2Components) {
SnomedDocument existingObject = existingComponents.get(rf2Component.getId());
if (existingObject == null) {
// new component, add to new components and register row for import
newComponents.put(rf2Component.getId(), createIdOnlyDoc(rf2Component.getId(), type));
componentsToImport.add(rf2Component);
} else if (existingObject instanceof SnomedDocument && rf2Component instanceof SnomedComponent) {
final SnomedComponent rf2Row = (SnomedComponent) rf2Component;
final SnomedDocument existingRow = (SnomedDocument) existingObject;
if (rf2Row.getEffectiveTime() == null || EffectiveTimes.getEffectiveTime(rf2Row.getEffectiveTime()) > existingRow.getEffectiveTime()) {
componentsToImport.add(rf2Component);
}
}
// check and register refset props on concept docs
if (rf2Component instanceof SnomedReferenceSetMember) {
final SnomedReferenceSetMember member = (SnomedReferenceSetMember) rf2Component;
// seed the refset if missing
final String refSetId = member.getRefsetId();
SnomedConceptDocument conceptDocToUpdate = existingRefSets.get(refSetId);
if (conceptDocToUpdate == null || newComponents.containsKey(refSetId)) {
conceptDocToUpdate = (SnomedConceptDocument) newComponents.get(refSetId);
}
if (conceptDocToUpdate.getRefSetType() == null) {
final String referencedComponentType = SnomedComponent.getType(member.getReferencedComponentId());
String mapTargetComponentType = TerminologyRegistry.UNKNOWN_COMPONENT_TYPE;
try {
mapTargetComponentType = SnomedComponent.getType((String) member.getProperties().get(SnomedRf2Headers.FIELD_MAP_TARGET));
} catch (IllegalArgumentException e) {
// ignored
}
final SnomedReferenceSet refSet = new SnomedReferenceSet();
refSet.setType(member.type());
refSet.setReferencedComponentType(referencedComponentType);
refSet.setMapTargetComponentType(mapTargetComponentType);
final SnomedConceptDocument updatedConcept = SnomedConceptDocument.builder(conceptDocToUpdate).refSet(refSet).build();
if (newComponents.containsKey(refSetId)) {
newComponents.put(refSetId, updatedConcept);
newRefSetIds.add(refSetId);
} else {
update(conceptDocToUpdate, updatedConcept);
}
}
}
}
// apply row changes
for (SnomedComponent rf2Component : componentsToImport) {
final String id = rf2Component.getId();
SnomedDocument existingRevision = null;
SnomedDocument.Builder<?, ?> newRevision;
if (newComponents.containsKey(id)) {
newRevision = createDocBuilder(id, type, newComponents.get(id));
} else if (existingComponents.containsKey(id)) {
existingRevision = existingComponents.get(id);
newRevision = createDocBuilder(id, type, existingRevision);
} else {
throw new IllegalStateException(String.format("Current revision is null for %s", id));
}
final SnomedComponentBuilder builder;
if (rf2Component instanceof SnomedCoreComponent) {
builder = prepareCoreComponent(rf2Component);
} else if (rf2Component instanceof SnomedReferenceSetMember) {
builder = prepareMember((SnomedReferenceSetMember) rf2Component);
} else {
throw new UnsupportedOperationException("Unsupported component: " + rf2Component);
}
// apply row changes
builder.init(newRevision, this);
if (existingRevision == null) {
// in this case the component is new, and the default values are okay to use
add(newRevision.build());
} else {
// in this case, recalculate the released flag based on the currently available revision
if (existingRevision.isReleased()) {
update(existingRevision, newRevision.released(existingRevision.isReleased()).build());
} else {
update(existingRevision, newRevision.build());
}
}
}
// make sure we always attach refset properties to identifier concepts
final StagingArea staging = service(StagingArea.class);
for (String newRefSetId : newRefSetIds) {
SnomedConceptDocument newRefSet = (SnomedConceptDocument) newComponents.get(newRefSetId);
SnomedConceptDocument stagedNewRefSet = (SnomedConceptDocument) staging.getNewObject(SnomedConceptDocument.class, newRefSetId);
if (newRefSet != null && stagedNewRefSet != null) {
if (stagedNewRefSet.getRefSetType() == null) {
add(SnomedConceptDocument.builder(stagedNewRefSet).refSetType(newRefSet.getRefSetType()).referencedComponentType(newRefSet.getReferencedComponentType()).mapTargetComponentType(newRefSet.getMapTargetComponentType()).build());
}
}
}
}
}
use of com.b2international.snowowl.snomed.core.domain.refset.SnomedReferenceSetMember in project snow-owl by b2ihealthcare.
the class BranchCompareRequestTest method prepareBranchWithNewChanges.
private Set<ComponentIdentifier> prepareBranchWithNewChanges(String branchPath) {
final IBranchPath branch = BranchPathUtils.createPath(branchPath);
final String newConceptId = createNewConcept(branch);
final SnomedConcept concept = getComponent(branch, SnomedComponentType.CONCEPT, newConceptId, "descriptions(expand(members())),relationships()").extract().as(SnomedConcept.class);
final Set<ComponentIdentifier> newIds = newHashSet();
newIds.add(ComponentIdentifier.of(SnomedConcept.TYPE, concept.getId()));
for (SnomedDescription description : concept.getDescriptions()) {
newIds.add(ComponentIdentifier.of(SnomedDescription.TYPE, description.getId()));
for (SnomedReferenceSetMember member : description.getMembers()) {
newIds.add(ComponentIdentifier.of(SnomedReferenceSetMember.TYPE, member.getId()));
}
}
for (SnomedRelationship relationship : concept.getRelationships()) {
newIds.add(ComponentIdentifier.of(SnomedRelationship.TYPE, relationship.getId()));
}
return newIds;
}
Aggregations