org.cqframework.cql.elm.execution.ExpressionDef Java Examples

The following examples show how to use org.cqframework.cql.elm.execution.ExpressionDef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CqlEngine.java    From cql_engine with Apache License 2.0 6 votes vote down vote up
private LibraryResult evaluateLibrary(Context context, Library library, Set<String> expressions) {
    LibraryResult result = new LibraryResult();

    for (String expression : expressions) {
        ExpressionDef def = context.resolveExpressionRef(expression);

        // TODO: We should probably move this validation further up the chain.
        // For example, we should tell the user that they've tried to evaluate a function def through incorrect
        // CQL or input parameters. And the code that gather the list of expressions to evaluate together should
        // not include function refs.
        if (def instanceof FunctionDef) {
            continue;
        }
        
        context.enterContext(def.getContext());
        Object object = def.evaluate(context);
        result.expressionResults.put(expression, object);
    }

    return result;
}
 
Example #2
Source File: CqlTestSuite.java    From cql_engine with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorSuite() throws IOException, JAXBException, UcumException {
    Library library = translate("portable/CqlErrorTestSuite.cql");
    Context context = new Context(library, new DateTime(TemporalHelper.getDefaultOffset(), 2018, 1, 1, 7, 0, 0, 0));
    if (library.getStatements() != null) {
        for (ExpressionDef expression : library.getStatements().getDef()) {
            try {
                expression.evaluate(context);
                logger.error("Test " + expression.getName() + " should result in an error");
                Assert.fail();
            }
            catch (Exception e) {
                // pass
                logger.info(expression.getName() + " TEST PASSED");
            }
        }
    }
}
 
Example #3
Source File: ExpressionProcessor.java    From synthea with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates the expression with the given parameters.
 * @param params parameters as a map of variable names to values
 * @return evaluation result
 */
public Object evaluate(Map<String,Object> params) {
  // Keep track to make sure all parameters are set
  Set<String> setParams = new HashSet<String>();
  for (Entry<String,Object> entry : params.entrySet()) {
    // Set the CQL compatible parameter name in the context
    context.setParameter(null, cqlParamMap.get(entry.getKey()), entry.getValue());
    setParams.add(entry.getKey());
  }
  
  Set<String> missing = Sets.difference(cqlParamMap.keySet(), setParams);
  Set<String> extra = Sets.difference(setParams, cqlParamMap.keySet());
  
  if (missing.size() > 0) {
    throw new IllegalArgumentException("Missing parameter(s): " + String.join(", ", missing)
    + " for expression \"" + expression + "\"");
  }
  if (extra.size() > 0) {
    Logger.getLogger(ExpressionProcessor.class.getName()).log(Level.WARNING,
            "unused parameter(s) provided for expression \"{0}\": {1}",
            new Object[]{expression, String.join(", ",extra)});
  }
  
  Object retVal = null;

  for (ExpressionDef statement : library.getStatements().getDef()) {
    retVal = statement.evaluate(context);
  }

  try {
    return retVal;
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #4
Source File: CqlEngine.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
private Map<VersionedIdentifier, Set<String>> getExpressionMap(Library library) {
    Map<VersionedIdentifier, Set<String>> map = new HashMap<>();
    Set<String> expressionNames = new HashSet<>();
    if (library.getStatements() != null && library.getStatements().getDef() != null) {
        for (ExpressionDef ed : library.getStatements().getDef()) {
            expressionNames.add(ed.getName());
        }
    }

    map.put(library.getIdentifier(), expressionNames);

    return map;
}
 
Example #5
Source File: CqlTestSuite.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
@Test
public void testMainSuite() throws IOException, JAXBException, UcumException {
    Library library = translate("portable/CqlTestSuite.cql");
    Context context = new Context(library, new DateTime(TemporalHelper.getDefaultOffset(), 2018, 1, 1, 7, 0, 0, 0));
    if (library.getStatements() != null) {
        for (ExpressionDef expression : library.getStatements().getDef()) {
            if (expression instanceof FunctionDef) {
                continue;
            }
            if (expression.getName().startsWith("test")) {
                logger.info((String) expression.evaluate(context));
            }
        }
    }
}