<commit_before>/*
 * Copyright 2011-2015 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.snomed.datastore;

import static com.b2international.snowowl.datastore.cdo.CDOUtils.check;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.ATTRIBUTE_VALUE;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.COMPLEX_MAP;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.DESCRIPTION_TYPE;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.EXTENDED_MAP;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.LANGUAGE;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.QUERY;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.SIMPLE;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.SIMPLE_MAP;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.get;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.unmodifiableSet;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Map;

import javax.annotation.Nullable;

import org.apache.lucene.document.Document;
import org.eclipse.emf.ecore.EClass;

import com.b2international.commons.BooleanUtils;
import com.b2international.snowowl.core.CoreTerminologyBroker;
import com.b2international.snowowl.core.SnowOwlApplication;
import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration;
import com.b2international.snowowl.snomed.datastore.index.mapping.SnomedMappings;
import com.b2international.snowowl.snomed.snomedrefset.DataType;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSet;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetPackage;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType;
import com.google.common.base.Preconditions;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

/** 
 * Utility class collecting commons operations related to SNOMED CT reference sets. 
 *
 */
public abstract class SnomedRefSetUtil {

	private static final SnomedRefSetType[] REF_SET_VIEW_ROOTS = new SnomedRefSetType[] {
		ATTRIBUTE_VALUE, 
		COMPLEX_MAP, 
		DESCRIPTION_TYPE, 
		QUERY, 
		SIMPLE_MAP, 
		SIMPLE,
		EXTENDED_MAP
	};

	/***
	 * Mapping between the association indication reference set IDs and the human readable names instead of
	 * their CAPITAL CASE preferred terms.
	 */
	public static final Map<String, String> ASSOCIATION_REFSETS = ImmutableMap.<String, String>builder()
			.put(Concepts.REFSET_ALTERNATIVE_ASSOCIATION, "Alternative concept")
			.put(Concepts.REFSET_MOVED_FROM_ASSOCIATION, "Moved from")
			.put(Concepts.REFSET_MOVED_TO_ASSOCIATION, "Moved to")
			.put(Concepts.REFSET_POSSIBLY_EQUIVALENT_TO_ASSOCIATION, "Possibly equivalent to")
			.put(Concepts.REFSET_REFERS_TO_ASSOCIATION, "Refers to")
			.put(Concepts.REFSET_REPLACED_BY_ASSOCIATION, "Replaced by")
			.put(Concepts.REFSET_SAME_AS_ASSOCIATION, "Same as")
			.put(Concepts.REFSET_SIMILAR_TO_ASSOCIATION, "Similar to")
			.put(Concepts.REFSET_WAS_A_ASSOCIATION, "Was a")
			.build();
	
	/**
	 * Map for looking up the concrete domain reference set identifier concept IDs based on the associated concrete domain data types.
	 */
	public static final BiMap<com.b2international.snowowl.snomed.mrcm.DataType, String> MRCM_DATATYPE_TO_REFSET_MAP = ImmutableBiMap.<com.b2international.snowowl.snomed.mrcm.DataType, String>builder()
			.put(com.b2international.snowowl.snomed.mrcm.DataType.BOOLEAN, getCoreConfiguration().getBooleanDatatypeRefsetIdentifier())
			.put(com.b2international.snowowl.snomed.mrcm.DataType.DATE, getCoreConfiguration().getDatetimeDatatypeRefsetIdentifier())
			.put(com.b2international.snowowl.snomed.mrcm.DataType.FLOAT, getCoreConfiguration().getFloatDatatypeRefsetIdentifier())
			.put(com.b2international.snowowl.snomed.mrcm.DataType.INTEGER, getCoreConfiguration().getIntegerDatatypeRefsetIdentifier())
			.put(com.b2international.snowowl.snomed.mrcm.DataType.STRING, getCoreConfiguration().getStringDatatypeRefsetIdentifier())
			.build();
	
