Search in sources :

Example 1 with GroovyScriptValidationRuleEvaluator

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;
        });
    }
}
Also used : GroovyScriptValidationRuleEvaluator(com.b2international.snowowl.core.validation.eval.GroovyScriptValidationRuleEvaluator) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Index(com.b2international.index.Index) ValidationIssue(com.b2international.snowowl.core.validation.issue.ValidationIssue) ValidationWhiteList(com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList) ValidationRule(com.b2international.snowowl.core.validation.rule.ValidationRule) Mappings(com.b2international.index.mapping.Mappings) ValidationIssueDetailExtensionProvider(com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RepositoryConfiguration(com.b2international.snowowl.core.config.RepositoryConfiguration) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with GroovyScriptValidationRuleEvaluator

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());
}
Also used : Path(java.nio.file.Path) GroovyScriptValidationRuleEvaluator(com.b2international.snowowl.core.validation.eval.GroovyScriptValidationRuleEvaluator) SnomedPlugin(com.b2international.snowowl.snomed.core.SnomedPlugin) SnomedQueryValidationRuleEvaluator(com.b2international.snowowl.snomed.validation.SnomedQueryValidationRuleEvaluator) BeforeClass(org.junit.BeforeClass)

Aggregations

GroovyScriptValidationRuleEvaluator (com.b2international.snowowl.core.validation.eval.GroovyScriptValidationRuleEvaluator)2 Index (com.b2international.index.Index)1 Mappings (com.b2international.index.mapping.Mappings)1 RepositoryConfiguration (com.b2international.snowowl.core.config.RepositoryConfiguration)1 ValidationIssue (com.b2international.snowowl.core.validation.issue.ValidationIssue)1 ValidationIssueDetailExtensionProvider (com.b2international.snowowl.core.validation.issue.ValidationIssueDetailExtensionProvider)1 ValidationRule (com.b2international.snowowl.core.validation.rule.ValidationRule)1 ValidationWhiteList (com.b2international.snowowl.core.validation.whitelist.ValidationWhiteList)1 SnomedPlugin (com.b2international.snowowl.snomed.core.SnomedPlugin)1 SnomedQueryValidationRuleEvaluator (com.b2international.snowowl.snomed.validation.SnomedQueryValidationRuleEvaluator)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 Path (java.nio.file.Path)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BeforeClass (org.junit.BeforeClass)1