<commit_before>/*
 * Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * 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 org.hawkular.inventory.impl.tinkerpop;

import static com.tinkerpop.gremlin.java.GremlinFluentUtility.optimizePipelineForQuery;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hawkular.inventory.api.FilterFragment;
import org.hawkular.inventory.api.Query;
import org.hawkular.inventory.api.QueryFragment;
import org.hawkular.inventory.api.filters.Contained;
import org.hawkular.inventory.api.filters.Defined;
import org.hawkular.inventory.api.filters.Filter;
import org.hawkular.inventory.api.filters.Incorporated;
import org.hawkular.inventory.api.filters.Marker;
import org.hawkular.inventory.api.filters.RecurseFilter;
import org.hawkular.inventory.api.filters.Related;
import org.hawkular.inventory.api.filters.RelationWith;
import org.hawkular.inventory.api.filters.SwitchElementType;
import org.hawkular.inventory.api.filters.With;
import org.hawkular.inventory.base.spi.NoopFilter;

import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.filter.IntervalFilterPipe;
import com.tinkerpop.pipes.filter.PropertyFilterPipe;
import com.tinkerpop.pipes.filter.RangeFilterPipe;
import com.tinkerpop.pipes.transform.VertexQueryPipe;
import com.tinkerpop.pipes.transform.VerticesEdgesPipe;

/**
 * A filter applicator applies a filter to a Gremlin query.
 *
 * @author <NAME>
 * @author <NAME>
 * @see FilterVisitor
 * @since 0.0.1
 */
abstract class FilterApplicator<T extends Filter> {
    private static Map<Class<? extends Filter>, Class<? extends FilterApplicator<?>>> applicators;

    static {
        applicators = new HashMap<>();
        applicators.put(Related.class, RelatedApplicator.class);
        applicators.put(Contained.class, RelatedApplicator.class);
        applicators.put(Defined.class, RelatedApplicator.class);
        applicators.put(Incorporated.class, RelatedApplicator.class);
        applicators.put(With.Ids.class, WithIdsApplicator.class);
        applicators.put(With.Types.class, WithTypesApplicator.class);
        applicators.put(With.PropertyValues.class, WithPropertyValuesApplicator.class);
        applicators.put(RelationWith.Ids.class, RelationWithIdsApplicator.class);
        applicators.put(RelationWith.PropertyValues.class, RelationWithPropertiesApplicator.class);
        applicators.put(RelationWith.SourceOfType.class, RelationWithSourcesOfTypesApplicator.class);
        applicators.put(RelationWith.TargetOfType.class, RelationWithTargetsOfTypesApplicator.class);
        applicators.put(RelationWith.SourceOrTargetOfType.class, RelationWithSourcesOrTargetsOfTypesApplicator.class);
        applicators.put(SwitchElementType.class, SwitchElementTypeApplicator.class);
        applicators.put(NoopFilter.class, NoopApplicator.class);
        applicators.put(With.CanonicalPaths.class, CanonicalPathApplicator.class);
        applicators.put(With.RelativePaths.class, RelativePathApplicator.class);
        applicators.put(Marker.class, MarkerApplicator.class);
        applicators.put(With.DataAt.class, DataAtApplicator.class);
        applicators.put(With.DataValued.class, DataValuedApplicator.class);
        applicators.put(With.DataOfTypes.class, DataOfTypesApplicator.class);
        applicators.put(RecurseFilter.class, RecurseApplicator.class);
        applicators.put(With.SameIdentityHash.class, SameIdentityHashApplicator.class);
    }

    protected final T filter;
    protected final FilterVisitor visitor = new FilterVisitor();

    private FilterApplicator(T f) {
        this.filter = f;
    }

