Search in sources :

Example 1 with ValueSetInfo

use of org.opencds.cqf.cql.engine.terminology.ValueSetInfo in project cqf-ruler by DBCG.

the class PrefetchDataProviderR4 method retrieve.

@Override
public Iterable<Object> retrieve(String context, String contextPath, Object contextValue, String dataType, String templateId, String codePath, Iterable<Code> codes, String valueSet, String datePath, String dateLowPath, String dateHighPath, Interval dateRange) {
    if (codePath == null && (codes != null || valueSet != null)) {
        throw new IllegalArgumentException("A code path must be provided when filtering on codes or a valueset.");
    }
    if (dataType == null) {
        throw new IllegalArgumentException("A data type (i.e. Procedure, Valueset, etc...) must be specified for clinical data retrieval");
    }
    // not be in the pre-fetch bundle, or might required a lookup by Id
    if (context.equals("Patient") && contextPath == null) {
        return Collections.emptyList();
    }
    List<Object> resourcesOfType = prefetchResources.get(dataType);
    if (resourcesOfType == null) {
        return Collections.emptyList();
    }
    // no resources or no filtering -> return list
    if (resourcesOfType.isEmpty() || (dateRange == null && codePath == null)) {
        return resourcesOfType;
    }
    List<Object> returnList = new ArrayList<>();
    for (Object resource : resourcesOfType) {
        boolean includeResource = true;
        if (dateRange != null) {
            if (datePath != null) {
                if (dateHighPath != null || dateLowPath != null) {
                    throw new IllegalArgumentException("If the datePath is specified, the dateLowPath and dateHighPath attributes must not be present.");
                }
                Object dateObject = PrefetchDataProviderHelper.getR4DateTime(this.resolver.resolvePath(resource, datePath));
                DateTime date = dateObject instanceof DateTime ? (DateTime) dateObject : null;
                Interval dateInterval = dateObject instanceof Interval ? (Interval) dateObject : null;
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, date));
                if (date != null && !(InEvaluator.in(date, dateRange, precision))) {
                    includeResource = false;
                } else // TODO - add precision to includes evaluator
                if (dateInterval != null && !(IncludesEvaluator.includes(dateRange, dateInterval, precision))) {
                    includeResource = false;
                }
            } else {
                if (dateHighPath == null && dateLowPath == null) {
                    throw new IllegalArgumentException("If the datePath is not given, either the lowDatePath or highDatePath must be provided.");
                }
                DateTime lowDate = dateLowPath == null ? null : (DateTime) PrefetchDataProviderHelper.getR4DateTime(this.resolver.resolvePath(resource, dateLowPath));
                DateTime highDate = dateHighPath == null ? null : (DateTime) PrefetchDataProviderHelper.getR4DateTime(this.resolver.resolvePath(resource, dateHighPath));
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, lowDate, highDate));
                Interval interval = new Interval(lowDate, true, highDate, true);
                // TODO - add precision to includes evaluator
                if (!IncludesEvaluator.includes(dateRange, interval, precision)) {
                    includeResource = false;
                }
            }
        }
        if (codePath != null && !codePath.equals("") && includeResource) {
            if (valueSet != null && terminologyProvider != null) {
                if (valueSet.startsWith("urn:oid:")) {
                    valueSet = valueSet.replace("urn:oid:", "");
                }
                ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet);
                codes = terminologyProvider.expand(valueSetInfo);
            }
            if (codes != null) {
                Object codeObject = PrefetchDataProviderHelper.getR4Code(this.resolver.resolvePath(resource, codePath));
                includeResource = PrefetchDataProviderHelper.checkCodeMembership(codes, codeObject, this.codeUtil);
            }
        }
        if (includeResource) {
            returnList.add(resource);
        }
    }
    return returnList;
}
Also used : ArrayList(java.util.ArrayList) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) Interval(org.opencds.cqf.cql.engine.runtime.Interval)

Example 2 with ValueSetInfo

use of org.opencds.cqf.cql.engine.terminology.ValueSetInfo in project cqf-ruler by DBCG.

the class PrefetchDataProviderStu3 method retrieve.

