Merge pull request #1144 from hapifhir/do-20230227-fhircat-shex-fix

Fix for build failures in #1120
This commit is contained in:
Grahame Grieve 2023-03-01 04:54:10 +11:00 committed by GitHub
commit 94de728553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1841 additions and 209 deletions

View File

@ -50,6 +50,14 @@
<optional>true</optional>
</dependency>
<!-- ShEx -->
<dependency>
<groupId>es.weso</groupId>
<artifactId>shexs_2.12</artifactId>
<version>0.2.31</version>
<optional>true</optional>
</dependency>
<!-- JSON Parsers -->
<dependency>
<groupId>com.google.code.gson</groupId>

View File

@ -0,0 +1,221 @@
package org.hl7.fhir.r5.test;
import org.hl7.fhir.r5.model.StructureDefinition;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* ShexGeneratorTestUtils
*
* This class provides auxiliary methos to perform testing of
* Shape Expressions (ShEx) translation of various FHIR Resources.
*
* Author: Deepak Sharma
*/
public class ShexGeneratorTestUtils {
public class resDef {
public String name;
public String url;
public String info;
public RESOURCE_CATEGORY kind;
public resDef(String _name, String _url, String _info, RESOURCE_CATEGORY _kind){
this.name = _name;
this.url = _url;
this.info = _info;
this.kind = _kind;
}
@Override
public String toString() {
return " " + name + " (Kind: " + kind + " ) [ " + url + " ]";
}
}
public enum RESOURCE_CATEGORY{
LOGICAL_NAME, STRUCTURE_DEFINITION, EXTENSION, PROFILE, ALL, META_OR_EXAMPLE_OR_OTHER_IGNORE
}
/**
* Filters Structure Definitions from a given list of
* mixed types of StructureDefinitons based on resource categories
* @param sds List of StructureDefiniton of any resource type
* @param cat Resource category filter criteria (inclusive)
* @return List of resDef with just Structure Definition's name, url and description.
*/
public List<resDef> getSDs(List<StructureDefinition> sds, RESOURCE_CATEGORY cat) {
List<resDef> selSDs = new ArrayList<resDef>();
sds.forEach((StructureDefinition sd) -> {
switch(cat) {
case STRUCTURE_DEFINITION:
if (getCategory(sd).equals(RESOURCE_CATEGORY.STRUCTURE_DEFINITION))
selSDs.add(new resDef(sd.getName(), sd.getUrl(), getSDInfo(sd), RESOURCE_CATEGORY.STRUCTURE_DEFINITION));
break;
case LOGICAL_NAME:
if (getCategory(sd).equals(RESOURCE_CATEGORY.LOGICAL_NAME))
selSDs.add(new resDef(sd.getName(), sd.getUrl(), getSDInfo(sd), RESOURCE_CATEGORY.LOGICAL_NAME));
break;
case EXTENSION:
if (getCategory(sd).equals(RESOURCE_CATEGORY.EXTENSION))
selSDs.add(new resDef(sd.getName(), sd.getUrl(), getSDInfo(sd), RESOURCE_CATEGORY.EXTENSION));
break;
case PROFILE:
if (getCategory(sd).equals(RESOURCE_CATEGORY.PROFILE))
selSDs.add(new resDef(sd.getName(), sd.getUrl(), getSDInfo(sd), RESOURCE_CATEGORY.PROFILE));
break;
default:
selSDs.add(new resDef(sd.getName(), sd.getUrl(), getSDInfo(sd), getCategory(sd)));
}
});
Collections.sort(selSDs, new Comparator<resDef>() {
@Override
public int compare(resDef o1, resDef o2) {
return o1.name.compareTo(o2.name);
}
});
return selSDs;
}
private RESOURCE_CATEGORY getCategory(StructureDefinition sd) {
if ("Extension".equals(sd.getType()))
return RESOURCE_CATEGORY.EXTENSION;
if (sd.getBaseDefinition() == null)
return RESOURCE_CATEGORY.LOGICAL_NAME;
if (sd.getType().trim().equals(sd.getName().trim()))
return RESOURCE_CATEGORY.STRUCTURE_DEFINITION;
if (!((sd.getBaseDefinition() == null) ||
("Extension".equals(sd.getType())) ||
(sd.getType().trim().equals(sd.getName().trim()))))
return RESOURCE_CATEGORY.PROFILE;
return RESOURCE_CATEGORY.META_OR_EXAMPLE_OR_OTHER_IGNORE;
}
/**
* This method is used in testing only - during Resource Constraint translation to ShEx constructs.
* It can be used by future developers to avoid unnecessary processing of constraints
* that belong to these Meta Level Resources of FHIR. This list was created by FHIRCat
* Project group for testing (fhircat.org). It does not skip these SDs for the build.
* @return List of StructureDefinitions at meta Level
*/
public static List<String> getMetaStructureDefinitionsToSkip(){
List<String> skipSDs = new ArrayList<String>();
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ActivityDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/AdministrableProductDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ChargeItemDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ClinicalUseDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/CodeSystem");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ConceptMap");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/CompartmentDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/CompartmentDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/DeviceDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ElementDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/EventDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/GraphDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/GuidanceResponse");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/Library");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ManufacturedItemDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/Measure");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/MeasureReport");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/MedicinalProductDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/MessageDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/NamingSystem");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ObservationDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/OperationDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/PackagedProductDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ParameterDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/PlanDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/SearchParameter");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/SpecimenDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/StructureDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/SubstanceDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/Task");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/TerminologyCapabilities");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/TriggerDefinition");
skipSDs.add("http://hl7.org/fhir/StructureDefinition/ValueSet");
return skipSDs;
}
/**
* This method prepares selected extensions to test the policy in which ShEx Generator
* only translates selected resource extensions
* @return List<String> List of selected resource extensions for testing
*/
public static List<String> getSelectedExtensions(){
List<String> selectedExtesnsions = new ArrayList<String>();
selectedExtesnsions.add("http://fhir-registry.smarthealthit.org/StructureDefinition/capabilities");
selectedExtesnsions.add("http://fhir-registry.smarthealthit.org/StructureDefinition/oauth-uris");
return selectedExtesnsions;
}
/**
* Structure Definition Information for debug/printing purpose during testing.
* @param sd Structure Definition
* @return String representation of basic SD Information
*/
public static String getSDInfo(StructureDefinition sd) {
if (sd != null) {
String kind = " ";
try {
kind = sd.getKind().name();
} catch (Exception e) {
System.out.println("Kind is null");
}
String name = " ";
try {
name = sd.getName();
} catch (Exception e) {
System.out.println("Name is null");
}
String type = " ";
try {
type = sd.getType();
} catch (Exception e) {
System.out.println("Type is null");
}
String derv = " ";
try {
derv = sd.getDerivation().name();
} catch (Exception e) {
System.out.println("Derivation is null");
}
String url = " ";
try {
url = sd.getUrl();
} catch (Exception e) {
System.out.println("URL is null");
}
String base = " ";
try {
base = sd.getBaseDefinition();
} catch (Exception e) {
System.out.println("Base is null");
}
return kind + "\t" + name + "\t" + type + "\t" + derv + "\t" + url + "\t" + base;
}
return "";
}
/**
* Utility Print method to print interim resDef object for debug/testing purpose.
* @param title title of resDef types (Resources, Extensions, profiles, etc.)
* @param items List of resDef objects
*/
public static void printList(String title, List<ShexGeneratorTestUtils.resDef> items) {
System.out.println("************************************************************************");
System.out.println("Printing " + title);
System.out.println("************************************************************************");
items.forEach((resDef item) -> {
System.out.println(item.name + " \t[" + item.url + "]\t" + item.info);
});
}
}