    public static FilterApplicator of(Filter filter) {
        if (filter == null) {
            throw new IllegalArgumentException("filter == null");
        }
        Class<? extends Filter> filterClazz = filter.getClass();
        Class<? extends FilterApplicator<?>> applicatorClazz = applicators.get(filterClazz);
        if (applicatorClazz == null) {
            throw new IllegalArgumentException("Unsupported filter type " + filterClazz);
        }
        Constructor<? extends FilterApplicator<?>> constructor = null;
        try {
            constructor = applicatorClazz.getDeclaredConstructor(filterClazz);
        } catch (NoSuchMethodException e) {
            try {
                // Contained, Defined, Owned
                constructor = applicatorClazz.getDeclaredConstructor(filterClazz.getSuperclass());
            } catch (NoSuchMethodException e1) {
                throw new IllegalArgumentException("Unable to create an instance of " + applicatorClazz);
            }
        }
        try {<commit_msg>Recognize the Names filter in Tinkerpop backend. D'oh!<commit_after>/*
 * Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * 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 org.hawkular.inventory.impl.tinkerpop;

import static com.tinkerpop.gremlin.java.GremlinFluentUtility.optimizePipelineForQuery;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hawkular.inventory.api.FilterFragment;
import org.hawkular.inventory.api.Query;
import org.hawkular.inventory.api.QueryFragment;
import org.hawkular.inventory.api.filters.Contained;
import org.hawkular.inventory.api.filters.Defined;
import org.hawkular.inventory.api.filters.Filter;
import org.hawkular.inventory.api.filters.Incorporated;
import org.hawkular.inventory.api.filters.Marker;
import org.hawkular.inventory.api.filters.RecurseFilter;
import org.hawkular.inventory.api.filters.Related;
import org.hawkular.inventory.api.filters.RelationWith;
import org.hawkular.inventory.api.filters.SwitchElementType;
import org.hawkular.inventory.api.filters.With;
import org.hawkular.inventory.base.spi.NoopFilter;

import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.filter.IntervalFilterPipe;
import com.tinkerpop.pipes.filter.PropertyFilterPipe;
import com.tinkerpop.pipes.filter.RangeFilterPipe;
import com.tinkerpop.pipes.transform.VertexQueryPipe;
import com.tinkerpop.pipes.transform.VerticesEdgesPipe;

/**
 * A filter applicator applies a filter to a Gremlin query.
 *
 * @author <NAME>
 * @author <NAME>
 * @see FilterVisitor
 * @since 0.0.1
 */
abstract class FilterApplicator<T extends Filter> {
    private static Map<Class<? extends Filter>, Class<? extends FilterApplicator<?>>> applicators;

    static {
        applicators = new HashMap<>();
        applicators.put(Related.class, RelatedApplicator.class);
        applicators.put(Contained.class, RelatedApplicator.class);
        applicators.put(Defined.class, RelatedApplicator.class);
        applicators.put(Incorporated.class, RelatedApplicator.class);
        applicators.put(With.Ids.class, WithIdsApplicator.class);
        applicators.put(With.Types.class, WithTypesApplicator.class);
        applicators.put(With.PropertyValues.class, WithPropertyValuesApplicator.class);
        applicators.put(RelationWith.Ids.class, RelationWithIdsApplicator.class);
        applicators.put(RelationWith.PropertyValues.class, RelationWithPropertiesApplicator.class);
        applicators.put(RelationWith.SourceOfType.class, RelationWithSourcesOfTypesApplicator.class);
        applicators.put(RelationWith.TargetOfType.class, RelationWithTargetsOfTypesApplicator.class);
        applicators.put(RelationWith.SourceOrTargetOfType.class, RelationWithSourcesOrTargetsOfTypesApplicator.class);
        applicators.put(SwitchElementType.class, SwitchElementTypeApplicator.class);
        applicators.put(NoopFilter.class, NoopApplicator.class);
        applicators.put(With.CanonicalPaths.class, CanonicalPathApplicator.class);
        applicators.put(With.RelativePaths.class, RelativePathApplicator.class);
        applicators.put(Marker.class, MarkerApplicator.class);
        applicators.put(With.DataAt.class, DataAtApplicator.class);
        applicators.put(With.DataValued.class, DataValuedApplicator.class);
        applicators.put(With.DataOfTypes.class, DataOfTypesApplicator.class);
        applicators.put(RecurseFilter.class, RecurseApplicator.class);
        applicators.put(With.SameIdentityHash.class, SameIdentityHashApplicator.class);
        applicators.put(With.Names.class, NamesApplicator.class);
    }

    protected final T filter;
    protected final FilterVisitor visitor = new FilterVisitor();

    private FilterApplicator(T f) {
        this.filter = f;
    }

    public static FilterApplicator of(Filter filter) {
        if (filter == null) {
            throw new IllegalArgumentException("filter == null");
        }
        Class<? extends Filter> filterClazz = filter.getClass();
        Class<? extends FilterApplicator<?>> applicatorClazz = applicators.get(filterClazz);
        if (applicatorClazz == null) {
            throw new IllegalArgumentException("Unsupported filter type " + filterClazz);
        }
        Constructor<? extends FilterApplicator<?>> constructor = null;
        try {
            constructor = applicatorClazz.getDeclaredConstructor(filterClazz);
        } catch (NoSuchMethodException e) {
            try {
                // Contained, Defined, Owned
                constructor = applicatorClazz.getDeclaredConstructor(filterClazz.getSuperclass());
            } catch (NoSuchMethodException e1) {
                throw new IllegalArgumentException("Unable to create an instance of " + applicatorClazz);
            }
        }
        try {
{'fraction_non_alphanumeric': 0.09513742071881606, 'fraction_numerical': 0.002941446824156632, 'mean_word_length': 4.0769948670088665, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '540762', 'n_tokens_mistral': 3157, 'n_tokens_neox': 3088, 'n_words': 631}