<commit_before>/*
 * Copyright 2002-2015 The Jamocha Team
 * 
 * 
 * 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.jamocha.org/
 * 
 * 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.jamocha.rating.fraj;

import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
import static org.jamocha.util.Lambdas.toHashSet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.DoubleBinaryOperator;
import java.util.function.Function;
import java.util.stream.DoubleStream;
import java.util.stream.DoubleStream.Builder;
import java.util.stream.Stream;

import lombok.AllArgsConstructor;

import org.apache.commons.lang3.tuple.Pair;
import org.jamocha.dn.SideEffectFunctionToNetwork;
import org.jamocha.dn.nodes.Edge;
import org.jamocha.dn.nodes.Node;
import org.jamocha.dn.nodes.TerminalNode;
import org.jamocha.filter.Path;
import org.jamocha.filter.PathCollector;
import org.jamocha.filter.PathFilter;
import org.jamocha.filter.PathFilterList;
import org.jamocha.filter.PathNodeFilterSet;
import org.jamocha.rating.StatisticsProvider;
import org.jamocha.rating.StatisticsProvider.Data;
import org.jamocha.util.Lambdas;

import com.atlassian.fugue.Iterables;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

/**
 * Actual implementation of the Rating algorithm.
 * 
 * @author <NAME> <<EMAIL>>
 */
@AllArgsConstructor
public class RatingProvider implements org.jamocha.rating.RatingProvider {
	final DoubleBinaryOperator cpuAndMemCostCombiner;

	@Override
	public double rateVirtualAlpha(final StatisticsProvider statisticsProvider, final PathNodeFilterSet toRate,
			final Set<PathFilterList> preNetwork) {
		return calculateAlphaInformation(statisticsProvider, toRate, preNetwork, 0, 1);
	}

	@Override
	public double rateMaterialisedAlpha(final StatisticsProvider statisticsProvider, final PathNodeFilterSet toRate,
			final Set<PathFilterList> preNetwork) {
		return calculateAlphaInformation(statisticsProvider, toRate, preNetwork, 1, 2);
	}

	private double calculateAlphaInformation(final StatisticsProvider statisticsProvider,
			final PathNodeFilterSet toRate, final Set<PathFilterList> preNetwork, final int memMulti, final int cpuMulti) {
		final Data data = statisticsProvider.getData(preNetwork);
		final double selectivity = statisticsProvider.getSelectivity(toRate, preNetwork);
		final double newRows = selectivity * data.getRowCount();
		final double newFinsert = selectivity * data.getFinsert();
		final double newFdelete = selectivity * data.getFdelete();
		final Data newData = new Data(newFinsert, newFdelete, newRows, 1);
		final HashSet<PathFilterList> newPreNetwork = new HashSet<>(preNetwork);
		newPreNetwork.add(toRate);
		statisticsProvider.setData(newPreNetwork, newData);
		return cpuAndMemCostCombiner.applyAsDouble(memMulti * newData.getRowCount(), cpuMulti
				* (newData.getFinsert() + newData.getFdelete()));
	}

	@Override
	public double rateBeta(
			final StatisticsProvider statisticsProvider,
			final PathNodeFilterSet toRate,
			final Map<Set<PathFilterList>, List<Pair<List<Set<PathFilterList>>, List<PathFilter>>>> componentToJoinOrder,
			final Map<Path, Set<PathFilterList>> pathToPreNetworkComponents) {
		if (toRate.getNegativeExistentialPaths().isEmpty() && toRate.getPositiveExistentialPaths().isEmpty()) {
			return rateBetaWithoutExistentials(statisticsProvider, toRate, componentToJoinOrder,
					pathToPreNetworkComponents);
		}
		return rateBetaWithExistentials(statisticsProvider, toRate, componentToJoinOrder, pathToPreNetworkComponents);
	}

	private double cardenas(final double m, final double k) {
		if (k <= 1)
			return k;
		return m * (1 - Math.pow((1 - 1 / m), k));
	}

	private double jc(final StatisticsProvider statisticsProvider, final double jsf, final Data r, final double size) {
		return cardenas(m(statisticsProvider, r), size * r.getRowCount() * jsf);
	}

	private double m(final StatisticsProvider statisticsProvider, final Data data) {
		return m(data.getRowCount(), statisticsProvider.getPageSize() / data.getTupleSize());
	}

	private double m(final double rowCount, final double tuplesPerPage) {
		return Math.ceil(rowCount / tuplesPerPage);
	}

