<commit_before>/*
 * ARX: Efficient, Stable and Optimal Data Anonymization
 * Copyright (C) 2012 - 2013 <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.gui.model;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.deidentifier.arx.ARXAnonymizer;
import org.deidentifier.arx.ARXLattice.ARXNode;
import org.deidentifier.arx.ARXResult;
import org.deidentifier.arx.AttributeType;
import org.deidentifier.arx.DataSubset;
import org.deidentifier.arx.AttributeType.Hierarchy;
import org.deidentifier.arx.DataHandle;
import org.deidentifier.arx.criteria.DPresence;

public class Model implements Serializable {

	private static final long serialVersionUID = -7669920657919151279L;

	// TODO: Check if all initial values are ok
    private transient Set<ARXNode>                clipboard            = new HashSet<ARXNode>();
    private transient DataHandle                  output               = null;
    private transient ARXNode                     outputNode           = null;
    private transient ARXResult                   result               = null;
    private transient ARXNode                     selectedNode         = null;
    private transient ARXAnonymizer               anonymizer           = null;
    private transient String                      path                 = null;

    private String                                name                 = null;
    private char                                  separator            = ';';
    private String                                description;
    private int                                   historySize          = 200;
    private double                                snapshotSizeDataset  = 0.2d;
    private double                                snapshotSizeSnapshot = 0.8d;
    private int                                   initialNodesInViewer = 100;
    private int                                   maxNodesInLattice    = 100000;
    private int                                   maxNodesInViewer     = 700;

    private String                                selectedAttribute    = null;
    private ModelNodeFilter                       nodeFilter           = null;
    private boolean                               modified             = false;
    private long                                  inputBytes           = 0L;
    private String[]                              pair                 = new String[] { null, null };

    private String                              optimalNodeAsString;
    private String                              outputNodeAsString;

    private long                                time;

    private ModelConfiguration                    inputConfig          = new ModelConfiguration();
    private ModelConfiguration                    outputConfig         = null;

    private String                                suppressionString    = "*";                                            //$NON-NLS-1$

    private int[]                                 groups;

    private ModelKAnonymityCriterion              kAnonymityModel      = new ModelKAnonymityCriterion();
    private ModelDPresenceCriterion               dPresenceModel       = new ModelDPresenceCriterion();
    private Map<String, ModelLDiversityCriterion> lDiversityModel      = new HashMap<String, ModelLDiversityCriterion>();
    private Map<String, ModelTClosenessCriterion> tClosenessModel      = new HashMap<String, ModelTClosenessCriterion>();

    private String                                query                = "";
    private String                                subsetOrigin         = "All";
    private ModelViewConfig                       viewConfig           = new ModelViewConfig();
    private ModelViewConfig                       oldViewConfig        = viewConfig.clone();
    
	public Model(final String name, final String description) {
		this.name = name;
		this.description = description;
		setModified();
	}

	public ARXAnonymizer createAnonymizer() {
	    
		// Initialize anonymizer
		this.anonymizer = new ARXAnonymizer();
		this.anonymizer.setHistorySize(getHistorySize());
		this.anonymizer.setMaximumSnapshotSizeDataset(getSnapshotSizeDataset());
		this.anonymizer.setSuppressionString(getSuppressionString());
		this.anonymizer.setMaximumSnapshotSizeSnapshot(getSnapshotSizeSnapshot());
		this.anonymizer.setRemoveOutliers(inputConfig.isRemoveOutliers());
		
		// Initialize the config
		inputConfig.removeAllCriteria();
		
		if (this.kAnonymityModel != null &&
		    this.kAnonymityModel.isActive() &&
		    this.kAnonymityModel.isEnabled()) {
		    inputConfig.addCriterion(this.kAnonymityModel.getCriterion(this));
		}

        if (this.dPresenceModel != null && 
            this.dPresenceModel.isActive() && 
            this.dPresenceModel.isEnabled()) {
            inputConfig.addCriterion(this.dPresenceModel.getCriterion(this));
        }
		
		for (Entry<String, ModelLDiversityCriterion> entry : this.lDiversityModel.entrySet()){
	        if (entry.getValue() != null &&
	            entry.getValue().isActive() &&
	            entry.getValue().isEnabled()) {
	            inputConfig.addCriterion(entry.getValue().getCriterion(this));
	        }
		}
        
        for (Entry<String, ModelTClosenessCriterion> entry : this.tClosenessModel.entrySet()){
            if (entry.getValue() != null &&
                entry.getValue().isActive() &&
                entry.getValue().isEnabled()) {
                inputConfig.addCriterion(entry.getValue().getCriterion(this));
            }
        }

        // Allow adding removing tuples
        if (!inputConfig.containsCriterion(DPresence.class)){
            DataSubset subset = DataSubset.create(getInputConfig().getInput(), 
                                                  getInputConfig().getResearchSubset());
            inputConfig.addCriterion(new DPresence(0d, 1d, subset));
        }

        // Clone the config
        outputConfig = inputConfig.clone();
        
        // Return the anonymizer
		return anonymizer;
	}

	public ARXAnonymizer getAnonymizer() {
		return anonymizer;
	}

	public String[] getAttributePair() {
		if (pair == null)
			pair = new String[] { null, null };
		return pair;
	}

	public Set<ARXNode> getClipboard() {<commit_msg>Make the GUI insert enclosure instead of d-presence<commit_after>/*
 * ARX: Efficient, Stable and Optimal Data Anonymization
 * Copyright (C) 2012 - 2013 <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.gui.model;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.deidentifier.arx.ARXAnonymizer;
import org.deidentifier.arx.ARXLattice.ARXNode;
import org.deidentifier.arx.ARXResult;
import org.deidentifier.arx.AttributeType;
import org.deidentifier.arx.AttributeType.Hierarchy;
import org.deidentifier.arx.DataHandle;
import org.deidentifier.arx.DataSubset;
import org.deidentifier.arx.criteria.DPresence;
import org.deidentifier.arx.criteria.Enclosure;

public class Model implements Serializable {

	private static final long serialVersionUID = -7669920657919151279L;

	// TODO: Check if all initial values are ok
    private transient Set<ARXNode>                clipboard            = new HashSet<ARXNode>();
    private transient DataHandle                  output               = null;
    private transient ARXNode                     outputNode           = null;
    private transient ARXResult                   result               = null;
    private transient ARXNode                     selectedNode         = null;
    private transient ARXAnonymizer               anonymizer           = null;
    private transient String                      path                 = null;

    private String                                name                 = null;
    private char                                  separator            = ';';
    private String                                description;
    private int                                   historySize          = 200;
    private double                                snapshotSizeDataset  = 0.2d;
    private double                                snapshotSizeSnapshot = 0.8d;
    private int                                   initialNodesInViewer = 100;
    private int                                   maxNodesInLattice    = 100000;
    private int                                   maxNodesInViewer     = 700;

    private String                                selectedAttribute    = null;
    private ModelNodeFilter                       nodeFilter           = null;
    private boolean                               modified             = false;
    private long                                  inputBytes           = 0L;
    private String[]                              pair                 = new String[] { null, null };

    private String                              optimalNodeAsString;
    private String                              outputNodeAsString;

    private long                                time;

    private ModelConfiguration                    inputConfig          = new ModelConfiguration();
    private ModelConfiguration                    outputConfig         = null;

    private String                                suppressionString    = "*";                                            //$NON-NLS-1$

    private int[]                                 groups;

    private ModelKAnonymityCriterion              kAnonymityModel      = new ModelKAnonymityCriterion();
    private ModelDPresenceCriterion               dPresenceModel       = new ModelDPresenceCriterion();
    private Map<String, ModelLDiversityCriterion> lDiversityModel      = new HashMap<String, ModelLDiversityCriterion>();
    private Map<String, ModelTClosenessCriterion> tClosenessModel      = new HashMap<String, ModelTClosenessCriterion>();

    private String                                query                = "";
    private String                                subsetOrigin         = "All";
    private ModelViewConfig                       viewConfig           = new ModelViewConfig();
    private ModelViewConfig                       oldViewConfig        = viewConfig.clone();
    
	public Model(final String name, final String description) {
		this.name = name;
		this.description = description;
		setModified();
	}

	public ARXAnonymizer createAnonymizer() {
	    
		// Initialize anonymizer
		this.anonymizer = new ARXAnonymizer();
		this.anonymizer.setHistorySize(getHistorySize());
		this.anonymizer.setMaximumSnapshotSizeDataset(getSnapshotSizeDataset());
		this.anonymizer.setSuppressionString(getSuppressionString());
		this.anonymizer.setMaximumSnapshotSizeSnapshot(getSnapshotSizeSnapshot());
		this.anonymizer.setRemoveOutliers(inputConfig.isRemoveOutliers());
		
		// Initialize the config
		inputConfig.removeAllCriteria();
		
		if (this.kAnonymityModel != null &&
		    this.kAnonymityModel.isActive() &&
		    this.kAnonymityModel.isEnabled()) {
		    inputConfig.addCriterion(this.kAnonymityModel.getCriterion(this));
		}

        if (this.dPresenceModel != null && 
            this.dPresenceModel.isActive() && 
            this.dPresenceModel.isEnabled()) {
            inputConfig.addCriterion(this.dPresenceModel.getCriterion(this));
        }
		
		for (Entry<String, ModelLDiversityCriterion> entry : this.lDiversityModel.entrySet()){
	        if (entry.getValue() != null &&
	            entry.getValue().isActive() &&
	            entry.getValue().isEnabled()) {
	            inputConfig.addCriterion(entry.getValue().getCriterion(this));
	        }
		}
        
        for (Entry<String, ModelTClosenessCriterion> entry : this.tClosenessModel.entrySet()){
            if (entry.getValue() != null &&
                entry.getValue().isActive() &&
                entry.getValue().isEnabled()) {
                inputConfig.addCriterion(entry.getValue().getCriterion(this));
            }
        }

        // Allow adding removing tuples
        if (!inputConfig.containsCriterion(DPresence.class)){
            DataSubset subset = DataSubset.create(getInputConfig().getInput(), 
                                                  getInputConfig().getResearchSubset());
            inputConfig.addCriterion(new Enclosure(subset));
        }

        // Clone the config
        outputConfig = inputConfig.clone();
        
        // Return the anonymizer
		return anonymizer;
	}

	public ARXAnonymizer getAnonymizer() {
		return anonymizer;
	}

	public String[] getAttributePair() {
		if (pair == null)
			pair = new String[] { null, null };
		return pair;
	}

	public Set<ARXNode> getClipboard() {
{'fraction_non_alphanumeric': 0.07654216185625354, 'fraction_numerical': 0.007074136955291454, 'mean_word_length': 1.8461848198107509, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3742734', 'n_tokens_mistral': 3619, 'n_tokens_neox': 3355, 'n_words': 867}