@Override
public Iterable<Object> retrieve(String context, String contextPath, Object contextValue, String dataType, String templateId, String codePath, Iterable<Code> codes, String valueSet, String datePath, String dateLowPath, String dateHighPath, Interval dateRange) {
    if (codePath == null && (codes != null || valueSet != null)) {
        throw new IllegalArgumentException("A code path must be provided when filtering on codes or a valueset.");
    }
    if (dataType == null) {
        throw new IllegalArgumentException("A data type (i.e. Procedure, Valueset, etc...) must be specified for clinical data retrieval");
    }
    // not be in the pre-fetch bundle, or might required a lookup by Id
    if (context.equals("Patient") && contextPath == null) {
        return Collections.emptyList();
    }
    List<Object> resourcesOfType = prefetchResources.get(dataType);
    if (resourcesOfType == null) {
        return Collections.emptyList();
    }
    // no resources or no filtering -> return list
    if (resourcesOfType.isEmpty() || (dateRange == null && codePath == null)) {
        return resourcesOfType;
    }
    List<Object> returnList = new ArrayList<>();
    for (Object resource : resourcesOfType) {
        boolean includeResource = true;
        if (dateRange != null) {
            if (datePath != null) {
                if (dateHighPath != null || dateLowPath != null) {
                    throw new IllegalArgumentException("If the datePath is specified, the dateLowPath and dateHighPath attributes must not be present.");
                }
                Object dateObject = PrefetchDataProviderHelper.getStu3DateTime(this.resolver.resolvePath(resource, datePath));
                DateTime date = dateObject instanceof DateTime ? (DateTime) dateObject : null;
                Interval dateInterval = dateObject instanceof Interval ? (Interval) dateObject : null;
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, date));
                if (date != null && !(InEvaluator.in(date, dateRange, precision))) {
                    includeResource = false;
                } else // TODO - add precision to includes evaluator
                if (dateInterval != null && !(IncludesEvaluator.includes(dateRange, dateInterval, precision))) {
                    includeResource = false;
                }
            } else {
                if (dateHighPath == null && dateLowPath == null) {
                    throw new IllegalArgumentException("If the datePath is not given, either the lowDatePath or highDatePath must be provided.");
                }
                DateTime lowDate = dateLowPath == null ? null : (DateTime) PrefetchDataProviderHelper.getStu3DateTime(this.resolver.resolvePath(resource, dateLowPath));
                DateTime highDate = dateHighPath == null ? null : (DateTime) PrefetchDataProviderHelper.getStu3DateTime(this.resolver.resolvePath(resource, dateHighPath));
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, lowDate, highDate));
                Interval interval = new Interval(lowDate, true, highDate, true);
                // TODO - add precision to includes evaluator
                if (!IncludesEvaluator.includes(dateRange, interval, precision)) {
                    includeResource = false;
                }
            }
        }
        if (codePath != null && !codePath.equals("") && includeResource) {
            if (valueSet != null && terminologyProvider != null) {
                if (valueSet.startsWith("urn:oid:")) {
                    valueSet = valueSet.replace("urn:oid:", "");
                }
                ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet);
                codes = terminologyProvider.expand(valueSetInfo);
            }
            if (codes != null) {
                Object codeObject = PrefetchDataProviderHelper.getStu3Code(this.resolver.resolvePath(resource, codePath));
                includeResource = PrefetchDataProviderHelper.checkCodeMembership(codes, codeObject, this.codeUtil);
            }
        }
        if (includeResource) {
            returnList.add(resource);
        }
    }
    return returnList;
}
Also used : ArrayList(java.util.ArrayList) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) Interval(org.opencds.cqf.cql.engine.runtime.Interval)

Example 3 with ValueSetInfo

use of org.opencds.cqf.cql.engine.terminology.ValueSetInfo in project cqf-ruler by DBCG.

the class PrefetchDataProviderDstu2 method retrieve.

