Skip to content

Commit

Permalink
Various ADL serializer improvements (#652)
Browse files Browse the repository at this point in the history
* Fix double newline after rules section

* Prevent trailing whitespace after complex object constraints

* Add comment after closed archetype slots

* Remove extra space in archetype slots

* Add comment after archetype slots without constraints

* Add comment after archetype roots without attributes
  • Loading branch information
J3173 authored Jan 9, 2025
1 parent e071e79 commit b7fbc56
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected void appendRules() {

builder.newline().append("rules").newIndentedLine();
rulesSerializer.appendRules(archetype.getRules());
builder.newUnindentedLine();
builder.unindent().tryNewLine();
}

protected void appendRmOverlay() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void serialize(ArchetypeSlot cobj) {
.append(cobj.getRmTypeName()).append("[").append(cobj.getNodeId()).append("]");
if(cobj.isClosed()) {
builder.append(" closed");
builder.lineComment(serializer.getTermText(cobj));
} else {
if (cobj.getOccurrences() != null) {
builder.append(" occurrences matches {");
Expand All @@ -44,11 +45,11 @@ public void serialize(ArchetypeSlot cobj) {

private void appendMatches(ArchetypeSlot cobj) {
int mark = builder.mark();
builder.append(" matches { ");
builder.append(" matches {");
builder.lineComment(serializer.getTermText(cobj));
boolean hasContent = false;

ADLRulesSerializer serializer = new ADLRulesSerializer(builder, super.serializer);
ADLRulesSerializer rulesSerializer = new ADLRulesSerializer(builder, super.serializer);
if (cobj.getIncludes() != null && cobj.getIncludes().size() > 0) {
hasContent = true;
builder.indent().newline()
Expand All @@ -57,7 +58,7 @@ private void appendMatches(ArchetypeSlot cobj) {

for (Assertion a : cobj.getIncludes()) {
builder.newline();
serializer.serializeRuleElement(a.getExpression());
rulesSerializer.serializeRuleElement(a.getExpression());
}
builder.unindent().unindent();
}
Expand All @@ -68,7 +69,7 @@ private void appendMatches(ArchetypeSlot cobj) {
.indent();
for (Assertion a : cobj.getExcludes()) {
builder.newline();
serializer.serializeRuleElement(a.getExpression());
rulesSerializer.serializeRuleElement(a.getExpression());
}
builder.unindent().unindent();
}
Expand All @@ -78,6 +79,7 @@ private void appendMatches(ArchetypeSlot cobj) {
builder.newline().append("}");
} else {
builder.revert(mark);
builder.lineComment(serializer.getTermText(cobj));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public void serialize(CArchetypeRoot cobj) {
builder.lineComment(serializer.getTermText(cobj));
buildAttributesAndTuples(cobj);
builder.append("}");
} else {
builder.lineComment(serializer.getTermText(cobj));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ public void serialize(T cobj) {
if (cobj.getNodeId() != null) {
builder.append("[").append(cobj.getNodeId()).append("]");
}
builder.append(" ");
appendOccurrences(cobj);
if (cobj.getAttributes().isEmpty() && cobj.getAttributeTuples().isEmpty() && cobj.getDefaultValue() == null) {
builder.lineComment(serializer.getTermText(cobj));
} else {
builder.append("matches {");
builder.ensureSpace().append("matches {");
builder.lineComment(serializer.getTermText(cobj));
buildAttributesAndTuples(cobj);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void appendOccurrences(T cobj) {
builder.ensureSpace();
builder.append("occurrences matches {");
buildOccurrences(builder, cobj.getOccurrences());
builder.append("} ");
builder.append("}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ public void serializeTerminologyCode() {
assertPrimitive("terminology_code_attr102", "[ac1; at1003]");
}

@Test
public void serializeBasicCComplexObject() throws Exception {
Archetype archetype = loadRoot("adl2-tests/features/aom_structures/basic/openEHR-TEST_PKG-WHOLE.most_minimal.v1.0.0.adls");
CComplexObject whole = archetype.getDefinition();
String serialized = serializeConstraint(whole);
assertThat(serialized, equalTo("\n WHOLE[id1] -- most minimal"));

whole.setOccurrences(MultiplicityInterval.createMandatory());
String serialized2 = serializeConstraint(whole);
assertThat(serialized2, equalTo("\n WHOLE[id1] occurrences matches {1} -- most minimal"));
}

@Test
public void serializeTupleOrdinal() throws Exception {
Archetype archetype = loadRoot("adl2-tests/features/aom_structures/tuples/CIMI-CORE-ITEM_GROUP.real_ordinal.v1.0.0.adls");
Expand Down Expand Up @@ -244,7 +256,7 @@ public void serializeArchetypeSlot() throws Exception {
Archetype archetype = loadRoot("adl2-tests/features/aom_structures/slots/openEHR-EHR-SECTION.slot_include_any_exclude_empty.v1.0.0.adls");
ArchetypeSlot slot = archetype.itemAtPath("/items[id2]");
String serialized = serializeConstraint(slot);
assertEquals("\n allow_archetype OBSERVATION[id2] occurrences matches {0..1} matches { -- Vital signs\n" +
assertEquals("\n allow_archetype OBSERVATION[id2] occurrences matches {0..1} matches { -- Vital signs\n" +
" include\n" +
" archetype_id/value matches {/.*/}\n" +
" }", serialized);
Expand All @@ -253,7 +265,7 @@ public void serializeArchetypeSlot() throws Exception {
archetype = load("openEHR-EHR-CLUSTER.device.v1.adls");
slot = archetype.itemAtPath("/items[id10]");
serialized = serializeConstraint(slot);
assertEquals("\n allow_archetype CLUSTER[id10] occurrences matches {0..*} matches { -- Properties\n" +
assertEquals("\n allow_archetype CLUSTER[id10] occurrences matches {0..*} matches { -- Properties\n" +
" include\n" +
" archetype_id/value matches {/openEHR-EHR-CLUSTER\\.dimensions(-a-zA-Z0-9_]+)*\\.v1|openEHR-EHR-CLUSTER\\.catheter(-a-zA-Z0-9_]+)*\\.v1/}\n" +
" }", serialized);
Expand All @@ -262,15 +274,20 @@ public void serializeArchetypeSlot() throws Exception {
archetype = loadRoot("adl2-tests/features/aom_structures/slots/openEHR-EHR-SECTION.slot_include_empty_exclude_non_any.v1.0.0.adls");
slot = archetype.itemAtPath("/items[id2]");
serialized = serializeConstraint(slot);
assertEquals("\n allow_archetype OBSERVATION[id2] occurrences matches {0..1} matches { -- Vital signs\n" +
assertEquals("\n allow_archetype OBSERVATION[id2] occurrences matches {0..1} matches { -- Vital signs\n" +
" exclude\n" +
" archetype_id/value matches {/openEHR-EHR-OBSERVATION\\.blood_pressure([a-zA-Z0-9_]+)*\\.v1/}\n" +
" }", serialized);

archetype = loadRoot("adl2-tests/features/aom_structures/slots/openEHR-EHR-SECTION.slot_include_empty_exclude_empty.v1.0.0.adls");
slot = archetype.itemAtPath("/items[id2]");
serialized = serializeConstraint(slot);
assertEquals("\n allow_archetype OBSERVATION[id2] occurrences matches {0..1} -- Vital signs", serialized);

archetype = loadRoot("adl2-tests/features/aom_structures/slots/openEHR-EHR-SECTION.slot_closed.v1.0.0.adls");
slot = archetype.itemAtPath("/items[id2]");
serialized = serializeConstraint(slot);
assertEquals("\n allow_archetype OBSERVATION[id2] closed", serialized);
assertEquals("\n allow_archetype OBSERVATION[id2] closed -- Vital signs", serialized);
}

@Test
Expand All @@ -280,9 +297,9 @@ public void serializeCArchetypeRoot() throws IOException, ADLParseException {
List<CObject> archetypeRoots = archetype.getDefinition().getAttribute("content").getChildren();

assertThat(serializeConstraint(archetypeRoots.get(0)).trim(),
equalTo("use_archetype SECTION[id2, openEHR-EHR-SECTION.section_parent.v1] occurrences matches {0..1}"));
equalTo("use_archetype SECTION[id2, openEHR-EHR-SECTION.section_parent.v1] occurrences matches {0..1} -- Section"));
assertThat(serializeConstraint(archetypeRoots.get(1)).trim(),
equalTo("use_archetype OBSERVATION[id3, openEHR-EHR-OBSERVATION.spec_test_obs.v1] occurrences matches {1}"));
equalTo("use_archetype OBSERVATION[id3, openEHR-EHR-OBSERVATION.spec_test_obs.v1] occurrences matches {1} -- Observation"));
}

@Test
Expand Down

0 comments on commit b7fbc56

Please sign in to comment.