Minor correction to align with FHIR R5 RDF changes

This commit is contained in:
dksharma 2023-02-26 21:44:07 -06:00
parent e64461e940
commit 5fde3a2106
2 changed files with 32 additions and 20 deletions

View File

@ -113,7 +113,7 @@ public class ShExGenerator {
); );
private List<String> shortIdException = Arrays.asList( private List<String> shortIdException = Arrays.asList(
"Element", "Resource", "Extension", "base64Binary", "boolean", "base64Binary", "boolean",
"date", "dateTime", "decimal", "instant", "integer64", "date", "dateTime", "decimal", "instant", "integer64",
"integer", "string", "time", "uri" "integer", "string", "time", "uri"
); );
@ -174,7 +174,7 @@ public class ShExGenerator {
private static String SIMPLE_ELEMENT_DEFN_TEMPLATE = "@<$typ$>$vsdef$"; private static String SIMPLE_ELEMENT_DEFN_TEMPLATE = "@<$typ$>$vsdef$";
// Value Set Element // Value Set Element
private static String VALUESET_DEFN_TEMPLATE = " AND\n\t{fhir:value @$vsn$}"; private static String VALUESET_DEFN_TEMPLATE = " AND\n\t{fhir:v @$vsn$}";
// Fixed Value Template // Fixed Value Template
private static String FIXED_VALUE_TEMPLATE = " AND\n\t{fhir:value [\"$val$\"]}"; private static String FIXED_VALUE_TEMPLATE = " AND\n\t{fhir:value [\"$val$\"]}";
@ -442,9 +442,6 @@ public class ShExGenerator {
"This might not be an issue, if this resource is normative base or a meta resource"); "This might not be an issue, if this resource is normative base or a meta resource");
shapeDefinitions.append("<" + sd.getName() + "> CLOSED {\n}"); shapeDefinitions.append("<" + sd.getName() + "> CLOSED {\n}");
} }
if (!imports.isEmpty())
imports.removeIf(s -> s.contains(sd.getName()));
} }
shapeDefinitions.append(emitInnerTypes()); shapeDefinitions.append(emitInnerTypes());
@ -502,6 +499,10 @@ public class ShExGenerator {
StringBuffer allImports = new StringBuffer(""); StringBuffer allImports = new StringBuffer("");
if (!imports.isEmpty()) { if (!imports.isEmpty()) {
uniq_structures.forEach((StructureDefinition sdstruct) -> {
imports.removeIf(s -> s.contains(sdstruct.getName()));
});
imports.sort(Comparator.comparingInt(String::length)); imports.sort(Comparator.comparingInt(String::length));
imports.forEach((String imp) -> { imports.forEach((String imp) -> {
ST import_def = tmplt(IMPORT_TEMPLATE); ST import_def = tmplt(IMPORT_TEMPLATE);
@ -553,7 +554,8 @@ public class ShExGenerator {
private String getExtendedType(ElementDefinition ed){ private String getExtendedType(ElementDefinition ed){
String bd = getBaseTypeName(ed); String bd = getBaseTypeName(ed);
if (bd != null && !baseDataTypes.contains(bd)) { //if (bd != null && !baseDataTypes.contains(bd)) {
if (bd!=null) {
addImport("<" + bd + ">"); addImport("<" + bd + ">");
bd = "> EXTENDS @<" + bd; bd = "> EXTENDS @<" + bd;
} }
@ -589,12 +591,15 @@ public class ShExGenerator {
if ("Element".equals(sd.getName())) if ("Element".equals(sd.getName()))
shape_defn.add("resourceDecl", tmplt(ROOT_TEMPLATE).render()); shape_defn.add("resourceDecl", tmplt(ROOT_TEMPLATE).render());
else { else {
String rootTmpl = tmplt(ROOT_TEMPLATE).render(); String rootTmpl = tmplt(ROOT_TEMPLATE).render();
if (baseDataTypes.contains(getBaseTypeName(sd))) String btn = getBaseTypeName(sd);
rootTmpl = ""; if ((baseDataTypes.contains(btn))||
ST resource_decl = tmplt(RESOURCE_DECL_TEMPLATE). (shortIdException.contains(btn)))
add("id", sd.getId()). rootTmpl = "\n";
add("root", rootTmpl);
ST resource_decl = tmplt(RESOURCE_DECL_TEMPLATE).
add("id", sd.getId()).
add("root", rootTmpl);
shape_defn.add("resourceDecl", resource_decl.render()); shape_defn.add("resourceDecl", resource_decl.render());
} }
@ -646,9 +651,7 @@ public class ShExGenerator {
String[] els = cstype.split("/"); String[] els = cstype.split("/");
cstype = els[els.length - 1]; cstype = els[els.length - 1];
} }
// Implement here if SD type == constraint source OR SD type is Primitive or DataType then add constraint to SD otherwise skip it.
//if ((sdType.equals(cstype)) || baseDataTypes.contains(sdType) || baseDataTypes.contains(bd)) {
//if ((sdType.equals(cstype)) || baseDataTypes.contains(sdType)) {
String id = ed.hasBase() ? ed.getBase().getPath() : ed.getPath(); String id = ed.hasBase() ? ed.getBase().getPath() : ed.getPath();
String shortId = id.substring(id.lastIndexOf(".") + 1); String shortId = id.substring(id.lastIndexOf(".") + 1);
if ((ed.hasContentReference() && (!ed.hasType())) || (id.equals(sd.getName() + "." + shortId))) { if ((ed.hasContentReference() && (!ed.hasType())) || (id.equals(sd.getName() + "." + shortId))) {
@ -1305,12 +1308,16 @@ public class ShExGenerator {
innerTypes.add(new ImmutablePair<StructureDefinition, ElementDefinition>(sd, ed)); innerTypes.add(new ImmutablePair<StructureDefinition, ElementDefinition>(sd, ed));
} }
if ("BackboneElement".equals(typ))
typ = id;
defn = simpleElement(sd, ed, typ); defn = simpleElement(sd, ed, typ);
String refChoices = ""; String refChoices = "";
if (id.endsWith("[x]")) { if (id.endsWith("[x]")) {
defn = " (" + genChoiceTypes(sd, ed, shortId) + ") "; //defn = " (" + genChoiceTypes(sd, ed, shortId) + ")";
defn = " " + genChoiceTypes(sd, ed, shortId) + " ";
//defn += " AND { rdf:type IRI } "; //defn += " AND { rdf:type IRI } ";
} else { } else {
if (ed.getType().size() == 1) { if (ed.getType().size() == 1) {
@ -1371,7 +1378,7 @@ public class ShExGenerator {
} else { } else {
if (!refChoices.isEmpty()) { if (!refChoices.isEmpty()) {
defn += " AND {fhir:link \n\t\t\t@<" + defn += " AND {fhir:link \n\t\t\t@<" +
refChoices.replaceAll("_OR_", "> OR \n\t\t\t@<") + "> }"; refChoices.replaceAll("_OR_", "> OR \n\t\t\t@<") + "> ? }";
} }
} }
@ -1384,7 +1391,7 @@ public class ShExGenerator {
} }
private boolean changeShortName(StructureDefinition sd, ElementDefinition ed){ private boolean changeShortName(StructureDefinition sd, ElementDefinition ed){
return !shortIdException.contains(sd.getName()); return shortIdException.contains(sd.getName());
} }
private void addImport(String typeDefn) { private void addImport(String typeDefn) {
@ -1803,7 +1810,7 @@ public class ShExGenerator {
for(ValueSet.ValueSetExpansionContainsComponent vsec : vse.getValueset().getExpansion().getContains()) for(ValueSet.ValueSetExpansionContainsComponent vsec : vse.getValueset().getExpansion().getContains())
valid_codes.add("\"" + vsec.getCode() + "\""); valid_codes.add("\"" + vsec.getCode() + "\"");
} }
return vsd.add("val_list", valid_codes.size() > 0? " [" + StringUtils.join(valid_codes, " ") + ']' : " EXTERNAL").render(); return vsd.add("val_list", valid_codes.size() > 0? " [" + StringUtils.join(valid_codes, " ") + ']' : " xsd:string #EXTERNAL").render();
} }

View File

@ -58,6 +58,11 @@ public class ShexGeneratorTests {
doTest("uri", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION); doTest("uri", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
} }
@Test
public void testPatient() throws FHIRException, IOException, UcumException {
doTest("Patient", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test @Test
public void testObservation() throws FHIRException, IOException, UcumException { public void testObservation() throws FHIRException, IOException, UcumException {
doTest("Observation", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION); doTest("Observation", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
@ -277,7 +282,6 @@ public class ShexGeneratorTests {
doTestBatchSD(sds, useSelectedExtensions, policy, true, true, false); doTestBatchSD(sds, useSelectedExtensions, policy, true, true, false);
} }
System.out.println("************************ END PROCESSING ******************************"); System.out.println("************************ END PROCESSING ******************************");
System.out.println("************************************************************************"); System.out.println("************************************************************************");
@ -330,6 +334,7 @@ public class ShexGeneratorTests {
Schema sch = validator.schema(); Schema sch = validator.schema();
Assert.assertNotNull(sch); Assert.assertNotNull(sch);
System.out.println("VALIDATION PASSED for ShEx Schema " + sd.getName() + " (Kind:" + cat + ")"); System.out.println("VALIDATION PASSED for ShEx Schema " + sd.getName() + " (Kind:" + cat + ")");
} catch (Exception e) { } catch (Exception e) {
System.out.println("VALIDATION FAILED for ShEx Schema " + sd.getName() + " (Kind:" + cat + ")"); System.out.println("VALIDATION FAILED for ShEx Schema " + sd.getName() + " (Kind:" + cat + ")");