Corrected TypeArcs and Node Roles

This commit is contained in:
dksharma 2023-02-24 23:12:43 -06:00
parent 9ca531ff0a
commit e64461e940
2 changed files with 245 additions and 174 deletions

View File

@ -108,7 +108,14 @@ public class ShExGenerator {
// Base DataTypes
private List<String> baseDataTypes = Arrays.asList(
"DataType",
"PrimitiveType"
"PrimitiveType",
"BackboneElement"
);
private List<String> shortIdException = Arrays.asList(
"Element", "Resource", "Extension", "base64Binary", "boolean",
"date", "dateTime", "decimal", "instant", "integer64",
"integer", "string", "time", "uri"
);
private List<String> mappedFunctions = Arrays.asList(
@ -133,7 +140,7 @@ public class ShExGenerator {
// Resource Definition
// an open shape of type Resource. Used when completeModel = false.
private static String RESOURCE_SHAPE_TEMPLATE =
"$comment$\n<Resource> {a .+;" +
"$comment$\n<Resource> {" +
"\n $elements$" +
"\n $contextOfUse$" +
"\n} $constraints$ \n";
@ -147,10 +154,10 @@ public class ShExGenerator {
// Resource Declaration
// a type node
// an optional treeRoot declaration (identifies the entry point)
private static String RESOURCE_DECL_TEMPLATE = "\na [fhir:$id$];$root$";
private static String RESOURCE_DECL_TEMPLATE = "\na [fhir:$id$]?;$root$";
// Root Declaration.
private static String ROOT_TEMPLATE = "\nfhir:nodeRole [fhir:treeRoot]?;";
private static String ROOT_TEMPLATE = "\nfhir:nodeRole [fhir:treeRoot]?;\n";
// Element
// a predicate, type and cardinality triple
@ -511,16 +518,32 @@ public class ShExGenerator {
return shex_def.render();
}
private String getExtendedType(StructureDefinition sd){
private String getBaseTypeName(StructureDefinition sd){
if (sd == null)
return null;
String sId = sd.getId();
String bd = "";
String bd = null;
if (sd.hasBaseDefinition()) {
bd = sd.getBaseDefinition();
String[] els = bd.split("/");
bd = els[els.length - 1];
}
return bd;
}
private String getBaseTypeName(ElementDefinition ed){
if (ed == null)
return null;
return (ed.getType().size() > 0)? (ed.getType().get(0).getCode()) : null;
}
private String getExtendedType(StructureDefinition sd){
String bd = getBaseTypeName(sd);
String sId = sd.getId();
if (bd!=null) {
addImport("<" + bd + ">");
sId += "> EXTENDS @<" + bd;
}
@ -529,10 +552,8 @@ public class ShExGenerator {
}
private String getExtendedType(ElementDefinition ed){
if (ed == null)
return "";
String bd = (ed.getType().size() > 0)? (ed.getType().get(0).getCode()) : "";
if (bd != null && !bd.isEmpty() && !baseDataTypes.contains(bd)) {
String bd = getBaseTypeName(ed);
if (bd != null && !baseDataTypes.contains(bd)) {
addImport("<" + bd + ">");
bd = "> EXTENDS @<" + bd;
}
@ -555,19 +576,31 @@ public class ShExGenerator {
// if (sd.getName().equals("ActivityDefinition")){
// debug("ActivityDefinition found");
// }
if("Resource".equals(sd.getName())) {
shape_defn = tmplt(RESOURCE_SHAPE_TEMPLATE);
known_resources.add(sd.getName());
} else {
shape_defn = tmplt(SHAPE_DEFINITION_TEMPLATE).add("id", getExtendedType(sd));
known_resources.add(sd.getName());
ST resource_decl = tmplt(RESOURCE_DECL_TEMPLATE).
add("id", sd.getId()).
add("root", tmplt(ROOT_TEMPLATE));
shape_defn.add("resourceDecl", resource_decl.render());
if("Resource".equals(sd.getName())) {
shape_defn = tmplt(RESOURCE_SHAPE_TEMPLATE);
known_resources.add(sd.getName());
} else {
shape_defn = tmplt(SHAPE_DEFINITION_TEMPLATE).add("id", getExtendedType(sd));
known_resources.add(sd.getName());
if (baseDataTypes.contains(sd.getType())) {
shape_defn.add("resourceDecl", "\n");
} else {
if ("Element".equals(sd.getName()))
shape_defn.add("resourceDecl", tmplt(ROOT_TEMPLATE).render());
else {
String rootTmpl = tmplt(ROOT_TEMPLATE).render();
if (baseDataTypes.contains(getBaseTypeName(sd)))
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("fhirType", " ");
}
// Generate the defining elements
List<String> elements = new ArrayList<String>();
@ -1243,7 +1276,12 @@ public class ShExGenerator {
if ((ed.getType().size() > 0) &&
(ed.getType().get(0).getCode().startsWith(Constants.NS_SYSTEM_TYPE))) {
shortId = "v";
if (changeShortName(sd, ed)) {
debug("VALUE NAME CHANGED to v from " + shortId + " for " + sd.getName() + ":" + ed.getPath());
shortId = "v";
}
typ = ed.getType().get(0).getWorkingCode();
}
@ -1273,7 +1311,7 @@ public class ShExGenerator {
if (id.endsWith("[x]")) {
defn = " (" + genChoiceTypes(sd, ed, shortId) + ") ";
defn += " AND { rdf:type IRI } ";
//defn += " AND { rdf:type IRI } ";
} else {
if (ed.getType().size() == 1) {
// Single entry
@ -1345,6 +1383,10 @@ public class ShExGenerator {
return element_def.render();
}
private boolean changeShortName(StructureDefinition sd, ElementDefinition ed){
return !shortIdException.contains(sd.getName());
}
private void addImport(String typeDefn) {
if ((typeDefn != null) && (!typeDefn.isEmpty())) {
// String importType = StringUtils.substringBetween(typeDefn, "<", ">");
@ -1678,7 +1720,7 @@ public class ShExGenerator {
for (ElementDefinition.ElementDefinitionConstraintComponent constraint : ed.getConstraint()) {
String sdType = sd.getType();
String cstype = constraint.getSource();
if ((!cstype.isEmpty()) && (cstype.indexOf("/") != -1)) {
if ((cstype != null) && (cstype.indexOf("/") != -1)) {
String[] els = cstype.split("/");
cstype = els[els.length - 1];
}

View File

@ -27,8 +27,6 @@ import org.junit.jupiter.api.Test;
import static org.hl7.fhir.r5.test.ShexGeneratorTestUtils.printList;
public class ShexGeneratorTests {
public static List<String> selectedExtesnsions = new ArrayList<String>();
public ShExGenerator shexGenerator;
@BeforeAll
@ -45,8 +43,11 @@ public class ShexGeneratorTests {
// For Testing Schema Processing and Constraint Mapping related Development
// If you un-comment the following lines, please comment all other lines in this method.
this.doTestSingleSD(name.toLowerCase(), cat, name, false, ShExGenerator.ConstraintTranslationPolicy.ALL, true, true, false);
this.doTestSingleSD(name.toLowerCase(), cat, name,
false, ShExGenerator.ConstraintTranslationPolicy.ALL,
true, true, false);
}
@Test
public void testId() throws FHIRException, IOException, UcumException {
doTest("id", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
@ -71,6 +72,7 @@ public class ShexGeneratorTests {
public void testAccount() throws FHIRException, IOException, UcumException {
doTest("Account", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testAppointment() throws FHIRException, IOException, UcumException {
doTest("Appointment", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
@ -111,167 +113,87 @@ public class ShexGeneratorTests {
doTest("Signature", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Ignore
@Test
public void testCapabilityStatement() throws FHIRException, IOException, UcumException {
doTest("CapabilityStatement", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
private void doTestSingleSD(String shortName, ShexGeneratorTestUtils.RESOURCE_CATEGORY cat, String name, boolean useSelectedExtensions, ShExGenerator.ConstraintTranslationPolicy policy, boolean debugMode, boolean validateShEx, boolean excludeMetaSDs) {
IWorkerContext ctx = TestingUtilities.getSharedWorkerContext();
StructureDefinition sd = ctx.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(name, null));
if (sd == null) {
throw new FHIRException("StructuredDefinition for " + name + "(Kind:" + cat + ") was null");
}
//Path outPath = FileSystems.getDefault().getPath(System.getProperty("java.io.tmpdir"), name.toLowerCase() + ".shex");
Path outPath = FileSystems.getDefault().getPath(System.getProperty("user.home") + "/runtime_environments/ShExSchemas", shortName + ".shex");
try {
this.shexGenerator = new ShExGenerator(ctx);
this.shexGenerator.debugMode = debugMode;
this.shexGenerator.constraintPolicy = policy;
if (excludeMetaSDs) {
// ShEx Generator skips resources which are at Meta level of FHIR Resource definitions
this.shexGenerator.setExcludedStructureDefinitionUrls(
ShexGeneratorTestUtils.getMetaStructureDefinitionsToSkip());
}
// when ShEx translates only selected resource extensions
if (useSelectedExtensions) {
List<StructureDefinition> selExtns = new ArrayList<StructureDefinition>();
for (String eUrl : ShexGeneratorTestUtils.getSelectedExtensions()) {
StructureDefinition esd = ctx.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(eUrl, null));
if (esd != null)
selExtns.add(esd);
}
this.shexGenerator.setSelectedExtension(selExtns);
}
String schema = this.shexGenerator.generate(HTMLLinkPolicy.NONE, sd);
if (!schema.isEmpty()) {
if (validateShEx) {
try {
ShExsValidator validator = ShExsValidatorBuilder.fromStringSync(schema, "ShexC");
Schema sch = validator.schema();
Assert.assertNotNull(sch);
System.out.println("VALIDATION PASSED for ShEx Schema " + sd.getName() + " (Kind:" + cat + ")" );
} catch (Exception e) {
System.out.println("VALIDATION FAILED for ShEx Schema " + sd.getName() + " (Kind:" + cat + ")" );
e.printStackTrace();
}
}
TextFile.stringToFile(schema, outPath.toString());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
@Test
public void testElement() throws FHIRException, IOException, UcumException {
doTest("Element", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
private void doTestBatchSD(List<StructureDefinition> sds, boolean useSelectedExtensions, ShExGenerator.ConstraintTranslationPolicy policy, boolean debugMode, boolean validateShEx, boolean excludeMetaSDs) {
IWorkerContext ctx = TestingUtilities.getSharedWorkerContext();
//Path outPath = FileSystems.getDefault().getPath(System.getProperty("java.io.tmpdir"), name.toLowerCase() + ".shex");
Path outPath = FileSystems.getDefault().getPath(System.getProperty("user.home") + "/runtime_environments/ShExSchemas", "ShEx.shex");
try {
this.shexGenerator = new ShExGenerator(ctx);
@Test
public void doTestAllSingleSDMode() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
this.shexGenerator.debugMode = debugMode;
this.shexGenerator.constraintPolicy = policy;
if (excludeMetaSDs) {
// ShEx Generator skips resources which are at Meta level of FHIR Resource definitions
this.shexGenerator.setExcludedStructureDefinitionUrls(
ShexGeneratorTestUtils.getMetaStructureDefinitionsToSkip());
}
// when ShEx translates only selected resource extensions
if (useSelectedExtensions) {
List<StructureDefinition> selExtns = new ArrayList<StructureDefinition>();
for (String eUrl : ShexGeneratorTestUtils.getSelectedExtensions()) {
StructureDefinition esd = ctx.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(eUrl, null));
if (esd != null)
selExtns.add(esd);
}
this.shexGenerator.setSelectedExtension(selExtns);
}
String schema = this.shexGenerator.generate(HTMLLinkPolicy.NONE, sds);
if (!schema.isEmpty()) {
if (validateShEx) {
try {
ShExsValidator validator = ShExsValidatorBuilder.fromStringSync(schema, "ShexC");
Schema sch = validator.schema();
Assert.assertNotNull(sch);
System.out.println("VALIDATION PASSED for ShEx Schema ALL SHEX STRUCTURES");
} catch (Exception e) {
System.out.println("VALIDATION FAILED for ShEx Schema ALL SHEX STRUCTURES");
e.printStackTrace();
}
}
TextFile.stringToFile(schema, outPath.toString());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process all extensions
// Process all types of constraints, do not skip
ShExGenerator.ConstraintTranslationPolicy.ALL,
// BatchMode - All Shex Schemas in one single file
false
);
}
@Test
public void doTestAll() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
@Test
public void doTestAllBatchMode() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process all extensions
ShExGenerator.ConstraintTranslationPolicy.ALL,
// Process all types of constraints, do not skip
true
);
}
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process all extensions
// Process all types of constraints, do not skip
ShExGenerator.ConstraintTranslationPolicy.ALL,
// BatchMode - All Shex Schemas in one single file
true
);
}
@Ignore
public void doTestGenericExtensionsOnlyPolicy() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
@Ignore
public void doTestGenericExtensionsOnlyPolicy() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process all extensions
ShExGenerator.ConstraintTranslationPolicy.GENERIC_ONLY,
// Process generic constraints only, ignore constraints of type 'context of use'
false
);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process all extensions
ShExGenerator.ConstraintTranslationPolicy.GENERIC_ONLY,
// Process generic constraints only, ignore constraints of type 'context of use'
false
);
}
}
@Ignore
public void doTestContextOfUseExtensionsOnlyPolicy() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
@Ignore
public void doTestContextOfUseExtensionsOnlyPolicy() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process all extensions
ShExGenerator.ConstraintTranslationPolicy.CONTEXT_OF_USE_ONLY,
// Process constraints only where context of use found, skip otherwise
false
);
}
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process all extensions
ShExGenerator.ConstraintTranslationPolicy.CONTEXT_OF_USE_ONLY,
// Process constraints only where context of use found, skip otherwise
false
);
}
@Ignore
public void doTestSelectedExtensions() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
@Ignore
public void doTestSelectedExtensions() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
true, //Process only given/selected extensions, ignore other extensions
ShExGenerator.ConstraintTranslationPolicy.ALL, // Process all type of constraints
false
);
}
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
true, //Process only given/selected extensions, ignore other extensions
ShExGenerator.ConstraintTranslationPolicy.ALL, // Process all type of constraints
false
);
}
@Ignore
public void testStructureDefinitionsOnly() throws FileNotFoundException, IOException, FHIRException, UcumException {
@ -326,8 +248,8 @@ public class ShexGeneratorTests {
private void processSDList(ShexGeneratorTestUtils.RESOURCE_CATEGORY cat,
List<StructureDefinition> sds,
boolean useSelectedExtensions,
ShExGenerator.ConstraintTranslationPolicy policy,
boolean useSelectedExtensions,
ShExGenerator.ConstraintTranslationPolicy policy,
boolean batchMode) {
if ((sds == null) || (sds.isEmpty())) {
throw new FHIRException("No StructuredDefinition found!");
@ -363,4 +285,111 @@ public class ShexGeneratorTests {
System.out.println("Total Items processed: " + sds.size());
System.out.println("************************************************************************");
}
private void doTestSingleSD(String shortName, ShexGeneratorTestUtils.RESOURCE_CATEGORY cat,
String name, boolean useSelectedExtensions,
ShExGenerator.ConstraintTranslationPolicy policy,
boolean debugMode, boolean validateShEx,
boolean excludeMetaSDs) {
IWorkerContext ctx = TestingUtilities.getSharedWorkerContext();
StructureDefinition sd = ctx.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(name, null));
if (sd == null) {
throw new FHIRException("StructuredDefinition for " + name + "(Kind:" + cat + ") was null");
}
//Path outPath = FileSystems.getDefault().getPath(System.getProperty("java.io.tmpdir"), name.toLowerCase() + ".shex");
Path outPath = FileSystems.getDefault().getPath(System.getProperty("user.home") + "/runtime_environments/ShExSchemas", shortName + ".shex");
try {
this.shexGenerator = new ShExGenerator(ctx);
this.shexGenerator.debugMode = debugMode;
this.shexGenerator.constraintPolicy = policy;
if (excludeMetaSDs) {
// ShEx Generator skips resources which are at Meta level of FHIR Resource definitions
this.shexGenerator.setExcludedStructureDefinitionUrls(
ShexGeneratorTestUtils.getMetaStructureDefinitionsToSkip());
}
// when ShEx translates only selected resource extensions
if (useSelectedExtensions) {
List<StructureDefinition> selExtns = new ArrayList<StructureDefinition>();
for (String eUrl : ShexGeneratorTestUtils.getSelectedExtensions()) {
StructureDefinition esd = ctx.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(eUrl, null));
if (esd != null)
selExtns.add(esd);
}
this.shexGenerator.setSelectedExtension(selExtns);
}
String schema = this.shexGenerator.generate(HTMLLinkPolicy.NONE, sd);
if (!schema.isEmpty()) {
if (validateShEx) {
try {
ShExsValidator validator = ShExsValidatorBuilder.fromStringSync(schema, "ShexC");
Schema sch = validator.schema();
Assert.assertNotNull(sch);
System.out.println("VALIDATION PASSED for ShEx Schema " + sd.getName() + " (Kind:" + cat + ")");
} catch (Exception e) {
System.out.println("VALIDATION FAILED for ShEx Schema " + sd.getName() + " (Kind:" + cat + ")");
e.printStackTrace();
}
}
TextFile.stringToFile(schema, outPath.toString());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void doTestBatchSD(List<StructureDefinition> sds, boolean useSelectedExtensions,
ShExGenerator.ConstraintTranslationPolicy policy, boolean debugMode,
boolean validateShEx, boolean excludeMetaSDs) {
IWorkerContext ctx = TestingUtilities.getSharedWorkerContext();
//Path outPath = FileSystems.getDefault().getPath(System.getProperty("java.io.tmpdir"), name.toLowerCase() + ".shex");
Path outPath = FileSystems.getDefault().getPath(System.getProperty("user.home") + "/runtime_environments/ShExSchemas", "ShEx.shex");
try {
this.shexGenerator = new ShExGenerator(ctx);
this.shexGenerator.debugMode = debugMode;
this.shexGenerator.constraintPolicy = policy;
if (excludeMetaSDs) {
// ShEx Generator skips resources which are at Meta level of FHIR Resource definitions
this.shexGenerator.setExcludedStructureDefinitionUrls(
ShexGeneratorTestUtils.getMetaStructureDefinitionsToSkip());
}
// when ShEx translates only selected resource extensions
if (useSelectedExtensions) {
List<StructureDefinition> selExtns = new ArrayList<StructureDefinition>();
for (String eUrl : ShexGeneratorTestUtils.getSelectedExtensions()) {
StructureDefinition esd = ctx.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(eUrl, null));
if (esd != null)
selExtns.add(esd);
}
this.shexGenerator.setSelectedExtension(selExtns);
}
String schema = this.shexGenerator.generate(HTMLLinkPolicy.NONE, sds);
if (!schema.isEmpty()) {
if (validateShEx) {
try {
ShExsValidator validator = ShExsValidatorBuilder.fromStringSync(schema, "ShexC");
Schema sch = validator.schema();
Assert.assertNotNull(sch);
System.out.println("VALIDATION PASSED for ShEx Schema ALL SHEX STRUCTURES");
} catch (Exception e) {
System.out.println("VALIDATION FAILED for ShEx Schema ALL SHEX STRUCTURES");
e.printStackTrace();
}
}
TextFile.stringToFile(schema, outPath.toString());
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}