diff --git a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/TacticExecutor.java b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/TacticExecutor.java index 49865c04743222a9f81156ec9ea26ba94ee9f723..08797f14be666fada73fa5279c19c984b71d1b17 100644 --- a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/TacticExecutor.java +++ b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/TacticExecutor.java @@ -31,20 +31,21 @@ import edu.kit.ipd.descartes.mm.resourcelandscape.ResourcelandscapePackage; public class TacticExecutor { - private static Logger logger = Logger.getLogger(TacticExecutor.class); - private List<IObserver> observers = new ArrayList<IObserver>(); - - private IActionHandler dmmModelActionHandler; - private TacticsHistory hist; - private ChangeDescription changeDescription = null; - - public TacticExecutor(IActionHandler actionExecutor) { - dmmModelActionHandler = actionExecutor; - hist = TacticsHistory.getInstance(); - register(BlueYonderExperimentTracker.getInstance()); // TODO implement clean observer pattern - } - - /** + private static Logger logger = Logger.getLogger(TacticExecutor.class); + private List<IObserver> observers = new ArrayList<IObserver>(); + + private IActionHandler dmmModelActionHandler; + private TacticsHistory hist; + private ChangeDescription changeDescription = null; + + public TacticExecutor(IActionHandler actionExecutor) { + dmmModelActionHandler = actionExecutor; + hist = TacticsHistory.getInstance(); + register(BlueYonderExperimentTracker.getInstance()); // TODO implement clean observer + // pattern + } + + /** * Applies the the given tactic. * * @param currentTactic @@ -52,38 +53,37 @@ public class TacticExecutor { * @return */ public void applyTactic(WeightedTactic weightedTactic) { - try { - AdaptationProcessModelLoader.startRecording(); + try { + logger.info("Applying tactic " + weightedTactic.getUsedTactic().getName() + ", ID: " + + weightedTactic.getUsedTactic().getId() + "."); + AdaptationProcessModelLoader.startRecording(); executeAdaptationPlan(weightedTactic.getUsedTactic().getImplementedPlan()); - logger.info("Tactic " + weightedTactic.getUsedTactic().getName() - + " successfully applied."); + logger.info("Tactic " + weightedTactic.getUsedTactic().getName() + " successfully applied."); hist.add(weightedTactic, TacticsHistory.NO_RESULT, true); - + // if this point is reached everything was fine, so persist them (save the models) dmmModelActionHandler.persistActions(); changeDescription = AdaptationProcessModelLoader.endRecording(); return; } catch (OperationNotPerformedException e) { - logger.error("Tactic " + weightedTactic.getUsedTactic().getName() - + " could not be executed. Please check adaptation plan and/or log files for errors.", e); - hist.add(weightedTactic, TacticsHistory.NO_RESULT, false); + logger.error("Tactic " + weightedTactic.getUsedTactic().getName() + + " could not be executed. Please check adaptation plan and/or log files for errors.", e); + hist.add(weightedTactic, TacticsHistory.NO_RESULT, false); /* - * TODO Something went wrong. - * 1) Detect error - * 2) reevaluate tactics - * 3) apply different tactic. + * TODO Something went wrong. 1) Detect error 2) reevaluate tactics 3) apply different + * tactic. */ } } private StartAction findStartAction(AdaptationPlan plan) { - for (AbstractControlFlowElement abstractControlFlowElement : plan.getSteps()) { - // Find start of adaptation plan - if (abstractControlFlowElement instanceof StartAction) { - return (StartAction) abstractControlFlowElement; - } - } - return null; + for (AbstractControlFlowElement abstractControlFlowElement : plan.getSteps()) { + // Find start of adaptation plan + if (abstractControlFlowElement instanceof StartAction) { + return (StartAction) abstractControlFlowElement; + } + } + return null; } /** @@ -97,50 +97,49 @@ public class TacticExecutor { private void executeAdaptationPlan(AdaptationPlan plan) throws OperationNotPerformedException { StartAction start = findStartAction(plan); - if (start == null) - throw new OperationNotPerformedException("No start action for adaptation plan " + plan.getName() + " found."); + if (start == null) + throw new OperationNotPerformedException("No start action for adaptation plan " + plan.getName() + + " found."); - logger.info("Executing the adaptation plan <<" + plan.getName() + ">>."); try { executeNextStep(start.getSuccessor()); } catch (InvalidAdaptationPlan e) { - logger.error("Invalid adaptation plan <<" + plan.getName() + ">> when executing action with ID <<" + start.getId() + ">>.", e); + logger.error( + "Invalid adaptation plan <<" + plan.getName() + ">> when executing action with ID <<" + + start.getId() + ">>.", e); } - logger.info("Adaptation plan of adaptation plan <<" + plan.getName() + ">> terminated."); } - private void executeNextStep(AbstractControlFlowElement step) - throws OperationNotPerformedException, InvalidAdaptationPlan { - - if (step == null) - throw new InvalidAdaptationPlan("Current AbstractControlFlowElement is null!"); - - if (step instanceof StopAction) { - return; - } else if (step instanceof BranchAction) { - executeBranchAction(step); - } else if (step instanceof LoopAction) { - execLoopAction(step); - } else if (step instanceof ActionReference) { - executeReferredAction(step); - } else // Handle invalid actions and control flow elements - throw new OperationNotPerformedException( - "No valid control flow element " + step.toString()); - - // in any case (except stop action) - // continue with the next step - executeNextStep(step.getSuccessor()); - } + private void executeNextStep(AbstractControlFlowElement step) throws OperationNotPerformedException, + InvalidAdaptationPlan { + + if (step == null) + throw new InvalidAdaptationPlan("Current AbstractControlFlowElement is null!"); + + if (step instanceof StopAction) { + return; + } else if (step instanceof BranchAction) { + executeBranchAction(step); + } else if (step instanceof LoopAction) { + execLoopAction(step); + } else if (step instanceof ActionReference) { + executeReferredAction(step); + } else + // Handle invalid actions and control flow elements + throw new OperationNotPerformedException("No valid control flow element " + step.toString()); + + // in any case (except stop action) + // continue with the next step + executeNextStep(step.getSuccessor()); + } private void executeReferredAction(AbstractControlFlowElement step) throws OperationNotPerformedException { ActionReference ref = (ActionReference) step; Action action = ref.getRefersTo(); logger.info("Executing action <<" + action.getName() + ">>."); - AdaptationPoint currentAdaptationPoint = action - .getReferredAdaptationPoint(); - - dmmModelActionHandler.execute(currentAdaptationPoint, - action.getAdaptationActionOperation()); + AdaptationPoint currentAdaptationPoint = action.getReferredAdaptationPoint(); + + dmmModelActionHandler.execute(currentAdaptationPoint, action.getAdaptationActionOperation()); } private void executeBranchAction(AbstractControlFlowElement branchAction) throws OperationNotPerformedException { @@ -155,9 +154,9 @@ public class TacticExecutor { LoopAction loop = (LoopAction) loopAction; Parameter counter = loop.getCounter(); int counterValue = Integer.parseInt(counter.getValue()); - + int i = 1; - + while (i <= counterValue) { logger.debug("Executing iteration " + i + " of loop action " + loop.getBody().getName()); executeAdaptationPlan(loop.getBody()); @@ -176,7 +175,7 @@ public class TacticExecutor { case ContainerrepositoryPackage.CONTAINER_TEMPLATE: context = ContainerrepositoryPackage.Literals.CONTAINER_TEMPLATE; break; - // TODO add further contexts if necessary + // TODO add further contexts if necessary default: logger.error("Could not set context for eClass " + obj.getClass()); return false; @@ -188,16 +187,16 @@ public class TacticExecutor { public void undoPreviousTactic() { logger.info("Reverting model to previous state"); - for(IObserver o : observers) + for (IObserver o : observers) o.undo(); if (changeDescription != null) changeDescription.apply(); } - + public void register(IObserver observer) { observers.add(observer); } - + public void remove(IObserver observer) { observers.remove(observer); }