Java/Groovy Support of openEHR Operational Templates, Refernce Model, Data Generators and other tools for www.CaboLabs.com projects.
This will be used in CaboLabs apps like EHRGen, EHRServer, EMRApp and XML Rule Engine.
The build was tested with Gradle 6.4.1 installed from SDKMAN!.
$ cd openEHR-OPT
$ gradle build
Note: check the opt.sh/opt.bat files to see if the correct path to the groovy dependencies on your machine is set there.
That will run the tests and build the file ./build/libs/opt.jar
For running tests, there are many options, examples below:
- Run specific test case from a specific suite
- Run all tests from a specific suite
- Run all suites in a package
- Run all tests
$ cd openEHR-OPT
$ gradle test --tests com.cabolabs.openehr.opt.OPTParserTest.testCompleteOPT
$ gradle test --tests com.cabolabs.openehr.opt.OPTParserTest
$ gradle test --tests com.cabolabs.openehr.opt*
$ gradle test
The test report in HTML will be under ./build/reports/tests/test/index.html
$ ./opt.sh uigen path_to_opt dest_folder
$ ./opt.sh ingen path_to_opt dest_folder [amount] [version|composition|version_committer|tagged|json_version|json_composition|json_compo_with_errors] [withParticipations]
- amount: defines how many XML instances will be generated
- version: generates an instance of a VERSION object
- composition: generates an instance of a COMPOSITION object
- version_committer: generates an instance with the format required by the EHRCommitter to generate the UI and load data to test the EHRServer.
- tagged: generates a version instance with tags instead of data, useful to inject data from your app to commit to the EHRServer
- json_version: openEHR canonical JSON VERSION object
- json_composition: openEHR canonical JSON COMPOSITION object
- json_compo_with_errors: canonical JSON COMPOSITION object with violating data elements for cardinality constraints (purpose: data validation testing)
Validate one instance:
$ ./opt.sh inval path_to_xml_or_json_instance
Validate all instances in folder:
$ ./opt.sh inval path_to_folder_with_xml_or_json_instances
Note: if the folder contains JSON and XML, it will validate both with the correct schema, but the files should have .json or .xml extensions for the mixed validation to work OK.
In both cases, the output is "file IS VALID" or the list of validation errors if the file is not valid against the schemas.
$ ./opt.sh trans opt path_to_opt destination_folder
To transform a XML COMPOSITION to JSON:
$ ./opt.sh trans composition path_to_compo.xml destination_folder
To transform a JSON COMPOSITION to JSON:
$ ./opt.sh trans composition path_to_compo.json destination_folder
Note: the transformation of COMPOSITIONS between foramts relies on the file extension, only .xml or .json files are allowed.
OperationalTemplate loadAndParse(String path)
{
def parser = new OperationalTemplateParser()
def optFile = new File(getClass().getResource(path).toURI())
def text = optFile.getText()
return parser.parse(text)
}
To add the atttributes that are in the openEHR Reference Model but are not in the source OPT file, use the method complete():
def opt = loadAndParse("vital_signs.opt")
opt.complete()
String path = "vital_signs.json"
File file = new File(getClass().getResource(path).toURI())
String json = file.text
def parser = new OpenEhrJsonParser()
Composition c = (Composition)parser.parseJson(json)
Composition compo = ...
def serializer = new OpenEhrJsonSerializer()
String json = serializer.serialize(compo)
String path = "vital_signs.xml"
File file = new File(getClass().getResource(path).toURI())
String xml = file.text
def parser = new OpenEhrXmlParser()
Composition c = (Composition)parser.parseXml(xml)
Composition compo = ...
OpenEhrXmlSerializer marshal = new OpenEhrXmlSerializer()
String xml = marshal.serialize(compo)
// setup OPT repository
String opt_repo_path = "opts"
OptRepository repo = new OptRepositoryFSImpl(getClass().getResource(opt_repo_path).toURI())
OptManager opt_manager = OptManager.getInstance()
opt_manager.init(repo)
// load COMPOSITION
Composition compo = ...
// the validator automatically gets the tempalte from the repo based on the template_id in the COMPOSITION
RmValidator validator = new RmValidator(opt_manager)
RmValidationReport report = validator.dovalidate(compo, OptManager.DEFAULT_NAMESPACE)
report.errors.each { error ->
println error
}
String xml = ...
def parser = new OpenEhrXmlParser()
Composition c = (Composition)parser.parseXml(xml)
def serializer = new OpenEhrJsonSerializer()
String json = serializer.serialize(c)
String json ...
def parser = new OpenEhrJsonParser()
Composition c = (Composition)parser.parseJson(json)
def serializer = new OpenEhrXmlSerializer()
String xml = serializer.serialize(c)
def opt = loadAndParse('vital_signs.opt')
def igen = new JsonInstanceCanonicalGenerator2()
String json = igen.generateJSONVersionStringFromOPT(opt, true, true)
def opt = loadAndParse('vital_signs.opt')
def igen = new XmlInstanceGenerator()
String xml = igen.generateXMLCompositionStringFromOPT(opt, true)