/*
 *    Copyright (c) 2013, University of Toronto.
 * 
 *    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 edu.toronto.cs.xcurator.discoverer;

import edu.toronto.cs.xcurator.common.DataDocument;
import edu.toronto.cs.xcurator.mapping.Attribute;
import edu.toronto.cs.xcurator.mapping.Schema;
import edu.toronto.cs.xcurator.mapping.Mapping;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ekzhu
 */
public class KeyAttributeDiscovery implements MappingDiscoveryStep {

    @Override
    public void process(List<DataDocument> dataDocuments, Mapping mapping) {
        System.out.println("process KeyAttributeDiscovery...");
        Iterator<Schema> it = mapping.getEntityIterator();
        while (it.hasNext()) {
            // For each entity, find attribute whose instances are unique
            // That is, the cardinality of the attribute instances should equal
            // to the cardinality of the entity instances

            // The value attribute should not be used as key.
            // Its instance count should be zero
            Schema entity = it.next();
            int instanceCount = entity.getXmlInstanceCount();
            Iterator<Attribute> attrIt = entity.getAttributeIterator();
            while (attrIt.hasNext()) {
                Attribute attr = attrIt.next();
                // This is a hack, the key identification algorithm needs to be 
                // improved.
                if (attr.getInstances().size() == instanceCount
                        && attr.getId().endsWith(".id")) {
                    attr.asKey();
                }
            }
        }
    }
}

@@ -1966,16 +1966,147 @@
 proved.%0A
+                System.out.println(attr);%0A                System.out.println(attr.getInstances().size() + %22 %3C%3E %22 + instanceCount);%0A
         
@@ -2161,16 +2161,18 @@
 ceCount%0A
+//
         
@@ -2189,16 +2189,42 @@
       &&
+%0A//                       
  attr.ge
@@ -2244,16 +2244,41 @@
 h(%22.id%22)
+%0A                        
 ) %7B%0A    

remove attribute name check for key discovery, need more generic solution