View File

@ -4,75 +4,421 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import es.weso.shex.Schema;
import es.weso.shex.validator.ShExsValidator;
import es.weso.shex.validator.ShExsValidatorBuilder;
import org.fhir.ucum.UcumException;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
import org.hl7.fhir.r5.conformance.ShExGenerator;
import org.hl7.fhir.r5.conformance.ShExGenerator.HTMLLinkPolicy;
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.test.utils.TestingUtilities;
import org.hl7.fhir.utilities.TextFile;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class ShexGeneratorTests {
import static org.hl7.fhir.r5.test.ShexGeneratorTestUtils.printList;
private void doTest(String name) throws FileNotFoundException, IOException, FHIRException, UcumException {
public class ShexGeneratorTests {
public ShExGenerator shexGenerator;
@BeforeAll
public static void setup() {
}
private void doTest(String name, ShexGeneratorTestUtils.RESOURCE_CATEGORY cat) throws FileNotFoundException, IOException, FHIRException, UcumException {
// ------- Comment following for debugging/testing
StructureDefinition sd = TestingUtilities.getSharedWorkerContext().fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(name, null));
if (sd == null) {
throw new FHIRException("StructuredDefinition for " + name + "was null");
}
Path outPath = FileSystems.getDefault().getPath(System.getProperty("java.io.tmpdir"), name.toLowerCase() + ".shex");
TextFile.stringToFile(new ShExGenerator(TestingUtilities.getSharedWorkerContext()).generate(HTMLLinkPolicy.NONE, sd), outPath.toString());
// For Testing Schema Processing and Constraint Mapping related Development
// If you un-comment the following lines, please comment all other lines in this method.
// Test with processing constraints flag
// ----------------- Uncomment following to testing/Debugging -----
// boolean processConstraints = false;
// this.doTestSingleSD(name.toLowerCase(), cat, name,
// false, ShExGenerator.ConstraintTranslationPolicy.ALL,
// true, true, false, processConstraints);
}
@Test
public void testId() throws FHIRException, IOException, UcumException {
doTest("id");
doTest("id", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testUri() throws FHIRException, IOException, UcumException {
doTest("uri");
doTest("uri", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testPatient() throws FHIRException, IOException, UcumException {
doTest("Patient", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testObservation() throws FHIRException, IOException, UcumException {
doTest("Observation");
doTest("Observation", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testRef() throws FHIRException, IOException, UcumException {
doTest("Reference");
doTest("Reference", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testAccount() throws FHIRException, IOException, UcumException {
doTest("Account");
doTest("Account", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testAppointment() throws FHIRException, IOException, UcumException {
doTest("Appointment", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testBundle() throws FHIRException, IOException, UcumException {
doTest("Bundle", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testAge() throws FHIRException, IOException, UcumException {
doTest("Age", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testMedicationRequest() throws FHIRException, IOException, UcumException {
doTest("MedicationRequest");
doTest("MedicationRequest", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testAllergyIntolerance() throws FHIRException, IOException, UcumException {
doTest("AllergyIntolerance");
doTest("AllergyIntolerance", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testCoding() throws FHIRException, IOException, UcumException {
doTest("Coding");
doTest("Coding", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testTiming() throws FHIRException, IOException, UcumException {
doTest("Timing");
doTest("Timing", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testSignature() throws FHIRException, IOException, UcumException {
doTest("Signature");
doTest("Signature", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testCapabilityStatement() throws FHIRException, IOException, UcumException {
doTest("CapabilityStatement", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Test
public void testElement() throws FHIRException, IOException, UcumException {
doTest("Element", ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION);
}
@Ignore
public void doTestAllSingleSDMode() 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
// Process all types of constraints, do not skip
ShExGenerator.ConstraintTranslationPolicy.ALL,
// BatchMode - All Shex Schemas in one single file
false,
// process constraints or not
false
);
}
@Ignore
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
// Process all types of constraints, do not skip
ShExGenerator.ConstraintTranslationPolicy.ALL,
// BatchMode - All Shex Schemas in one single file
true,
// process constraints or not
true
);
}
@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,
// process constraints or not
true
);
}
@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,
// process constraints or not
true
);
}
@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,
// process constraints or not
true
);
}
@Ignore
public void testStructureDefinitionsOnly() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.STRUCTURE_DEFINITION, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process only given/selected extensions, ignore other extensions
ShExGenerator.ConstraintTranslationPolicy.ALL, // Process all type of constraints
false,
// process constraints or not
true
);
}
@Ignore
public void testExtensionsOnly() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.EXTENSION, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process only given/selected extensions, ignore other extensions
ShExGenerator.ConstraintTranslationPolicy.ALL, // Process all type of constraints
false,
// process constraints or not
true
);
}
@Ignore
public void testLogicalNamesOnly() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.LOGICAL_NAME, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process only given/selected extensions, ignore other extensions
ShExGenerator.ConstraintTranslationPolicy.ALL, // Process all type of constraints
false,
// process constraints or not
true
);
}
@Ignore
public void testProfilesOnly() throws FileNotFoundException, IOException, FHIRException, UcumException {
List<StructureDefinition> sds = TestingUtilities.getSharedWorkerContext().fetchResourcesByType(StructureDefinition.class);
processSDList(
ShexGeneratorTestUtils.RESOURCE_CATEGORY.PROFILE, // Processing All kinds of Structure Definitions
sds, // List of Structure Definitions
false, //Process only given/selected extensions, ignore other extensions
ShExGenerator.ConstraintTranslationPolicy.ALL, // Process all type of constraints
false,
// process constraints or not
true
);
}
private void processSDList(ShexGeneratorTestUtils.RESOURCE_CATEGORY cat,
List<StructureDefinition> sds,
boolean useSelectedExtensions,
ShExGenerator.ConstraintTranslationPolicy policy,
boolean batchMode, boolean processConstraints) {
if ((sds == null) || (sds.isEmpty())) {
throw new FHIRException("No StructuredDefinition found!");
}
ShexGeneratorTestUtils shexTestUtils = new ShexGeneratorTestUtils();
List<ShexGeneratorTestUtils.resDef> sdDefs = shexTestUtils.getSDs(sds, cat);
printList(cat.toString(), sdDefs);
System.out.println("************************************************************************");
System.out.println("Processing " + cat);
System.out.println("************************************************************************");
if (!batchMode) {
sdDefs.forEach((ShexGeneratorTestUtils.resDef resDef) -> {
String name = resDef.url;
if (resDef.url.indexOf("/") != -1) {
String els[] = resDef.url.split("/");
name = els[els.length - 1];
}
System.out.println("******************** " + resDef + " *********************");
doTestSingleSD(name, resDef.kind, resDef.url, useSelectedExtensions, policy, true, true, false, processConstraints);
});
} else {
doTestBatchSD(sds, useSelectedExtensions, policy, true, true, false, processConstraints);
}
System.out.println("************************ END PROCESSING ******************************");
System.out.println("************************************************************************");
List<String> skipped = this.shexGenerator.getExcludedStructureDefinitionUrls();
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, boolean processConstraints) {
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.processConstraints = processConstraints;
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, boolean processConstraints) {
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.processConstraints = processConstraints;
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);
}
}
}