	public static final BiMap<DataType, String> DATATYPE_TO_REFSET_MAP = ImmutableBiMap.<DataType, String>builder()
			.put(DataType.BOOLEAN, getCoreConfiguration().getBooleanDatatypeRefsetIdentifier())
			.put(DataType.DATE, getCoreConfiguration().getDatetimeDatatypeRefsetIdentifier())
			.put(DataType.DECIMAL, getCoreConfiguration().getFloatDatatypeRefsetIdentifier())
			.put(DataType.INTEGER, getCoreConfiguration().getIntegerDatatypeRefsetIdentifier())
			.put(DataType.STRING, getCoreConfiguration().getStringDatatypeRefsetIdentifier())
			.build();

	/**
	 * Map for navigating between the {@link com.b2international.snowowl.snomed.mrcm.DataType} and the {@link DataType} enumerations.
	 */
	public static final BiMap<com.b2international.snowowl.snomed.mrcm.DataType, DataType> MRCM_DATATYPE_TO_DATATYPE_MAP = ImmutableBiMap.<com.b2international.snowowl.snomed.mrcm.DataType, DataType> builder()
			.put(com.b2international.snowowl.snomed.mrcm.DataType.BOOLEAN, DataType.BOOLEAN)
			.put(com.b2international.snowowl.snomed.mrcm.DataType.DATE, DataType.DATE)
			.put(com.b2international.snowowl.snomed.mrcm.DataType.FLOAT, DataType.DECIMAL)
			.put(com.b2international.snowowl.snomed.mrcm.DataType.INTEGER, DataType.INTEGER)
			.put(com.b2international.snowowl.snomed.mrcm.DataType.STRING, DataType.STRING)
			.build();
	
	public static final ImmutableSet<com.b2international.snowowl.snomed.mrcm.DataType> UNSUPPORTED_DATATYPES = ImmutableSet
			.<com.b2international.snowowl.snomed.mrcm.DataType> of(com.b2international.snowowl.snomed.mrcm.DataType.DATE);
	
	private static SnomedCoreConfiguration getCoreConfiguration() {
		return SnowOwlApplication.INSTANCE.getConfiguration().getModuleConfig(SnomedCoreConfiguration.class);
	}
	
	public static EClass getRefSetMemberClass(final SnomedRefSetType type) {
		
		switch (type) {
			case SIMPLE:
				return SnomedRefSetPackage.Literals.SNOMED_REF_SET_MEMBER;
			case ATTRIBUTE_VALUE:
				return SnomedRefSetPackage.Literals.SNOMED_ATTRIBUTE_VALUE_REF_SET_MEMBER;
			case LANGUAGE:
				return SnomedRefSetPackage.Literals.SNOMED_LANGUAGE_REF_SET_MEMBER;
			case COMPLEX_MAP: //$FALL-THROUGH$
			case EXTENDED_MAP:
				return SnomedRefSetPackage.Literals.SNOMED_COMPLEX_MAP_REF_SET_MEMBER;
			case SIMPLE_MAP:
				return SnomedRefSetPackage.Literals.SNOMED_SIMPLE_MAP_REF_SET_MEMBER;
			case QUERY:
				return SnomedRefSetPackage.Literals.SNOMED_QUERY_REF_SET_MEMBER;
			case DESCRIPTION_TYPE:
				return SnomedRefSetPackage.Literals.SNOMED_DESCRIPTION_TYPE_REF_SET_MEMBER;
			case ASSOCIATION:
				return SnomedRefSetPackage.Literals.SNOMED_ASSOCIATION_REF_SET_MEMBER;
			case CONCRETE_DATA_TYPE:
				return SnomedRefSetPackage.Literals.SNOMED_CONCRETE_DATA_TYPE_REF_SET_MEMBER;
			case MODULE_DEPENDENCY:
				return SnomedRefSetPackage.Literals.SNOMED_MODULE_DEPENDENCY_REF_SET_MEMBER;
			default:
				throw new IllegalArgumentException("Unsupported reference set type: " + type);
		}
	}
	
	public static final Collection<EClass> REFSET_CLASSES = unmodifiableSet(newHashSet(
			SnomedRefSetPackage.Literals.SNOMED_MAPPING_REF_SET, 
			SnomedRefSetPackage.Literals.SNOMED_STRUCTURAL_REF_SET, 
			SnomedRefSetPackage.Literals.SNOMED_REGULAR_REF_SET
	));
	
	public static String getTableName(final SnomedRefSetType type) {
		
		switch (type) {
			case SIMPLE:
				return "SNOMEDREFSET_SNOMEDREFSETMEMBER";
			case ATTRIBUTE_VALUE:
				return "SNOMEDREFSET_SNOMEDATTRIBUTEVALUEREFSETMEMBER";
			case LANGUAGE:
				return "SNOMEDREFSET_SNOMEDLANGUAGEREFSETMEMBER";
			case SIMPLE_MAP:
				return "SNOMEDREFSET_SNOMEDSIMPLEMAPREFSETMEMBER";
			case QUERY:
				return "SNOMEDREFSET_SNOMEDQUERYREFSETMEMBER";
			case COMPLEX_MAP: //$FALL-THROUGH$
			case EXTENDED_MAP:
				return "SNOMEDREFSET_SNOMEDCOMPLEXMAPREFSETMEMBER";
			case DESCRIPTION_TYPE:
				return "SNOMEDREFSET_SNOMEDDESCRIPTIONTYPEREFSETMEMBER";
			case CONCRETE_DATA_TYPE:
				return "SNOMEDREFSET_SNOMEDCONCRETEDATATYPEREFSETMEMBER";
			default:
				throw new IllegalArgumentException("Unsupported reference set type: " + type);
		}
	}

	public static String getSpecificCondition(final SnomedRefSetType type) {
		
		switch (type) {
			case COMPLEX_MAP: //$FALL-THROUGH$
			case SIMPLE_MAP: //$FALL-THROUGH$
			case EXTENDED_MAP:
				return "MAPTARGETID";
			case ATTRIBUTE_VALUE:
				return "VALUEID";
			case QUERY:
				return "QUERY";
			case SIMPLE: //$FALL-THROUGH$
			case LANGUAGE:
				return "ACCEPTABILITYID";
			case DESCRIPTION_TYPE:
				return "DESCRIPTIONFORMAT";
			default:
				throw new IllegalArgumentException("Unsupported reference set type: " + type);
		}
	}
	
	/**
	 * Returns with the identifier concept ID of the concrete domain reference set specified by the data type enumeration.
	 * <br>May return with {@code null}.
	 * @param dataType the data type.
	 * @return the identifier concept ID of the SNOMED&nbsp;CT concrete domain reference set.
	 */
	public static String getRefSetId(final com.b2international.snowowl.snomed.mrcm.DataType dataType) {
		return MRCM_DATATYPE_TO_REFSET_MAP.get(dataType);
	}
	
	/**
	 * Returns with the identifier concept ID of the concrete domain reference set specified by the data type enumeration.
	 * <br>May return with {@code null}.
	 * @param dataType the data type.
	 * @return the identifier concept ID of the SNOMED&nbsp;CT concrete domain reference set.
	 */
	public static String getRefSetId(final DataType dataType) {
		return DATATYPE_TO_REFSET_MAP.get(dataType);
	}<commit_msg>SO-607 Removed unused mrcm to snomed refset DataType bimaps and method.<commit_after>/*
 * Copyright 2011-2015 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.snomed.datastore;

import static com.b2international.snowowl.datastore.cdo.CDOUtils.check;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.ATTRIBUTE_VALUE;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.COMPLEX_MAP;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.DESCRIPTION_TYPE;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.EXTENDED_MAP;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.LANGUAGE;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.QUERY;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.SIMPLE;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.SIMPLE_MAP;
import static com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType.get;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.unmodifiableSet;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Map;

import javax.annotation.Nullable;

import org.apache.lucene.document.Document;
import org.eclipse.emf.ecore.EClass;

import com.b2international.commons.BooleanUtils;
import com.b2international.snowowl.core.CoreTerminologyBroker;
import com.b2international.snowowl.core.SnowOwlApplication;
import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.common.SnomedTerminologyComponentConstants;
import com.b2international.snowowl.snomed.datastore.config.SnomedCoreConfiguration;
import com.b2international.snowowl.snomed.datastore.index.mapping.SnomedMappings;
import com.b2international.snowowl.snomed.snomedrefset.DataType;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSet;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetPackage;
import com.b2international.snowowl.snomed.snomedrefset.SnomedRefSetType;
import com.google.common.base.Preconditions;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

/** 
 * Utility class collecting commons operations related to SNOMED CT reference sets. 
 *
 */
public abstract class SnomedRefSetUtil {

