Skip to content

Commit

Permalink
Copy constructors for settings classes (#564)
Browse files Browse the repository at this point in the history
* clone() for EvaluationSettins, RetrieveSetttings, and TerminologySettings

* Clean up

* Add copy constructors
  • Loading branch information
JPercival authored Oct 16, 2024
1 parent 4d44f0c commit a1f7df8
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,16 @@ public class Measures {
@Setup(Level.Trial)
public void setupTrial() throws Exception {
var evaluationOptions = MeasureEvaluationOptions.defaultOptions();

var retrieveSettings = evaluationOptions.getEvaluationSettings().getRetrieveSettings();
retrieveSettings
evaluationOptions.getEvaluationSettings().setLibraryCache(new HashMap<>());
evaluationOptions
.getEvaluationSettings()
.getRetrieveSettings()
.setSearchParameterMode(SEARCH_FILTER_MODE.FILTER_IN_MEMORY)
.setTerminologyParameterMode(TERMINOLOGY_FILTER_MODE.FILTER_IN_MEMORY);

var terminologySettings = evaluationOptions.getEvaluationSettings().getTerminologySettings();
terminologySettings.setValuesetExpansionMode(VALUESET_EXPANSION_MODE.PERFORM_NAIVE_EXPANSION);

var settingsBuilder = evaluationOptions.getEvaluationSettings().toBuilder();

settingsBuilder.libraryCache(new HashMap<>());
settingsBuilder.retrieveSettings(retrieveSettings);
settingsBuilder.terminologySettings(terminologySettings);

evaluationOptions.setEvaluationSettings(settingsBuilder.build());
evaluationOptions
.getEvaluationSettings()
.getTerminologySettings()
.setValuesetExpansionMode(VALUESET_EXPANSION_MODE.PERFORM_NAIVE_EXPANSION);

this.when = Measure.given()
.repositoryFor("CaseRepresentation101")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,16 @@ public class MeasuresAdditionalData {
@Setup(Level.Trial)
public void setupTrial() throws Exception {
var evaluationOptions = MeasureEvaluationOptions.defaultOptions();

var retrieveSettings = evaluationOptions.getEvaluationSettings().getRetrieveSettings();
retrieveSettings
evaluationOptions.getEvaluationSettings().setLibraryCache(new HashMap<>());
evaluationOptions
.getEvaluationSettings()
.getRetrieveSettings()
.setSearchParameterMode(SEARCH_FILTER_MODE.FILTER_IN_MEMORY)
.setTerminologyParameterMode(TERMINOLOGY_FILTER_MODE.FILTER_IN_MEMORY);

var terminologySettings = evaluationOptions.getEvaluationSettings().getTerminologySettings();
terminologySettings.setValuesetExpansionMode(VALUESET_EXPANSION_MODE.PERFORM_NAIVE_EXPANSION);

var settingsBuilder = evaluationOptions.getEvaluationSettings().toBuilder();

settingsBuilder.libraryCache(new HashMap<>());
settingsBuilder.retrieveSettings(retrieveSettings);
settingsBuilder.terminologySettings(terminologySettings);

evaluationOptions.setEvaluationSettings(settingsBuilder.build());
evaluationOptions
.getEvaluationSettings()
.getTerminologySettings()
.setValuesetExpansionMode(VALUESET_EXPANSION_MODE.PERFORM_NAIVE_EXPANSION);

Bundle additionalData = (Bundle) FhirContext.forR4Cached()
.newJsonParser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ private static Map<String, DataProvider> buildDataProviders(
var providers = new ArrayList<RetrieveProvider>();
var modelResolver = FhirModelResolverCache.resolverForVersion(
repository.fhirContext().getVersion().getVersion());
// TODO: Make a federated repository here once that is ready for sure
// var fedRepo = new FederatedRepository(repository, bundleRepo);

var retrieveProvider = new RepositoryRetrieveProvider(repository, terminologyProvider, retrieveSettings);
providers.add(retrieveProvider);
if (additionalData != null && modelResolver.resolvePath(additionalData, "entry") != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,162 +14,151 @@
import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings;
import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings;

/**
* This class contains settings used to set up CQL evaluation. This class is immutable once constructed.
* Use the Builder to create and instance of this class, and the "toBuilder()" function to create a
* new mutable builder to clone these settings if required.
*/
public class EvaluationSettings {

private final Map<ModelIdentifier, Model> modelCache;
private final Map<VersionedIdentifier, CompiledLibrary> libraryCache;
private final Map<String, List<Code>> valueSetCache;
private final List<LibrarySourceProvider> librarySourceProviders;
private final CqlOptions cqlOptions;
private final RetrieveSettings retrieveSettings;
private final TerminologySettings terminologySettings;
private final NpmProcessor npmProcessor;

private EvaluationSettings(
Map<ModelIdentifier, Model> modelCache,
Map<VersionedIdentifier, CompiledLibrary> libraryCache,
Map<String, List<Code>> valueSetCache,
List<LibrarySourceProvider> librarySourceProviders,
CqlOptions cqlOptions,
RetrieveSettings retrieveSettings,
TerminologySettings terminologySettings,
NpmProcessor npmProcessor) {
this.modelCache = modelCache;
this.libraryCache = libraryCache;
this.valueSetCache = valueSetCache;
this.librarySourceProviders = librarySourceProviders;
this.cqlOptions = cqlOptions;
this.retrieveSettings = retrieveSettings;
this.terminologySettings = terminologySettings;
this.npmProcessor = npmProcessor;
}
private Map<ModelIdentifier, Model> modelCache;
private Map<VersionedIdentifier, CompiledLibrary> libraryCache;
private Map<String, List<Code>> valueSetCache;
private List<LibrarySourceProvider> librarySourceProviders;

private CqlOptions cqlOptions;

private RetrieveSettings retrieveSettings;
private TerminologySettings terminologySettings;
private NpmProcessor npmProcessor;

public static EvaluationSettings.Builder builder() {
return new Builder();
public static EvaluationSettings getDefault() {
return new EvaluationSettings();
}

public EvaluationSettings.Builder toBuilder() {
return new EvaluationSettings.Builder()
.cqlOptions(this.cqlOptions)
.modelCache(this.modelCache)
.libraryCache(this.libraryCache)
.valueSetCache(this.valueSetCache)
.retrieveSettings(this.retrieveSettings)
.terminologySettings(this.terminologySettings)
.librarySourceProviders(new ArrayList<>(this.librarySourceProviders))
.npmProcessor(this.npmProcessor);
EvaluationSettings() {
this.modelCache = new ConcurrentHashMap<>();
this.libraryCache = new ConcurrentHashMap<>();
this.valueSetCache = new ConcurrentHashMap<>();
this.librarySourceProviders = new ArrayList<>();
this.cqlOptions = CqlOptions.defaultOptions();
this.retrieveSettings = new RetrieveSettings();
this.terminologySettings = new TerminologySettings();
this.npmProcessor = null;
}

public static EvaluationSettings getDefault() {
var options = CqlOptions.defaultOptions();
var builder = new EvaluationSettings.Builder()
.cqlOptions(options)
.modelCache(new ConcurrentHashMap<>())
.libraryCache(new ConcurrentHashMap<>())
.valueSetCache(new ConcurrentHashMap<>())
.retrieveSettings(new RetrieveSettings())
.terminologySettings(new TerminologySettings())
.librarySourceProviders(new ArrayList<>());
return builder.build();
/**
* Copy constructor for EvaluationSettings
* @param settings
*/
public EvaluationSettings(EvaluationSettings settings) {
this.modelCache = new ConcurrentHashMap<>(settings.modelCache);
this.libraryCache = new ConcurrentHashMap<>(settings.libraryCache);
this.valueSetCache = new ConcurrentHashMap<>(settings.valueSetCache);
this.cqlOptions = settings.cqlOptions;
this.retrieveSettings = new RetrieveSettings(settings.retrieveSettings);
this.terminologySettings = new TerminologySettings(settings.terminologySettings);
this.librarySourceProviders = new ArrayList<>(settings.librarySourceProviders);
this.npmProcessor =
settings.npmProcessor != null ? new NpmProcessor(settings.npmProcessor.getIgContext()) : null;
}

public Map<ModelIdentifier, Model> getModelCache() {
return this.modelCache;
}

public void setModelCache(Map<ModelIdentifier, Model> modelCache) {
this.modelCache = modelCache;
}

public EvaluationSettings withModelCache(Map<ModelIdentifier, Model> modelCache) {
setModelCache(modelCache);
return this;
}

public Map<VersionedIdentifier, CompiledLibrary> getLibraryCache() {
return this.libraryCache;
}

public void setLibraryCache(Map<VersionedIdentifier, CompiledLibrary> libraryCache) {
this.libraryCache = libraryCache;
}

public EvaluationSettings withLibraryCache(Map<VersionedIdentifier, CompiledLibrary> libraryCache) {
setLibraryCache(libraryCache);
return this;
}

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 CqlOptions getCqlOptions() {
return this.cqlOptions;
}

public EvaluationSettings withCqlOptions(CqlOptions cqlOptions) {
setCqlOptions(cqlOptions);
return this;
}

public void setCqlOptions(CqlOptions cqlOptions) {
this.cqlOptions = cqlOptions;
}

public RetrieveSettings getRetrieveSettings() {
return this.retrieveSettings;
}

public EvaluationSettings withRetrieveSettings(RetrieveSettings retrieveSettings) {
setRetrieveSettings(retrieveSettings);
return this;
}

public void setRetrieveSettings(RetrieveSettings retrieveSettings) {
this.retrieveSettings = retrieveSettings;
}

public TerminologySettings getTerminologySettings() {
return this.terminologySettings;
}

public EvaluationSettings withTerminologySettings(TerminologySettings terminologySettings) {
setTerminologySettings(terminologySettings);
return this;
}

public void setTerminologySettings(TerminologySettings terminologySettings) {
this.terminologySettings = terminologySettings;
}

public List<LibrarySourceProvider> getLibrarySourceProviders() {
return librarySourceProviders;
}

public void setLibrarySourceProviders(List<LibrarySourceProvider> librarySourceProviders) {
this.librarySourceProviders = librarySourceProviders;
}

public EvaluationSettings withLibrarySourceProviders(List<LibrarySourceProvider> librarySourceProviders) {
setLibrarySourceProviders(librarySourceProviders);
return this;
}

public NpmProcessor getNpmProcessor() {
return this.npmProcessor;
}

public static class Builder {
private Map<ModelIdentifier, Model> modelCache;
private Map<VersionedIdentifier, CompiledLibrary> libraryCache;
private Map<String, List<Code>> valueSetCache;
private List<LibrarySourceProvider> librarySourceProviders;
private CqlOptions cqlOptions;
private RetrieveSettings retrieveSettings;
private TerminologySettings terminologySettings;
private NpmProcessor npmProcessor;

public Builder modelCache(Map<ModelIdentifier, Model> modelCache) {
this.modelCache = modelCache;
return this;
}

public Builder libraryCache(Map<VersionedIdentifier, CompiledLibrary> libraryCache) {
this.libraryCache = libraryCache;
return this;
}

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

public Builder librarySourceProviders(List<LibrarySourceProvider> librarySourceProviders) {
this.librarySourceProviders = librarySourceProviders;
return this;
}

public Builder cqlOptions(CqlOptions cqlOptions) {
this.cqlOptions = cqlOptions;
return this;
}

public Builder retrieveSettings(RetrieveSettings retrieveSettings) {
this.retrieveSettings = retrieveSettings;
return this;
}

public Builder terminologySettings(TerminologySettings terminologySettings) {
this.terminologySettings = terminologySettings;
return this;
}

public Builder npmProcessor(NpmProcessor npmProcessor) {
this.npmProcessor = npmProcessor;
return this;
}

public EvaluationSettings build() {
return new EvaluationSettings(
this.modelCache,
this.libraryCache,
this.valueSetCache,
this.librarySourceProviders,
this.cqlOptions,
this.retrieveSettings,
this.terminologySettings,
this.npmProcessor);
}
return npmProcessor;
}

public void setNpmProcessor(NpmProcessor npmProcessor) {
this.npmProcessor = npmProcessor;
}

public EvaluationSettings withNpmProcessor(NpmProcessor npmProcessor) {
setNpmProcessor(npmProcessor);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.cqframework.cql.cql2elm.LibrarySourceProvider;
import org.cqframework.cql.cql2elm.StringLibrarySourceProvider;
import org.hl7.elm.r1.VersionedIdentifier;
import org.hl7.fhir.instance.model.api.IBase;
Expand Down Expand Up @@ -129,16 +128,12 @@ public IBaseParameters evaluateExpression(
Set<String> expressions = new HashSet<>();
expressions.add("return");

List<LibrarySourceProvider> librarySourceProviders = new ArrayList<>();
librarySourceProviders.add(new StringLibrarySourceProvider(Lists.newArrayList(cql)));
var requestSettings = new EvaluationSettings(settings);

var requestSettings = settings.toBuilder().libraryCache(null).build();
requestSettings.getLibrarySourceProviders().add(new StringLibrarySourceProvider(Lists.newArrayList(cql)));

var engine = Engines.forRepository(repository, requestSettings, bundle);
var providers = engine.getEnvironment().getLibraryManager().getLibrarySourceLoader();
for (var source : librarySourceProviders) {
providers.registerProvider(source);
}

var evaluationParameters = cqlFhirParametersConverter.toCqlParameters(parameters);
if (contextParameter != null) {
evaluationParameters.put("%fhirpathcontext", contextParameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ public class RetrieveSettings {
private PROFILE_MODE profileMode = PROFILE_MODE.OFF;
private TERMINOLOGY_FILTER_MODE terminologyParameterMode = TERMINOLOGY_FILTER_MODE.AUTO;

/**
* Default constructor for RetrieveSettings
*/
public RetrieveSettings() {
// intentionally empty
}

/**
* Copy constructor for RetrieveSettings
* @param retrieveSettings
*/
public RetrieveSettings(RetrieveSettings retrieveSettings) {
this.searchParameterMode = retrieveSettings.searchParameterMode;
this.profileMode = retrieveSettings.profileMode;
this.terminologyParameterMode = retrieveSettings.terminologyParameterMode;
}

// Decreasing order of performance
// Applies to all search parameters
// EXCEPT terminology search parameters, which are controlled by the terminology settings.
Expand Down
Loading

0 comments on commit a1f7df8

Please sign in to comment.