use of com.b2international.snowowl.snomed.datastore.internal.rf2.AbstractSnomedDsvExportItem in project snow-owl by b2ihealthcare.
the class SnomedRefSetDSVExportTest method transformToExportItems.
private List<AbstractSnomedDsvExportItem> transformToExportItems() /*final Iterable<SnomedConstraint> constraints*/
{
// XXX: Migrate to new MRCM model
List<AbstractSnomedDsvExportItem> results = Lists.newArrayList();
// for (final SnomedConstraint constraint : constraints) {
// SnomedPredicate predicate = constraint.getPredicate();
//
// // Inspect the predicate within the cardinality predicate
// if (predicate instanceof SnomedCardinalityPredicate) {
// predicate = ((SnomedCardinalityPredicate) predicate).getPredicate();
// }
//
// if (predicate instanceof SnomedDescriptionPredicate) {
// final String descriptionTypeId = ((SnomedDescriptionPredicate) predicate).getTypeId();
// final ComponentIdSnomedDsvExportItem descriptionExportItem = new ComponentIdSnomedDsvExportItem(SnomedDsvExportItemType.DESCRIPTION, descriptionTypeId, descriptionTypeId);
// results.add(descriptionExportItem);
// } else if (predicate instanceof SnomedRelationshipPredicate) {
// final String typeId = ((SnomedRelationshipPredicate) predicate).getAttributeExpression(); // XXX: only single-SCTID expressions are accepted
// final ComponentIdSnomedDsvExportItem relationshipExportItem = new ComponentIdSnomedDsvExportItem(SnomedDsvExportItemType.RELATIONSHIP, typeId, typeId);
// results.add(relationshipExportItem);
// } else if (predicate instanceof SnomedConcreteDomainPredicate) {
// final String attributeId = ((SnomedConcreteDomainPredicate) predicate).getAttributeExpression(); // XXX: only single-SCTID expressions are accepted
// final DataType dataType = ((SnomedConcreteDomainPredicate) predicate).getRange();
// final boolean dataTypeBoolean = DataType.BOOLEAN.equals(dataType);
// final DatatypeSnomedDsvExportItem datatypeExportItem = new DatatypeSnomedDsvExportItem(SnomedDsvExportItemType.DATAYPE, attributeId, attributeId, dataTypeBoolean);
// results.add(datatypeExportItem);
// }
// }
results.add(new SimpleSnomedDsvExportItem(SnomedDsvExportItemType.PREFERRED_TERM));
results.add(new SimpleSnomedDsvExportItem(SnomedDsvExportItemType.MODULE));
results.add(new SimpleSnomedDsvExportItem(SnomedDsvExportItemType.EFFECTIVE_TIME));
results.add(new SimpleSnomedDsvExportItem(SnomedDsvExportItemType.STATUS_LABEL));
results.add(new SimpleSnomedDsvExportItem(SnomedDsvExportItemType.DEFINITION_STATUS));
return results;
}
use of com.b2international.snowowl.snomed.datastore.internal.rf2.AbstractSnomedDsvExportItem in project snow-owl by b2ihealthcare.
the class SnomedSimpleTypeRefSetDSVExporter method writeValues.
private void writeValues(BufferedWriter writer, SnomedConcepts chunk) throws IOException {
List<String> dataRow = newArrayList();
for (SnomedConcept concept : chunk) {
dataRow.clear();
for (AbstractSnomedDsvExportItem exportItem : exportItems) {
switch(exportItem.getType()) {
case DESCRIPTION:
{
final ComponentIdSnomedDsvExportItem descriptionItem = (ComponentIdSnomedDsvExportItem) exportItem;
final String typeId = descriptionItem.getComponentId();
int occurrences = descriptionCount.get(typeId);
final Map<String, String> termsById = concept.getDescriptions().stream().filter(d -> typeId.equals(d.getTypeId())).collect(Collectors.toMap(SnomedDescription::getId, SnomedDescription::getTerm));
addCells(dataRow, occurrences, includeDescriptionId, termsById);
break;
}
case RELATIONSHIP:
{
final ComponentIdSnomedDsvExportItem relationshipItem = (ComponentIdSnomedDsvExportItem) exportItem;
for (Integer propertyGroup : propertyCountByGroup.keySet()) {
final String typeId = relationshipItem.getComponentId();
final Map<String, Integer> groupOccurrences = propertyCountByGroup.getOrDefault(propertyGroup, NO_OCCURRENCES);
final int occurrences = groupOccurrences.getOrDefault(typeId, 0);
concept.getRelationships().stream().filter(r -> typeId.equals(r.getTypeId()) && Objects.equals(r.getRelationshipGroup(), propertyGroup) && (Concepts.INFERRED_RELATIONSHIP.equals(r.getCharacteristicTypeId()) || Concepts.ADDITIONAL_RELATIONSHIP.equals(r.getCharacteristicTypeId()))).forEach(relationship -> {
if (relationship.hasValue()) {
addCells(dataRow, occurrences, includeRelationshipId, ImmutableMap.of(relationship.getValue(), ""));
} else {
addCells(dataRow, occurrences, includeRelationshipId, ImmutableMap.of(relationship.getDestinationId(), getPreferredTerm(relationship.getDestination())));
}
});
}
break;
}
case DATAYPE:
{
final DatatypeSnomedDsvExportItem datatypeItem = (DatatypeSnomedDsvExportItem) exportItem;
for (Integer propertyGroup : propertyCountByGroup.keySet()) {
Map<String, Integer> groupedOccurrences = propertyCountByGroup.getOrDefault(propertyGroup, NO_OCCURRENCES);
final String typeId = datatypeItem.getComponentId();
int occurrences = groupedOccurrences.getOrDefault(typeId, 0);
if (occurrences < 1) {
break;
}
final List<String> properties = concept.getMembers().stream().filter(m -> SnomedRefSetType.CONCRETE_DATA_TYPE.equals(m.type()) && m.isActive() && typeId.equals(m.getProperties().get(SnomedRf2Headers.FIELD_TYPE_ID)) && Objects.equals(m.getProperties().get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP), propertyGroup) && (Concepts.INFERRED_RELATIONSHIP.equals(m.getProperties().get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)) || Concepts.ADDITIONAL_RELATIONSHIP.equals(m.getProperties().get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)))).map(m -> m.getProperties().get(SnomedRf2Headers.FIELD_VALUE)).map(p -> {
if (datatypeItem.isBooleanDatatype()) {
return "1".equals(p) ? "Yes" : "No";
} else {
return p.toString();
}
}).sorted().collect(Collectors.toList());
for (String value : properties) {
dataRow.add(value);
occurrences--;
}
while (occurrences > 0) {
dataRow.add("");
occurrences--;
}
}
break;
}
case PREFERRED_TERM:
if (includeDescriptionId) {
dataRow.add(getPreferredTermId(concept));
dataRow.add(getPreferredTerm(concept));
} else {
dataRow.add(getPreferredTerm(concept));
}
break;
case CONCEPT_ID:
dataRow.add(concept.getId());
break;
case MODULE:
dataRow.add(concept.getModuleId());
break;
case EFFECTIVE_TIME:
dataRow.add(EffectiveTimes.format(concept.getEffectiveTime()));
break;
case STATUS_LABEL:
dataRow.add(concept.isActive() ? "Active" : "Inactive");
break;
case DEFINITION_STATUS:
dataRow.add(concept.getDefinitionStatusId());
break;
default:
break;
}
}
writer.write(joiner.join(dataRow));
writer.write(lineSeparator);
}
}
use of com.b2international.snowowl.snomed.datastore.internal.rf2.AbstractSnomedDsvExportItem in project snow-owl by b2ihealthcare.
the class SnomedSimpleTypeRefSetDSVExporter method computeHeader.
private void computeHeader(SnomedConcepts chunk) {
for (SnomedConcept concept : chunk) {
for (AbstractSnomedDsvExportItem exportItem : exportItems) {
switch(exportItem.getType()) {
case DESCRIPTION:
ComponentIdSnomedDsvExportItem descriptionItem = (ComponentIdSnomedDsvExportItem) exportItem;
String descriptionTypeId = descriptionItem.getComponentId();
Integer matchingDescriptions = concept.getDescriptions().stream().filter(d -> descriptionTypeId.equals(d.getTypeId())).collect(Collectors.reducing(0, description -> 1, Integer::sum));
descriptionCount.merge(descriptionTypeId, matchingDescriptions, Math::max);
break;
case RELATIONSHIP:
ComponentIdSnomedDsvExportItem relationshipItem = (ComponentIdSnomedDsvExportItem) exportItem;
String relationshipTypeId = relationshipItem.getComponentId();
Map<Integer, Integer> matchingRelationships = concept.getRelationships().stream().filter(r -> relationshipTypeId.equals(r.getTypeId()) && (Concepts.INFERRED_RELATIONSHIP.equals(r.getCharacteristicTypeId()) || Concepts.ADDITIONAL_RELATIONSHIP.equals(r.getCharacteristicTypeId()))).collect(Collectors.groupingBy(SnomedRelationship::getRelationshipGroup, Collectors.reducing(0, relationship -> 1, Integer::sum)));
matchingRelationships.entrySet().stream().forEach(entry -> {
propertyCountByGroup.compute(entry.getKey(), (key, oldValue) -> {
Map<String, Integer> propertyCountForGroup = ofNullable(oldValue).orElseGet(HashMap::new);
propertyCountForGroup.merge(relationshipTypeId, entry.getValue(), Math::max);
return propertyCountForGroup;
});
});
break;
case DATAYPE:
ComponentIdSnomedDsvExportItem dataTypeItem = (ComponentIdSnomedDsvExportItem) exportItem;
String dataTypeId = dataTypeItem.getComponentId();
Map<Integer, Integer> matchingMembers = concept.getMembers().stream().filter(m -> SnomedRefSetType.CONCRETE_DATA_TYPE.equals(m.type()) && m.isActive() && dataTypeId.equals(m.getProperties().get(SnomedRf2Headers.FIELD_TYPE_ID)) && (Concepts.INFERRED_RELATIONSHIP.equals(m.getProperties().get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)) || Concepts.ADDITIONAL_RELATIONSHIP.equals(m.getProperties().get(SnomedRf2Headers.FIELD_CHARACTERISTIC_TYPE_ID)))).collect(Collectors.groupingBy(m -> (Integer) m.getProperties().get(SnomedRf2Headers.FIELD_RELATIONSHIP_GROUP), Collectors.reducing(0, relationship -> 1, Integer::sum)));
matchingMembers.entrySet().stream().forEach(entry -> {
propertyCountByGroup.compute(entry.getKey(), (key, oldValue) -> {
Map<String, Integer> propertyCountForGroup = ofNullable(oldValue).orElseGet(HashMap::new);
propertyCountForGroup.merge(dataTypeId, entry.getValue(), Math::max);
return propertyCountForGroup;
});
});
break;
default:
// Single-use fields don't need to be counted in advance
break;
}
}
}
}
use of com.b2international.snowowl.snomed.datastore.internal.rf2.AbstractSnomedDsvExportItem in project snow-owl by b2ihealthcare.
the class SnomedSimpleTypeRefSetDSVExporter method writeHeader.
private void writeHeader(BufferedWriter writer) throws IOException {
Map<String, String> descriptionTypeIdMap = createTypeIdMap(Concepts.DESCRIPTION_TYPE_ROOT_CONCEPT);
// includes object and data attributes
Map<String, String> propertyTypeIdMap = createTypeIdMap(Concepts.CONCEPT_MODEL_ATTRIBUTE);
List<String> propertyHeader = newArrayList();
List<String> detailHeader = newArrayList();
for (AbstractSnomedDsvExportItem exportItem : exportItems) {
switch(exportItem.getType()) {
case DESCRIPTION:
{
ComponentIdSnomedDsvExportItem descriptionItem = (ComponentIdSnomedDsvExportItem) exportItem;
String typeId = descriptionItem.getComponentId();
String displayName = descriptionTypeIdMap.getOrDefault(typeId, descriptionItem.getDisplayName());
int occurrences = descriptionCount.get(typeId);
if (occurrences < 2) {
if (includeDescriptionId) {
propertyHeader.add(displayName);
propertyHeader.add(displayName);
detailHeader.add("ID");
detailHeader.add("Term");
} else {
propertyHeader.add(displayName);
detailHeader.add("");
}
} else {
for (int j = 1; j <= occurrences; j++) {
String numberedDisplayName = String.format("%s (%s)", displayName, j);
if (includeDescriptionId) {
propertyHeader.add(numberedDisplayName);
propertyHeader.add(numberedDisplayName);
detailHeader.add("ID");
detailHeader.add("Term");
} else {
propertyHeader.add(numberedDisplayName);
detailHeader.add("");
}
}
}
break;
}
case RELATIONSHIP:
{
ComponentIdSnomedDsvExportItem relationshipItem = (ComponentIdSnomedDsvExportItem) exportItem;
String typeId = relationshipItem.getComponentId();
String displayName = propertyTypeIdMap.getOrDefault(typeId, relationshipItem.getDisplayName());
for (Integer group : propertyCountByGroup.keySet()) {
Map<String, Integer> occurrencesByType = propertyCountByGroup.getOrDefault(group, NO_OCCURRENCES);
int occurrences = occurrencesByType.getOrDefault(typeId, 0);
if (occurrences < 1) {
continue;
}
String groupTag = group == 0 ? "" : String.format(" (AG%s)", group);
if (occurrences < 2) {
if (includeRelationshipId) {
String groupedDisplayName = displayName + groupTag;
propertyHeader.add(groupedDisplayName);
propertyHeader.add(groupedDisplayName);
detailHeader.add("ID");
detailHeader.add("Destination");
} else {
propertyHeader.add(displayName);
detailHeader.add("");
}
} else {
for (int j = 1; j <= occurrences; j++) {
String numberedDisplayName = String.format("%s (%s)%s", displayName, j, groupTag);
if (includeRelationshipId) {
propertyHeader.add(numberedDisplayName);
propertyHeader.add(numberedDisplayName);
detailHeader.add("ID");
detailHeader.add("Destination");
} else {
propertyHeader.add(numberedDisplayName);
detailHeader.add("");
}
}
}
}
break;
}
case DATAYPE:
{
ComponentIdSnomedDsvExportItem dataTypeItem = (ComponentIdSnomedDsvExportItem) exportItem;
String typeId = dataTypeItem.getComponentId();
String displayName = propertyTypeIdMap.getOrDefault(typeId, dataTypeItem.getDisplayName());
for (Integer groupId : propertyCountByGroup.keySet()) {
Map<String, Integer> occurrencesByType = propertyCountByGroup.getOrDefault(groupId, NO_OCCURRENCES);
int occurrences = occurrencesByType.getOrDefault(typeId, 0);
if (occurrences < 1) {
continue;
}
String groupTag = groupId == 0 ? "" : String.format(" (AG%s)", groupId);
if (occurrences < 2) {
String groupedDisplayName = displayName + groupTag;
propertyHeader.add(groupedDisplayName);
detailHeader.add("");
} else {
for (int j = 1; j <= occurrences; j++) {
String numberedDisplayName = String.format("%s (%s)%s", displayName, j, groupTag);
propertyHeader.add(numberedDisplayName);
detailHeader.add("");
}
}
}
break;
}
case PREFERRED_TERM:
if (includeDescriptionId) {
propertyHeader.add(exportItem.getDisplayName());
propertyHeader.add(exportItem.getDisplayName());
detailHeader.add("ID");
detailHeader.add("Term");
} else {
propertyHeader.add(exportItem.getDisplayName());
detailHeader.add("");
}
break;
default:
propertyHeader.add(exportItem.getDisplayName());
detailHeader.add("");
break;
}
}
// write the header to the file
writer.write(joiner.join(propertyHeader));
writer.write(lineSeparator);
if (includeDescriptionId || includeRelationshipId) {
writer.write(joiner.join(detailHeader));
writer.write(lineSeparator);
}
}
Aggregations