Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,24 @@ public String format(OtherPredicatesFormat otherPredicatesFormat, boolean includ
}

private String formatUncached(OtherPredicatesFormat otherPredicatesFormat, boolean includeAttributeName) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(estimateStringSize());
appendFormat(sb, otherPredicatesFormat, includeAttributeName);
return sb.toString();
}

private int estimateStringSize() {
return switch (nodes.length) {
case 0 -> 0;
case 1 -> 75;
case 2 -> 150;
case 3 -> 175;
case 4 -> 200;
case 5 -> 225;
case 6 -> 250;
default -> 275;
};
}

private void appendFormat(
StringBuilder sb, OtherPredicatesFormat otherPredicatesFormat, boolean includeAttributeName) {
if (isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static Predicate buildPredicate(CharSequence predicate) {
} else {

LogicalOperator predicateLogicalOperator = buildLogicalOperator(
Arrays.asList(boolList),
boolList,
s -> {
final LogicalOperator ret;
if (PredicateLogicalOperatorSymbol.AND.equals(s)) {
Expand All @@ -109,11 +109,11 @@ public static Predicate buildPredicate(CharSequence predicate) {
}

static Object[] parsePredicate(CharSequence predicate) {

List<CharSequence> split = AqlPathHelper.split(predicate, 0, -1, true, PREDICATES_MATCHER);
int l = split.size();

Object[] ret = new Object[split.size()];
for (int i = 0, l = split.size(); i < l; i++) {
Object[] ret = new Object[l];
for (int i = 0; i < l; i++) {
if (i % 2 == 0) {
ret[i] = handleOperator(split.get(i), i);
} else {
Expand All @@ -124,17 +124,11 @@ static Object[] parsePredicate(CharSequence predicate) {
}

private static PredicateLogicalOperatorSymbol handleSymbol(CharSequence sequence) {
switch (CharSequenceHelper.trim(sequence).toString()) {
case ",":
case "and":
case "AND":
return PredicateLogicalOperatorSymbol.AND;
case "or":
case "OR":
return PredicateLogicalOperatorSymbol.OR;
default:
throw new SdkException(String.format("Unknown symbol %s", sequence));
}
return switch (CharSequenceHelper.trim(sequence).toString()) {
case ",", "and", "AND" -> PredicateLogicalOperatorSymbol.AND;
case "or", "OR" -> PredicateLogicalOperatorSymbol.OR;
default -> throw new SdkException(String.format("Unknown symbol %s", sequence));
};
}

private static PredicateComparisonOperator handleOperator(CharSequence sequence, int i) {
Expand Down Expand Up @@ -166,17 +160,18 @@ private static PredicateComparisonOperator handleOperator(CharSequence sequence,

private static PathPredicateOperand parseValue(String statement, CharSequence s) {

if (s.length() > 0 && s.charAt(0) == '$') {
int length = s.length();
if (length > 0 && s.charAt(0) == '$') {
QueryParameter parameterValue = new QueryParameter();
parameterValue.setName(CharSequenceHelper.subSequence(s, 1).toString());
return parameterValue;
}

if (ARCHETYPE_NODE_ID.equals(statement)) {
return new StringPrimitive(s.toString());
} else if (s.length() > 1 && s.charAt(0) == '\'') {
} else if (length > 1 && s.charAt(0) == '\'') {
return new StringPrimitive(
CharSequenceHelper.subSequence(s, 1, s.length() - 1).toString());
CharSequenceHelper.subSequence(s, 1, length - 1).toString());
} else if (representsPlainInteger(s)) {
return new LongPrimitive(Long.parseLong(s.toString()));
} else {
Expand Down Expand Up @@ -446,7 +441,7 @@ private static <P extends DisjunctablePredicate> P removeInternal(
}

private static <S, T> LogicalOperator<S, T> buildLogicalOperator(
List<Object> boolList, Function<S, LogicalOperator<S, T>> creator, ToIntFunction<S> precedenceFunction) {
Object[] boolList, Function<S, LogicalOperator<S, T>> creator, ToIntFunction<S> precedenceFunction) {
OperatorStructure<S> structure = buildLogicalOperatorStructure(boolList, precedenceFunction);
return buildLogicalOperator(structure, creator);
}
Expand All @@ -467,15 +462,15 @@ private static <S, T> LogicalOperator<S, T> buildLogicalOperator(
}

private static <S> OperatorStructure<S> buildLogicalOperatorStructure(
List<Object> boolList, ToIntFunction<S> precedenceFunction) {
Object[] boolList, ToIntFunction<S> precedenceFunction) {

S currentSymbol = (S) boolList.get(1);
OperatorStructure<S> currentOperator = new OperatorStructure(currentSymbol, boolList.get(0));
S currentSymbol = (S) boolList[1];
OperatorStructure<S> currentOperator = new OperatorStructure<>(currentSymbol, boolList[0]);

OperatorStructure<S> lowestOperator = currentOperator;
for (int i = 2, l = boolList.size(); i < l; i += 2) {
S nextSymbol = i + 1 < l ? (S) boolList.get(i + 1) : null;
Object currentOpValue = boolList.get(i);
for (int i = 2, l = boolList.length; i < l; i += 2) {
S nextSymbol = i + 1 < l ? (S) boolList[i + 1] : null;
Object currentOpValue = boolList[i];
if (nextSymbol == null || Objects.equals(currentSymbol, nextSymbol)) {
currentOperator.addChild(currentOpValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,43 +330,27 @@ void addInputs(WebTemplateNode node, Map<String, WebTemplateInput> templateInput
break;

case RmConstants.DV_DURATION:
String pattern = Optional.ofNullable(templateInputMap.get("value"))
.map(WebTemplateInput::getValidation)
.map(WebTemplateValidation::getPattern)
.orElse(null);
Map<String, Integer> minConstrains =
buildDurationConstrains(Optional.ofNullable(templateInputMap.get("value"))
.map(WebTemplateInput::getValidation)
.map(WebTemplateValidation::getRange)
.map(WebTemplateInterval::getMin)
.map(Object::toString)
.orElse(null));
WebTemplateComparisonSymbol minOperator = Optional.ofNullable(templateInputMap.get("value"))
.map(WebTemplateInput::getValidation)
.map(WebTemplateValidation::getRange)
.map(WebTemplateInterval::getMinOp)
.orElse(null);

Map<String, Integer> maxConstrains =
buildDurationConstrains(Optional.ofNullable(templateInputMap.get("value"))
.map(WebTemplateInput::getValidation)
.map(WebTemplateValidation::getRange)
.map(WebTemplateInterval::getMax)
.map(Object::toString)
.orElse(null));
Optional<WebTemplateValidation> validation =
Optional.ofNullable(templateInputMap.get("value")).map(WebTemplateInput::getValidation);
String pattern =
validation.map(WebTemplateValidation::getPattern).orElse(null);
var range = validation.map(WebTemplateValidation::getRange);
Map<String, Integer> minConstrains = buildDurationConstrains(range.map(WebTemplateInterval::getMin)
.map(Object::toString)
.orElse(null));
WebTemplateComparisonSymbol minOperator =
range.map(WebTemplateInterval::getMinOp).orElse(null);

Map<String, Integer> maxConstrains = buildDurationConstrains(range.map(WebTemplateInterval::getMax)
.map(Object::toString)
.orElse(null));

Map<String, Integer> defaults =
buildDurationConstrains(findDefaultValue(node, "value").orElse(null));
Integer df = 0;
if (defaults.isEmpty()) {
df = null;
}
Integer df = defaults.isEmpty() ? null : 0;

WebTemplateComparisonSymbol maxOperator = Optional.ofNullable(templateInputMap.get("value"))
.map(WebTemplateInput::getValidation)
.map(WebTemplateValidation::getRange)
.map(WebTemplateInterval::getMaxOp)
.orElse(null);
WebTemplateComparisonSymbol maxOperator =
range.map(WebTemplateInterval::getMaxOp).orElse(null);

boolean blank = StringUtils.isBlank(pattern);
buildDurationInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,16 +653,20 @@
makeIdUnique(node);

cardinaltyList.forEach(p -> {
String[] nodeIds = p.getValue().stream()
.flatMap(s -> node.streamMatching(n -> n.getAqlPathDto().equals(s)))
.map(WebTemplateNode::getId)
.toArray(String[]::new);
// only add non-trivial cardinalities.
WebtemplateCardinality key = p.getKey();
Integer max = key.getMax();
if ((max != null && max != -1 && max < nodeIds.length) || (key.getMin() != null && key.getMin() > 1)) {
key.getIds().addAll(Arrays.asList(nodeIds));
node.getCardinalities().add(key);
boolean nonTrivialMin = (key.getMin() != null && key.getMin() > 1);
int max = key.getMax() == null ? -1 : key.getMax();

if (nonTrivialMin || max != -1) {
String[] nodeIds = p.getValue().stream()
.flatMap(s -> node.streamMatching(n -> n.getAqlPathDto().equals(s)))
.map(WebTemplateNode::getId)
.toArray(String[]::new);
// only add non-trivial cardinalities
if (nonTrivialMin || max < nodeIds.length) {
key.getIds().addAll(Arrays.asList(nodeIds));
node.getCardinalities().add(key);
}
}
});

Expand Down Expand Up @@ -779,17 +783,15 @@
.values()
.forEach(l -> {
if (l.size() > 1) {
for (int i = 0; i < l.size(); i++) {
if (i > 0) {
WebTemplateNode n = l.get(i);
int optionalIdNumber = i + 1;
n.setOptionalIdNumber(optionalIdNumber);

if (RmConstants.ELEMENT.equals(n.getRmType())) {
n.getChildren().stream()
.filter(c -> c.getId().equals(n.getId(false)))
.forEach(c -> c.setOptionalIdNumber(optionalIdNumber));
}
for (int i = 1; i < l.size(); i++) {
WebTemplateNode n = l.get(i);
int optionalIdNumber = i + 1;
n.setOptionalIdNumber(optionalIdNumber);

if (RmConstants.ELEMENT.equals(n.getRmType())) {
n.getChildren().stream()
.filter(c -> c.getId().equals(n.getId(false)))
.forEach(c -> c.setOptionalIdNumber(optionalIdNumber));
}
}
} else {
Expand All @@ -798,7 +800,7 @@
});
}

private void addRMAttributes(

Check failure on line 803 in web-template/src/main/java/org/ehrbase/openehr/sdk/webtemplate/parser/OPTParser.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=ehrbase_openEHR_SDK&issues=AZ0KiCVv9n3_gm6Ya2iQ&open=AZ0KiCVv9n3_gm6Ya2iQ&pullRequest=733
WebTemplateNode node, AqlPath aqlPath, Map<String, Map<String, TermDefinition>> termDefinitionMap) {
// Add RM Attributes
RMTypeInfo typeInfo = ARCHIE_RM_INFO_LOOKUP.getTypeInfo(node.getRmType());
Expand All @@ -808,37 +810,52 @@
Class<?> javaClass = typeInfo.getJavaClass();
if (Pathable.class.isAssignableFrom(javaClass) || DvInterval.class.isAssignableFrom(javaClass)) {

List<WebTemplateNode> children = node.getChildren();
typeInfo.getAttributes().values().stream()
.filter(s -> !s.isComputed())
.filter(s -> !"value".equals(s.getRmName()))
// EVENT.offset is not marked computed in archie
.filter(s -> !(Event.class.isAssignableFrom(javaClass) && "offset".equals(s.getRmName())))
.filter(s -> !(Element.class.isAssignableFrom(javaClass)
&& !Set.of("name", "feeder_audit", "null_flavour").contains(s.getRmName())))
.filter(s -> !Locatable.class.isAssignableFrom(s.getTypeInCollection()))
.filter(s -> !(DvInterval.class.isAssignableFrom(javaClass) && "interval".equals(s.getRmName())))
.map(i -> buildNodeForAttribute(i, aqlPath, termDefinitionMap))
// only add if not already there
.filter(n -> node.getChildren().stream()
.map(WebTemplateNode::getId)
.noneMatch(s -> s.equals(n.getId())))
.forEach(node.getChildren()::add);
.filter(s -> {
String rmName = s.getRmName();
return !(s.isComputed()
|| "value".equals(rmName)
// EVENT.offset is not marked computed in archie
|| (Event.class.isAssignableFrom(javaClass) && "offset".equals(rmName))
|| (Element.class.isAssignableFrom(javaClass)
&& switch (rmName) {
case "name", "feeder_audit", "null_flavour" -> false;
default -> true;
})
|| Locatable.class.isAssignableFrom(s.getTypeInCollection())
|| (DvInterval.class.isAssignableFrom(javaClass) && "interval".equals(rmName)));
})
.map(i -> {
String rmName = i.getRmName();
String id = buildId(rmName);
// only add if not already there
for (WebTemplateNode child : children) {
if (child.getId().equals(id)) {
return null;
}
}
return buildNodeForAttribute(i, aqlPath, rmName, id, termDefinitionMap);
})
.filter(Objects::nonNull)
.forEach(children::add);
}
}

private WebTemplateNode buildNodeForAttribute(
RMAttributeInfo attributeInfo,
AqlPath aqlPath,
String rmName,
String id,
Map<String, Map<String, TermDefinition>> termDefinitionMap) {
WebTemplateNode node = new WebTemplateNode();
node.setAqlPath(aqlPath.addEnd(attributeInfo.getRmName()));
node.setName(attributeInfo.getRmName());
node.setId(buildId(attributeInfo.getRmName()));
node.setAqlPath(aqlPath.addEnd(rmName));
node.setName(rmName);
node.setId(id);
node.setRmType(attributeInfo.getTypeNameInCollection());
node.setMax(attributeInfo.isMultipleValued() ? -1 : 1);
node.setMin(attributeInfo.isNullable() ? 0 : 1);
if ("action_archetype_id".equals(attributeInfo.getRmName())
|| "math_function".equals(attributeInfo.getRmName())) {
if ("action_archetype_id".equals(rmName) || "math_function".equals(rmName)) {
node.setMin(1);
}

Expand Down
Loading