Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix matix multi #386

Merged
merged 5 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Note: version releases in the 0.x.y range may introduce breaking changes.
## [unreleased]
### Added
### Fixed
- new Matrix serialisation use rm-model for index and fix section handling ([#386](https://github.com/ehrbase/openEHR_SDK/pull/386))

## [1.20.0]
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.ehrbase.aql.dto.path.AqlPath;
import org.ehrbase.serialisation.walker.Context;
import org.ehrbase.serialisation.walker.FromCompositionWalker;
import org.ehrbase.serialisation.walker.RmPrimitive;
import org.ehrbase.util.exception.SdkException;
import org.ehrbase.webtemplate.interpreter.Interpreter;
import org.ehrbase.webtemplate.model.WebTemplateNode;

/**
Expand All @@ -58,12 +58,19 @@
public class CompositionToMatrixWalker extends FromCompositionWalker<FromWalkerDto> {

public static final String MAGNITUDE = "/magnitude";
private List<String> resolveTo = List.of("OBSERVATION", "EVALUATION", "INSTRUCTION", "ACTION", "ADMIN_ENTRY");
private List<String> resolveTo =
List.of("OBSERVATION", "EVALUATION", "INSTRUCTION", "ACTION", "ADMIN_ENTRY", "SECTION");

@Override
protected FromWalkerDto extract(
Context<FromWalkerDto> context, WebTemplateNode child, boolean isChoice, Integer i) {

if (i == null
&& Interpreter.findRmAttributeInfo(context.getNodeDeque().peek(), child)
.isMultipleValued()) {
i = 0;
}

FromWalkerDto next = new FromWalkerDto(context.getObjectDeque().peek());

// this belongs to composition thus COMPOSITION is the root
Expand All @@ -73,14 +80,12 @@ protected FromWalkerDto extract(
}

// Is this a RM type to which we resolve to, then add a new Entity
if (child.getNodeId() != null
&& findTypeName(child.getNodeId()) != null
&& resolveTo.contains(findTypeName(child.getNodeId()))) {
if (child.getNodeId() != null && resolveTo.contains(child.getRmType())) {

Entity nextEntity = new Entity(next.getCurrentEntity());
nextEntity.setArchetypeId(child.getNodeId());
nextEntity.setPathFromRoot(
child.getAqlPathDto().removeStart(next.getCurrentEntity().getPathFromRoot()));
nextEntity.setRmType(child.getRmType());
nextEntity.setPathFromRoot(child.getAqlPathDto());
if (i != null) {
nextEntity.getEntityIdx().add(child.getAqlPath(), i);
}
Expand Down Expand Up @@ -288,18 +293,4 @@ private void add(FromWalkerDto fromWalkerDto, AqlPath relativ, Object o) {
.putAll(flatten(relativ, MARSHAL_OM.valueToTree(o)));
}
}

static String findTypeName(String atCode) {
String typeName = null;

if (atCode.contains("openEHR-EHR-")) {

typeName = StringUtils.substringBetween(atCode, "openEHR-EHR-", ".");
} else if (atCode.startsWith("at")) {
typeName = null;
} else {
typeName = atCode;
}
return typeName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Entity {
private AqlPath pathFromRoot;
private String archetypeId;

private String rmType;

private Index entityIdx = new Index();

public Entity() {}
Expand All @@ -36,6 +38,7 @@ public Entity(Entity other) {
this.pathFromRoot = AqlPath.parse(other.pathFromRoot.getPath());
this.archetypeId = other.archetypeId;
this.entityIdx = new Index(other.entityIdx);
this.rmType = other.getRmType();
}

public AqlPath getPathFromRoot() {
Expand All @@ -62,8 +65,17 @@ public void setEntityIdx(Index entityIdx) {
this.entityIdx = entityIdx;
}

public String getRmType() {
return rmType;
}

public void setRmType(String rmType) {
this.rmType = rmType;
}

@Override
public boolean equals(Object o) {

if (this == o) {
return true;
}
Expand All @@ -73,19 +85,21 @@ public boolean equals(Object o) {
Entity entity = (Entity) o;
return Objects.equals(pathFromRoot, entity.pathFromRoot)
&& Objects.equals(archetypeId, entity.archetypeId)
&& Objects.equals(rmType, entity.rmType)
&& Objects.equals(entityIdx, entity.entityIdx);
}

@Override
public int hashCode() {
return Objects.hash(pathFromRoot, archetypeId, entityIdx);
return Objects.hash(pathFromRoot, archetypeId, rmType, entityIdx);
}

@Override
public String toString() {
return "Resolve{" + "pathFromRoot="
return "Entity{" + "pathFromRoot="
+ pathFromRoot + ", archetypeId='"
+ archetypeId + '\'' + ", count="
+ archetypeId + '\'' + ", rmType='"
+ rmType + '\'' + ", entityIdx="
+ entityIdx + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package org.ehrbase.serialisation.matrixencoding;

import static org.ehrbase.serialisation.matrixencoding.CompositionToMatrixWalker.findTypeName;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -100,6 +98,7 @@ Map<Entity, Map<Index, Map<AqlPath, Object>>> toMatrix(Composition composition)
Entity currentEntity = new Entity();
currentEntity.setPathFromRoot(AqlPath.ROOT_PATH);
currentEntity.setArchetypeId(composition.getArchetypeNodeId());
currentEntity.setRmType("COMPOSITION");
FromWalkerDto fromWalkerDto = new FromWalkerDto();
fromWalkerDto.updateEntity(currentEntity);
new CompositionToMatrixWalker()
Expand All @@ -123,6 +122,9 @@ public List<Row> toTable(Composition composition) {
flatten.forEach(this::encode);
}

flatten.forEach(r -> r.setTemplateId(
composition.getArchetypeDetails().getTemplateId().getValue()));

return flatten;
}

Expand All @@ -146,6 +148,7 @@ private Row toRow(Entity entity, Map.Entry<Index, Map<AqlPath, Object>> e) {
row.setEntityIdx(entity.getEntityIdx().getRepetitions());
row.setArchetypeId(entity.getArchetypeId());
row.setEntityPath(entity.getPathFromRoot());
row.setRmType(entity.getRmType());
row.setFields(e.getValue());
row.setFieldIdx(e.getKey().getRepetitions());
return row;
Expand Down Expand Up @@ -187,6 +190,7 @@ private Row toRow(CSVRecord csvRecord) {
row.setEntityIdx(buildArray(csvRecord.get(HEADERS.ENTITY_IDX)));
row.setFieldIdx(buildArray(csvRecord.get(HEADERS.FIELD_IDX)));
row.setArchetypeId(csvRecord.get(HEADERS.ENTITY_CONCEPT));
row.setRmType(csvRecord.get(HEADERS.RM_ENTITY));
row.setEntityPath(AqlPath.parse(csvRecord.get(HEADERS.ENTITY_PATH)));
try {
Map<String, Object> map = MAPPER.readValue(
Expand Down Expand Up @@ -240,7 +244,7 @@ private static void printRow(CSVPrinter printer, Row r) {
printer.printRecord(
r.getNum(),
r.getArchetypeId(),
findTypeName(r.getArchetypeId()),
r.getRmType(),
r.getEntityPath().format(AqlPath.OtherPredicatesFormat.SHORTED, true),
printArray(r.getEntityIdx()),
printArray(r.getFieldIdx()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.ehrbase.serialisation.walker.Context;
import org.ehrbase.serialisation.walker.ToCompositionWalker;
import org.ehrbase.util.exception.SdkException;
import org.ehrbase.webtemplate.interpreter.Interpreter;
import org.ehrbase.webtemplate.model.WebTemplateNode;

/**
Expand Down Expand Up @@ -87,6 +88,12 @@ protected List<ToWalkerDto> extract(
return null;
}

if (i == null
&& Interpreter.findRmAttributeInfo(context.getNodeDeque().peek(), child)
.isMultipleValued()) {
i = 0;
}

List<ToWalkerDto> filter = filter(context.getObjectDeque().peek(), child.getAqlPathDto(), i != null, i);

// Check that the type is correct
Expand All @@ -101,6 +108,9 @@ && filter(filter, child.getAqlPathDto().addEnd("/_type"), false, null).stream()
if (filter.isEmpty()) {
return null;
}
if (i != null) {
filter = filter.stream().map(MatrixToCompositionWalker::removeIndex).collect(Collectors.toList());
}
return filter;
}

Expand Down Expand Up @@ -199,6 +209,10 @@ private static Map<String, Object> unflatten(List<ToWalkerDto> entries) {
.collect(Collectors.groupingBy(e -> e.path.getBaseNode().getName()));

return collect.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> {
if (e.getKey() == null) {
System.out.println("ff");
vmueller-vg marked this conversation as resolved.
Show resolved Hide resolved
}

// Elementar Value found
if (e.getValue().size() == 1 && e.getValue().get(0).path.getNodeCount() <= 1) {
return e.getValue().get(0).value;
Expand Down Expand Up @@ -346,4 +360,11 @@ private static boolean matches(ToWalkerDto toWalkerDto, AqlPath path, boolean ch

return index == null || toWalkerDto.index.get(0).equals(index);
}

private static ToWalkerDto removeIndex(ToWalkerDto current) {

ToWalkerDto toWalkerDto = new ToWalkerDto(current);
toWalkerDto.index.remove(0);
return toWalkerDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class Row {
private AqlPath entityPath;
private String archetypeId;

private String rmType;

private String templateId;

private Integer[] entityIdx;

private Integer[] fieldIdx;
Expand Down Expand Up @@ -76,6 +80,22 @@ public void setFields(Map<AqlPath, Object> fields) {
this.fields = fields;
}

public String getTemplateId() {
return templateId;
}

public void setTemplateId(String templateId) {
this.templateId = templateId;
}

public String getRmType() {
return rmType;
}

public void setRmType(String rmType) {
this.rmType = rmType;
}

public int getNum() {
return num;
}
Expand All @@ -96,14 +116,16 @@ public boolean equals(Object o) {
return num == row.num
&& Objects.equals(entityPath, row.entityPath)
&& Objects.equals(archetypeId, row.archetypeId)
&& Objects.equals(rmType, row.rmType)
&& Objects.equals(templateId, row.templateId)
&& Arrays.equals(entityIdx, row.entityIdx)
&& Arrays.equals(fieldIdx, row.fieldIdx)
&& Objects.equals(fields, row.fields);
}

@Override
public int hashCode() {
int result = Objects.hash(num, entityPath, archetypeId, fields);
int result = Objects.hash(num, entityPath, archetypeId, rmType, templateId, fields);
result = 31 * result + Arrays.hashCode(entityIdx);
result = 31 * result + Arrays.hashCode(fieldIdx);
return result;
Expand All @@ -112,11 +134,13 @@ public int hashCode() {
@Override
public String toString() {
return "Row{" + "num="
+ num + ", pathFromRoot="
+ num + ", entityPath="
+ entityPath + ", archetypeId='"
+ archetypeId + '\'' + ", count="
+ Arrays.toString(entityIdx) + ", index="
+ Arrays.toString(fieldIdx) + ", other="
+ archetypeId + '\'' + ", rmType='"
+ rmType + '\'' + ", templateId='"
+ templateId + '\'' + ", entityIdx="
+ Arrays.toString(entityIdx) + ", fieldIdx="
+ Arrays.toString(fieldIdx) + ", fields="
+ fields + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.ehrbase.serialisation.matrixencoding;

import java.util.ArrayList;
import java.util.List;
import org.ehrbase.aql.dto.path.AqlPath;

Expand All @@ -28,4 +29,12 @@ class ToWalkerDto {
AqlPath path;
List<Integer> index;
Object value;

public ToWalkerDto() {}

public ToWalkerDto(ToWalkerDto other) {
this.path = other.path;
this.index = new ArrayList<>(other.index);
this.value = other.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,6 @@ void toMatrixIPS() throws IOException {
assertThat(actual).isEqualToNormalizingNewlines(expected);
}

@Test
void toMatrixIPSEncode() throws IOException {

String corona = IOUtils.toString(CompositionTestDataCanonicalJson.IPS.getStream(), StandardCharsets.UTF_8);

MatrixFormat cut = new MatrixFormat(new TestDataTemplateProvider(), new FixedCodeSetEncoder());

String actual = cut.marshal(new CanonicalJson().unmarshal(corona));

String expected =
IOUtils.toString(MatrixFormat.class.getResourceAsStream("/csv/IPSEncode.csv"), StandardCharsets.UTF_8);

assertThat(actual).isEqualToNormalizingNewlines(expected);
}

@Test
void toMatrixCoronaEncode() throws IOException {

String corona = IOUtils.toString(CompositionTestDataCanonicalJson.CORONA.getStream(), StandardCharsets.UTF_8);

MatrixFormat cut = new MatrixFormat(new TestDataTemplateProvider(), new FixedCodeSetEncoder());

String actual = cut.marshal(new CanonicalJson().unmarshal(corona));

String expected = IOUtils.toString(
MatrixFormat.class.getResourceAsStream("/csv/CORONAEncode.csv"), StandardCharsets.UTF_8);

assertThat(actual).isEqualToNormalizingNewlines(expected);
}

@Test
void fromMatrix() throws IOException {
MatrixFormat cut = new MatrixFormat(new TestDataTemplateProvider());
Expand Down
Loading