/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protegex.util;

import java.util.*;

import edu.stanford.smi.protege.model.*;
/*
 * Recursive copy does not create new slots or facets qua slots or facets.
 * It does, however, create new slots or facets if they are *targets*.
 */

/**
 *  Recursive copy of simple instances
 *
 * @author    William Grosso <grosso@smi.stanford.edu>
 */
public class RecursiveCopy {

    private static void copyOwnSlot(
        Slot slot,
        Instance source,
        Instance destination,
        int depth,
        HashMap alreadyDuplicatedInstances,
        List newInstancesCreated) {
        Iterator values = (source.getOwnSlotValues(slot)).iterator();
        while (values.hasNext()) {
            Object value = values.next();
            if (value instanceof Instance) {
                if ((value instanceof Cls) || (value instanceof Facet) || (value instanceof Slot) || (depth == 0)) {
                    destination.addOwnSlotValue(slot, value);
                } else {
                    destination.addOwnSlotValue(
                        slot,
                        recursivelyCopyInstance(
                            (Instance) value,
                            depth - 1,
                            alreadyDuplicatedInstances,
                            newInstancesCreated));
                }
            } else {
                destination.addOwnSlotValue(slot, value);
            }
        }
    }

    private static void copyOwnSlots(
        Instance source,
        Instance destination,
        int depth,
        HashMap alreadyDuplicatedInstances,
        List newInstancesCreated) {
        Iterator slots = (source.getOwnSlots()).iterator();
        while (slots.hasNext()) {
            Slot nextSlot = (Slot) slots.next();
            if (isCopyableSlot(nextSlot)) {
                copyOwnSlot(nextSlot, source, destination, depth, alreadyDuplicatedInstances, newInstancesCreated);
            }
        }
        return;
    }

    private static boolean isCopyableSlot(Slot slot) {
        String name = slot.getName();
        return !(name.equals(Model.Slot.NAME) || (name.equals(Model.Slot.DIRECT_TYPES)));
    }

    public static Instance recursivelyCopyInstance(Instance instance, int depth) {
        return recursivelyCopyInstance(instance, depth, new HashMap(), null);
    }

    private static Instance recursivelyCopyInstance(
        Instance instance,
        int depth,
        HashMap alreadyDuplicatedInstances,
        List newInstancesCreated) {
        Instance returnValue = (Instance) alreadyDuplicatedInstances.get(instance);
        if (null != returnValue) {
            return returnValue;
        }
        Cls cls = instance.getDirectType();
        KnowledgeBase kb = instance.getKnowledgeBase();
        // create an instance but don't fill in the default values
        returnValue = kb.createInstance((String) null, cls, false);
        alreadyDuplicatedInstances.put(instance, returnValue);
        if (null != newInstancesCreated) {
            newInstancesCreated.add(returnValue);
        }
        copyOwnSlots(instance, returnValue, depth, alreadyDuplicatedInstances, newInstancesCreated);
        return returnValue;
    }

    public static Instance recursivelyCopyInstance(Instance instance, int depth, List newInstancesCreated) {
        return recursivelyCopyInstance(instance, depth, new HashMap(), newInstancesCreated);
    }
}