	private double calcBetaUnfilteredSize(
			final StatisticsProvider statisticsProvider,
			final Map<Set<PathFilterList>, List<Pair<List<Set<PathFilterList>>, List<PathFilter>>>> componentToJoinOrder,
			final Map<Path, Set<PathFilterList>> pathToPreNetworkComponents,
			final Set<Set<PathFilterList>> regularComponents) {
		double size = 0;
		for (final Set<PathFilterList> component : regularComponents) {
			final Data data = statisticsProvider.getData(component);
			size +=
					data.getRowCount()
							* joinSize(statisticsProvider, component, componentToJoinOrder.get(component),
									regularComponents, pathToPreNetworkComponents);
		}
		return size / regularComponents.size();
	}<commit_msg>cárdenas return 0 if m is 0<commit_after>/*
 * Copyright 2002-2015 The Jamocha Team
 * 
 * 
 * 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.jamocha.org/
 * 
 * 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.jamocha.rating.fraj;

import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
import static org.jamocha.util.Lambdas.toHashSet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.DoubleBinaryOperator;
import java.util.function.Function;
import java.util.stream.DoubleStream;
import java.util.stream.DoubleStream.Builder;
import java.util.stream.Stream;

import lombok.AllArgsConstructor;

import org.apache.commons.lang3.tuple.Pair;
import org.jamocha.dn.SideEffectFunctionToNetwork;
import org.jamocha.dn.nodes.Edge;
import org.jamocha.dn.nodes.Node;
import org.jamocha.dn.nodes.TerminalNode;
import org.jamocha.filter.Path;
import org.jamocha.filter.PathCollector;
import org.jamocha.filter.PathFilter;
import org.jamocha.filter.PathFilterList;
import org.jamocha.filter.PathNodeFilterSet;
import org.jamocha.rating.StatisticsProvider;
import org.jamocha.rating.StatisticsProvider.Data;
import org.jamocha.util.Lambdas;

import com.atlassian.fugue.Iterables;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

/**
 * Actual implementation of the Rating algorithm.
 * 
 * @author <NAME> <<EMAIL>>
 */
@AllArgsConstructor
public class RatingProvider implements org.jamocha.rating.RatingProvider {
	final DoubleBinaryOperator cpuAndMemCostCombiner;

	@Override
	public double rateVirtualAlpha(final StatisticsProvider statisticsProvider, final PathNodeFilterSet toRate,
			final Set<PathFilterList> preNetwork) {
		return calculateAlphaInformation(statisticsProvider, toRate, preNetwork, 0, 1);
	}

	@Override
	public double rateMaterialisedAlpha(final StatisticsProvider statisticsProvider, final PathNodeFilterSet toRate,
			final Set<PathFilterList> preNetwork) {
		return calculateAlphaInformation(statisticsProvider, toRate, preNetwork, 1, 2);
	}

	private double calculateAlphaInformation(final StatisticsProvider statisticsProvider,
			final PathNodeFilterSet toRate, final Set<PathFilterList> preNetwork, final int memMulti, final int cpuMulti) {
		final Data data = statisticsProvider.getData(preNetwork);
		final double selectivity = statisticsProvider.getSelectivity(toRate, preNetwork);
		final double newRows = selectivity * data.getRowCount();
		final double newFinsert = selectivity * data.getFinsert();
		final double newFdelete = selectivity * data.getFdelete();
		final Data newData = new Data(newFinsert, newFdelete, newRows, 1);
		final HashSet<PathFilterList> newPreNetwork = new HashSet<>(preNetwork);
		newPreNetwork.add(toRate);
		statisticsProvider.setData(newPreNetwork, newData);
		return cpuAndMemCostCombiner.applyAsDouble(memMulti * newData.getRowCount(), cpuMulti
				* (newData.getFinsert() + newData.getFdelete()));
	}

	@Override
	public double rateBeta(
			final StatisticsProvider statisticsProvider,
			final PathNodeFilterSet toRate,
			final Map<Set<PathFilterList>, List<Pair<List<Set<PathFilterList>>, List<PathFilter>>>> componentToJoinOrder,
			final Map<Path, Set<PathFilterList>> pathToPreNetworkComponents) {
		if (toRate.getNegativeExistentialPaths().isEmpty() && toRate.getPositiveExistentialPaths().isEmpty()) {
			return rateBetaWithoutExistentials(statisticsProvider, toRate, componentToJoinOrder,
					pathToPreNetworkComponents);
		}
		return rateBetaWithExistentials(statisticsProvider, toRate, componentToJoinOrder, pathToPreNetworkComponents);
	}

	private double cardenas(final double m, final double k) {
		if (k <= 1)
			return k;
		if (m == 0) {
			return 0;
		}
		return m * (1 - Math.pow((1 - 1 / m), k));
	}

	private double jc(final StatisticsProvider statisticsProvider, final double jsf, final Data r, final double size) {
		return cardenas(m(statisticsProvider, r), size * r.getRowCount() * jsf);
	}

	private double m(final StatisticsProvider statisticsProvider, final Data data) {
		return m(data.getRowCount(), statisticsProvider.getPageSize() / data.getTupleSize());
	}

	private double m(final double rowCount, final double tuplesPerPage) {
		return Math.ceil(rowCount / tuplesPerPage);
	}

	private double calcBetaUnfilteredSize(
			final StatisticsProvider statisticsProvider,
			final Map<Set<PathFilterList>, List<Pair<List<Set<PathFilterList>>, List<PathFilter>>>> componentToJoinOrder,
			final Map<Path, Set<PathFilterList>> pathToPreNetworkComponents,
			final Set<Set<PathFilterList>> regularComponents) {
		double size = 0;
		for (final Set<PathFilterList> component : regularComponents) {
			final Data data = statisticsProvider.getData(component);
			size +=
					data.getRowCount()
							* joinSize(statisticsProvider, component, componentToJoinOrder.get(component),
									regularComponents, pathToPreNetworkComponents);
		}
		return size / regularComponents.size();
	}
{'fraction_non_alphanumeric': 0.09351230425055929, 'fraction_numerical': 0.004116331096196868, 'mean_word_length': 5.7244283995186525, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 61, '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': '818056', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3155, 'n_words': 833}