/*
 * Copyright 2019 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.reasoner.external;

import java.util.Collection;
import java.util.List;

import com.b2international.snowowl.snomed.datastore.index.taxonomy.IInternalSctIdMultimap;
import com.b2international.snowowl.snomed.datastore.index.taxonomy.IInternalSctIdSet;
import com.b2international.snowowl.snomed.datastore.index.taxonomy.IReasonerTaxonomy;
import com.b2international.snowowl.snomed.datastore.index.taxonomy.InternalIdMap;
import com.b2international.snowowl.snomed.datastore.index.taxonomy.InternalSctIdMultimap;
import com.b2international.snowowl.snomed.datastore.index.taxonomy.InternalSctIdMultimap.Builder;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

/**
 *
 * @since 6.12.2
 */
public class ExternalReasonerTaxonomy implements IReasonerTaxonomy {

	private Multimap<String, String> equivalentConceptIds;

	public ExternalReasonerTaxonomy(Collection<List<String>> equivalentConceptLines) {
		this.equivalentConceptIds = processEquivalentConceptLines(equivalentConceptLines);
	}
	
	private Multimap<String, String> processEquivalentConceptLines(Collection<List<String>> equivalentConceptLines) {
		
		ArrayListMultimap<String, String> equivalentConcepts = ArrayListMultimap.create();
		
		equivalentConceptLines.forEach(elements -> {
			
			String referencedComponent = elements.get(5); // equivalent concept
			String mapTarget = elements.get(6); // id of equivalent set
			
			equivalentConcepts.put(mapTarget, referencedComponent);
			
		});
		
		return equivalentConcepts;
		
	}

	@Override
	public IInternalSctIdSet getUnsatisfiableConcepts() {
		return IInternalSctIdSet.EMPTY;
	}

	@Override
	public IInternalSctIdMultimap getEquivalentConcepts() {
		Builder builder = InternalSctIdMultimap.builder(InternalIdMap.builder()
				.addAll(equivalentConceptIds.keySet())
				.addAll(equivalentConceptIds.values())
				.build());
		equivalentConceptIds.asMap().forEach( (id,values) -> builder.putAll(id, values));
		return builder.build();
	}

}

@@ -679,16 +679,74 @@
 ernal;%0A%0A
+import static com.google.common.collect.Maps.newHashMap;%0A%0A
 import j
@@ -788,16 +788,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -1973,19 +1973,79 @@
 eate();%0A
+%09%09Map%3CString, String%3E knownEquivalentSetIds = newHashMap();%0A
 %09%09%0A
-
 %09%09equiva
@@ -2232,29 +2232,111 @@
 %0A%09%09%09
-equivalentConcepts.pu
+if (knownEquivalentSetIds.containsKey(mapTarget)) %7B%0A%09%09%09%09equivalentConcepts.put(knownEquivalentSetIds.ge
 t(ma
@@ -2342,16 +2342,17 @@
 apTarget
+)
 , refere
@@ -2367,16 +2367,96 @@
 onent);%0A
+%09%09%09%7D else %7B%0A%09%09%09%09knownEquivalentSetIds.put(mapTarget, referencedComponent);%0A%09%09%09%7D%0A
 %09%09%09%0A%09%09%7D)

Fix equivalent concept set processing