/*
 * ARX: Efficient, Stable and Optimal Data Anonymization
 * Copyright (C) 2012 - 2014 Florian Kohlmayer, Fabian Prasser
 * 
 * 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.metric;

import java.util.Set;

import org.deidentifier.arx.ARXConfiguration;
import org.deidentifier.arx.criteria.DPresence;
import org.deidentifier.arx.framework.check.groupify.HashGroupifyEntry;
import org.deidentifier.arx.framework.check.groupify.IHashGroupify;
import org.deidentifier.arx.framework.data.Data;
import org.deidentifier.arx.framework.data.GeneralizationHierarchy;
import org.deidentifier.arx.framework.lattice.Node;

/**
 * This class provides an implementation of the (normalized) average equivalence class size metric.
 * We dont normailze the metric as proposed in the original publication [1], as this would only be possible for k-anonymity.
 * [1] LeFevre K, DeWitt DJ, Ramakrishnan R. Mondrian Multidimensional K-Anonymity. IEEE; 2006:25-25.
 * 
 * @author Fabian Prasser
 * @author Florian Kohlmayer
 */
public class MetricAECS extends MetricDefault {

    private static final long serialVersionUID = -532478849890959974L;
    private double            total  = 0d;
    private boolean           dPresence        = false;

    protected MetricAECS() {
        super(true, false);
    }

    @Override
    protected InformationLossDefault evaluateInternal(final Node node, final IHashGroupify g) {

        int size = 0;
        // TODO: This is buggy. All EQs that are suppressed should result in one large equivalence class
        // When enforcing d-presence, use only ECs which contain at least one tuple from the research subset
        if (dPresence) { 
            HashGroupifyEntry m = g.getFirstEntry();
            while (m != null) {
                if (m.count > 0) {
                    size++;
                }
                m = m.nextOrdered;
            }
        } else {
            size = g.size();
        }

        final double value = total / size;
        return new InformationLossDefault(value);
    }

    @Override
    protected void initializeInternal(final Data input, final GeneralizationHierarchy[] ahierarchies, final ARXConfiguration config) {

        if (config.containsCriterion(DPresence.class)) {
            dPresence = true;
            Set<DPresence> crits = config.getCriteria(DPresence.class);
            if (crits.size() > 1) { throw new IllegalArgumentException("Only one d-presence criterion supported!"); }
            for (DPresence dPresence : crits) {
                total = dPresence.getSubset().getArray().length;
            }
        } else {
            total = input.getDataLength();
        }
    }
}

@@ -813,31 +813,8 @@
 c;%0A%0A
-import java.util.Set;%0A%0A
 impo
@@ -859,56 +859,8 @@
 on;%0A
-import org.deidentifier.arx.criteria.DPresence;%0A
 impo
@@ -1682,107 +1682,8 @@
 74L;
-%0A    private double            total  = 0d;%0A    private boolean           dPresence        = false;
 %0A%0A  
@@ -1723,19 +1723,20 @@
   super(
-tru
+fals
 e, false
@@ -1868,235 +1868,198 @@
     
-int size = 0;%0A        // TODO: This is buggy. All EQs that are suppressed should result in one large equivalence class%0A        // When enforcing d-presence, use only ECs which contain at least one tuple from the research subset
+// The total number of groups%0A        int groups = 0;%0A        // The total number of tuples%0A        int tuples = 0;%0A        // Are there suppressed tuples%0A        boolean suppressed = false;
 %0A   
@@ -2067,30 +2067,9 @@
     
-if (dPresence) %7B %0A    
+%0A
     
@@ -2121,20 +2121,16 @@
         
-    
 while (m
@@ -2149,28 +2149,24 @@
             
-    
 if (m.count 
@@ -2192,18 +2192,25 @@
     
-    size++
+tuples += m.count
 ;%0A  
@@ -2215,33 +2215,65 @@
                 
-%7D
+groups += m.isNotOutlier ? 1 : 0;
 %0A               
@@ -2277,25 +2277,37 @@
     
-m 
+suppressed %7C
 = 
+!
 m.
-nextOrdered
+isNotOutlier
 ;%0A  
@@ -2330,44 +2330,29 @@
     
-%7D else %7B%0A            size = g.size()
+    m = m.nextOrdered
 ;%0A  
@@ -2359,25 +2359,24 @@
       %7D%0A
-%0A
         
 final do
@@ -2371,723 +2371,229 @@
     
-final double value = total / size;%0A        return new InformationLossDefault(value);%0A    %7D%0A%0A    @Override%0A    protected void initializeInternal(final Data input, final GeneralizationHierarchy%5B%5D ahierarchies, final ARXConfiguration config) %7B%0A%0A        if (config.containsCriterion(DPresence.class)) %7B%0A            dPresence = true;%0A            Set%3CDPresence%3E crits = config.getCriteria(DPresence.class);%0A            if (crits.size() %3E 1) %7B throw new IllegalArgumentException(%22Only one d-presence criterion supported!%22); %7D%0A            for (DPresence dPresence : crits) %7B%0A                total = dPresence.getSubset().getArray().length;%0A            %7D%0A        %7D else %7B%0A            total = input.getDataLength();%0A        %7D
+%0A        // If there are suppressed tuples, they form one additional group%0A        groups += suppressed ? 1 : 0;%0A        %0A        // Compute AECS%0A        return new InformationLossDefault((double)tuples / (double)groups);
 %0A   

fix AECS metric. (1) Correctly handle suppressed tuples, (2) AECS is not monotonic