fix issues with R3R4MapTester

This commit is contained in:
Grahame Grieve 2023-03-16 14:08:00 +11:00
parent 27598a382d
commit 413cbb115a
8 changed files with 286 additions and 63 deletions

View File

@ -115,6 +115,7 @@ public class StructureMapUtilities {
private ValidationOptions terminologyServiceOptions = new ValidationOptions();
private final ProfileUtilities profileUtilities;
private boolean exceptionsForChecks = true;
private boolean debug;
public StructureMapUtilities(IWorkerContext worker, ITransformerServices services, ProfileKnowledgeProvider pkp) {
super();
@ -1217,10 +1218,12 @@ public class StructureMapUtilities {
}
private void log(String cnt) {
if (getServices() != null)
getServices().log(cnt);
else
System.out.println(cnt);
if (debug) {
if (getServices() != null)
getServices().log(cnt);
else
System.out.println(cnt);
}
}
/**
@ -1308,7 +1311,7 @@ public class StructureMapUtilities {
if (source != null) {
for (Variables v : source) {
for (StructureMapGroupRuleTargetComponent t : rule.getTarget()) {
processTarget(rule.getName(), context, v, map, group, t, rule.getSource().size() == 1 ? rule.getSourceFirstRep().getVariable() : null, atRoot, vars);
processTarget(map.getName()+"|"+group.getName()+"|"+rule.getName(), context, v, map, group, t, rule.getSource().size() == 1 ? rule.getSourceFirstRep().getVariable() : null, atRoot, vars);
}
if (rule.hasRule()) {
for (StructureMapGroupRuleComponent childrule : rule.getRule()) {
@ -1320,7 +1323,9 @@ public class StructureMapUtilities {
}
} else if (rule.getSource().size() == 1 && rule.getSourceFirstRep().hasVariable() && rule.getTarget().size() == 1 && rule.getTargetFirstRep().hasVariable() && rule.getTargetFirstRep().getTransform() == StructureMapTransform.CREATE && !rule.getTargetFirstRep().hasParameter()) {
// simple inferred, map by type
System.out.println(v.summary());
if (debug) {
log(v.summary());
}
Base src = v.get(VariableMode.INPUT, rule.getSourceFirstRep().getVariable());
Base tgt = v.get(VariableMode.OUTPUT, rule.getTargetFirstRep().getVariable());
String srcType = src.fhirType();
@ -1702,18 +1707,18 @@ public class StructureMapUtilities {
return type.equals(item.fhirType());
}
private void processTarget(String ruleId, TransformContext context, Variables vars, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, String srcVar, boolean atRoot, Variables sharedVars) throws FHIRException {
private void processTarget(String rulePath, TransformContext context, Variables vars, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, String srcVar, boolean atRoot, Variables sharedVars) throws FHIRException {
Base dest = null;
if (tgt.hasContext()) {
dest = vars.get(VariableMode.OUTPUT, tgt.getContext());
if (dest == null)
throw new FHIRException("Rule \"" + ruleId + "\": target context not known: " + tgt.getContext());
throw new FHIRException("Rule \"" + rulePath + "\": target context not known: " + tgt.getContext());
if (!tgt.hasElement())
throw new FHIRException("Rule \"" + ruleId + "\": Not supported yet");
throw new FHIRException("Rule \"" + rulePath + "\": Not supported yet");
}
Base v = null;
if (tgt.hasTransform()) {
v = runTransform(ruleId, context, map, group, tgt, vars, dest, tgt.getElement(), srcVar, atRoot);
v = runTransform(rulePath, context, map, group, tgt, vars, dest, tgt.getElement(), srcVar, atRoot);
if (v != null && dest != null)
v = dest.setProperty(tgt.getElement().hashCode(), tgt.getElement(), v); // reset v because some implementations may have to rewrite v when setting the value
} else if (dest != null) {
@ -1731,7 +1736,7 @@ public class StructureMapUtilities {
vars.add(VariableMode.OUTPUT, tgt.getVariable(), v);
}
private Base runTransform(String ruleId, TransformContext context, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, Variables vars, Base dest, String element, String srcVar, boolean root) throws FHIRException {
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()) {
case CREATE:
@ -1755,7 +1760,7 @@ public class StructureMapUtilities {
}
}
}
Base res = services != null ? services.createType(context.getAppInfo(), tn) : ResourceFactory.createResourceOrType(tn);
Base res = services != null ? services.createType(context.getAppInfo(), tn) : typeFactory(tn);
if (res.isResource() && !res.fhirType().equals("Parameters")) {
// res.setIdBase(tgt.getParameter().size() > 1 ? getParamString(vars, tgt.getParameter().get(0)) : UUID.randomUUID().toString().toLowerCase());
if (services != null)
@ -1776,7 +1781,7 @@ public class StructureMapUtilities {
if (v.size() == 0)
return null;
else if (v.size() != 1)
throw new FHIRException("Rule \"" + ruleId + "\": Evaluation of " + expr.toString() + " returned " + v.size() + " objects");
throw new FHIRException("Rule \"" + rulePath+ "\": Evaluation of " + expr.toString() + " returned " + v.size() + " objects");
else
return v.get(0);
@ -1790,7 +1795,7 @@ public class StructureMapUtilities {
}
return new StringType(src);
case ESCAPE:
throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
throw new Error("Rule \"" + rulePath + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
case CAST:
src = getParamString(vars, tgt.getParameter().get(0));
if (tgt.getParameter().size() == 1)
@ -1849,9 +1854,9 @@ public class StructureMapUtilities {
case REFERENCE:
Base b = getParam(vars, tgt.getParameter().get(0));
if (b == null)
throw new FHIRException("Rule \"" + ruleId + "\": Unable to find parameter " + ((IdType) tgt.getParameter().get(0).getValue()).asStringValue());
throw new FHIRException("Rule \"" + rulePath + "\": Unable to find parameter " + ((IdType) tgt.getParameter().get(0).getValue()).asStringValue());
if (!b.isResource())
throw new FHIRException("Rule \"" + ruleId + "\": Transform engine cannot point at an element of type " + b.fhirType());
throw new FHIRException("Rule \"" + rulePath + "\": Transform engine cannot point at an element of type " + b.fhirType());
else {
String id = b.getIdBase();
if (id == null) {
@ -1861,7 +1866,7 @@ public class StructureMapUtilities {
return new StringType(b.fhirType() + "/" + id);
}
case DATEOP:
throw new Error("Rule \"" + ruleId + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
throw new Error("Rule \"" + rulePath + "\": Transform " + tgt.getTransform().toCode() + " not supported yet");
case UUID:
return new IdType(UUID.randomUUID().toString());
case POINTER:
@ -1869,7 +1874,7 @@ public class StructureMapUtilities {
if (b instanceof Resource)
return new UriType("urn:uuid:" + ((Resource) b).getId());
else
throw new FHIRException("Rule \"" + ruleId + "\": Transform engine cannot point at an element of type " + b.fhirType());
throw new FHIRException("Rule \"" + rulePath + "\": Transform engine cannot point at an element of type " + b.fhirType());
case CC:
CodeableConcept cc = new CodeableConcept();
cc.addCoding(buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString())));
@ -1878,10 +1883,28 @@ public class StructureMapUtilities {
Coding c = buildCoding(getParamStringNoNull(vars, tgt.getParameter().get(0), tgt.toString()), getParamStringNoNull(vars, tgt.getParameter().get(1), tgt.toString()));
return c;
default:
throw new Error("Rule \"" + ruleId + "\": Transform Unknown: " + tgt.getTransform().toCode());
throw new Error("Rule \"" + rulePath + "\": Transform Unknown: " + tgt.getTransform().toCode());
}
} catch (Exception e) {
throw new FHIRException("Exception executing transform " + tgt.toString() + " on Rule \"" + ruleId + "\": " + e.getMessage(), e);
throw new FHIRException("Exception executing transform " + tgt.toString() + " on Rule \"" + rulePath + "\": " + e.getMessage(), e);
}
}
private Base typeFactory(String tn) {
if (Utilities.isAbsoluteUrl(tn) && !tn.startsWith("http://hl7.org/fhir/StructureDefinition")) {
StructureDefinition sd = worker.fetchTypeDefinition(tn);
if (sd == null) {
if (Utilities.existsInList(tn, "http://hl7.org/fhirpath/System.String")) {
sd = worker.fetchTypeDefinition("string");
}
}
if (sd == null) {
throw new FHIRException("Unable to create type "+tn);
} else {
return Manager.build(worker, sd);
}
} else {
return ResourceFactory.createResourceOrType(tn);
}
}
@ -2787,4 +2810,12 @@ public class StructureMapUtilities {
}
}
public boolean isDebug() {
return debug;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
}

View File

@ -850,6 +850,7 @@ public class I18nConstants {
public static final String CONCEPTMAP_GROUP_SOURCE_INCOMPLETE = "CONCEPTMAP_GROUP_SOURCE_INCOMPLETE";
public static final String CONCEPTMAP_GROUP_TARGET_INCOMPLETE = "CONCEPTMAP_GROUP_TARGET_INCOMPLETE";
public static final String UNABLE_TO_RESOLVE_SYSTEM_SYSTEM_IS_INDETERMINATE = "UNABLE_TO_RESOLVE_SYSTEM_SYSTEM_IS_INDETERMINATE";
public static final String SD_NO_TYPE_CODE_ON_CODE = "SD_NO_TYPE_CODE_ON_CODE";
}

View File

@ -837,4 +837,8 @@ public class ValidationMessage implements Comparator<ValidationMessage>, Compara
}
return this;
}
public boolean isError() {
return level == IssueSeverity.ERROR || level == IssueSeverity.FATAL;
}
}

View File

@ -899,4 +899,7 @@ CONCEPTMAP_GROUP_TARGET_PROPERTY_CODE_INVALID = The code {0} is invalid in the s
CONCEPTMAP_GROUP_TARGET_PROPERTY_TYPE_UNKNOWN_SYSTEM = The system {0} is unknown, so code values can''t be checked
SM_DEPENDENT_PARAM_TYPE_MISMATCH_DUPLICATE = The group {0} has alr
CONCEPTMAP_GROUP_SOURCE_INCOMPLETE = Source Code System {0} doesn''t have all content (content = {1}), so the source codes cannot be checked
CONCEPTMAP_GROUP_TARGET_INCOMPLETE = Target Code System {0} doesn''t have all content (content = {1}), so the source codes cannot be checked
CONCEPTMAP_GROUP_TARGET_INCOMPLETE = Target Code System {0} doesn''t have all content (content = {1}), so the source codes cannot be checked
SD_NO_TYPE_CODE_ON_CODE = Snapshot for {1} element {0} has type.code without a value

View File

@ -1,12 +1,15 @@
package org.hl7.fhir.validation;
import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.hl7.fhir.convertors.loaders.loaderR5.R4BToR5Loader;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
@ -72,6 +75,7 @@ import org.hl7.fhir.utilities.FileFormat;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.json.JsonException;
import org.hl7.fhir.utilities.npm.CommonPackages;
import org.hl7.fhir.validation.cli.model.CliContext;
import org.hl7.fhir.validation.cli.services.ComparisonService;
@ -79,6 +83,7 @@ import org.hl7.fhir.validation.cli.services.ValidationService;
import org.hl7.fhir.validation.cli.utils.Display;
import org.hl7.fhir.validation.cli.utils.EngineMode;
import org.hl7.fhir.validation.cli.utils.Params;
import org.hl7.fhir.validation.special.R4R5MapTester;
import org.hl7.fhir.validation.testexecutor.TestExecutor;
import org.hl7.fhir.validation.testexecutor.TestExecutorParams;
@ -132,13 +137,28 @@ public class ValidatorCli {
}
} else if (Params.hasParam(args, Params.TEST)) {
parseTestParamsAndExecute(args);
}
else {
} else if (Params.hasParam(args, Params.SPECIAL)) {
executeSpecial(args);
} else {
Display.printCliArgumentsAndInfo(args);
doValidation(tt, tts, cliContext);
}
}
private static void executeSpecial(String[] args) throws JsonException, IOException {
String specialMode = Params.getParam(args, Params.SPECIAL);
if ("r4r5tests".equals(specialMode)) {
final String target = Params.getParam(args, Params.TARGET);
final String source = Params.getParam(args, Params.SOURCE);
final String filter = Params.getParam(args, Params.FILTER);
if (new File(target).exists()) {
new R4R5MapTester().testMaps(target, source, filter);
}
} else {
System.out.println("Unknown SpecialMode "+specialMode);
}
}
private static void setJavaSystemProxyParamsFromParams(String[] args) {
setJavaSystemProxyHostFromParams(args, Params.PROXY, HTTP_PROXY_HOST, HTTP_PROXY_PORT);

View File

@ -83,6 +83,10 @@ public class Params {
public static final String TEST_MODULES = "-test-modules";
public static final String TEST_NAME_FILTER = "-test-classname-filter";
public static final String SPECIAL = "-special";
public static final String TARGET = "-target";
public static final String SOURCE = "-source";
public static final String FILTER = "-filter";
/**
* Checks the list of passed in params to see if it contains the passed in param.

View File

@ -169,7 +169,7 @@ public class StructureDefinitionValidator extends BaseValidator {
}
if (Utilities.noString(tc) && type.hasChild("code")) {
if (VersionUtilities.isR4Plus(context.getVersion())) {
throw new Error("Snapshot for " + sd.getId() +" element " + path + " has type.code without a value ");
rule(errors, "2023-03-16", IssueType.INVALID, stack.getLiteralPath(), false, I18nConstants.SD_NO_TYPE_CODE_ON_CODE, path, sd.getId());
}
}
if (!Utilities.noString(tc)) {

View File

@ -2,14 +2,24 @@ package org.hl7.fhir.validation.special;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.hl7.fhir.r5.model.CanonicalResource;
import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
import org.hl7.fhir.r5.model.Parameters;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.ResourceFactory;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r5.model.StructureMap;
@ -18,17 +28,19 @@ import org.hl7.fhir.convertors.loaders.loaderR5.R4ToR5Loader;
import org.hl7.fhir.exceptions.DefinitionException;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r4.formats.IParser.OutputStyle;
import org.hl7.fhir.r4.model.Resource;
import org.hl7.fhir.r4.model.ResourceFactory;
import org.hl7.fhir.r4.test.utils.TestingUtilities;
import org.hl7.fhir.r5.context.SimpleWorkerContext;
import org.hl7.fhir.r5.context.IWorkerContext.IContextResourceLoader;
import org.hl7.fhir.r5.context.SimpleWorkerContext.SimpleWorkerContextBuilder;
import org.hl7.fhir.r5.elementmodel.Element;
import org.hl7.fhir.r5.elementmodel.Manager;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.utils.structuremap.ResolvedGroup;
import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities;
import org.hl7.fhir.r5.utils.structuremap.VariableMode;
import org.hl7.fhir.r5.utils.validation.IResourceValidator;
import org.hl7.fhir.r5.utils.validation.IValidatorResourceFetcher;
import org.hl7.fhir.r5.utils.validation.constants.BestPracticeWarningLevel;
import org.hl7.fhir.r5.utils.validation.constants.IdStatus;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
@ -39,11 +51,13 @@ import org.hl7.fhir.utilities.json.model.JsonProperty;
import org.hl7.fhir.utilities.json.parser.JsonParser;
import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.hl7.fhir.utilities.validation.ValidationMessage;
import org.hl7.fhir.validation.IgLoader;
import org.hl7.fhir.validation.ValidatorUtils;
import org.hl7.fhir.validation.instance.InstanceValidator;
import org.hl7.fhir.validation.special.R4R5MapTester.Stats;
public class R4R5MapTester {
public class R4R5MapTester implements IValidatorResourceFetcher {
public class Stats {
@ -51,6 +65,9 @@ public class R4R5MapTester {
private int total;
private int parsed;
private int forward;
private int validated;
private int error;
private int back;
public void example() {
total++;
@ -76,6 +93,22 @@ public class R4R5MapTester {
return parsed;
}
public int forwardCount() {
return forward;
}
public int validatedCount() {
return validated;
}
public int errorCount() {
return error;
}
public int backCount() {
return back;
}
public String summary() {
if (errors.size() == 0) {
return "All OK";
@ -87,22 +120,36 @@ public class R4R5MapTester {
public boolean ok() {
return errors.size() == 0;
}
public void valid(boolean valid) {
validated++;
if (!valid) {
error++;
}
}
public void back() {
back++;
}
}
private boolean saveProcess = false;
private boolean saveProcess = true;
private SimpleWorkerContext context;
private FilesystemPackageCacheManager pcm;
private StructureMapUtilities utils;
private List<StructureMap> allMaps;
private InstanceValidator validator;
public static void main(String[] args) throws JsonException, IOException {
// arg[0] is the location of the fhir-extensions repo
new R4R5MapTester().testMaps(args[0]);
new R4R5MapTester().testMaps(args[0], args[1], args[2]);
}
public void testMaps(String src) throws JsonException, IOException {
public void testMaps(String src, String maps, String filter) throws JsonException, IOException {
log("Load Test Outcomes");
JsonObject json = JsonParser.parseObjectFromFile(Utilities.path(src, "input", "_data", "conversions.json"));
log("Load R5");
@ -111,17 +158,37 @@ public class R4R5MapTester {
log("Load Maps");
// context.loadFromPackage(pcm.loadPackage(), null);
loadPackage("hl7.fhir.uv.extensions#dev");
loadPackage("hl7.fhir.r4.core#4.0.1");
loadPackage("hl7.fhir.r4b.core#4.3.0");
loadPackage("hl7.terminology.r5#5.0.0", false);
utils = new StructureMapUtilities(context);
utils.setDebug(false);
loadPackage("hl7.fhir.uv.extensions#dev", maps == null);
if (maps != null) {
loadFromFolder(Utilities.path(maps, "r4-2-r5"));
loadFromFolder(Utilities.path(maps, "r4b-2-r5"));
loadFromFolder(Utilities.path(maps, "r5-2-r4"));
loadFromFolder(Utilities.path(maps, "r5-2-r4b"));
}
loadPackage("hl7.fhir.r4.core#4.0.1", false);
loadPackage("hl7.fhir.r4b.core#4.3.0", false);
validator = new InstanceValidator(context, null, null);
validator.setSuppressLoincSnomedMessages(true);
validator.setResourceIdRule(IdStatus.REQUIRED);
validator.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning);
validator.getExtensionDomains().add("http://hl7.org/fhir/us");
validator.setFetcher(this);
validator.setAllowExamples(true);
validator.setDebug(false);
validator.setForPublication(true);
validator.setNoTerminologyChecks(true);
context.setExpansionProfile(new Parameters());
log("Load R4 Examples");
NpmPackage r4Examples = pcm.loadPackage("hl7.fhir.r4.examples");
log("Load R4B Examples");
NpmPackage r4bExamples = pcm.loadPackage("hl7.fhir.r4b.examples");
utils = new StructureMapUtilities(context);
allMaps = context.fetchResourcesByType(StructureMap.class);
log("Go. "+context.getResourceNames().size()+" types of resources");
@ -129,17 +196,19 @@ public class R4R5MapTester {
boolean changed = false;
for (JsonProperty jp : json.getProperties()) {
String rn = jp.getName();
log(" "+rn);
JsonObject o = json.getJsonObject(rn);
StructureDefinition sd = context.fetchTypeDefinition(rn);
List<StructureMap> mapSrc = utils.getMapsForUrl(allMaps, sd.getUrl(), StructureMapInputMode.SOURCE);
List<StructureMap> mapTgt = utils.getMapsForUrl(allMaps, sd.getUrl(), StructureMapInputMode.TARGET);
changed = checkMaps(sd, o.getJsonObject("r4"), "http://hl7.org/fhir/4.0", mapSrc, mapTgt, r4Examples) || changed;
changed = checkMaps(sd, o.getJsonObject("r4b"), "http://hl7.org/fhir/4.3", mapSrc, mapTgt, r4bExamples) || changed;
}
if (changed) {
JsonParser.compose(json, new FileOutputStream(Utilities.path(src, "input", "_data", "conversions.json")), true);
if ("*".equals(filter) || rn.equals(filter)) {
log(" "+rn);
JsonObject o = json.getJsonObject(rn);
StructureDefinition sd = context.fetchTypeDefinition(rn);
List<StructureMap> mapSrc = utils.getMapsForUrl(allMaps, sd.getUrl(), StructureMapInputMode.SOURCE);
List<StructureMap> mapTgt = utils.getMapsForUrl(allMaps, sd.getUrl(), StructureMapInputMode.TARGET);
changed = checkMaps(sd, o.getJsonObject("r4"), "r4", "http://hl7.org/fhir/4.0", mapSrc, mapTgt, r4Examples) || changed;
changed = checkMaps(sd, o.getJsonObject("r4b"), "r4b", "http://hl7.org/fhir/4.3", mapSrc, mapTgt, r4bExamples) || changed;
JsonParser.compose(json, new FileOutputStream(Utilities.path(src, "input", "_data", "conversions.json")), true);
System.out.println(" .. done");
}
}
log("Done!");
// load R4
// load R4B
// load the maps
@ -157,15 +226,31 @@ public class R4R5MapTester {
}
private void loadPackage(String pid) throws FHIRException, IOException {
private void loadFromFolder(String path) throws FHIRFormatError, FHIRException, FileNotFoundException, IOException {
log("Load "+path);
for (File f : new File(path).listFiles()) {
if (f.getName().endsWith(".json")) {
context.cacheResource(new org.hl7.fhir.r5.formats.JsonParser().parse(new FileInputStream(f)));
}
if (f.getName().endsWith(".fml")) {
context.cacheResource(utils.parse(TextFile.fileToString(f), f.getName()));
}
}
}
private void loadPackage(String pid, boolean loadMaps) throws FHIRException, IOException {
log("Load "+pid);
NpmPackage npm = pcm.loadPackage(pid);
IContextResourceLoader loader = ValidatorUtils.loaderForVersion(npm.fhirVersion());
if (!loadMaps && loader.getTypes().contains("StructureMap")) {
loader.getTypes().remove("StructureMap");
}
loader.setPatchUrls(VersionUtilities.isCorePackage(npm.id()));
int count = context.loadFromPackage(npm, loader);
context.loadFromPackage(npm, loader);
}
private boolean checkMaps(StructureDefinition sd, JsonObject json, String ns, List<StructureMap> mapSrc, List<StructureMap> mapTgt, NpmPackage examples) throws IOException {
private boolean checkMaps(StructureDefinition sd, JsonObject json, String code, String ns, List<StructureMap> mapSrc, List<StructureMap> mapTgt, NpmPackage examples) throws IOException {
List<StructureMap> src = utils.getMapsForUrlPrefix(mapSrc, ns, StructureMapInputMode.TARGET);
List<StructureMap> tgt = utils.getMapsForUrlPrefix(mapTgt, ns, StructureMapInputMode.SOURCE);
if (src.size() + tgt.size() == 0) {
@ -199,7 +284,7 @@ public class R4R5MapTester {
if (tsd == null) {
json.set("testMessage", "Undefined type "+srcU);
} else {
testRoundTrips(sd, json, tgtG, srcG, tsd, examples);
testRoundTrips(sd, json, tgtG, srcG, tsd, examples, code);
}
}
} else {
@ -213,41 +298,85 @@ public class R4R5MapTester {
return true;
}
private void testRoundTrips(StructureDefinition sd, JsonObject json, ResolvedGroup tgtG, ResolvedGroup srcG, StructureDefinition tsd, NpmPackage examples) throws IOException {
private void testRoundTrips(StructureDefinition sd, JsonObject json, ResolvedGroup tgtG, ResolvedGroup srcG, StructureDefinition tsd, NpmPackage examples, String code) throws IOException {
Stats stats = new Stats();
for (String s : examples.listResources(tsd.getType())) {
log(" Test "+examples.id()+"::"+s);
try {
testRoundTrip(sd, tsd, tgtG, srcG, stats, examples.load("package", s));
testRoundTrip(json, sd, tsd, tgtG, srcG, stats, examples.load("package", s), code);
} catch (Exception e) {
log("error: "+e.getMessage());
stats.error("Error: "+e.getMessage());
}
}
json.set("total", stats.totalCount());
json.set("parsed", stats.parseCount());
json.set("totalInstances", stats.totalCount());
json.set("parsedOk", stats.parseCount());
json.set("convertToR5OK", stats.forwardCount());
json.set("testMessage", stats.summary());
json.set("r5validated", stats.validatedCount());
json.set("r5InError", stats.errorCount());
json.remove("r5validatedOK");
json.set("convertToR4OK", stats.backCount());
if (stats.ok()) {
json.set("testColor", "#d4ffdf");
}
}
private void testRoundTrip(StructureDefinition sd, StructureDefinition tsd, ResolvedGroup tgtG, ResolvedGroup srcG, Stats stats, InputStream stream) throws FHIRFormatError, DefinitionException, FHIRException, IOException {
private void testRoundTrip(JsonObject json, StructureDefinition sd, StructureDefinition tsd, ResolvedGroup tgtG, ResolvedGroup srcG, Stats stats, InputStream stream, String code) throws FHIRFormatError, DefinitionException, FHIRException, IOException {
stats.example();
Element r4 = new org.hl7.fhir.r5.elementmodel.JsonParser(context).setLogical(tsd).parseSingle(stream);
stats.parsed();
String id = r4.getIdBase();
checkSave(id, "src.loaded", r4);
json.remove(id);
checkSave(id+"."+code, "loaded", r4);
Resource r5 = ResourceFactory.createResource(sd.getType());
utils.transform(context, r4, tgtG.getTargetMap(), r4);
stats.forward();
Resource r5 = null;
try {
r5 = ResourceFactory.createResource(sd.getType());
utils.transform(context, r4, tgtG.getTargetMap(), r5);
stats.forward();
checkSave(id+"."+code,"converted", r5);
} catch (Exception e) {
json.forceObject(id).set("conversion-error", e.getMessage());
throw e;
}
try {
List<ValidationMessage> r5validationErrors = new ArrayList<ValidationMessage>();
validator.validate(null, r5validationErrors, r5);
boolean valid = true;
for (ValidationMessage vm : r5validationErrors) {
if (vm.isError()) {
// json.forceObject(id).forceArray("r5-errors").add(vm.summary());
valid = false;
}
}
stats.valid(valid);
} catch (Exception e) {
json.forceObject(id).set("validation-error", e.getMessage());
throw e;
}
try {
Element rt4 = Manager.build(context, tsd);
utils.transform(context, r5, srcG.getTargetMap(), rt4);
stats.back();
checkSave(id+"."+code, "returned", r4);
} catch (Exception e) {
json.forceObject(id).set("return-error", e.getMessage());
throw e;
}
}
private void checkSave(String id, String state, Element e) {
private void checkSave(String id, String state, Element e) throws FHIRException, FileNotFoundException, IOException {
if (saveProcess) {
// new org.hl7.fhir.r4.elementmodel.JsonParser(context).compose(r3, bso, OutputStyle.PRETTY, null);
new org.hl7.fhir.r5.elementmodel.JsonParser(context).compose(e, new FileOutputStream(Utilities.path("[tmp]", "r4r5", e.fhirType()+"-"+id+"-"+state+".json")), OutputStyle.PRETTY, id);
}
}
private void checkSave(String id, String state, Resource r) throws FHIRException, FileNotFoundException, IOException {
if (saveProcess) {
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "r4r5", r.fhirType()+"-"+id+"-"+state+".json")), r);
}
}
@ -255,4 +384,35 @@ public class R4R5MapTester {
System.out.println(msg);
}
@Override
public Element fetch(IResourceValidator validator, Object appContext, String url) throws FHIRException, IOException {
return null;
}
@Override
public boolean resolveURL(IResourceValidator validator, Object appContext, String path, String url, String type)
throws IOException, FHIRException {
return true;
}
@Override
public byte[] fetchRaw(IResourceValidator validator, String url) throws IOException {
throw new Error("Not done yet");
}
@Override
public IValidatorResourceFetcher setLocale(Locale locale) {
throw new Error("Not done yet");
}
@Override
public CanonicalResource fetchCanonicalResource(IResourceValidator validator, String url) throws URISyntaxException {
return null;
}
@Override
public boolean fetchesCanonicalResource(IResourceValidator validator, String url) {
return false;
}
}