Corrected TypeArcs and Node Roles
This commit is contained in:
parent
9ca531ff0a
commit
e64461e940
|
@ -108,7 +108,14 @@ public class ShExGenerator {
|
||||||
// Base DataTypes
|
// Base DataTypes
|
||||||
private List<String> baseDataTypes = Arrays.asList(
|
private List<String> baseDataTypes = Arrays.asList(
|
||||||
"DataType",
|
"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(
|
private List<String> mappedFunctions = Arrays.asList(
|
||||||
|
@ -133,7 +140,7 @@ public class ShExGenerator {
|
||||||
// Resource Definition
|
// Resource Definition
|
||||||
// an open shape of type Resource. Used when completeModel = false.
|
// an open shape of type Resource. Used when completeModel = false.
|
||||||
private static String RESOURCE_SHAPE_TEMPLATE =
|
private static String RESOURCE_SHAPE_TEMPLATE =
|
||||||
"$comment$\n<Resource> {a .+;" +
|
"$comment$\n<Resource> {" +
|
||||||
"\n $elements$" +
|
"\n $elements$" +
|
||||||
"\n $contextOfUse$" +
|
"\n $contextOfUse$" +
|
||||||
"\n} $constraints$ \n";
|
"\n} $constraints$ \n";
|
||||||
|
@ -147,10 +154,10 @@ public class ShExGenerator {
|
||||||
// Resource Declaration
|
// Resource Declaration
|
||||||
// a type node
|
// a type node
|
||||||
// an optional treeRoot declaration (identifies the entry point)
|
// 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.
|
// Root Declaration.
|
||||||
private static String ROOT_TEMPLATE = "\nfhir:nodeRole [fhir:treeRoot]?;";
|
private static String ROOT_TEMPLATE = "\nfhir:nodeRole [fhir:treeRoot]?;\n";
|
||||||
|
|
||||||
// Element
|
// Element
|
||||||
// a predicate, type and cardinality triple
|
// a predicate, type and cardinality triple
|
||||||
|
@ -511,16 +518,32 @@ public class ShExGenerator {
|
||||||
return shex_def.render();
|
return shex_def.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getExtendedType(StructureDefinition sd){
|
private String getBaseTypeName(StructureDefinition sd){
|
||||||
if (sd == null)
|
if (sd == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
String sId = sd.getId();
|
String bd = null;
|
||||||
String bd = "";
|
|
||||||
if (sd.hasBaseDefinition()) {
|
if (sd.hasBaseDefinition()) {
|
||||||
bd = sd.getBaseDefinition();
|
bd = sd.getBaseDefinition();
|
||||||
String[] els = bd.split("/");
|
String[] els = bd.split("/");
|
||||||
bd = els[els.length - 1];
|
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 + ">");
|
addImport("<" + bd + ">");
|
||||||
sId += "> EXTENDS @<" + bd;
|
sId += "> EXTENDS @<" + bd;
|
||||||
}
|
}
|
||||||
|
@ -529,10 +552,8 @@ public class ShExGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getExtendedType(ElementDefinition ed){
|
private String getExtendedType(ElementDefinition ed){
|
||||||
if (ed == null)
|
String bd = getBaseTypeName(ed);
|
||||||
return "";
|
if (bd != null && !baseDataTypes.contains(bd)) {
|
||||||
String bd = (ed.getType().size() > 0)? (ed.getType().get(0).getCode()) : "";
|
|
||||||
if (bd != null && !bd.isEmpty() && !baseDataTypes.contains(bd)) {
|
|
||||||
addImport("<" + bd + ">");
|
addImport("<" + bd + ">");
|
||||||
bd = "> EXTENDS @<" + bd;
|
bd = "> EXTENDS @<" + bd;
|
||||||
}
|
}
|
||||||
|
@ -555,19 +576,31 @@ public class ShExGenerator {
|
||||||
// if (sd.getName().equals("ActivityDefinition")){
|
// if (sd.getName().equals("ActivityDefinition")){
|
||||||
// debug("ActivityDefinition found");
|
// debug("ActivityDefinition found");
|
||||||
// }
|
// }
|
||||||
if("Resource".equals(sd.getName())) {
|
if("Resource".equals(sd.getName())) {
|
||||||
shape_defn = tmplt(RESOURCE_SHAPE_TEMPLATE);
|
shape_defn = tmplt(RESOURCE_SHAPE_TEMPLATE);
|
||||||
known_resources.add(sd.getName());
|
known_resources.add(sd.getName());
|
||||||
} else {
|
} else {
|
||||||
shape_defn = tmplt(SHAPE_DEFINITION_TEMPLATE).add("id", getExtendedType(sd));
|
shape_defn = tmplt(SHAPE_DEFINITION_TEMPLATE).add("id", getExtendedType(sd));
|
||||||
known_resources.add(sd.getName());
|
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 (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", " ");
|
shape_defn.add("fhirType", " ");
|
||||||
}
|
|
||||||
|
|
||||||
// Generate the defining elements
|
// Generate the defining elements
|
||||||
List<String> elements = new ArrayList<String>();
|
List<String> elements = new ArrayList<String>();
|
||||||
|
@ -1243,7 +1276,12 @@ public class ShExGenerator {
|
||||||
|
|
||||||
if ((ed.getType().size() > 0) &&
|
if ((ed.getType().size() > 0) &&
|
||||||
(ed.getType().get(0).getCode().startsWith(Constants.NS_SYSTEM_TYPE))) {
|
(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();
|
typ = ed.getType().get(0).getWorkingCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1273,7 +1311,7 @@ public class ShExGenerator {
|
||||||
|
|
||||||
if (id.endsWith("[x]")) {
|
if (id.endsWith("[x]")) {
|
||||||
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) {
|
||||||
// Single entry
|
// Single entry
|
||||||
|
@ -1345,6 +1383,10 @@ public class ShExGenerator {
|
||||||
return element_def.render();
|
return element_def.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean changeShortName(StructureDefinition sd, ElementDefinition ed){
|
||||||
|
return !shortIdException.contains(sd.getName());
|
||||||
|
}
|
||||||
|
|
||||||
private void addImport(String typeDefn) {
|
private void addImport(String typeDefn) {
|
||||||
if ((typeDefn != null) && (!typeDefn.isEmpty())) {
|
if ((typeDefn != null) && (!typeDefn.isEmpty())) {
|
||||||
// String importType = StringUtils.substringBetween(typeDefn, "<", ">");
|
// String importType = StringUtils.substringBetween(typeDefn, "<", ">");
|
||||||
|
@ -1678,7 +1720,7 @@ public class ShExGenerator {
|
||||||
for (ElementDefinition.ElementDefinitionConstraintComponent constraint : ed.getConstraint()) {
|
for (ElementDefinition.ElementDefinitionConstraintComponent constraint : ed.getConstraint()) {
|
||||||
String sdType = sd.getType();
|
String sdType = sd.getType();
|
||||||
String cstype = constraint.getSource();
|
String cstype = constraint.getSource();
|
||||||
if ((!cstype.isEmpty()) && (cstype.indexOf("/") != -1)) {
|
if ((cstype != null) && (cstype.indexOf("/") != -1)) {
|
||||||
String[] els = cstype.split("/");
|
String[] els = cstype.split("/");
|
||||||
cstype = els[els.length - 1];
|
cstype = els[els.length - 1];
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,6 @@ import org.junit.jupiter.api.Test;
|
||||||
import static org.hl7.fhir.r5.test.ShexGeneratorTestUtils.printList;
|
import static org.hl7.fhir.r5.test.ShexGeneratorTestUtils.printList;
|
||||||
|
|
||||||
public class ShexGeneratorTests {
|
public class ShexGeneratorTests {
|
||||||
public static List<String> selectedExtesnsions = new ArrayList<String>();
|
|
||||||
|
|
||||||
public ShExGenerator shexGenerator;
|
public ShExGenerator shexGenerator;
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
|
@ -45,8 +43,11 @@ public class ShexGeneratorTests {
|
||||||
|
|
||||||
// For Testing Schema Processing and Constraint Mapping related Development
|
// For Testing Schema Processing and Constraint Mapping related Development
|
||||||
// If you un-comment the following lines, please comment all other lines in this method.
|
// 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
|
@Test
|
||||||
public void testId() throws FHIRException, IOException, UcumException {
|
public void testId() throws FHIRException, IOException, UcumException {
|
||||||
doTest("id", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
doTest("id", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
||||||
|
@ -71,6 +72,7 @@ public class ShexGeneratorTests {
|
||||||
public void testAccount() throws FHIRException, IOException, UcumException {
|
public void testAccount() throws FHIRException, IOException, UcumException {
|
||||||
doTest("Account", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
doTest("Account", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAppointment() throws FHIRException, IOException, UcumException {
|
public void testAppointment() throws FHIRException, IOException, UcumException {
|
||||||
doTest("Appointment", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
doTest("Appointment", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
||||||
|
@ -111,167 +113,87 @@ public class ShexGeneratorTests {
|
||||||
doTest("Signature", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
doTest("Signature", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
@Test
|
||||||
public void testCapabilityStatement() throws FHIRException, IOException, UcumException {
|
public void testCapabilityStatement() throws FHIRException, IOException, UcumException {
|
||||||
doTest("CapabilityStatement", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
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) {
|
@Test
|
||||||
IWorkerContext ctx = TestingUtilities.getSharedWorkerContext();
|
public void testElement() throws FHIRException, IOException, UcumException {
|
||||||
StructureDefinition sd = ctx.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(name, null));
|
doTest("Element", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
|
||||||
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) {
|
@Test
|
||||||
IWorkerContext ctx = TestingUtilities.getSharedWorkerContext();
|
public void doTestAllSingleSDMode() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
||||||
//Path outPath = FileSystems.getDefault().getPath(System.getProperty("java.io.tmpdir"), name.toLowerCase() + ".shex");
|
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
||||||
Path outPath = FileSystems.getDefault().getPath(System.getProperty("user.home") + "/runtime_environments/ShExSchemas", "ShEx.shex");
|
|
||||||
try {
|
|
||||||
this.shexGenerator = new ShExGenerator(ctx);
|
|
||||||
|
|
||||||
this.shexGenerator.debugMode = debugMode;
|
processSDList(
|
||||||
this.shexGenerator.constraintPolicy = policy;
|
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
||||||
|
sds, // List of Structure Definitions
|
||||||
if (excludeMetaSDs) {
|
false, //Process all extensions
|
||||||
// ShEx Generator skips resources which are at Meta level of FHIR Resource definitions
|
// Process all types of constraints, do not skip
|
||||||
this.shexGenerator.setExcludedStructureDefinitionUrls(
|
ShExGenerator.ConstraintTranslationPolicy.ALL,
|
||||||
ShexGeneratorTestUtils.getMetaStructureDefinitionsToSkip());
|
// BatchMode - All Shex Schemas in one single file
|
||||||
}
|
false
|
||||||
|
);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void doTestAll() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
public void doTestAllBatchMode() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
||||||
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
||||||
|
|
||||||
processSDList(
|
processSDList(
|
||||||
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
||||||
sds, // List of Structure Definitions
|
sds, // List of Structure Definitions
|
||||||
false, //Process all extensions
|
false, //Process all extensions
|
||||||
ShExGenerator.ConstraintTranslationPolicy.ALL,
|
// Process all types of constraints, do not skip
|
||||||
// Process all types of constraints, do not skip
|
ShExGenerator.ConstraintTranslationPolicy.ALL,
|
||||||
true
|
// BatchMode - All Shex Schemas in one single file
|
||||||
);
|
true
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Ignore
|
@Ignore
|
||||||
public void doTestGenericExtensionsOnlyPolicy() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
public void doTestGenericExtensionsOnlyPolicy() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
||||||
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
||||||
|
|
||||||
processSDList(
|
processSDList(
|
||||||
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
||||||
sds, // List of Structure Definitions
|
sds, // List of Structure Definitions
|
||||||
false, //Process all extensions
|
false, //Process all extensions
|
||||||
ShExGenerator.ConstraintTranslationPolicy.GENERIC_ONLY,
|
ShExGenerator.ConstraintTranslationPolicy.GENERIC_ONLY,
|
||||||
// Process generic constraints only, ignore constraints of type 'context of use'
|
// Process generic constraints only, ignore constraints of type 'context of use'
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
@Ignore
|
||||||
public void doTestContextOfUseExtensionsOnlyPolicy() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
public void doTestContextOfUseExtensionsOnlyPolicy() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
||||||
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
||||||
|
|
||||||
processSDList(
|
processSDList(
|
||||||
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
||||||
sds, // List of Structure Definitions
|
sds, // List of Structure Definitions
|
||||||
false, //Process all extensions
|
false, //Process all extensions
|
||||||
ShExGenerator.ConstraintTranslationPolicy.CONTEXT_OF_USE_ONLY,
|
ShExGenerator.ConstraintTranslationPolicy.CONTEXT_OF_USE_ONLY,
|
||||||
// Process constraints only where context of use found, skip otherwise
|
// Process constraints only where context of use found, skip otherwise
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
@Ignore
|
||||||
public void doTestSelectedExtensions() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
public void doTestSelectedExtensions() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
||||||
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
|
||||||
|
|
||||||
processSDList(
|
processSDList(
|
||||||
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
ShexGeneratorTestUtils.RESOURCE_CATEGORY.ALL, // Processing All kinds of Structure Definitions
|
||||||
sds, // List of Structure Definitions
|
sds, // List of Structure Definitions
|
||||||
true, //Process only given/selected extensions, ignore other extensions
|
true, //Process only given/selected extensions, ignore other extensions
|
||||||
ShExGenerator.ConstraintTranslationPolicy.ALL, // Process all type of constraints
|
ShExGenerator.ConstraintTranslationPolicy.ALL, // Process all type of constraints
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore
|
@Ignore
|
||||||
public void testStructureDefinitionsOnly() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
public void testStructureDefinitionsOnly() throws FileNotFoundException, IOException, FHIRException, UcumException {
|
||||||
|
@ -326,8 +248,8 @@ public class ShexGeneratorTests {
|
||||||
|
|
||||||
private void processSDList(ShexGeneratorTestUtils.RESOURCE_CATEGORY cat,
|
private void processSDList(ShexGeneratorTestUtils.RESOURCE_CATEGORY cat,
|
||||||
List<StructureDefinition> sds,
|
List<StructureDefinition> sds,
|
||||||
boolean useSelectedExtensions,
|
boolean useSelectedExtensions,
|
||||||
ShExGenerator.ConstraintTranslationPolicy policy,
|
ShExGenerator.ConstraintTranslationPolicy policy,
|
||||||
boolean batchMode) {
|
boolean batchMode) {
|
||||||
if ((sds == null) || (sds.isEmpty())) {
|
if ((sds == null) || (sds.isEmpty())) {
|
||||||
throw new FHIRException("No StructuredDefinition found!");
|
throw new FHIRException("No StructuredDefinition found!");
|
||||||
|
@ -363,4 +285,111 @@ public class ShexGeneratorTests {
|
||||||
System.out.println("Total Items processed: " + sds.size());
|
System.out.println("Total Items processed: " + sds.size());
|
||||||
System.out.println("************************************************************************");
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue