Skip to content

Commit

Permalink
Add Debug logging for expressions (cqframework#1341)
Browse files Browse the repository at this point in the history
* Initial logging fix

* Add an initial test for DebugMap

* push some gated logic down
  • Loading branch information
JPercival authored Mar 15, 2024
1 parent 1353100 commit 4586863
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public DebugResult() {
}

public void logDebugResult(Element node, Library currentLibrary, Object result, DebugAction action) {
if (action == DebugAction.NONE) {
return;
}

try {
DebugLibraryResultEntry libraryResultEntry =
libraryResults.get(currentLibrary.getIdentifier().getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,15 @@ public EvaluationResult evaluate(
}

private void initializeState(Library library, DebugMap debugMap, ZonedDateTime evaluationDateTime) {
if (evaluationDateTime != null) {
this.state.setEvaluationDateTime(evaluationDateTime);
} else {
this.state.setEvaluationDateTime(ZonedDateTime.now());
if (evaluationDateTime == null) {
evaluationDateTime = ZonedDateTime.now();
}

this.state.setEvaluationDateTime(evaluationDateTime);
this.state.init(library);
this.state.setDebugMap(debugMap);
if (debugMap != null) {
this.state.setDebugMap(debugMap);
}
}

private EvaluationResult evaluateExpressions(Set<String> expressions) {
Expand All @@ -237,11 +238,14 @@ private EvaluationResult evaluateExpressions(Set<String> expressions) {
continue;
}

// TODO: How do we want to return handle errors?
try {
var action = getState().shouldDebug(def);

Object object = this.evaluationVisitor.visitExpressionDef(def, this.state);
result.expressionResults.put(
expression, new ExpressionResult(object, this.state.getEvaluatedResources()));

getState().logDebugResult(def, object, action);
} catch (CqlException ce) {
processException(ce, def);
} catch (Exception e) {
Expand Down Expand Up @@ -367,7 +371,7 @@ private Set<String> getExpressionSet(Library library) {

public void processException(CqlException e, Element element) {
if (e.getSourceLocator() == null) {
e.setSourceLocator(SourceLocator.fromNode(element, null));
e.setSourceLocator(SourceLocator.fromNode(element, this.getState().getCurrentLibrary()));
DebugAction action = state.shouldDebug(e);
if (action != DebugAction.NONE) {
state.logDebugError(e);
Expand All @@ -378,7 +382,8 @@ public void processException(CqlException e, Element element) {
}

public void processException(Exception e, Element element, String message) {
CqlException ce = new CqlException(message, e, SourceLocator.fromNode(element, null));
CqlException ce = new CqlException(
message, e, SourceLocator.fromNode(element, this.getState().getCurrentLibrary()));
DebugAction action = state.shouldDebug(ce);
if (action != DebugAction.NONE) {
state.logDebugError(ce);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.opencds.cqf.cql.engine.execution;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.opencds.cqf.cql.engine.debug.DebugMap;
import org.testng.annotations.Test;

class CqlEngineTest extends CqlTestBase {

@Test
void testDebugMap() {

// The specific library isn't important, just that it has a debug map
var debugMap = new DebugMap();
debugMap.setIsLoggingEnabled(true);
var results = engine.evaluate(toElmIdentifier("CqlEngineTest"), null, null, null, debugMap);

var libraryDebug = results.getDebugResult()
.getLibraryResults()
.get("CqlEngineTest")
.getResults();

assertNotNull(libraryDebug);

// Find the debug result for the AnInteger expression
// It's indexed by location
var result = libraryDebug.keySet().stream()
.filter(e -> e.getLocator().equals("6:1-6:21"))
.findFirst();

assertTrue(result.isPresent());

var debugResult = libraryDebug.get(result.get());
assertEquals(1, debugResult.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
library CqlEngineTest

// This is mostly a dummy library to doing some testing on top-level CQL engine Java APIS.
// It's not meant to test innner execution of the engine
define "AnInteger": 1

0 comments on commit 4586863

Please sign in to comment.