<commit_before>/*
 * Copyright 2017 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.internal.validation;

import static com.google.common.collect.Sets.newHashSet;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b2international.index.Index;
import com.b2international.index.Indexes;
import com.b2international.index.mapping.Mappings;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.b2international.snowowl.core.config.SnowOwlConfiguration;
import com.b2international.snowowl.core.setup.DefaultBootstrapFragment;
import com.b2international.snowowl.core.setup.Environment;
import com.b2international.snowowl.core.validation.ValidationRequests;
import com.b2international.snowowl.core.validation.issue.ValidationIssue;
import com.b2international.snowowl.core.validation.rule.ValidationRule;
import com.b2international.snowowl.datastore.config.IndexSettings;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * @since 6.0
 */
public final class ValidationBootstrap extends DefaultBootstrapFragment {

	private static final Logger LOG = LoggerFactory.getLogger("validation");
	
	@Override
	public void run(SnowOwlConfiguration configuration, Environment env, IProgressMonitor monitor) throws Exception {
		if (env.isEmbedded() || env.isServer()) {
			final ObjectMapper mapper = env.service(ObjectMapper.class);
			final Index validationIndex = Indexes.createIndex(
				"validations", 
				mapper, 
				new Mappings(ValidationIssue.class, ValidationRule.class), 
				env.service(IndexSettings.class)
			);
			
			final ValidationRepository repository = new ValidationRepository(validationIndex);
			env.services().registerService(ValidationRepository.class, repository);
			
			// initialize validation thread pool
			// TODO make this configurable
			int numberOfValidationThreads = Math.max(2, Runtime.getRuntime().availableProcessors() / 2);
			env.services().registerService(ValidationThreadPool.class, new ValidationThreadPool(numberOfValidationThreads));

			// synchronize rules from a default validation-rules file<commit_msg>[validation] move validation repository init to preRun phase<commit_after>/*
 * Copyright 2017 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.core.internal.validation;

import static com.google.common.collect.Sets.newHashSet;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b2international.index.Index;
import com.b2international.index.Indexes;
import com.b2international.index.mapping.Mappings;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.b2international.snowowl.core.config.SnowOwlConfiguration;
import com.b2international.snowowl.core.setup.DefaultBootstrapFragment;
import com.b2international.snowowl.core.setup.Environment;
import com.b2international.snowowl.core.setup.PreRunCapableBootstrapFragment;
import com.b2international.snowowl.core.validation.ValidationRequests;
import com.b2international.snowowl.core.validation.issue.ValidationIssue;
import com.b2international.snowowl.core.validation.rule.ValidationRule;
import com.b2international.snowowl.datastore.config.IndexSettings;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * @since 6.0
 */
public final class ValidationBootstrap extends DefaultBootstrapFragment implements PreRunCapableBootstrapFragment {

	private static final Logger LOG = LoggerFactory.getLogger("validation");
	
	@Override
	public void preRun(SnowOwlConfiguration configuration, Environment env) throws Exception {
		if (env.isEmbedded() || env.isServer()) {
			final ObjectMapper mapper = env.service(ObjectMapper.class);
			final Index validationIndex = Indexes.createIndex(
				"validations", 
				mapper, 
				new Mappings(ValidationIssue.class, ValidationRule.class), 
				env.service(IndexSettings.class)
			);
			
			final ValidationRepository repository = new ValidationRepository(validationIndex);
			env.services().registerService(ValidationRepository.class, repository);
			
			// initialize validation thread pool
			// TODO make this configurable
			int numberOfValidationThreads = Math.max(2, Runtime.getRuntime().availableProcessors() / 2);
			env.services().registerService(ValidationThreadPool.class, new ValidationThreadPool(numberOfValidationThreads));

			// synchronize rules from a default validation-rules file
{'fraction_non_alphanumeric': 0.09093861643390712, 'fraction_numerical': 0.009581032802858071, 'mean_word_length': 5.316923076923077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5666099', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1663, 'n_words': 428}