@Override
public Iterable<Object> retrieve(String context, String contextPath, Object contextValue, String dataType, String templateId, String codePath, Iterable<Code> codes, String valueSet, String datePath, String dateLowPath, String dateHighPath, Interval dateRange) {
    if (codePath == null && (codes != null || valueSet != null)) {
        throw new IllegalArgumentException("A code path must be provided when filtering on codes or a valueset.");
    }
    if (dataType == null) {
        throw new IllegalArgumentException("A data type (i.e. Procedure, Valueset, etc...) must be specified for clinical data retrieval");
    }
    // not be in the pre-fetch bundle, or might required a lookup by Id
    if (context.equals("Patient") && contextPath == null) {
        return Collections.emptyList();
    }
    List<Object> resourcesOfType = prefetchResources.get(dataType);
    if (resourcesOfType == null) {
        return Collections.emptyList();
    }
    // no resources or no filtering -> return list
    if (resourcesOfType.isEmpty() || (dateRange == null && codePath == null)) {
        return resourcesOfType;
    }
    List<Object> returnList = new ArrayList<>();
    for (Object resource : resourcesOfType) {
        boolean includeResource = true;
        if (dateRange != null) {
            if (datePath != null) {
                if (dateHighPath != null || dateLowPath != null) {
                    throw new IllegalArgumentException("If the datePath is specified, the dateLowPath and dateHighPath attributes must not be present.");
                }
                Object dateObject = PrefetchDataProviderHelper.getDstu2DateTime(this.resolver.resolvePath(resource, datePath));
                DateTime date = dateObject instanceof DateTime ? (DateTime) dateObject : null;
                Interval dateInterval = dateObject instanceof Interval ? (Interval) dateObject : null;
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, date));
                if (date != null && !(InEvaluator.in(date, dateRange, precision))) {
                    includeResource = false;
                } else // TODO - add precision to includes evaluator
                if (dateInterval != null && !(IncludesEvaluator.includes(dateRange, dateInterval, precision))) {
                    includeResource = false;
                }
            } else {
                if (dateHighPath == null && dateLowPath == null) {
                    throw new IllegalArgumentException("If the datePath is not given, either the lowDatePath or highDatePath must be provided.");
                }
                DateTime lowDate = dateLowPath == null ? null : (DateTime) PrefetchDataProviderHelper.getDstu2DateTime(this.resolver.resolvePath(resource, dateLowPath));
                DateTime highDate = dateHighPath == null ? null : (DateTime) PrefetchDataProviderHelper.getDstu2DateTime(this.resolver.resolvePath(resource, dateHighPath));
                String precision = PrefetchDataProviderHelper.getPrecision(Arrays.asList(dateRange, lowDate, highDate));
                Interval interval = new Interval(lowDate, true, highDate, true);
                // TODO - add precision to includes evaluator
                if (!IncludesEvaluator.includes(dateRange, interval, precision)) {
                    includeResource = false;
                }
            }
        }
        if (codePath != null && !codePath.equals("") && includeResource) {
            if (valueSet != null && terminologyProvider != null) {
                if (valueSet.startsWith("urn:oid:")) {
                    valueSet = valueSet.replace("urn:oid:", "");
                }
                ValueSetInfo valueSetInfo = new ValueSetInfo().withId(valueSet);
                codes = terminologyProvider.expand(valueSetInfo);
            }
            if (codes != null) {
                Object codeObject = PrefetchDataProviderHelper.getDstu2Code(this.resolver.resolvePath(resource, codePath));
                includeResource = PrefetchDataProviderHelper.checkCodeMembership(codes, codeObject, this.codeUtil);
            }
        }
        if (includeResource) {
            returnList.add(resource);
        }
    }
    return returnList;
}
Also used : ArrayList(java.util.ArrayList) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) Interval(org.opencds.cqf.cql.engine.runtime.Interval)

Example 4 with ValueSetInfo

use of org.opencds.cqf.cql.engine.terminology.ValueSetInfo in project quality-measure-and-cohort-service by Alvearie.

the class CQLParameterTests method runTest.

