<commit_before>/*
 * ARX: Powerful Data Anonymization
 * Copyright (C) 2012 - 2014 <NAME>, <NAME>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx.algorithm;

import org.deidentifier.arx.ARXConfiguration;
import org.deidentifier.arx.criteria.DPresence;
import org.deidentifier.arx.criteria.Inclusion;
import org.deidentifier.arx.criteria.LDiversity;
import org.deidentifier.arx.criteria.TCloseness;
import org.deidentifier.arx.framework.check.INodeChecker;
import org.deidentifier.arx.framework.check.history.History;
import org.deidentifier.arx.framework.lattice.Lattice;
import org.deidentifier.arx.framework.lattice.Node;
import org.deidentifier.arx.framework.lattice.NodeAction;
import org.deidentifier.arx.framework.lattice.NodeAction.NodeActionConstant;
import org.deidentifier.arx.framework.lattice.NodeAction.NodeActionInverse;
import org.deidentifier.arx.metric.Metric;

/**
 * This class provides a static method for instantiating the FLASH algorithm
 * 
 * @author <NAME>
 * @author <NAME>
 */
public class FLASHAlgorithm {
    
    /**
     * Monotonicity
     */
    private static enum Monotonicity {
        FULL,
        PARTIAL,
        NONE
    }

    /**
     * Creates a new instance of the FLASH algorithm
     * @param lattice
     * @param checker
     * @param strategy
     * @return
     */
    public static AbstractAlgorithm create(final Lattice lattice,
                                           final INodeChecker checker,
                                           final FLASHStrategy strategy){

        // Init
        ARXConfiguration config = checker.getConfiguration();
        Metric<?> metric = checker.getMetric();
        
        // NOTE: 
        // - If we assume practical monotonicity then we assume
        //   monotonicity for both criterion AND metric
        // - Without suppression we assume monotonicity for all criteria
        // - Without suppression we assume monotonicity for all metrics
        
        // Determine monotonicity of metric
        Monotonicity monotonicityMetric;
        if (metric.isMonotonic() || config.getMaxOutliers() == 0d || config.isPracticalMonotonicity()) {
            monotonicityMetric = Monotonicity.FULL;
        } else {
            monotonicityMetric = Monotonicity.NONE;
        }
        
        // Determine type of criteria to be enforced
        boolean containsTCloseness = config.containsCriterion(TCloseness.class);
        boolean containsLDiversity = config.containsCriterion(LDiversity.class);
        boolean containsDPresence = false; 
        for (DPresence d : config.getCriteria(DPresence.class)) {
            containsDPresence |= !(d instanceof Inclusion);
        }
        
        // Determine monotonicity of criteria
        Monotonicity monotonicityCriteria;
        if (config.getMaxOutliers() == 0d || config.isPracticalMonotonicity()) {
            monotonicityCriteria = Monotonicity.FULL;
        } else {
            if (config.getMinimalGroupSize() != Integer.MAX_VALUE) {
                if (containsTCloseness || containsLDiversity || containsDPresence) {
                    monotonicityCriteria = Monotonicity.PARTIAL;
                } else {
                    monotonicityCriteria = Monotonicity.FULL;
                    
                }
            } else {
                monotonicityCriteria = Monotonicity.NONE;
            }
        }
        
        // ******************************
        // CASE 1
        // ******************************
        if (monotonicityCriteria == Monotonicity.FULL && monotonicityMetric == Monotonicity.FULL) {
            return createFullFull(lattice, checker, strategy);
        }
        
        
        // ******************************
        // CASE 2
        // ******************************
        if (monotonicityCriteria == Monotonicity.FULL && monotonicityMetric == Monotonicity.NONE) {
            return createFullNone(lattice, checker, strategy);
        }
        
        // ******************************
        // CASE 3
        // ******************************
        if (monotonicityCriteria == Monotonicity.PARTIAL && monotonicityMetric == Monotonicity.FULL) {
            return createPartialFull(lattice, checker, strategy);<commit_msg>Prepare pruning based on monotonic sub-metric<commit_after>/*
 * ARX: Powerful Data Anonymization
 * Copyright (C) 2012 - 2014 <NAME>, <NAME>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx.algorithm;

import org.deidentifier.arx.ARXConfiguration;
import org.deidentifier.arx.criteria.DPresence;
import org.deidentifier.arx.criteria.Inclusion;
import org.deidentifier.arx.criteria.LDiversity;
import org.deidentifier.arx.criteria.TCloseness;
import org.deidentifier.arx.framework.check.INodeChecker;
import org.deidentifier.arx.framework.check.history.History;
import org.deidentifier.arx.framework.lattice.Lattice;
import org.deidentifier.arx.framework.lattice.Node;
import org.deidentifier.arx.framework.lattice.NodeAction;
import org.deidentifier.arx.framework.lattice.NodeAction.NodeActionConstant;
import org.deidentifier.arx.framework.lattice.NodeAction.NodeActionInverse;
import org.deidentifier.arx.metric.Metric;

/**
 * This class provides a static method for instantiating the FLASH algorithm
 * 
 * @author <NAME>
 * @author <NAME>
 */
public class FLASHAlgorithm {
    
    /**
     * Monotonicity
     */
    private static enum Monotonicity {
        FULL,
        PARTIAL,
        NONE
    }

    /**
     * Creates a new instance of the FLASH algorithm
     * @param lattice
     * @param checker
     * @param strategy
     * @return
     */
    public static AbstractAlgorithm create(final Lattice lattice,
                                           final INodeChecker checker,
                                           final FLASHStrategy strategy){

        // Init
        ARXConfiguration config = checker.getConfiguration();
        Metric<?> metric = checker.getMetric();
        
        // NOTE: 
        // - If we assume practical monotonicity then we assume
        //   monotonicity for both criterion AND metric
        // - Without suppression we assume monotonicity for all criteria
        // - Without suppression we assume monotonicity for all metrics
        
        // Determine monotonicity of metric
        Monotonicity monotonicityMetric;
        if (metric.isMonotonic() || config.getMaxOutliers() == 0d || config.isPracticalMonotonicity()) {
            monotonicityMetric = Monotonicity.FULL;
        } else {
            monotonicityMetric = Monotonicity.NONE;
        }
        
        // Determine type of criteria to be enforced
        boolean containsTCloseness = config.containsCriterion(TCloseness.class);
        boolean containsLDiversity = config.containsCriterion(LDiversity.class);
        boolean containsDPresence = false; 
        for (DPresence d : config.getCriteria(DPresence.class)) {
            containsDPresence |= !(d instanceof Inclusion);
        }
        
        // Determine monotonicity of criteria
        Monotonicity monotonicityCriteria;
        if (config.getMaxOutliers() == 0d || config.isPracticalMonotonicity()) {
            monotonicityCriteria = Monotonicity.FULL;
        } else {
            if (config.getMinimalGroupSize() != Integer.MAX_VALUE) {
                if (containsTCloseness || containsLDiversity || containsDPresence) {
                    monotonicityCriteria = Monotonicity.PARTIAL;
                } else {
                    monotonicityCriteria = Monotonicity.FULL;
                    
                }
            } else {
                monotonicityCriteria = Monotonicity.NONE;
            }
        }
        // *********************************************
        // PREPARE PRUNING BASED ON MONOTONIC SUB-METRIC
        // *********************************************
        if (monotonicityMetric == Monotonicity.NONE) {
            for (Node[] level : lattice.getLevels()) {
                for (Node node : level) {
                    node.setData(metric.getLowerBound(node));
                }
            }
        }
        
        // ******************************
        // CASE 1
        // ******************************
        if (monotonicityCriteria == Monotonicity.FULL && monotonicityMetric == Monotonicity.FULL) {
            return createFullFull(lattice, checker, strategy);
        }
        
        
        // ******************************
        // CASE 2
        // ******************************
        if (monotonicityCriteria == Monotonicity.FULL && monotonicityMetric == Monotonicity.NONE) {
            return createFullNone(lattice, checker, strategy);
        }
        
        // ******************************
        // CASE 3
        // ******************************
        if (monotonicityCriteria == Monotonicity.PARTIAL && monotonicityMetric == Monotonicity.FULL) {
            return createPartialFull(lattice, checker, strategy);
{'fraction_non_alphanumeric': 0.12354494766702534, 'fraction_numerical': 0.0027389220385405457, 'mean_word_length': 2.3876739562624256, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1215998', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2583, 'n_words': 682}