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

org.btrplace.plan.DefaultReconfigurationPlanMonitor Maven / Gradle / Ivy

/*
 * Copyright (c) 2016 University Nice Sophia Antipolis
 *
 * This file is part of btrplace.
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */

package org.btrplace.plan;

import org.btrplace.model.Model;
import org.btrplace.plan.event.Action;

import java.util.*;

/**
 * Simulated execution of a {@link ReconfigurationPlan}.
 * The execution relies on the dependencies between the actions, retrieved using
 * <<<<<<< HEAD:api/src/main/java/btrplace/plan/DefaultReconfigurationPlanMonitor.java
 * {@link ReconfigurationPlan#getDirectDependencies(btrplace.plan.event.Action)}.
 * 

* The dependencies are updated each time an action is committed, which means the action * have been successfully executed. * ======= * {@link ReconfigurationPlan#getDirectDependencies(org.btrplace.plan.event.Action)}. *

* The dependencies are updated each time an action is committed, which means the action * have been successfully executed. *

* >>>>>>> master:api/src/main/java/org/btrplace/plan/DefaultReconfigurationPlanMonitor.java * * @author Fabien Hermenier */ public class DefaultReconfigurationPlanMonitor implements ReconfigurationPlanMonitor { private ReconfigurationPlan plan; private Model curModel; private final Map> pre; private final Map dependencies; private final Object lock; private int nbCommitted; /** * Make a new monitor. * * @param p the plan to execute */ public DefaultReconfigurationPlanMonitor(ReconfigurationPlan p) { this.plan = p; pre = new HashMap<>(); dependencies = new HashMap<>(); lock = new Object(); reset(); } private void reset() { synchronized (lock) { curModel = plan.getOrigin().copy(); pre.clear(); nbCommitted = 0; for (Action a : plan) { Set deps = plan.getDirectDependencies(a); if (deps.isEmpty()) { this.dependencies.put(a, new Dependency(a, Collections.emptySet())); } else { Dependency dep = new Dependency(a, deps); this.dependencies.put(a, dep); for (Action x : dep.getDependencies()) { Set pres = pre.get(x); if (pres == null) { pres = new HashSet<>(); pre.put(x, pres); } pres.add(dep); } } } } } @Override public Model getCurrentModel() { Model cpy; synchronized (lock) { cpy = curModel.copy(); } return cpy; } @Override public Set commit(Action a) { Set s = new HashSet<>(); synchronized (lock) { boolean ret = a.apply(curModel); if (!ret) { throw new InfeasibleActionException(curModel, a); } nbCommitted++; //Browse all its dependencies for the action Set deps = pre.get(a); if (deps != null) { for (Dependency dep : deps) { Set actions = dep.getDependencies(); actions.remove(a); if (actions.isEmpty()) { Action x = dep.getAction(); s.add(x); } } } } return s; } @Override public int getNbCommitted() { synchronized (lock) { return nbCommitted; } } @Override public boolean isBlocked(Action a) { synchronized (lock) { return !dependencies.get(a).getDependencies().isEmpty(); } } @Override public ReconfigurationPlan getReconfigurationPlan() { ReconfigurationPlan cpy; synchronized (lock) { cpy = new DefaultReconfigurationPlan(plan.getOrigin().copy()); for (Action a : plan) { //Cannot clone an action. Sad cpy.add(a); } } return cpy; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy