Skip to content

Commit a24e800

Browse files
committed
add checks for dv_proportion and respect inclusion of lower/upper
1 parent 7d8c8fd commit a24e800

File tree

1 file changed

+198
-29
lines changed

1 file changed

+198
-29
lines changed

src/main/java/de/uksh/medic/etl/openehrmapper/Generator.java

Lines changed: 198 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -762,18 +762,13 @@ public void gen_DV_QUANTITY(String path, String name, Object jsonmap,
762762
if (magnitude == null) {
763763
return;
764764
}
765-
Double lower = getBounds(path, "lower");
766-
Double upper = getBounds(path, "upper");
767765

768-
if ((lower == null || lower <= magnitude) && (upper == null || magnitude <= upper)) {
766+
if (isInBounds(path, unit, magnitude, violations)) {
769767
DvQuantity dvq = new DvQuantity(unit, magnitude, precision);
770768
if (unitDisplayName != null) {
771769
dvq.setUnitsDisplayName(unitDisplayName);
772770
}
773771
((Element) jsonmap).setValue(dvq);
774-
} else {
775-
violations.add(new Violation(magnitude, lower, upper, unit));
776-
Logger.error("Value " + magnitude + unit + " is not between bounds " + lower + " - " + upper);
777772
}
778773
}
779774

@@ -782,24 +777,20 @@ public void gen_DV_COUNT(String path, String name, Object jsonmap,
782777

783778
Long count = map.get(name);
784779

785-
Long lower = getBoundsCount(path, "lower");
786-
Long upper = getBoundsCount(path, "upper");
787-
788-
if ((lower == null || lower <= count) && (upper == null || count <= upper)) {
780+
if (isInBoundsCount(path, count, violations)) {
789781
((Element) jsonmap).setValue(new DvCount(count));
790-
} else {
791-
violations.add(new Violation(count, lower, upper));
792-
Logger.error("Value " + count + " is not between bounds " + lower + " - " + upper);
793782
}
794783

795784
}
796785

797786
public void gen_DV_PROPORTION(String path, String name, Object jsonmap,
798-
Map<String, Object> map, Map<String, Object> datatypes, List<Violation> violations) {
787+
Map<String, Object> map, Map<String, Object> datatypes, List<Violation> violations) throws Exception {
799788
switch (map.get(name)) {
800789
case String[] s -> {
801-
DvProportion dvp = new DvProportion(Double.valueOf(s[0]), Double.valueOf(s[1]), Long.valueOf(s[2]));
802-
((Element) jsonmap).setValue(dvp);
790+
if (isInBoundsProportion(path, Double.valueOf(s[0]), violations)) {
791+
DvProportion dvp = new DvProportion(Double.valueOf(s[0]), Double.valueOf(s[1]), Long.valueOf(s[2]));
792+
((Element) jsonmap).setValue(dvp);
793+
}
803794
}
804795
default -> {
805796
}
@@ -859,23 +850,201 @@ public void gen_DV_URI(String path, String name, Object jsonmap,
859850

860851
// XPath Query functions
861852

862-
private Double getBounds(String path, String type) throws Exception {
863-
String newPath = path + "/list/magnitude/" + type + "/text()";
864-
if (!cache.containsKey(newPath)) {
865-
XPathExpression expr = XP.compile(newPath);
866-
cache.put(newPath, (String) expr.evaluate(opt, XPathConstants.STRING));
853+
private boolean isInBounds(String path, String unit, Double magnitude, List<Violation> violations)
854+
throws Exception {
855+
String lowerPathValue = path + "/list[units=\"" + unit + "\"]/magnitude/lower/text()";
856+
String lowerPathInclude = path + "/list[units=\"" + unit + "\"]/magnitude/lower_included/text()";
857+
String lowerPathUnbound = path + "/list[units=\"" + unit + "\"]/magnitude/lower_unbounded/text()";
858+
String upperPathValue = path + "/list[units=\"" + unit + "\"]/magnitude/upper/text()";
859+
String upperPathInclude = path + "/list[units=\"" + unit + "\"]/magnitude/upper_included/text()";
860+
String upperPathUnbound = path + "/list[units=\"" + unit + "\"]/magnitude/upper_unbounded/text()";
861+
if (!cache.containsKey(lowerPathValue)) {
862+
XPathExpression expr = XP.compile(lowerPathValue);
863+
cache.put(lowerPathValue, (String) expr.evaluate(opt, XPathConstants.STRING));
864+
}
865+
if (!cache.containsKey(lowerPathInclude)) {
866+
XPathExpression expr = XP.compile(lowerPathInclude);
867+
cache.put(lowerPathInclude, (String) expr.evaluate(opt, XPathConstants.STRING));
867868
}
868-
return "".equals(cache.get(newPath)) ? null : Double.valueOf(cache.get(newPath));
869+
if (!cache.containsKey(lowerPathUnbound)) {
870+
XPathExpression expr = XP.compile(lowerPathUnbound);
871+
cache.put(lowerPathUnbound, (String) expr.evaluate(opt, XPathConstants.STRING));
872+
}
873+
if (!cache.containsKey(upperPathValue)) {
874+
XPathExpression expr = XP.compile(upperPathValue);
875+
cache.put(upperPathValue, (String) expr.evaluate(opt, XPathConstants.STRING));
876+
}
877+
if (!cache.containsKey(upperPathInclude)) {
878+
XPathExpression expr = XP.compile(upperPathInclude);
879+
cache.put(upperPathInclude, (String) expr.evaluate(opt, XPathConstants.STRING));
880+
}
881+
if (!cache.containsKey(upperPathUnbound)) {
882+
XPathExpression expr = XP.compile(upperPathUnbound);
883+
cache.put(upperPathUnbound, (String) expr.evaluate(opt, XPathConstants.STRING));
884+
}
885+
Double lowerValue = "".equals(cache.get(lowerPathValue)) ? null : Double.valueOf(cache.get(lowerPathValue));
886+
boolean lowerInclude = "".equals(cache.get(lowerPathInclude)) ? null
887+
: Boolean.parseBoolean(cache.get(lowerPathInclude));
888+
boolean lowerUnbound = "".equals(cache.get(lowerPathUnbound)) ? true
889+
: Boolean.parseBoolean(cache.get(lowerPathUnbound));
890+
891+
Double upperValue = "".equals(cache.get(upperPathValue)) ? null : Double.valueOf(cache.get(upperPathValue));
892+
boolean upperInclude = "".equals(cache.get(upperPathInclude)) ? null
893+
: Boolean.parseBoolean(cache.get(upperPathInclude));
894+
boolean upperUnbound = "".equals(cache.get(upperPathUnbound)) ? true
895+
: Boolean.parseBoolean(cache.get(upperPathUnbound));
896+
897+
boolean lower = lowerUnbound || lowerValue == null
898+
|| (lowerInclude ? lowerValue <= magnitude : lowerValue < magnitude);
899+
900+
boolean upper = upperUnbound || upperValue == null
901+
|| (upperInclude ? magnitude <= upperValue : magnitude < upperValue);
902+
903+
if (!lower || !upper) {
904+
violations.add(new Violation(magnitude, lower, upper, unit));
905+
String lowerSign = lowerInclude ? "[" : "(";
906+
String upperSign = upperInclude ? "]" : ")";
907+
Logger.error("Value " + magnitude + unit + " is not between bounds " + lowerSign + lowerValue + " - "
908+
+ upperValue + upperSign);
909+
}
910+
911+
return lower && upper;
912+
869913
}
870914

871-
private Long getBoundsCount(String path, String type) throws Exception {
872-
String newPath = path + "/attributes[rm_attribute_name = \"magnitude\"]/children/item/range/" + type
873-
+ "/text()";
874-
if (!cache.containsKey(newPath)) {
875-
XPathExpression expr = XP.compile(newPath);
876-
cache.put(newPath, (String) expr.evaluate(opt, XPathConstants.STRING));
915+
private boolean isInBoundsCount(String path, Long count, List<Violation> violations) throws Exception {
916+
String lowerPathValue = path
917+
+ "/attributes[rm_attribute_name = \"magnitude\"]/children/item/range/lower/text()";
918+
String lowerPathInclude = path
919+
+ "/attributes[rm_attribute_name = \"magnitude\"]/children/item/range/lower_included/text()";
920+
String lowerPathUnbound = path
921+
+ "/attributes[rm_attribute_name = \"magnitude\"]/children/item/range/lower_unbounded/text()";
922+
String upperPathValue = path
923+
+ "/attributes[rm_attribute_name = \"magnitude\"]/children/item/range/upper/text()";
924+
String upperPathInclude = path
925+
+ "/attributes[rm_attribute_name = \"magnitude\"]/children/item/range/upper_included/text()";
926+
String upperPathUnbound = path
927+
+ "/attributes[rm_attribute_name = \"magnitude\"]/children/item/range/upper_unbounded/text()";
928+
if (!cache.containsKey(lowerPathValue)) {
929+
XPathExpression expr = XP.compile(lowerPathValue);
930+
cache.put(lowerPathValue, (String) expr.evaluate(opt, XPathConstants.STRING));
931+
}
932+
if (!cache.containsKey(lowerPathInclude)) {
933+
XPathExpression expr = XP.compile(lowerPathInclude);
934+
cache.put(lowerPathInclude, (String) expr.evaluate(opt, XPathConstants.STRING));
935+
}
936+
if (!cache.containsKey(lowerPathUnbound)) {
937+
XPathExpression expr = XP.compile(lowerPathUnbound);
938+
cache.put(lowerPathUnbound, (String) expr.evaluate(opt, XPathConstants.STRING));
939+
}
940+
if (!cache.containsKey(upperPathValue)) {
941+
XPathExpression expr = XP.compile(upperPathValue);
942+
cache.put(upperPathValue, (String) expr.evaluate(opt, XPathConstants.STRING));
943+
}
944+
if (!cache.containsKey(upperPathInclude)) {
945+
XPathExpression expr = XP.compile(upperPathInclude);
946+
cache.put(upperPathInclude, (String) expr.evaluate(opt, XPathConstants.STRING));
947+
}
948+
if (!cache.containsKey(upperPathUnbound)) {
949+
XPathExpression expr = XP.compile(upperPathUnbound);
950+
cache.put(upperPathUnbound, (String) expr.evaluate(opt, XPathConstants.STRING));
951+
}
952+
Long lowerValue = "".equals(cache.get(lowerPathValue)) ? null : Long.valueOf(cache.get(lowerPathValue));
953+
boolean lowerInclude = "".equals(cache.get(lowerPathInclude)) ? null
954+
: Boolean.parseBoolean(cache.get(lowerPathInclude));
955+
boolean lowerUnbound = "".equals(cache.get(lowerPathUnbound)) ? true
956+
: Boolean.parseBoolean(cache.get(lowerPathUnbound));
957+
958+
Long upperValue = "".equals(cache.get(upperPathValue)) ? null : Long.valueOf(cache.get(upperPathValue));
959+
boolean upperInclude = "".equals(cache.get(upperPathInclude)) ? null
960+
: Boolean.parseBoolean(cache.get(upperPathInclude));
961+
boolean upperUnbound = "".equals(cache.get(upperPathUnbound)) ? true
962+
: Boolean.parseBoolean(cache.get(upperPathUnbound));
963+
964+
boolean lower = lowerUnbound || lowerValue == null
965+
|| (lowerInclude ? lowerValue <= count : lowerValue < count);
966+
967+
boolean upper = upperUnbound || upperValue == null
968+
|| (upperInclude ? count <= upperValue : count < upperValue);
969+
970+
if (!lower || !upper) {
971+
violations.add(new Violation(count, lower, upper));
972+
String lowerSign = lowerInclude ? "[" : "(";
973+
String upperSign = upperInclude ? "]" : ")";
974+
Logger.error("Value " + count + " is not between bounds " + lowerSign + lowerValue + " - " + upperValue
975+
+ upperSign);
877976
}
878-
return "".equals(cache.get(newPath)) ? null : Long.valueOf(cache.get(newPath));
977+
978+
return lower && upper;
979+
980+
}
981+
982+
private boolean isInBoundsProportion(String path, Double numerator, List<Violation> violations) throws Exception {
983+
String lowerPathValue = path
984+
+ "/attributes[rm_attribute_name = \"numerator\"]/children/item/range/lower/text()";
985+
String lowerPathInclude = path
986+
+ "/attributes[rm_attribute_name = \"numerator\"]/children/item/range/lower_included/text()";
987+
String lowerPathUnbound = path
988+
+ "/attributes[rm_attribute_name = \"numerator\"]/children/item/range/lower_unbounded/text()";
989+
String upperPathValue = path
990+
+ "/attributes[rm_attribute_name = \"numerator\"]/children/item/range/upper/text()";
991+
String upperPathInclude = path
992+
+ "/attributes[rm_attribute_name = \"numerator\"]/children/item/range/upper_included/text()";
993+
String upperPathUnbound = path
994+
+ "/attributes[rm_attribute_name = \"numerator\"]/children/item/range/upper_unbounded/text()";
995+
if (!cache.containsKey(lowerPathValue)) {
996+
XPathExpression expr = XP.compile(lowerPathValue);
997+
cache.put(lowerPathValue, (String) expr.evaluate(opt, XPathConstants.STRING));
998+
}
999+
if (!cache.containsKey(lowerPathInclude)) {
1000+
XPathExpression expr = XP.compile(lowerPathInclude);
1001+
cache.put(lowerPathInclude, (String) expr.evaluate(opt, XPathConstants.STRING));
1002+
}
1003+
if (!cache.containsKey(lowerPathUnbound)) {
1004+
XPathExpression expr = XP.compile(lowerPathUnbound);
1005+
cache.put(lowerPathUnbound, (String) expr.evaluate(opt, XPathConstants.STRING));
1006+
}
1007+
if (!cache.containsKey(upperPathValue)) {
1008+
XPathExpression expr = XP.compile(upperPathValue);
1009+
cache.put(upperPathValue, (String) expr.evaluate(opt, XPathConstants.STRING));
1010+
}
1011+
if (!cache.containsKey(upperPathInclude)) {
1012+
XPathExpression expr = XP.compile(upperPathInclude);
1013+
cache.put(upperPathInclude, (String) expr.evaluate(opt, XPathConstants.STRING));
1014+
}
1015+
if (!cache.containsKey(upperPathUnbound)) {
1016+
XPathExpression expr = XP.compile(upperPathUnbound);
1017+
cache.put(upperPathUnbound, (String) expr.evaluate(opt, XPathConstants.STRING));
1018+
}
1019+
Double lowerValue = "".equals(cache.get(lowerPathValue)) ? null : Double.valueOf(cache.get(lowerPathValue));
1020+
boolean lowerInclude = "".equals(cache.get(lowerPathInclude)) ? null
1021+
: Boolean.parseBoolean(cache.get(lowerPathInclude));
1022+
boolean lowerUnbound = "".equals(cache.get(lowerPathUnbound)) ? true
1023+
: Boolean.parseBoolean(cache.get(lowerPathUnbound));
1024+
1025+
Double upperValue = "".equals(cache.get(upperPathValue)) ? null : Double.valueOf(cache.get(upperPathValue));
1026+
boolean upperInclude = "".equals(cache.get(upperPathInclude)) ? null
1027+
: Boolean.parseBoolean(cache.get(upperPathInclude));
1028+
boolean upperUnbound = "".equals(cache.get(upperPathUnbound)) ? true
1029+
: Boolean.parseBoolean(cache.get(upperPathUnbound));
1030+
1031+
boolean lower = lowerUnbound || lowerValue == null
1032+
|| (lowerInclude ? lowerValue <= numerator : lowerValue < numerator);
1033+
1034+
boolean upper = upperUnbound || upperValue == null
1035+
|| (upperInclude ? numerator <= upperValue : numerator < upperValue);
1036+
1037+
if (!lower || !upper) {
1038+
violations.add(new Violation(numerator, lower, upper));
1039+
String lowerSign = lowerInclude ? "[" : "(";
1040+
String upperSign = upperInclude ? "]" : ")";
1041+
Logger.error("Value numerator " + numerator + " is not between bounds " + lowerSign + lowerValue + " - "
1042+
+ upperValue
1043+
+ upperSign);
1044+
}
1045+
1046+
return lower && upper;
1047+
8791048
}
8801049

8811050
private Boolean isMandatory(String path) throws Exception {

0 commit comments

Comments
 (0)