public void runTest(String type, Object expectedOutput, String defaultValue) throws Exception {
    String cql = "library \"Test\" version '1.0.0'\n";
    cql += "codesystem \"DUMMY\": 'urn:mysystem'\n";
    cql += "parameter Input " + type;
    if (defaultValue != null) {
        cql += " default ";
        cql += defaultValue;
    }
    cql += "\n";
    cql += "define Output:\n\tInput\n";
    Map<String, Object> parameters = null;
    if (defaultValue == null) {
        parameters = new HashMap<>();
        parameters.put("Input", expectedOutput);
    }
    TerminologyProvider tp = new TerminologyProvider() {

        @Override
        public boolean in(Code code, ValueSetInfo valueSet) {
            return false;
        }

        @Override
        public Iterable<Code> expand(ValueSetInfo valueSet) {
            return null;
        }

        @Override
        public Code lookup(Code code, CodeSystemInfo codeSystem) {
            return code;
        }
    };
    LibraryLoader ll = new InMemoryLibraryLoader(Arrays.asList(toLibrary(cql)));
    CqlEngine engine = new CqlEngine(ll, Collections.<String, DataProvider>emptyMap(), tp);
    EvaluationResult result = engine.evaluate("Test", parameters);
    assertEquals(1, result.expressionResults.size());
    Object actual = result.forExpression("Output");
    assertEquals(expectedOutput.toString(), actual.toString());
}
Also used : CodeSystemInfo(org.opencds.cqf.cql.engine.terminology.CodeSystemInfo) TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) CqlEngine(org.opencds.cqf.cql.engine.execution.CqlEngine) Code(org.opencds.cqf.cql.engine.runtime.Code) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) InMemoryLibraryLoader(org.opencds.cqf.cql.engine.execution.InMemoryLibraryLoader) InMemoryLibraryLoader(org.opencds.cqf.cql.engine.execution.InMemoryLibraryLoader) EvaluationResult(org.opencds.cqf.cql.engine.execution.EvaluationResult)

Example 5 with ValueSetInfo

use of org.opencds.cqf.cql.engine.terminology.ValueSetInfo in project quality-measure-and-cohort-service by Alvearie.

the class R4RestFhirTerminologyProviderTest method inOperationReturnsFalseWhenFhirReturnsFalse.

@Test
public void inOperationReturnsFalseWhenFhirReturnsFalse() throws Exception {
    ValueSetInfo info = new ValueSetInfo();
    info.setId("urn:oid:Test");
    Code code = new Code();
    code.setSystem(TEST_SYSTEM);
    code.setCode(TEST_CODE);
    code.setDisplay(TEST_DISPLAY);
    Parameters parameters = new Parameters();
    parameters.getParameterFirstRep().setName("result").setValue(new BooleanType(false));
    mockFhirResourceRetrieval("/ValueSet/Test/$validate-code?code=" + urlencode(code.getCode()) + "&system=" + urlencode(code.getSystem()) + "&_format=json", parameters);
    boolean result = provider.in(code, info);
    assertFalse(result);
}
Also used : Parameters(org.hl7.fhir.r4.model.Parameters) BooleanType(org.hl7.fhir.r4.model.BooleanType) ValueSetInfo(org.opencds.cqf.cql.engine.terminology.ValueSetInfo) Code(org.opencds.cqf.cql.engine.runtime.Code) Test(org.junit.Test)

Aggregations

ValueSetInfo (org.opencds.cqf.cql.engine.terminology.ValueSetInfo)25 Test (org.junit.Test)20 Code (org.opencds.cqf.cql.engine.runtime.Code)17 ArrayList (java.util.ArrayList)4 Parameters (org.hl7.fhir.r4.model.Parameters)4 BooleanType (org.hl7.fhir.r4.model.BooleanType)3 ValueSet (org.hl7.fhir.r4.model.ValueSet)3 DateTime (org.opencds.cqf.cql.engine.runtime.DateTime)3 Interval (org.opencds.cqf.cql.engine.runtime.Interval)3 CodeSystemInfo (org.opencds.cqf.cql.engine.terminology.CodeSystemInfo)2 CodeKey (com.ibm.cohort.datarow.model.CodeKey)1 DataRow (com.ibm.cohort.datarow.model.DataRow)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CqlEngine (org.opencds.cqf.cql.engine.execution.CqlEngine)1 EvaluationResult (org.opencds.cqf.cql.engine.execution.EvaluationResult)1 InMemoryLibraryLoader (org.opencds.cqf.cql.engine.execution.InMemoryLibraryLoader)1 LibraryLoader (org.opencds.cqf.cql.engine.execution.LibraryLoader)1 TerminologyProvider (org.opencds.cqf.cql.engine.terminology.TerminologyProvider)1