	private static final SnomedRefSetType[] REF_SET_VIEW_ROOTS = new SnomedRefSetType[] {
		ATTRIBUTE_VALUE, 
		COMPLEX_MAP, 
		DESCRIPTION_TYPE, 
		QUERY, 
		SIMPLE_MAP, 
		SIMPLE,
		EXTENDED_MAP
	};

	/***
	 * Mapping between the association indication reference set IDs and the human readable names instead of
	 * their CAPITAL CASE preferred terms.
	 */
	public static final Map<String, String> ASSOCIATION_REFSETS = ImmutableMap.<String, String>builder()
			.put(Concepts.REFSET_ALTERNATIVE_ASSOCIATION, "Alternative concept")
			.put(Concepts.REFSET_MOVED_FROM_ASSOCIATION, "Moved from")
			.put(Concepts.REFSET_MOVED_TO_ASSOCIATION, "Moved to")
			.put(Concepts.REFSET_POSSIBLY_EQUIVALENT_TO_ASSOCIATION, "Possibly equivalent to")
			.put(Concepts.REFSET_REFERS_TO_ASSOCIATION, "Refers to")
			.put(Concepts.REFSET_REPLACED_BY_ASSOCIATION, "Replaced by")
			.put(Concepts.REFSET_SAME_AS_ASSOCIATION, "Same as")
			.put(Concepts.REFSET_SIMILAR_TO_ASSOCIATION, "Similar to")
			.put(Concepts.REFSET_WAS_A_ASSOCIATION, "Was a")
			.build();
	
	
	public static final BiMap<DataType, String> DATATYPE_TO_REFSET_MAP = ImmutableBiMap.<DataType, String>builder()
			.put(DataType.BOOLEAN, getCoreConfiguration().getBooleanDatatypeRefsetIdentifier())
			.put(DataType.DATE, getCoreConfiguration().getDatetimeDatatypeRefsetIdentifier())
			.put(DataType.DECIMAL, getCoreConfiguration().getFloatDatatypeRefsetIdentifier())
			.put(DataType.INTEGER, getCoreConfiguration().getIntegerDatatypeRefsetIdentifier())
			.put(DataType.STRING, getCoreConfiguration().getStringDatatypeRefsetIdentifier())
			.build();

	public static final ImmutableSet<DataType> UNSUPPORTED_DATATYPES = ImmutableSet.<DataType> of(DataType.DATE);
	
	private static SnomedCoreConfiguration getCoreConfiguration() {
		return SnowOwlApplication.INSTANCE.getConfiguration().getModuleConfig(SnomedCoreConfiguration.class);
	}
	
