Java源码示例:org.opencds.cqf.cql.execution.Context
示例1
/**
* ExpressionProcessor constructor.
* @param expression Expression to evaluate for each future set of parameters.
* @param paramTypeMap Map of parameter names to their corresponding CQL types.
*/
public ExpressionProcessor(String expression, Map<String,String> paramTypeMap) {
this.cqlParamMap = HashBiMap.create();
this.paramTypeMap = paramTypeMap;
String cleanExpression = replaceParameters(expression);
String wrappedExpression = convertParameterizedExpressionToCql(cleanExpression);
// Compile our constructed CQL expression into elm once for execution
this.elm = cqlToElm(wrappedExpression);
try {
this.library = CqlLibraryReader.read(new ByteArrayInputStream(
elm.getBytes(StandardCharsets.UTF_8)));
} catch (IOException | JAXBException ex) {
throw new RuntimeException(ex);
}
this.context = new Context(library);
this.expression = expression;
}
示例2
public void TestCMS9v4_CQM() throws IOException, JAXBException {
File xmlFile = new File(URLDecoder.decode(TestFhirLibrary.class.getResource("CMS9v4_CQM.xml").getFile(), "UTF-8"));
Library library = CqlLibraryReader.read(xmlFile);
Context context = new Context(library);
FhirContext fhirContext = FhirContext.forDstu3();
Dstu3FhirModelResolver modelResolver = new Dstu3FhirModelResolver();
RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext), fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu3"));
CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
//BaseFhirDataProvider provider = new FhirDataProviderStu3().setEndpoint("http://fhirtest.uhn.ca/baseDstu3");
//BaseFhirDataProvider provider = new FhirDataProviderStu3().setEndpoint("http://fhir3.healthintersections.com.au/open/");
//BaseFhirDataProvider provider = new FhirDataProviderStu3().setEndpoint("http://wildfhir.aegis.net/fhir");
context.registerDataProvider("http://hl7.org/fhir", provider);
Object result = context.resolveExpressionRef("Breastfeeding Intention Assessment").evaluate(context);
assertThat(result, instanceOf(Iterable.class));
for (Object element : (Iterable)result) {
assertThat(element, instanceOf(RiskAssessment.class));
}
}
示例3
@Override
protected Object internalEvaluate(Context context) {
if (this.getContext() != null) {
context.enterContext(this.getContext());
}
try {
if (context.isExpressionCachingEnabled() && context.isExpressionInCache(this.getName())) {
return context.getExpressionResultFromCache(this.getName());
}
Object result = this.getExpression().evaluate(context);
if (context.isExpressionCachingEnabled() && !context.isExpressionInCache(this.getName())) {
context.addExpressionToCache(this.getName(), result);
}
return result;
}
finally {
if (this.getContext() != null) {
context.exitContext();
}
}
}
示例4
@Override
protected Object internalEvaluate(Context context) {
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);
String precision = getPrecision() == null ? null : getPrecision().value();
if (left == null && right == null) {
return null;
}
// null left operand case
if (getOperand().get(0) instanceof AsEvaluator) {
if (((AsEvaluator) getOperand().get(0)).getAsTypeSpecifier() instanceof IntervalTypeSpecifier) {
return InEvaluator.in(right, left, precision);
}
else {
return InEvaluator.in(right, left, null);
}
}
return contains(left, right, precision);
}
示例5
public Object evaluateInContext(DomainResource instance, String cql, String patientId) {
Iterable<CanonicalType> libraries = getLibraryReferences(instance);
String source = String.format(
"library LocalLibrary using FHIR version '4.0.0' include FHIRHelpers version '4.0.0' called FHIRHelpers %s parameter %s %s parameter \"%%context\" %s define Expression: %s",
buildIncludes(libraries), instance.fhirType(), instance.fhirType(), instance.fhirType(), cql);
LibraryLoader libraryLoader = LibraryHelper.createLibraryLoader(this.getLibraryResourceProvider());
org.cqframework.cql.elm.execution.Library library = TranslatorHelper.translateLibrary(source,
libraryLoader.getLibraryManager(), libraryLoader.getModelManager());
// resolve execution context
Context context = setupContext(instance, patientId, libraryLoader, library);
return context.resolveExpressionRef("Expression").evaluate(context);
}
示例6
@Override
protected Object internalEvaluate(Context context) {
Object target = null;
if (this.getSource() != null) {
target = getSource().evaluate(context);
// Tuple element access
if (target instanceof Tuple) {
// NOTE: translator will throw error if Tuple does not contain the specified element -- no need for x.containsKey() check
return ((Tuple)target).getElements().get(this.getPath());
}
}
else if (this.getScope() != null) {
target = context.resolveVariable(this.getScope(), true).getValue();
}
if (target == null) {
return null;
}
if (target instanceof Iterable) {
}
return context.resolvePath(target, this.getPath());
}
示例7
@Override
protected Object internalEvaluate(Context context) {
Expression operand = getOperand();
// Special case to handle literals of the minimum Integer value
// since usual implementation would try to cast 2147483648 as a
// signed 32 bit signed integer and throw
// java.lang.NumberFormatException: For input string: "2147483648".
if (operand instanceof LiteralEvaluator && ((LiteralEvaluator)operand).getValue().equals("2147483648"))
{
return Integer.MIN_VALUE;
}
Object source = operand.evaluate(context);
return negate(source);
}
示例8
public MeasureReport evaluatePatientMeasure(Measure measure, Context context, String patientId) {
logger.info("Generating individual report");
if (patientId == null) {
return evaluatePopulationMeasure(measure, context);
}
Patient patient = registry.getResourceDao(Patient.class).read(new IdType(patientId));
// Iterable<Object> patientRetrieve = provider.retrieve("Patient", "id",
// patientId, "Patient", null, null, null, null, null, null, null, null);
// Patient patient = null;
// if (patientRetrieve.iterator().hasNext()) {
// patient = (Patient) patientRetrieve.iterator().next();
// }
return evaluate(measure, context,
patient == null ? Collections.emptyList() : Collections.singletonList(patient),
MeasureReport.MeasureReportType.INDIVIDUAL);
}
示例9
@Override
protected Object internalEvaluate(Context context) {
String type = this.getValueType().getLocalPart();
if (type == null) {
return null;
}
if (type.endsWith("Integer")) {
return Value.MIN_INT;
}
if (type.endsWith("Decimal")) {
return Value.MIN_DECIMAL;
}
if (type.endsWith("Date")) {
return new Date(1, 1, 1).setPrecision(Precision.DAY);
}
if (type.endsWith("DateTime")) {
return new DateTime(TemporalHelper.zoneToOffset(context.getEvaluationDateTime().getDateTime().getOffset()), 1, 1, 1, 0, 0, 0, 0).withEvaluationOffset(context.getEvaluationDateTime().getDateTime().getOffset());
}
if (type.endsWith("Time")) {
return new Time(TemporalHelper.zoneToOffset(context.getEvaluationDateTime().getDateTime().getOffset()), 0, 0, 0, 0).withEvaluationOffset(context.getEvaluationDateTime().getDateTime().getOffset());
}
throw new InvalidOperatorArgument(String.format("The Minimum operator is not implemented for type %s", type));
}
示例10
@Test
public void testFhirHelpersStu3() throws UcumException {
String cql = getStringFromResourceStream("stu3/TestFHIRHelpers.cql");
Library library = translate(cql);
Context context = new Context(library);
context.registerLibraryLoader(getLibraryLoader());
Dstu3FhirModelResolver modelResolver = new Dstu3FhirModelResolver();
FhirContext fhirContext = FhirContext.forDstu3();
RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(new SearchParameterResolver(fhirContext),
fhirContext.newRestfulGenericClient("http://fhirtest.uhn.ca/baseDstu3"));
CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
// BaseFhirDataProvider provider = new
// FhirDataProviderStu3().setEndpoint("http://fhirtest.uhn.ca/baseDstu3");
context.registerDataProvider("http://hl7.org/fhir", provider);
Object result = context.resolveExpressionRef("TestPeriodToInterval").getExpression().evaluate(context);
// TODO - fix
// Assert.assertEquals(((DateTime)((Interval) result).getStart()).getPartial(),
// new Partial(DateTime.getFields(6), new int[] {2017, 5, 6, 18, 8, 0}));
// Assert.assertEquals(((DateTime)((Interval) result).getEnd()).getPartial(),
// new Partial(DateTime.getFields(6), new int[] {2017, 5, 6, 19, 8, 0}));
result = context.resolveExpressionRef("TestToQuantity").getExpression().evaluate(context);
// TODO: ModelInfo bug. Not aware of SimpleQuantity
result = context.resolveExpressionRef("TestRangeToInterval").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToCode").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToConcept").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToString").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestRequestStatusToString").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToDateTime").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToTime").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToInteger").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToDecimal").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToBoolean").getExpression().evaluate(context);
}
示例11
public void testFhirHelpersDstu2() throws UcumException {
String cql = getStringFromResourceStream("Dstu2/TestFHIRHelpersDstu2.cql");
Library library = translate(cql);
Context context = new Context(library);
context.registerLibraryLoader(getLibraryLoader());
Dstu2FhirModelResolver modelResolver = new Dstu2FhirModelResolver();
RestFhirRetrieveProvider retrieveProvider = new RestFhirRetrieveProvider(
new SearchParameterResolver(fhirContext), FhirContext.forDstu2().newRestfulGenericClient(""));
CompositeDataProvider provider = new CompositeDataProvider(modelResolver, retrieveProvider);
//BaseFhirDataProvider provider = new FhirDataProviderDstu2();
context.registerDataProvider("http://hl7.org/fhir", provider);
Object result = context.resolveExpressionRef("TestPeriodToInterval").getExpression().evaluate(context);
// TODO - millis shouldn't be populated - issue with DateTime.fromJavaDate(Date date)
// Assert.assertEquals(((DateTime)((Interval) result).getStart()).getPartial(), new Partial(DateTime.getFields(7), new int[] {2017, 5, 6, 18, 8, 0, 0}));
// Assert.assertEquals(((DateTime)((Interval) result).getEnd()).getPartial(), new Partial(DateTime.getFields(7), new int[] {2017, 5, 6, 19, 8, 0, 0}));
result = context.resolveExpressionRef("TestToQuantity").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestRangeToInterval").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToCode").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToConcept").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToString").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestRequestStatusToString").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToDateTime").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToTime").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToInteger").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToDecimal").getExpression().evaluate(context);
result = context.resolveExpressionRef("TestToBoolean").getExpression().evaluate(context);
}
示例12
public void testDateType() {
Context context = new Context(library);
context.registerDataProvider("http://hl7.org/fhir", dstu3Provider);
context.setContextValue("Patient", "Patient-12214");
Object result = context.resolveExpressionRef("testDateType").getExpression().evaluate(context);
Assert.assertTrue(result != null);
}
示例13
@Test
public void testFhirObjectEqual()
{
Context context = new Context(library);
context.registerDataProvider("http://hl7.org/fhir", dstu3Provider);
Object result = context.resolveExpressionRef("testFhirObjectEqual").getExpression().evaluate(context);
Assert.assertTrue((Boolean) result);
}
示例14
@Override
protected Object internalEvaluate(Context context) {
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);
return lessOrEqual(left, right);
}
示例15
@BeforeMethod
public void before() {
context = new Context(library);
context.registerDataProvider("http://hl7.org/fhir", dstu2Provider);
//BaseFhirDataProvider provider = new FhirDataProviderDstu2().setEndpoint("http://fhirtest.uhn.ca/baseDstu2");
// FhirDataProviderDstu2 primitiveProvider = new FhirDataProviderDstu2().withEndpoint("http://fhirtest.uhn.ca/baseDstu2").withPackageName("ca.uhn.fhir.model.primitive");
// context.registerDataProvider("http://hl7.org/fhir", primitiveProvider);
// FhirDataProviderDstu2 compositeProvider = new FhirDataProviderDstu2().withEndpoint("http://fhirtest.uhn.ca/baseDstu2").withPackageName("ca.uhn.fhir.model.dstu2.composite");
// context.registerDataProvider("http://hl7.org/fhir", compositeProvider);
}
示例16
@Override
protected Object internalEvaluate(Context context) {
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);
String precision = getPrecision() != null ? getPrecision().value() : null;
if (precision != null) {
return properIn(left, right, precision);
}
return properIn(left, right);
}
示例17
private void populateResourceMap(
Context context, MeasurePopulationType type, HashMap<String, Resource> resources,
HashMap<String,HashSet<String>> codeToResourceMap)
{
if (context.getEvaluatedResources().isEmpty()) {
return;
}
if (!codeToResourceMap.containsKey(type.toCode())) {
codeToResourceMap.put(type.toCode(), new HashSet<>());
}
HashSet<String> codeHashSet = codeToResourceMap.get((type.toCode()));
for (Object o : context.getEvaluatedResources()) {
if (o instanceof Resource){
Resource r = (Resource)o;
String id = (r.getIdElement().getResourceType() != null ? (r.getIdElement().getResourceType() + "/") : "") + r.getIdElement().getIdPart();
if (!codeHashSet.contains(id)) {
codeHashSet.add(id);
}
if (!resources.containsKey(id)) {
resources.put(id, r);
}
}
}
context.clearEvaluatedResources();
}
示例18
@Override
protected Object internalEvaluate(Context context) {
List<Object> operands = new ArrayList<>();
for (Expression operand : getOperand()) {
operands.add(operand.evaluate(context));
}
return coalesce(operands);
}
示例19
@Override
protected Object internalEvaluate(Context context) {
ArrayList<Code> codes = new ArrayList<>();
for (int i = 0; i < this.getCode().size(); ++i) {
codes.add((Code)this.getCode().get(i).evaluate(context));
}
String display = this.getDisplay();
return new org.opencds.cqf.cql.runtime.Concept().withCodes(codes).withDisplay(display);
}
示例20
@Override
protected Object internalEvaluate(Context context) {
Object pattern = getPattern().evaluate(context);
Object string = getString().evaluate(context);
return positionOf(pattern, string);
}
示例21
public CarePlan resolveCdsHooksPlanDefinition(Context context, PlanDefinition planDefinition, String patientId) {
CarePlanBuilder carePlanBuilder = new CarePlanBuilder();
RequestGroupBuilder requestGroupBuilder = new RequestGroupBuilder().buildStatus().buildIntent();
// links
if (planDefinition.hasRelatedArtifact()) {
List<Extension> extensions = new ArrayList<>();
for (RelatedArtifact relatedArtifact : planDefinition.getRelatedArtifact()) {
AttachmentBuilder attachmentBuilder = new AttachmentBuilder();
ExtensionBuilder extensionBuilder = new ExtensionBuilder();
if (relatedArtifact.hasDisplay()) { // label
attachmentBuilder.buildTitle(relatedArtifact.getDisplay());
}
if (relatedArtifact.hasUrl()) { // url
attachmentBuilder.buildUrl(relatedArtifact.getUrl());
}
if (relatedArtifact.hasExtension()) { // type
attachmentBuilder.buildExtension(relatedArtifact.getExtension());
}
extensionBuilder.buildUrl("http://example.org");
extensionBuilder.buildValue(attachmentBuilder.build());
extensions.add(extensionBuilder.build());
}
requestGroupBuilder.buildExtension(extensions);
}
resolveActions(planDefinition.getAction(), context, patientId, requestGroupBuilder, new ArrayList<>());
CarePlanActivityBuilder carePlanActivityBuilder = new CarePlanActivityBuilder();
carePlanActivityBuilder.buildReferenceTarget(requestGroupBuilder.build());
carePlanBuilder.buildActivity(carePlanActivityBuilder.build());
return carePlanBuilder.build();
}
示例22
@Override
protected Object internalEvaluate(Context context) {
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);
return indexer(left, right);
}
示例23
public Object evaluate(Context context) throws CqlException
{
try {
return internalEvaluate(context);
}
catch (Exception e) {
if (e instanceof CqlException) {
throw e;
}
else {
throw new CqlException(e);
}
}
}
示例24
@Override
protected Object internalEvaluate(Context context) {
Object source = getSource().evaluate(context);
Object elementToFind = getElement().evaluate(context);
return indexOf(source, elementToFind);
}
示例25
@Override
protected Object internalEvaluate(Context context)
{
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);
return intersect(left, right);
}
示例26
@Override
protected Object internalEvaluate(Context context) {
ArrayList<Object> arguments = new ArrayList<>();
for (Expression operand : this.getOperand()) {
arguments.add(operand.evaluate(context));
}
boolean enteredLibrary = context.enterLibrary(this.getLibraryName());
try {
// TODO: Use type specifiers from the operands here if they are available
FunctionDef functionDef = context.resolveFunctionRef(this.getName(), arguments, this.getLibraryName());
if (Optional.ofNullable(functionDef.isExternal()).orElse(false)) {
return context.getExternalFunctionProvider().evaluate(functionDef.getName(), arguments);
}
else {
context.pushWindow();
try {
for (int i = 0; i < arguments.size(); i++) {
context.push(new Variable().withName(functionDef.getOperand().get(i).getName()).withValue(arguments.get(i)));
}
return functionDef.getExpression().evaluate(context);
}
finally {
context.popWindow();
}
}
}
finally {
context.exitLibrary(enteredLibrary);
}
}
示例27
@Override
protected Object internalEvaluate(Context context) {
Object source = this.getSource().evaluate(context);
String separator = this.getSeparator() == null ? "" : (String) this.getSeparator().evaluate(context);
return combine(source, separator);
}
示例28
@Override
protected Object internalEvaluate(Context context) {
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);
String precision = getPrecision() == null ? null : getPrecision().value();
return meetsBefore(left, right, precision);
}
示例29
@Override
protected Object internalEvaluate(Context context) {
Object low = getLow() != null ? getLow().evaluate(context) : null;
Boolean lowClosed = getLowClosedExpression() != null ? (Boolean)getLowClosedExpression().evaluate(context) : this.lowClosed;
Object high = getHigh() != null ? getHigh().evaluate(context) : null;
Boolean highClosed = getHighClosedExpression() != null ? (Boolean)getHighClosedExpression().evaluate(context) : this.highClosed;
// An interval with no boundaries is not an interval
// TODO: the spec states that it is possible to have an interval with null boundaries, but the ELM is not providing a way to get the Interval type
if (low == null && high == null) {
return null;
}
return new org.opencds.cqf.cql.runtime.Interval(low, lowClosed == null ? true : lowClosed, high, highClosed == null ? true : highClosed);
}
示例30
@Override
protected Object internalEvaluate(Context context) {
Object left = getOperand().get(0).evaluate(context);
Object right = getOperand().get(1).evaluate(context);
return equal(left, right);
}