use of com.b2international.snowowl.core.validation.eval.GroovyScriptValidationRuleEvaluator in project snow-owl by b2ihealthcare.
the class ValidationPlugin method preRun.
@Override
public void preRun(SnowOwlConfiguration configuration, Environment env) throws Exception {
if (env.isServer()) {
final ObjectMapper mapper = env.service(ObjectMapper.class);
final Index validationIndex = Indexes.createIndex(VALIDATIONS_INDEX, mapper, new Mappings(ValidationIssue.class, ValidationRule.class, ValidationWhiteList.class), env.service(IndexSettings.class).forIndex(env.service(RepositoryConfiguration.class).getIndexConfiguration(), VALIDATIONS_INDEX));
final ValidationRepository repository = new ValidationRepository(validationIndex);
env.services().registerService(ValidationRepository.class, repository);
// register always available validation rule evaluators
ValidationRuleEvaluator.Registry.register(new GroovyScriptValidationRuleEvaluator(env.getConfigPath()));
// initialize validation thread pool
final ValidationConfiguration validationConfig = configuration.getModuleConfig(ValidationConfiguration.class);
int numberOfValidationThreads = validationConfig.getNumberOfValidationThreads();
int maxConcurrentExpensiveJobs = validationConfig.getMaxConcurrentExpensiveJobs();
int maxConcurrentNormalJobs = validationConfig.getMaxConcurrentNormalJobs();
env.services().registerService(ValidationConfiguration.class, validationConfig);
env.services().registerService(ValidationThreadPool.class, new ValidationThreadPool(numberOfValidationThreads, maxConcurrentExpensiveJobs, maxConcurrentNormalJobs));
env.services().registerService(ValidationIssueDetailExtensionProvider.class, new ValidationIssueDetailExtensionProvider(env.service(ClassPathScanner.class)));
final List<File> listOfFiles = Arrays.asList(env.getConfigPath().toFile().listFiles());
final Set<File> validationRuleFiles = Sets.newHashSet();
final Pattern validationFilenamePattern = Pattern.compile("(validation-rules)-(\\w+).(json)");
for (File file : listOfFiles) {
final String fileName = file.getName();
final Matcher match = validationFilenamePattern.matcher(fileName);
if (match.matches()) {
validationRuleFiles.add(file);
}
}
final List<ValidationRule> availableRules = Lists.newArrayList();
for (File validationRulesFile : validationRuleFiles) {
LOG.info("Synchronizing validation rules from file: " + validationRulesFile);
availableRules.addAll(mapper.readValue(validationRulesFile, new TypeReference<List<ValidationRule>>() {
}));
}
repository.write(writer -> {
final Map<String, ValidationRule> existingRules = Maps.uniqueIndex(ValidationRequests.rules().prepareSearch().all().buildAsync().getRequest().execute(env), ValidationRule::getId);
// index all rules from the file, this will update existing rules as well
final Set<String> ruleIds = newHashSet();
for (ValidationRule rule : availableRules) {
writer.put(rule);
ruleIds.add(rule.getId());
}
// delete rules and associated issues
Set<String> rulesToDelete = Sets.difference(existingRules.keySet(), ruleIds);
if (!rulesToDelete.isEmpty()) {
final Set<String> issuesToDelete = newHashSet(writer.searcher().search(Query.select(String.class).from(ValidationIssue.class).fields(ValidationIssue.Fields.ID).where(Expressions.builder().filter(Expressions.matchAny(ValidationIssue.Fields.RULE_ID, rulesToDelete)).build()).limit(Integer.MAX_VALUE).build()).getHits());
writer.removeAll(ImmutableMap.<Class<?>, Set<String>>of(ValidationRule.class, rulesToDelete, ValidationIssue.class, issuesToDelete));
}
writer.commit();
return null;
});
}
}
use of com.b2international.snowowl.core.validation.eval.GroovyScriptValidationRuleEvaluator in project snow-owl by b2ihealthcare.
the class AllGenericValidationTests method init.
@BeforeClass
public static void init() {
ValidationRuleEvaluator.Registry.register(new SnomedQueryValidationRuleEvaluator());
final Path resourcesDir = PlatformUtil.toAbsoluteBundlePath(BaseGenericValidationRuleTest.class.getClassLoader().getResource("src/main/resources"));
ValidationRuleEvaluator.Registry.register(new GroovyScriptValidationRuleEvaluator(resourcesDir));
TerminologyRegistry.INSTANCE.register(new SnomedPlugin());
}
Aggregations