All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.btrplace.scheduler.choco.view.ChocoViews Maven / Gradle / Ivy

/*
 * Copyright  2020 The BtrPlace Authors. All rights reserved.
 * Use of this source code is governed by a LGPL-style
 * license that can be found in the LICENSE.txt file.
 */

package org.btrplace.scheduler.choco.view;

import org.btrplace.model.Model;
import org.btrplace.scheduler.SchedulerException;
import org.btrplace.scheduler.SchedulerModelingException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

/**
 * Utility class to manipulate collection of {@link ChocoView}.
 */
public class ChocoViews {

    /**
     * Utility class. No instantiations.
     */
    private ChocoViews() {

    }
    /**
     * Flatten the views while considering their dependencies.
     * Operations over the views that respect the iteration order, satisfies the dependencies.
     *
     * @param mo    the model
     * @param views the associated solver views
     * @param base the identifier of the core views.
     * @return the list of views, dependency-free
     * @throws SchedulerException if there is a cyclic dependency
     */
    public static List resolveDependencies(Model mo, List views, Collection base) throws SchedulerException {
        Set done = new HashSet<>(base);
        List remaining = new ArrayList<>(views);
        List solved = new ArrayList<>();
        while (!remaining.isEmpty()) {
            ListIterator ite = remaining.listIterator();
            boolean blocked = true;
            while (ite.hasNext()) {
                ChocoView s = ite.next();
                if (done.containsAll(s.getDependencies())) {
                    ite.remove();
                    done.add(s.getIdentifier());
                    solved.add(s);
                    blocked = false;
                }
            }
            if (blocked) {
                throw new SchedulerModelingException(mo, "Missing dependencies or cyclic dependencies prevent from using: " + remaining);
            }
        }
        return solved;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy