Skip to content

Commit

Permalink
Merge pull request #198 from MeasureAuthoringTool/MAT-4844_FixingInco…
Browse files Browse the repository at this point in the history
…rrectDefinitionResults

Mat 4844 fixing incorrect definition results
  • Loading branch information
gregory-akins authored Oct 3, 2024
2 parents caf6dae + 5281cbc commit df9cd73
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@madie/cql-antlr-parser",
"version": "1.0.3",
"version": "1.0.4",
"description": "Antlr Parsing of CQL in typescript",
"publishConfig": {
"access": "public"
Expand Down
40 changes: 24 additions & 16 deletions src/AntlrUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,31 @@ export default class AntlrUtils {
return undefined;
}

static findChildTextByTypes(
children: ParseTree[] | undefined,
lexerType: number[],
occurrence = 1
): string | undefined {
let result: string | undefined = undefined;
lexerType.forEach((lexType) => {
const foundChild: string | undefined = this.findChildText(
children,
lexType,
occurrence
static findChildName(children: ParseTree[] | undefined): string | undefined {

if (children?.length != 4) {
console.error(
"########### Entering.. children length is ",
children?.length
);
if (foundChild) {
result = foundChild;
}
});
return result;
throw new Error("Definition might be malformed.");

}
return children ? children[1].text : undefined;
}

static findChildExpression(
children: ParseTree[] | undefined
): string | undefined {

if (children?.length != 4) {
console.error(
"########### Entering.. children length is ",
children?.length
)
throw new Error("Definition might be malformed.");
}
return children ? children[3].text : undefined;
}

static findChildText(
Expand Down
1 change: 1 addition & 0 deletions src/CqlAntlrListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default class CqlAntlrListener implements cqlListener {
enterExpressionDefinition(ctx: ExpressionDefinitionContext): void {
const cqlCode: CqlExpressionDefinition | undefined =
new CqlExpressionDefinitionCreator(ctx).buildDao();

if (cqlCode) {
this.cqlResult.expressionDefinitions.push(cqlCode);
}
Expand Down
8 changes: 2 additions & 6 deletions src/CqlExpressionDefinitionCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ export default class CqlExpressionDefinitionCreator extends CreatorBase<CqlExpre

protected build(): CqlExpressionDefinition {
CqlVersionCreator.setNameVersion(this.ctx.children, this.cqlDao);
this.cqlDao.name = this.findChildName();

this.cqlDao.name = this.findChildTextByTypes(
[cqlLexer.QUOTEDIDENTIFIER, cqlLexer.IDENTIFIER],
1
);

this.cqlDao.expression = this.findChildText(cqlLexer.IDENTIFIER, 1);
this.cqlDao.expression = this.findChildExpression();

if (!this.cqlDao.expression) {
const foundChild = AntlrUtils.findChild(
Expand Down
17 changes: 9 additions & 8 deletions src/CreatorBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ export default abstract class CreatorBase<T extends CqlText> {
return AntlrUtils.findChildText(children, cqlLexerId, occurrence);
}

protected findChildTextByTypes(
lexerIdArr: number[],
occurrence = 1,
protected findChildName(
children: ParseTree[] | undefined = this.ctx.children
): string | undefined {
const result: string | undefined = AntlrUtils.findChildName(children);
return result;
}

protected findChildExpression(
children: ParseTree[] | undefined = this.ctx.children
): string | undefined {
const result: string | undefined = AntlrUtils.findChildTextByTypes(
children,
lexerIdArr,
occurrence
);
const result: string | undefined = AntlrUtils.findChildExpression(children);
return result;
}

Expand Down
4 changes: 1 addition & 3 deletions test/CqlAntlr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ import CqlResult from "../src/dto/CqlResult";
describe("test antlr", () => {
it("parse simple Fhir CQL Definition", () => {
const cqlAntlr = new CqlAntlr(simpleDefinitionCql);

const cqlResult: CqlResult = cqlAntlr.parse();

expect(cqlResult.codes.length).toBe(0);
expect(cqlResult.valueSets.length).toBe(0);
expect(cqlResult.codeSystems.length).toBe(0);

expect(cqlResult.parameters.length).toBe(0);

expect(cqlResult.expressionDefinitions.length).toEqual(1);
expect(cqlResult.expressionDefinitions.length).toEqual(4);
cqlResult.expressionDefinitions.forEach((def) => {
expect(def.name).toBeDefined();
});
Expand Down
22 changes: 17 additions & 5 deletions test/testCql.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const simpleDefinitionCql = `library TJCOverall_FHIR4 version '4.0.000'
const simpleDefinitionCql = `
//MAT-4844: Test Define with no quotes
library ScreeningPrediabetesFHIR version '0.0.000'
using QICore version '4.1.1'
context Patient
define "SDE Ethnicity":
SDE."SDE Ethnicity"
//MAT-4844: Test Define with no quotes
define InitialPopulation:
true
define InitialPopulation:
"VTE Prophylaxis by Medication Administered or Device Applied"
define "Numerator":
"VTE Prophylaxis by Medication Administered or Device Applied"
define "VTE Prophylaxis by Medication Administered or Device Applied":
( ["MedicationAdministration": medication in "Low Dose Unfractionated Heparin for VTE Prophylaxis"] VTEMedication
where VTEMedication.status = 'completed'
)
`;

const fhirTestCql = `library TJCOverall_FHIR4 version '4.0.000'
Expand Down

0 comments on commit df9cd73

Please sign in to comment.