This commit is contained in:
Oliver Egger 2024-09-26 14:32:03 +02:00 committed by GitHub
commit c2647dce86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 13 deletions

View File

@ -1087,7 +1087,7 @@ public class StructureMapUtilities {
if (lexer.hasToken("log")) {
lexer.take();
ExpressionNode node = fpe.parse(lexer);
source.setUserData(MAP_WHERE_CHECK, node);
source.setUserData(MAP_WHERE_LOG, node);
source.setLogMessage(node.toString());
}
}
@ -1629,17 +1629,20 @@ public class StructureMapUtilities {
}
items.removeAll(remove);
}
if (src.hasCondition()) {
ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_EXPRESSION);
if (expr == null) {
expr = fpe.parse(src.getCondition());
// fpe.check(context.appInfo, ??, ??, expr)
src.setUserData(MAP_WHERE_EXPRESSION, expr);
}
List<Base> remove = new ArrayList<Base>();
for (Base item : items) {
if (!fpe.evaluateToBoolean(vars, null, null, item, expr)) {
Variables varsForSource = vars.copy();
if (src.hasVariable()) {
varsForSource.add(VariableMode.INPUT, src.getVariable(), item);
}
if (!fpe.evaluateToBoolean(varsForSource, null, null, item, expr)) {
log(indent + " condition [" + src.getCondition() + "] for " + item.toString() + " : false");
remove.add(item);
} else
@ -1652,12 +1655,14 @@ public class StructureMapUtilities {
ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_CHECK);
if (expr == null) {
expr = fpe.parse(src.getCheck());
// fpe.check(context.appInfo, ??, ??, expr)
src.setUserData(MAP_WHERE_CHECK, expr);
}
List<Base> remove = new ArrayList<Base>();
for (Base item : items) {
if (!fpe.evaluateToBoolean(vars, null, null, item, expr))
Variables varsForSource = vars.copy();
if (src.hasVariable()) {
varsForSource.add(VariableMode.INPUT, src.getVariable(), item);
}
if (!fpe.evaluateToBoolean(varsForSource, null, null, item, expr))
throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed");
}
}
@ -1666,16 +1671,20 @@ public class StructureMapUtilities {
ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_LOG);
if (expr == null) {
expr = fpe.parse(src.getLogMessage());
// fpe.check(context.appInfo, ??, ??, expr)
src.setUserData(MAP_WHERE_LOG, expr);
}
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
for (Base item : items)
b.appendIfNotNull(fpe.evaluateToString(vars, null, null, item, expr));
for (Base item : items) {
Variables varsForSource = vars.copy();
if (src.hasVariable()) {
varsForSource.add(VariableMode.INPUT, src.getVariable(), item);
}
b.appendIfNotNull(fpe.evaluateToString(varsForSource, null, null, item, expr));
}
if (b.length() > 0)
services.log(b.toString());
}
if (src.hasListMode() && !items.isEmpty()) {
switch (src.getListMode()) {
@ -1753,7 +1762,7 @@ public class StructureMapUtilities {
if (tgt.hasVariable() && v != null)
vars.add(VariableMode.OUTPUT, tgt.getVariable(), v);
}
private Base runTransform(String rulePath, TransformContext context, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, Variables vars, Base dest, String element, String srcVar, boolean root) throws FHIRException {
try {
switch (tgt.getTransform()) {
@ -2520,7 +2529,6 @@ public class StructureMapUtilities {
ExpressionNode expr = (ExpressionNode) tgt.getUserData(MAP_EXPRESSION);
if (expr == null) {
expr = fpe.parse(getParamString(vars, tgt.getParameter().get(tgt.getParameter().size() - 1)));
tgt.setUserData(MAP_WHERE_EXPRESSION, expr);
}
return fpe.check(vars, null, expr);
case TRANSLATE:

View File

@ -71,6 +71,19 @@ public class StructureMapUtilitiesTest implements ITransformerServices {
assertEquals("2023-10-26", fp.evaluateToString(target, "birthDate"));
assertEquals("2023-09-20T13:19:13.502Z", fp.evaluateToString(target, "deceased"));
}
@Test
public void testWhereClause() throws IOException, FHIRException {
StructureMapUtilities scu = new StructureMapUtilities(context, this);
scu.setDebug(true);
String fileMap = TestingUtilities.loadTestResource("r5", "structure-mapping", "whereclause.map");
Element source = Manager.parseSingle(context, TestingUtilities.loadTestResourceStream("r4", "examples", "capabilitystatement-example.json"), FhirFormat.JSON);
StructureMap structureMap = scu.parse(fileMap, "whereclause");
Element target = Manager.build(context, scu.getTargetType(structureMap));
scu.transform(null, source, structureMap, target);
FHIRPathEngine fp = new FHIRPathEngine(context);
assertEquals("true", fp.evaluateToString(target, "rest.resource.interaction.where(code='create').exists()"));
}
private void assertSerializeDeserialize(StructureMap structureMap) {
Assertions.assertEquals("syntax", structureMap.getName());