Skip to content

Commit

Permalink
Add ValueSet cache and Expansion Mode to EvaluationSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
JPercival committed Sep 20, 2023
1 parent 0ef3f1c commit 072611c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.opencds.cqf.fhir.cql.engine.retrieve.RepositoryRetrieveProvider;
import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings;
import org.opencds.cqf.fhir.cql.engine.terminology.RepositoryTerminologyProvider;
import org.opencds.cqf.fhir.cql.engine.terminology.RepositoryTerminologyProvider.EXPANSION_MODE;
import org.opencds.cqf.fhir.utility.Constants;
import org.opencds.cqf.fhir.utility.adapter.AdapterFactory;
import org.opencds.cqf.fhir.utility.repository.InMemoryFhirRepository;
Expand All @@ -55,7 +56,7 @@ public static CqlEngine forRepository(Repository repository, EvaluationSettings

public static CqlEngine forRepository(
Repository repository, EvaluationSettings settings, NpmProcessor npmProcessor, Boolean useLibraryCache) {
var terminologyProvider = new RepositoryTerminologyProvider(repository);
var terminologyProvider = new RepositoryTerminologyProvider(repository, settings.getValueSetCache(), settings.getExpansionMode());
var sources = Collections.singletonList(buildLibrarySource(repository));

var dataProviders = buildDataProviders(repository, null, terminologyProvider, settings.getRetrieveSettings());
Expand All @@ -80,7 +81,7 @@ public static CqlEngine forRepositoryAndSettings(
checkNotNull(settings);
checkNotNull(repository);

var terminologyProvider = new RepositoryTerminologyProvider(repository);
var terminologyProvider = new RepositoryTerminologyProvider(repository, settings.getValueSetCache(), settings.getExpansionMode());
var sourceProviders = new ArrayList<LibrarySourceProvider>();
sourceProviders.add(buildLibrarySource(repository));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package org.opencds.cqf.fhir.cql;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.cqframework.cql.cql2elm.model.CompiledLibrary;
import org.cqframework.cql.cql2elm.model.Model;
import org.hl7.cql.model.ModelIdentifier;
import org.hl7.elm.r1.VersionedIdentifier;
import org.opencds.cqf.cql.engine.runtime.Code;
import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings;
import org.opencds.cqf.fhir.cql.engine.terminology.RepositoryTerminologyProvider.EXPANSION_MODE;

public class EvaluationSettings {

private Map<ModelIdentifier, Model> modelCache;
private Map<VersionedIdentifier, CompiledLibrary> libraryCache;
private Map<String, List<Code>> valueSetCache;
private EXPANSION_MODE expansionMode;

private CqlOptions cqlOptions;

Expand All @@ -24,7 +29,9 @@ public static EvaluationSettings getDefault() {
settings.setCqlOptions(options);
settings.setModelCache(new ConcurrentHashMap<>());
settings.setLibraryCache(new ConcurrentHashMap<>());
settings.setValueSetCache(new ConcurrentHashMap<>());
settings.setRetrieveSettings(new RetrieveSettings());
settings.setExpansionMode(EXPANSION_MODE.AUTO);

return settings;
}
Expand Down Expand Up @@ -55,17 +62,34 @@ public EvaluationSettings withLibraryCache(Map<VersionedIdentifier, CompiledLibr
return this;
}

public CqlOptions getCqlOptions() {
return this.cqlOptions;
public Map<String, List<Code>> getValueSetCache() {
return this.valueSetCache;
}

public void setValueSetCache(Map<String, List<Code>> valueSetCache) {
this.valueSetCache = valueSetCache;
}

public EvaluationSettings withValueSetCache(Map<String, List<Code>> valueSetCache) {
setValueSetCache(valueSetCache);
return this;
}

public EXPANSION_MODE getExpansionMode() {
return this.expansionMode;
}

public void setExpansionMode(EXPANSION_MODE expansionMode) {
this.expansionMode = expansionMode;
}

public EvaluationSettings withExpansionMode(EXPANSION_MODE expansionMode) {
setExpansionMode(expansionMode);
return this;
}

/**
* @deprecated Left in for backwards compatibility. getCqlOptions().getCqlEngineOptions() should
* be used instead.
*/
@Deprecated
public CqlEngineOptions getEngineOptions() {
return this.cqlOptions.getCqlEngineOptions();
public CqlOptions getCqlOptions() {
return this.cqlOptions;
}

public EvaluationSettings withCqlOptions(CqlOptions cqlOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public class RepositoryTerminologyProvider implements TerminologyProvider {
// eventually, we want to be able to detect expansion capabilities from the
// capability statement. For now, we hard code based on the our knowledge
// of where we set this terminology provider up.
public enum EXPANSION_CAPABILITIES {
NO,
YES
public enum EXPANSION_MODE {
INTERNAL,
REPOSITORY,
AUTO
}

private static final Logger logger = LoggerFactory.getLogger(RepositoryTerminologyProvider.class);
Expand All @@ -49,7 +50,7 @@ public enum EXPANSION_CAPABILITIES {
private final FhirContext fhirContext;
private final IFhirPath fhirPath;
private final Map<String, List<Code>> valueSetIndex;
private final EXPANSION_CAPABILITIES repoExpansionCapabilities;
private final EXPANSION_MODE expansionMode;

// The cached expansions are sorted by code order
// This is used determine the range of codes to check
Expand All @@ -66,25 +67,25 @@ public Range(int start, int end) {
public final int end;
}

public RepositoryTerminologyProvider(Repository repository, EXPANSION_CAPABILITIES repoExpansionCapabilities) {
this(repository, new HashMap<>(), repoExpansionCapabilities);
public RepositoryTerminologyProvider(Repository repository, EXPANSION_MODE expansionMode) {
this(repository, new HashMap<>(), expansionMode);
}

public RepositoryTerminologyProvider(
Repository repository,
Map<String, List<Code>> valueSetIndex,
EXPANSION_CAPABILITIES repoExpansionCapabilities) {
EXPANSION_MODE expansionMode) {
this.repository = requireNonNull(repository, "repository can not be null.");
this.valueSetIndex = requireNonNull(valueSetIndex, "valueSetIndex can not be null.");
this.repoExpansionCapabilities =
requireNonNull(repoExpansionCapabilities, "repoExpansionCapabilities can not be null.");
this.expansionMode =
requireNonNull(expansionMode, "expansionMode can not be null.");

this.fhirContext = repository.fhirContext();
this.fhirPath = FhirPathCache.cachedForContext(fhirContext);
}

public RepositoryTerminologyProvider(Repository repository) {
this(repository, new HashMap<>(), EXPANSION_CAPABILITIES.NO);
this(repository, new HashMap<>(), EXPANSION_MODE.AUTO);
}

/**
Expand Down Expand Up @@ -180,9 +181,9 @@ private List<Code> tryExpand(ValueSetInfo valueSet) {
valueSet.getId());
}

// ValueSet didn't return with an expansion, and the underlying repository
// supports expansion, so try to expand it.
if (codes == null && this.repoExpansionCapabilities == EXPANSION_CAPABILITIES.YES) {
boolean shouldUseRepoExpansion = this.expansionMode == EXPANSION_MODE.INTERNAL ||
(this.expansionMode == EXPANSION_MODE.REPOSITORY && canRepositoryExpand(valueSet));
if (codes == null && shouldUseRepoExpansion) {
vs = this.repository.invoke(vs.getIdElement(), "$expand", null).getResource();
codes = ValueSets.getCodesInExpansion(this.fhirContext, vs);
}
Expand Down Expand Up @@ -298,4 +299,9 @@ private boolean containsExpansionLogic(IBaseResource resource) {
}
return false;
}

private boolean canRepositoryExpand(ValueSetInfo valueSetInfo) {
// TODO: check capabilities statement to see if expansion is supported.
return true;
}
}

0 comments on commit 072611c

Please sign in to comment.