<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.escg;

import java.io.IOException;

import com.b2international.collections.PrimitiveSets;
import com.b2international.collections.longs.LongSet;
import com.b2international.commons.collect.LongSets;
import com.b2international.index.Hits;
import com.b2international.index.query.Expression;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.b2international.index.revision.RevisionSearcher;
import com.b2international.snowowl.core.api.SnowowlRuntimeException;
import com.b2international.snowowl.core.exceptions.NotImplementedException;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry;
import com.b2international.snowowl.snomed.dsl.query.ast.AndClause;
import com.b2international.snowowl.snomed.dsl.query.ast.AttributeClause;
import com.b2international.snowowl.snomed.dsl.query.ast.ConceptRef;
import com.b2international.snowowl.snomed.dsl.query.ast.NotClause;
import com.b2international.snowowl.snomed.dsl.query.ast.NumericDataClause;
import com.b2international.snowowl.snomed.dsl.query.ast.NumericDataGroupClause;
import com.b2international.snowowl.snomed.dsl.query.ast.OrClause;
import com.b2international.snowowl.snomed.dsl.query.ast.RValue;
import com.b2international.snowowl.snomed.dsl.query.ast.RefSet;
import com.b2international.snowowl.snomed.dsl.query.ast.SubExpression;

/**
 * Evaluator service for getting SNOMED&nbsp;CT concept IDs.
 * @deprecated - see {@link IEscgQueryEvaluatorService}
 */
public class ConceptIdQueryEvaluator2 implements IQueryEvaluator<LongSet, com.b2international.snowowl.snomed.dsl.query.RValue> {
	
	private final RevisionSearcher searcher;
	private final IndexQueryQueryEvaluator queryEvaluator;

	public ConceptIdQueryEvaluator2(RevisionSearcher searcher) {
		this.searcher = searcher;
		this.queryEvaluator = new IndexQueryQueryEvaluator();
	}

	/**
	 * Evaluates the specified expression and returns with a set of SNOMED&nbsp;CT concept IDs representing the matching results.
	 */
	@Override
	public LongSet evaluate(final com.b2international.snowowl.snomed.dsl.query.RValue expression) {
		
		if (expression instanceof ConceptRef) {
			final Expression conceptRefExpression = queryEvaluator.evaluate(expression);
			return getMatchingConceptIds(conceptRefExpression);
		} else if (expression instanceof AttributeClause) {
			
			final AttributeClause clause = (AttributeClause) expression;
			
			final LongSet typeIds = evaluate(clause.getLeft());
			final LongSet destinationIds = evaluate(clause.getRight());

			final Query<SnomedRelationshipIndexEntry> query = Query.select(SnomedRelationshipIndexEntry.class)
					.where(Expressions.builder()
							.must(SnomedRelationshipIndexEntry.Expressions.active())
							.must(SnomedRelationshipIndexEntry.Expressions.typeIds(LongSets.toStringSet(typeIds)))
							.must(SnomedRelationshipIndexEntry.Expressions.destinationIds(LongSets.toStringSet(destinationIds)))
							.build())
					.limit(Integer.MAX_VALUE)
					.build();
			try {
				final Hits<SnomedRelationshipIndexEntry> hits = searcher.search(query);
				final LongSet sourceIds = PrimitiveSets.newLongOpenHashSet(hits.getHits().size());
				
				for (SnomedRelationshipIndexEntry hit : hits) {
					sourceIds.add(Long.parseLong(hit.getSourceId()));
				}
				
				return sourceIds;
			} catch (IOException e) {
				throw new SnowowlRuntimeException(e);
			}
			
		} else if (expression instanceof RefSet) {
			return getMatchingConceptIds(queryEvaluator.evaluate(expression));
		} else if (expression instanceof SubExpression) {
			return evaluate(((SubExpression) expression).getValue());
		} else if (expression instanceof OrClause) {<commit_msg>SO-1782: ESCG should query inferred/additional relationships only<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.escg;

import java.io.IOException;

import com.b2international.collections.PrimitiveSets;
import com.b2international.collections.longs.LongSet;
import com.b2international.commons.collect.LongSets;
import com.b2international.index.Hits;
import com.b2international.index.query.Expression;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.b2international.index.revision.RevisionSearcher;
import com.b2international.snowowl.core.api.SnowowlRuntimeException;
import com.b2international.snowowl.core.exceptions.NotImplementedException;
import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedConceptDocument;
import com.b2international.snowowl.snomed.datastore.index.entry.SnomedRelationshipIndexEntry;
import com.b2international.snowowl.snomed.dsl.query.ast.AndClause;
import com.b2international.snowowl.snomed.dsl.query.ast.AttributeClause;
import com.b2international.snowowl.snomed.dsl.query.ast.ConceptRef;
import com.b2international.snowowl.snomed.dsl.query.ast.NotClause;
import com.b2international.snowowl.snomed.dsl.query.ast.NumericDataClause;
import com.b2international.snowowl.snomed.dsl.query.ast.NumericDataGroupClause;
import com.b2international.snowowl.snomed.dsl.query.ast.OrClause;
import com.b2international.snowowl.snomed.dsl.query.ast.RValue;
import com.b2international.snowowl.snomed.dsl.query.ast.RefSet;
import com.b2international.snowowl.snomed.dsl.query.ast.SubExpression;
import com.google.common.collect.ImmutableSet;

/**
 * Evaluator service for getting SNOMED&nbsp;CT concept IDs.
 * @deprecated - see {@link IEscgQueryEvaluatorService}
 */
public class ConceptIdQueryEvaluator2 implements IQueryEvaluator<LongSet, com.b2international.snowowl.snomed.dsl.query.RValue> {
	
	private final RevisionSearcher searcher;
	private final IndexQueryQueryEvaluator queryEvaluator;

	public ConceptIdQueryEvaluator2(RevisionSearcher searcher) {
		this.searcher = searcher;
		this.queryEvaluator = new IndexQueryQueryEvaluator();
	}

	/**
	 * Evaluates the specified expression and returns with a set of SNOMED&nbsp;CT concept IDs representing the matching results.
	 */
	@Override
	public LongSet evaluate(final com.b2international.snowowl.snomed.dsl.query.RValue expression) {
		
		if (expression instanceof ConceptRef) {
			final Expression conceptRefExpression = queryEvaluator.evaluate(expression);
			return getMatchingConceptIds(conceptRefExpression);
		} else if (expression instanceof AttributeClause) {
			
			final AttributeClause clause = (AttributeClause) expression;
			
			final LongSet typeIds = evaluate(clause.getLeft());
			final LongSet destinationIds = evaluate(clause.getRight());

			final Query<SnomedRelationshipIndexEntry> query = Query.select(SnomedRelationshipIndexEntry.class)
					.where(Expressions.builder()
							.must(SnomedRelationshipIndexEntry.Expressions.active())
							.must(SnomedRelationshipIndexEntry.Expressions.typeIds(LongSets.toStringSet(typeIds)))
							.must(SnomedRelationshipIndexEntry.Expressions.characteristicTypeIds(ImmutableSet.of(Concepts.INFERRED_RELATIONSHIP, Concepts.ADDITIONAL_RELATIONSHIP)))
							.must(SnomedRelationshipIndexEntry.Expressions.destinationIds(LongSets.toStringSet(destinationIds)))
							.build())
					.limit(Integer.MAX_VALUE)
					.build();
			try {
				final Hits<SnomedRelationshipIndexEntry> hits = searcher.search(query);
				final LongSet sourceIds = PrimitiveSets.newLongOpenHashSet(hits.getHits().size());
				
				for (SnomedRelationshipIndexEntry hit : hits) {
					sourceIds.add(Long.parseLong(hit.getSourceId()));
				}
				
				return sourceIds;
			} catch (IOException e) {
				throw new SnowowlRuntimeException(e);
			}
			
		} else if (expression instanceof RefSet) {
			return getMatchingConceptIds(queryEvaluator.evaluate(expression));
		} else if (expression instanceof SubExpression) {
			return evaluate(((SubExpression) expression).getValue());
		} else if (expression instanceof OrClause) {
{'fraction_non_alphanumeric': 0.09280567802989569, 'fraction_numerical': 0.009355844714485428, 'mean_word_length': 5.400550584996559, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2283719', 'n_tokens_mistral': 3100, 'n_tokens_neox': 2765, 'n_words': 554}