|
| 1 | +package org.opencds.cqf.ruler.config; |
| 2 | + |
| 3 | +import ca.uhn.fhir.jpa.api.dao.DaoRegistry; |
| 4 | +import ca.uhn.fhir.jpa.cache.IResourceChangeListenerRegistry; |
| 5 | +import ca.uhn.fhir.jpa.cache.ResourceChangeListenerRegistryInterceptor; |
| 6 | +import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; |
| 7 | +import ca.uhn.fhir.rest.server.RestfulServer; |
| 8 | +import ca.uhn.fhir.rest.server.provider.ResourceProviderFactory; |
| 9 | +import org.cqframework.cql.cql2elm.CqlCompilerOptions; |
| 10 | +import org.cqframework.cql.cql2elm.model.CompiledLibrary; |
| 11 | +import org.cqframework.cql.cql2elm.model.Model; |
| 12 | +import org.hl7.cql.model.ModelIdentifier; |
| 13 | +import org.hl7.elm.r1.VersionedIdentifier; |
| 14 | +import org.opencds.cqf.cql.engine.execution.CqlEngine; |
| 15 | +import org.opencds.cqf.cql.engine.runtime.Code; |
| 16 | +import org.opencds.cqf.external.cr.CrConfigCondition; |
| 17 | +import org.opencds.cqf.external.cr.CrProperties; |
| 18 | +import org.opencds.cqf.external.cr.PostInitProviderRegisterer; |
| 19 | +import org.opencds.cqf.fhir.cql.EvaluationSettings; |
| 20 | +import org.opencds.cqf.fhir.cql.engine.retrieve.RetrieveSettings; |
| 21 | +import org.opencds.cqf.fhir.cql.engine.terminology.TerminologySettings; |
| 22 | +import org.opencds.cqf.fhir.cr.hapi.common.CodeCacheResourceChangeListener; |
| 23 | +import org.opencds.cqf.fhir.cr.hapi.common.CqlThreadFactory; |
| 24 | +import org.opencds.cqf.fhir.cr.hapi.common.ElmCacheResourceChangeListener; |
| 25 | +import org.opencds.cqf.fhir.cr.measure.CareGapsProperties; |
| 26 | +import org.opencds.cqf.fhir.cr.measure.MeasureEvaluationOptions; |
| 27 | +import org.opencds.cqf.fhir.utility.ValidationProfile; |
| 28 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Conditional; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.context.annotation.Primary; |
| 33 | +import org.springframework.security.concurrent.DelegatingSecurityContextExecutorService; |
| 34 | + |
| 35 | +import java.util.EnumSet; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.concurrent.ConcurrentHashMap; |
| 40 | +import java.util.concurrent.ExecutorService; |
| 41 | +import java.util.concurrent.Executors; |
| 42 | + |
| 43 | +@Configuration |
| 44 | +@Conditional({CrConfigCondition.class}) |
| 45 | +public class RulerCrCommonConfig { |
| 46 | + |
| 47 | + @Bean |
| 48 | + @ConfigurationProperties(prefix = "hapi.fhir.cr") |
| 49 | + CrProperties crProperties() { |
| 50 | + return new CrProperties(); |
| 51 | + } |
| 52 | + |
| 53 | + @Bean |
| 54 | + RetrieveSettings retrieveSettings(CrProperties theCrProperties) { |
| 55 | + return theCrProperties.getCql().getData(); |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + TerminologySettings terminologySettings(CrProperties theCrProperties) { |
| 60 | + return theCrProperties.getCql().getTerminology(); |
| 61 | + } |
| 62 | + |
| 63 | + @Bean |
| 64 | + public EvaluationSettings evaluationSettings( |
| 65 | + CrProperties theCrProperties, |
| 66 | + RetrieveSettings theRetrieveSettings, |
| 67 | + TerminologySettings theTerminologySettings, |
| 68 | + Map<VersionedIdentifier, CompiledLibrary> theGlobalLibraryCache, |
| 69 | + Map<ModelIdentifier, Model> theGlobalModelCache, |
| 70 | + Map<String, List<Code>> theGlobalValueSetCache) { |
| 71 | + var evaluationSettings = EvaluationSettings.getDefault(); |
| 72 | + var cqlOptions = evaluationSettings.getCqlOptions(); |
| 73 | + |
| 74 | + var cqlEngineOptions = cqlOptions.getCqlEngineOptions(); |
| 75 | + Set<CqlEngine.Options> options = EnumSet.noneOf(CqlEngine.Options.class); |
| 76 | + var cqlRuntimeProperties = theCrProperties.getCql().getRuntime(); |
| 77 | + if (cqlRuntimeProperties.isEnableExpressionCaching()) { |
| 78 | + options.add(CqlEngine.Options.EnableExpressionCaching); |
| 79 | + } |
| 80 | + if (cqlRuntimeProperties.isEnableValidation()) { |
| 81 | + options.add(CqlEngine.Options.EnableValidation); |
| 82 | + } |
| 83 | + cqlEngineOptions.setOptions(options); |
| 84 | + if (cqlRuntimeProperties.isDebugLoggingEnabled()) { |
| 85 | + cqlEngineOptions.setDebugLoggingEnabled(true); |
| 86 | + } |
| 87 | + cqlOptions.setCqlEngineOptions(cqlEngineOptions); |
| 88 | + |
| 89 | + var cqlCompilerOptions = new CqlCompilerOptions(); |
| 90 | + |
| 91 | + var cqlCompilerProperties = theCrProperties.getCql().getCompiler(); |
| 92 | + |
| 93 | + if (cqlCompilerProperties.isEnableDateRangeOptimization()) { |
| 94 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.EnableDateRangeOptimization); |
| 95 | + } |
| 96 | + if (cqlCompilerProperties.isEnableAnnotations()) { |
| 97 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.EnableAnnotations); |
| 98 | + } |
| 99 | + if (cqlCompilerProperties.isEnableLocators()) { |
| 100 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.EnableLocators); |
| 101 | + } |
| 102 | + if (cqlCompilerProperties.isEnableResultsType()) { |
| 103 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.EnableResultTypes); |
| 104 | + } |
| 105 | + cqlCompilerOptions.setVerifyOnly(cqlCompilerProperties.isVerifyOnly()); |
| 106 | + if (cqlCompilerProperties.isEnableDetailedErrors()) { |
| 107 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.EnableDetailedErrors); |
| 108 | + } |
| 109 | + cqlCompilerOptions.setErrorLevel(cqlCompilerProperties.getErrorSeverityLevel()); |
| 110 | + if (cqlCompilerProperties.isDisableListTraversal()) { |
| 111 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.DisableListTraversal); |
| 112 | + } |
| 113 | + if (cqlCompilerProperties.isDisableListDemotion()) { |
| 114 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.DisableListDemotion); |
| 115 | + } |
| 116 | + if (cqlCompilerProperties.isDisableListPromotion()) { |
| 117 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.DisableListPromotion); |
| 118 | + } |
| 119 | + if (cqlCompilerProperties.isEnableIntervalDemotion()) { |
| 120 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.EnableIntervalDemotion); |
| 121 | + } |
| 122 | + if (cqlCompilerProperties.isEnableIntervalPromotion()) { |
| 123 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.EnableIntervalPromotion); |
| 124 | + } |
| 125 | + if (cqlCompilerProperties.isDisableMethodInvocation()) { |
| 126 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.DisableMethodInvocation); |
| 127 | + } |
| 128 | + if (cqlCompilerProperties.isRequireFromKeyword()) { |
| 129 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.RequireFromKeyword); |
| 130 | + } |
| 131 | + cqlCompilerOptions.setValidateUnits(cqlCompilerProperties.isValidateUnits()); |
| 132 | + if (cqlCompilerProperties.isDisableDefaultModelInfoLoad()) { |
| 133 | + cqlCompilerOptions.setOptions(CqlCompilerOptions.Options.DisableDefaultModelInfoLoad); |
| 134 | + } |
| 135 | + cqlCompilerOptions.setSignatureLevel(cqlCompilerProperties.getSignatureLevel()); |
| 136 | + cqlCompilerOptions.setCompatibilityLevel(cqlCompilerProperties.getCompatibilityLevel()); |
| 137 | + cqlCompilerOptions.setAnalyzeDataRequirements(cqlCompilerProperties.isAnalyzeDataRequirements()); |
| 138 | + cqlCompilerOptions.setCollapseDataRequirements(cqlCompilerProperties.isCollapseDataRequirements()); |
| 139 | + |
| 140 | + cqlOptions.setCqlCompilerOptions(cqlCompilerOptions); |
| 141 | + evaluationSettings.setLibraryCache(theGlobalLibraryCache); |
| 142 | + evaluationSettings.setModelCache(theGlobalModelCache); |
| 143 | + evaluationSettings.setValueSetCache(theGlobalValueSetCache); |
| 144 | + evaluationSettings.setRetrieveSettings(theRetrieveSettings); |
| 145 | + evaluationSettings.setTerminologySettings(theTerminologySettings); |
| 146 | + return evaluationSettings; |
| 147 | + } |
| 148 | + |
| 149 | + @Primary |
| 150 | + @Bean |
| 151 | + public ExecutorService cqlExecutor() { |
| 152 | + CqlThreadFactory factory = new CqlThreadFactory(); |
| 153 | + ExecutorService executor = Executors.newFixedThreadPool(2, factory); |
| 154 | + executor = new DelegatingSecurityContextExecutorService(executor); |
| 155 | + |
| 156 | + return executor; |
| 157 | + } |
| 158 | + |
| 159 | + @Bean |
| 160 | + CareGapsProperties careGapsProperties(CrProperties theCrProperties) { |
| 161 | + var careGapsProperties = new CareGapsProperties(); |
| 162 | + // This check for the resource type really should be happening down in CR where the setting is actually used but |
| 163 | + // that will have to wait for a future CR release |
| 164 | + careGapsProperties.setCareGapsReporter( |
| 165 | + theCrProperties.getCareGaps().getReporter().replace("Organization/", "")); |
| 166 | + careGapsProperties.setCareGapsCompositionSectionAuthor( |
| 167 | + theCrProperties.getCareGaps().getSection_author().replace("Organization/", "")); |
| 168 | + return careGapsProperties; |
| 169 | + } |
| 170 | + |
| 171 | + @Bean |
| 172 | + MeasureEvaluationOptions measureEvaluationOptions( |
| 173 | + EvaluationSettings theEvaluationSettings, Map<String, ValidationProfile> theValidationProfiles) { |
| 174 | + MeasureEvaluationOptions measureEvalOptions = new MeasureEvaluationOptions(); |
| 175 | + measureEvalOptions.setEvaluationSettings(theEvaluationSettings); |
| 176 | + if (measureEvalOptions.isValidationEnabled()) { |
| 177 | + measureEvalOptions.setValidationProfiles(theValidationProfiles); |
| 178 | + } |
| 179 | + return measureEvalOptions; |
| 180 | + } |
| 181 | + |
| 182 | + @Bean |
| 183 | + public PostInitProviderRegisterer postInitProviderRegisterer( |
| 184 | + RestfulServer theRestfulServer, ResourceProviderFactory theResourceProviderFactory) { |
| 185 | + return new PostInitProviderRegisterer(theRestfulServer, theResourceProviderFactory); |
| 186 | + } |
| 187 | + |
| 188 | + @Bean |
| 189 | + public Map<VersionedIdentifier, CompiledLibrary> globalLibraryCache() { |
| 190 | + return new ConcurrentHashMap<>(); |
| 191 | + } |
| 192 | + |
| 193 | + @Bean |
| 194 | + public Map<ModelIdentifier, Model> globalModelCache() { |
| 195 | + return new ConcurrentHashMap<>(); |
| 196 | + } |
| 197 | + |
| 198 | + @Bean |
| 199 | + public Map<String, List<Code>> globalValueSetCache() { |
| 200 | + return new ConcurrentHashMap<>(); |
| 201 | + } |
| 202 | + |
| 203 | + @Bean |
| 204 | + public ElmCacheResourceChangeListener elmCacheResourceChangeListener( |
| 205 | + IResourceChangeListenerRegistry theResourceChangeListenerRegistry, |
| 206 | + DaoRegistry theDaoRegistry, |
| 207 | + EvaluationSettings theEvaluationSettings) { |
| 208 | + ElmCacheResourceChangeListener listener = |
| 209 | + new ElmCacheResourceChangeListener(theDaoRegistry, theEvaluationSettings.getLibraryCache()); |
| 210 | + theResourceChangeListenerRegistry.registerResourceResourceChangeListener( |
| 211 | + "Library", SearchParameterMap.newSynchronous(), listener, 1000); |
| 212 | + return listener; |
| 213 | + } |
| 214 | + |
| 215 | + @Bean |
| 216 | + public CodeCacheResourceChangeListener codeCacheResourceChangeListener( |
| 217 | + IResourceChangeListenerRegistry theResourceChangeListenerRegistry, |
| 218 | + EvaluationSettings theEvaluationSettings, |
| 219 | + DaoRegistry theDaoRegistry) { |
| 220 | + |
| 221 | + CodeCacheResourceChangeListener listener = |
| 222 | + new CodeCacheResourceChangeListener(theDaoRegistry, theEvaluationSettings.getValueSetCache()); |
| 223 | + // registry |
| 224 | + theResourceChangeListenerRegistry.registerResourceResourceChangeListener( |
| 225 | + "ValueSet", SearchParameterMap.newSynchronous(), listener, 1000); |
| 226 | + |
| 227 | + return listener; |
| 228 | + } |
| 229 | + |
| 230 | + @Bean |
| 231 | + public ResourceChangeListenerRegistryInterceptor resourceChangeListenerRegistryInterceptor() { |
| 232 | + return new ResourceChangeListenerRegistryInterceptor(); |
| 233 | + } |
| 234 | +} |
0 commit comments