	public static EClass getRefSetMemberClass(final SnomedRefSetType type) {
		
		switch (type) {
			case SIMPLE:
				return SnomedRefSetPackage.Literals.SNOMED_REF_SET_MEMBER;
			case ATTRIBUTE_VALUE:
				return SnomedRefSetPackage.Literals.SNOMED_ATTRIBUTE_VALUE_REF_SET_MEMBER;
			case LANGUAGE:
				return SnomedRefSetPackage.Literals.SNOMED_LANGUAGE_REF_SET_MEMBER;
			case COMPLEX_MAP: //$FALL-THROUGH$
			case EXTENDED_MAP:
				return SnomedRefSetPackage.Literals.SNOMED_COMPLEX_MAP_REF_SET_MEMBER;
			case SIMPLE_MAP:
				return SnomedRefSetPackage.Literals.SNOMED_SIMPLE_MAP_REF_SET_MEMBER;
			case QUERY:
				return SnomedRefSetPackage.Literals.SNOMED_QUERY_REF_SET_MEMBER;
			case DESCRIPTION_TYPE:
				return SnomedRefSetPackage.Literals.SNOMED_DESCRIPTION_TYPE_REF_SET_MEMBER;
			case ASSOCIATION:
				return SnomedRefSetPackage.Literals.SNOMED_ASSOCIATION_REF_SET_MEMBER;
			case CONCRETE_DATA_TYPE:
				return SnomedRefSetPackage.Literals.SNOMED_CONCRETE_DATA_TYPE_REF_SET_MEMBER;
			case MODULE_DEPENDENCY:
				return SnomedRefSetPackage.Literals.SNOMED_MODULE_DEPENDENCY_REF_SET_MEMBER;
			default:
				throw new IllegalArgumentException("Unsupported reference set type: " + type);
		}
	}
	
	public static final Collection<EClass> REFSET_CLASSES = unmodifiableSet(newHashSet(
			SnomedRefSetPackage.Literals.SNOMED_MAPPING_REF_SET, 
			SnomedRefSetPackage.Literals.SNOMED_STRUCTURAL_REF_SET, 
			SnomedRefSetPackage.Literals.SNOMED_REGULAR_REF_SET
	));
	
	public static String getTableName(final SnomedRefSetType type) {
		
		switch (type) {
			case SIMPLE:
				return "SNOMEDREFSET_SNOMEDREFSETMEMBER";
			case ATTRIBUTE_VALUE:
				return "SNOMEDREFSET_SNOMEDATTRIBUTEVALUEREFSETMEMBER";
			case LANGUAGE:
				return "SNOMEDREFSET_SNOMEDLANGUAGEREFSETMEMBER";
			case SIMPLE_MAP:
				return "SNOMEDREFSET_SNOMEDSIMPLEMAPREFSETMEMBER";
			case QUERY:
				return "SNOMEDREFSET_SNOMEDQUERYREFSETMEMBER";
			case COMPLEX_MAP: //$FALL-THROUGH$
			case EXTENDED_MAP:
				return "SNOMEDREFSET_SNOMEDCOMPLEXMAPREFSETMEMBER";
			case DESCRIPTION_TYPE:
				return "SNOMEDREFSET_SNOMEDDESCRIPTIONTYPEREFSETMEMBER";
			case CONCRETE_DATA_TYPE:
				return "SNOMEDREFSET_SNOMEDCONCRETEDATATYPEREFSETMEMBER";
			default:
				throw new IllegalArgumentException("Unsupported reference set type: " + type);
		}
	}

	public static String getSpecificCondition(final SnomedRefSetType type) {
		
		switch (type) {
			case COMPLEX_MAP: //$FALL-THROUGH$
			case SIMPLE_MAP: //$FALL-THROUGH$
			case EXTENDED_MAP:
				return "MAPTARGETID";
			case ATTRIBUTE_VALUE:
				return "VALUEID";
			case QUERY:
				return "QUERY";
			case SIMPLE: //$FALL-THROUGH$
			case LANGUAGE:
				return "ACCEPTABILITYID";
			case DESCRIPTION_TYPE:
				return "DESCRIPTIONFORMAT";
			default:
				throw new IllegalArgumentException("Unsupported reference set type: " + type);
		}
	}
	
	/**
	 * Returns with the identifier concept ID of the concrete domain reference set specified by the data type enumeration.
	 * <br>May return with {@code null}.
	 * @param dataType the data type.
	 * @return the identifier concept ID of the SNOMED&nbsp;CT concrete domain reference set.
	 */
	public static String getRefSetId(final DataType dataType) {
		return DATATYPE_TO_REFSET_MAP.get(dataType);
	}
{'fraction_non_alphanumeric': 0.084054323318127, 'fraction_numerical': 0.004928949714225788, 'mean_word_length': 5.727336860670194, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '837042', 'n_tokens_mistral': 7056, 'n_tokens_neox': 6391, 'n_words': 1144}