use of org.hl7.fhir.r5.context.BaseWorkerContext in project kindling by HL7.
the class TypeParser method parse.
public List<TypeRef> parse(String n, boolean inProfile, String profileExtensionBase, BaseWorkerContext resolver, boolean allowElement, String sheetName) throws Exception {
ArrayList<TypeRef> a = new ArrayList<TypeRef>();
String exceptionPrefix = sheetName == null ? "" : "Error parsing sheet " + sheetName + " - ";
if (n == null || n.equals("") || n.startsWith("!"))
return a;
// to allow splitting
if (n.indexOf("(") != -1 && n.indexOf("|") != -1) {
String[] typeParts = n.split("[\\(\\)]");
n = "";
for (int i = 0; i < typeParts.length; i++) {
n = n + typeParts[i++];
if (i < typeParts.length) {
n = n + "(" + typeParts[i].replace("|", ",") + ")";
}
}
}
String[] typeList = n.split("[\\|]");
for (int i = 0; i < typeList.length; i++) {
TypeRef t = new TypeRef();
String typeString = typeList[i].trim();
if (typeString.contains("<")) {
if (!inProfile) {
throw new Exception(exceptionPrefix + "Can't specify aggregation mode for types unless defining a profile: " + typeString);
}
int startPos = typeString.indexOf("<");
int endPos = typeString.indexOf(">");
if (endPos < startPos) {
throw new Exception(exceptionPrefix + "Missing '>' in data type definition: " + typeList[i]);
}
t.getAggregations().addAll(Arrays.asList(typeString.substring(startPos + 1, endPos).trim().split(",")));
typeString = typeString.substring(0, startPos);
}
if (typeString.contains("~")) {
String v = typeString.substring(typeString.indexOf("~"));
typeString = typeString.substring(0, typeString.indexOf("~") - 1);
t.setVersioning(org.hl7.fhir.r5.model.ElementDefinition.ReferenceVersionRules.fromCode(v));
}
if (typeString.contains("{")) {
if (!inProfile) {
throw new Exception(exceptionPrefix + "Can't specify profile for types unless defining a profile");
}
int startPos = typeString.indexOf("{");
int endPos = typeString.indexOf("}");
if (endPos < startPos) {
throw new Exception(exceptionPrefix + "Missing '}' in data type definition: " + typeList[i]);
}
String pt = typeString.substring(startPos + 1, endPos).trim();
typeString = typeString.substring(0, startPos);
if (pt.startsWith("#")) {
// what to do here depends on what it refers to
if (typeString.trim().equals("Extension"))
pt = profileExtensionBase + pt.substring(1);
else if (typeString.trim().startsWith("Reference") || typeString.trim().startsWith("Resource"))
pt = pt.substring(1).toLowerCase();
else
throw new Exception(exceptionPrefix + "Unhandled case");
}
t.setProfile(pt);
}
if (typeString.contains("(")) {
int startPos = typeString.indexOf("(");
int endPos = typeString.indexOf(")");
if (endPos < startPos) {
throw new Exception(exceptionPrefix + "Missing ')' in data type definition: " + typeList[i]);
}
String[] params = typeString.substring(startPos + 1, endPos).split(",");
for (int j = 0; j < params.length; j++) {
if (typeString.startsWith("Reference("))
if (inProfile && !resolver.getResourceNames().contains(params[j].trim()) && !"Any".equals(params[j].trim()))
throw new Exception(exceptionPrefix + "Unknown resource " + params[j].trim());
t.getParams().add(params[j].trim());
}
typeString = typeString.substring(0, startPos);
}
t.setName(typeString.trim());
if (t.getName().equals("Element") && !allowElement)
throw new Exception(exceptionPrefix + "The type 'Element' is illegal in this context");
a.add(t);
}
return a;
}
use of org.hl7.fhir.r5.context.BaseWorkerContext in project org.hl7.fhir.core by hapifhir.
the class ComparisonTests method test.
@ParameterizedTest(name = "{index}: id {0}")
@MethodSource("data")
public void test(String name, JsonObject content) throws Exception {
this.content = content;
if (content.has("use-test") && !content.get("use-test").getAsBoolean())
return;
if (context == null) {
System.out.println("---- Load R5 ----------------------------------------------------------------");
context = TestingUtilities.getSharedWorkerContext();
FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
NpmPackage npm = pcm.loadPackage("hl7.fhir.us.core#3.1.0");
BaseWorkerContext bc = (BaseWorkerContext) context;
boolean dupl = bc.isAllowLoadingDuplicates();
bc.setAllowLoadingDuplicates(true);
context.loadFromPackage(npm, new R4ToR5Loader(new String[] { "CapabilityStatement", "StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter", "OperationDefinition", "Questionnaire", "ConceptMap", "StructureMap", "NamingSystem" }, new NullLoaderKnowledgeProviderR5(), context.getVersion()));
bc.setAllowLoadingDuplicates(dupl);
}
if (!new File(Utilities.path("[tmp]", "comparison")).exists()) {
System.out.println("---- Set up Output ----------------------------------------------------------");
Utilities.createDirectory(Utilities.path("[tmp]", "comparison"));
FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
NpmPackage npm = pcm.loadPackage(CommonPackages.ID_PUBPACK, CommonPackages.VER_PUBPACK);
for (String f : npm.list("other")) {
TextFile.streamToFile(npm.load("other", f), Utilities.path("[tmp]", "comparison", f));
}
}
System.out.println("---- " + name + " ----------------------------------------------------------------");
CanonicalResource left = load("left");
CanonicalResource right = load("right");
ComparisonSession session = new ComparisonSession(context, context, "Comparison Tests", null);
if (left instanceof CodeSystem && right instanceof CodeSystem) {
CodeSystemComparer cs = new CodeSystemComparer(session);
CodeSystemComparison csc = cs.compare((CodeSystem) left, (CodeSystem) right);
Assertions.assertTrue(csc.getUnion().getConcept().size() > csc.getIntersection().getConcept().size());
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-union.json")), csc.getUnion());
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-intersection.json")), csc.getIntersection());
String xmle = new XhtmlComposer(true).compose(cs.renderErrors(csc));
String xml1 = new XhtmlComposer(true).compose(cs.renderMetadata(csc, "", ""));
String xml2 = new XhtmlComposer(true).compose(cs.renderConcepts(csc, "", ""));
TextFile.stringToFile(HEADER + hd("Messages") + xmle + BREAK + hd("Metadata") + xml1 + BREAK + hd("Concepts") + xml2 + FOOTER, Utilities.path("[tmp]", "comparison", name + ".html"));
checkOutcomes(csc.getMessages(), content);
} else if (left instanceof ValueSet && right instanceof ValueSet) {
ValueSetComparer cs = new ValueSetComparer(session);
ValueSetComparison csc = cs.compare((ValueSet) left, (ValueSet) right);
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-union.json")), csc.getUnion());
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-intersection.json")), csc.getIntersection());
String xmle = new XhtmlComposer(true).compose(cs.renderErrors(csc));
String xml1 = new XhtmlComposer(true).compose(cs.renderMetadata(csc, "", ""));
String xml2 = new XhtmlComposer(true).compose(cs.renderCompose(csc, "", ""));
String xml3 = new XhtmlComposer(true).compose(cs.renderExpansion(csc, "", ""));
TextFile.stringToFile(HEADER + hd("Messages") + xmle + BREAK + hd("Metadata") + xml1 + BREAK + hd("Definition") + xml2 + BREAK + hd("Expansion") + xml3 + FOOTER, Utilities.path("[tmp]", "comparison", name + ".html"));
checkOutcomes(csc.getMessages(), content);
} else if (left instanceof StructureDefinition && right instanceof StructureDefinition) {
ProfileUtilities utils = new ProfileUtilities(context, null, null);
genSnapshot(utils, (StructureDefinition) left);
genSnapshot(utils, (StructureDefinition) right);
ProfileComparer pc = new ProfileComparer(session, utils, utils);
ProfileComparison csc = pc.compare((StructureDefinition) left, (StructureDefinition) right);
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-union.json")), csc.getUnion());
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-intersection.json")), csc.getIntersection());
String xmle = new XhtmlComposer(true).compose(pc.renderErrors(csc));
String xml1 = new XhtmlComposer(true).compose(pc.renderMetadata(csc, "", ""));
String xml2 = new XhtmlComposer(true).compose(pc.renderStructure(csc, "", "", "http://hl7.org/fhir"));
// String xml3 = new XhtmlComposer(true).compose(cs.renderExpansion(csc, "", ""));
TextFile.stringToFile(HEADER + hd("Messages") + xmle + BREAK + hd("Metadata") + xml1 + BREAK + hd("Structure") + xml2 + FOOTER, Utilities.path("[tmp]", "comparison", name + ".html"));
checkOutcomes(csc.getMessages(), content);
} else if (left instanceof CapabilityStatement && right instanceof CapabilityStatement) {
CapabilityStatementComparer pc = new CapabilityStatementComparer(session);
CapabilityStatementComparison csc = pc.compare((CapabilityStatement) left, (CapabilityStatement) right);
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-union.json")), csc.getUnion());
new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path("[tmp]", "comparison", name + "-intersection.json")), csc.getIntersection());
String xmle = new XhtmlComposer(true).compose(pc.renderErrors(csc));
String xml1 = new XhtmlComposer(true).compose(pc.renderMetadata(csc, "", ""));
String xml2 = new XhtmlComposer(true).compose(pc.renderStatements(csc, "", ""));
// String xml3 = new XhtmlComposer(true).compose(cs.renderExpansion(csc, "", ""));
TextFile.stringToFile(HEADER + hd("Messages") + xmle + BREAK + hd("Metadata") + xml1 + BREAK + hd("Structure") + xml2 + FOOTER, Utilities.path("[tmp]", "comparison", name + ".html"));
checkOutcomes(csc.getMessages(), content);
} else {
throw new FHIRException("Can't compare " + left.fhirType() + " to " + right.fhirType());
}
}
Aggregations