diff --git a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/AdaptationControl.java b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/AdaptationControl.java index 9cecb12029be1c0bcac47199c914616c90c5f855..818d44fff97231e021a228c024b8abf620177eab 100644 --- a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/AdaptationControl.java +++ b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/AdaptationControl.java @@ -118,8 +118,8 @@ public class AdaptationControl { applyTactic(currentTactic); analyzeModel(); - processResults(currentTactic); - evaluate(currentTactic); + processResults(); + evaluate(); // save results try { @@ -174,14 +174,17 @@ public class AdaptationControl { * * @param tactic */ - public void processResults(WeightedTactic tactic) { + public void processResults() { -// Impact latestImpact = perfDataRepoHandler.getImpactAt(iteration); - Impact latestImpact = perfDataRepoHandler.getLatestImpact(tactic); + WeightedTactic tactic = TacticsHistory.getInstance().getLatestAppliedTactic(); - // Connect parsed results to the executed tactic - if (latestImpact != null) - tactic.setLastImpact(latestImpact); + if (TacticsHistory.getInstance().wasLatestTacticSuccessful()) { + Impact latestImpact = perfDataRepoHandler.getLatestImpact(tactic); + if (latestImpact != null) + tactic.setLastImpact(latestImpact); + } else { + // Do nothing, impact will be evaluated in the evaluation phase. + } } /** @@ -190,17 +193,26 @@ public class AdaptationControl { * * @param tactic */ - public void evaluate(WeightedTactic tactic) { - // Check if tactic has made anything stupid, i.e., violates the objectives. If yes, undo - // and/or assign bad weight. - if (evaluator.degradationDetected(tactic)) { - logger.warn("Degradation detected for tactic " + tactic.getName()); - // undo tactic - // assign negative weight + public void evaluate() { + + WeightedTactic tactic = TacticsHistory.getInstance().getLatestAppliedTactic(); + + if (TacticsHistory.getInstance().wasLatestTacticSuccessful()) { + + // Check if tactic has made anything stupid, i.e., violates the objectives. If yes, undo + // and/or assign bad weight. + if (evaluator.degradationDetected(tactic)) { + logger.warn("Degradation detected for tactic " + tactic.getName()); + // undo tactic + // assign negative weight + } + + // If everything is fine, assign new weight. + evaluator.evaluate(tactic); + } else { + evaluator.punishTactic(tactic); } - - // If everything is fine, assign new weight. - evaluator.evaluate(tactic); + } /** 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 77d1d8e8b4948f1bb2413e1e37a2f992c48093e2..6710af5d5bd74ac45ecfce6041eec4bca582832b 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 @@ -54,7 +54,6 @@ public class TacticExecutor { } catch (OperationNotPerformedException e) { logger.error("Tactic " + weightedTactic.getUsedTactic().getName() + " could not be executed. Please check adaptation plan and/or log files for errors.", e); - weightedTactic.setCurrentWeight(0.0); hist.add(weightedTactic, TacticsHistory.NO_RESULT, false); /* * TODO Something went wrong. diff --git a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/evaluation/IEvaluator.java b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/evaluation/IEvaluator.java index f66e9e70cd556de5b30a81ae5a43af11ffc74dcb..f9b22b23288c4ef5303bab64dd60342f86032bc1 100644 --- a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/evaluation/IEvaluator.java +++ b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/evaluation/IEvaluator.java @@ -26,4 +26,11 @@ public interface IEvaluator { */ public boolean degradationDetected(WeightedTactic tactic); + /** + * Reduces the weight of the given tactic. + * + * @param tactic + */ + public void punishTactic(WeightedTactic tactic); + } diff --git a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/evaluation/WeightingFunctionEvaluator.java b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/evaluation/WeightingFunctionEvaluator.java index 151f37a02b6edecc07ed7c22559cb5f0a14e1f37..a124c0ad3da4eeb5de35fda07da899d060b1f24e 100644 --- a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/evaluation/WeightingFunctionEvaluator.java +++ b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/evaluation/WeightingFunctionEvaluator.java @@ -76,4 +76,10 @@ public class WeightingFunctionEvaluator implements IEvaluator { return false; } + + @Override + public void punishTactic(WeightedTactic tactic) { + tactic.setCurrentWeight(0.0); + + } } diff --git a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/AdaptationProcessModelLoader.java b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/AdaptationProcessModelLoader.java index be9a749a75ea85f0cb532745a8e1da2553a5f1d7..6993974ffc60b26786f4e6d2b7876960aa77519d 100644 --- a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/AdaptationProcessModelLoader.java +++ b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/AdaptationProcessModelLoader.java @@ -11,6 +11,7 @@ import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.emf.ecore.xmi.DanglingHREFException; import org.eclipse.emf.ecore.xmi.XMIResource; import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; @@ -24,11 +25,8 @@ import edu.kit.ipd.descartes.mm.resourcelandscape.ResourcelandscapePackage; public class AdaptationProcessModelLoader extends AbstractEcoreModelLoader { - public static final EPackage[] DMM_EPACKAGES = new EPackage[] { - ResourcelandscapePackage.eINSTANCE, - AdaptationpointsPackage.eINSTANCE, - ContainerrepositoryPackage.eINSTANCE, - AdaptationPackage.eINSTANCE }; + public static final EPackage[] DMM_EPACKAGES = new EPackage[] { ResourcelandscapePackage.eINSTANCE, + AdaptationpointsPackage.eINSTANCE, ContainerrepositoryPackage.eINSTANCE, AdaptationPackage.eINSTANCE }; private static AdaptationProcessModelLoader instance = null; @@ -52,14 +50,21 @@ public class AdaptationProcessModelLoader extends AbstractEcoreModelLoader { options.put(XMIResource.OPTION_PROCESS_DANGLING_HREF, XMIResource.OPTION_PROCESS_DANGLING_HREF_THROW); for (Resource res : resources) { - res.save(options); + try { + res.save(options); + } catch (IOException e) { + if (e.getCause() instanceof DanglingHREFException) { + String newMessage = "Error while persisting " + res.getURI() + " because of dangling references. Probably not all changes have been saved.\n"; + throw new IOException(newMessage + e.getMessage(), e.getCause()); + } + } } } @Override public void initializeResourceSet(ResourceSet resourceSet) { setResourceSet(resourceSet); - + /* Register the default resource factory -- only needed for stand-alone! */ getResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap() .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); diff --git a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/DmmModelActionHandler.java b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/DmmModelActionHandler.java index ba2b589b03e122add726ad6a1e532fdf1a8ea51a..a52011478c469c75fcbd690dc1553949ed9655ca 100644 --- a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/DmmModelActionHandler.java +++ b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/DmmModelActionHandler.java @@ -7,7 +7,6 @@ import org.apache.log4j.Logger; import org.eclipse.emf.common.util.EList; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.util.EcoreUtil; -import org.eclipse.emf.ecore.xmi.DanglingHREFException; import edu.kit.ipd.descartes.adaptation.IActionHandler; import edu.kit.ipd.descartes.adaptation.exceptions.OperationNotPerformedException; @@ -45,20 +44,17 @@ public class DmmModelActionHandler implements IActionHandler { try { adaptProcessModelMgr.saveAll(); } catch (IOException e) { - if (e.getCause() instanceof DanglingHREFException) - logger.error("Error while persisting results because of dangling references. Probably not all changes have been saved"); - else - logger.error("Error while saving models", e); + logger.error("Error while saving models", e); } } @Override - public void execute(AdaptationPoint point, AdaptationActionOperation operation) throws OperationNotPerformedException { + public void execute(AdaptationPoint point, AdaptationActionOperation operation) + throws OperationNotPerformedException { - logger.debug("Executing adaptation action <<" + operation.getAdaptationOperationDirection().getLiteral() - + ", " + operation.getAdaptationOperationScope().getLiteral() - + ">> on adaptation point <<" - + point.getName() + ">>."); + logger.debug("Executing adaptation action <<" + operation.getAdaptationOperationDirection().getLiteral() + ", " + + operation.getAdaptationOperationScope().getLiteral() + ">> on adaptation point <<" + point.getName() + + ">>."); if (point instanceof ModelEntityConfigurationRange) { logger.debug("Type of Adaptation: EntityConfigurationRange"); @@ -84,7 +80,8 @@ public class DmmModelActionHandler implements IActionHandler { DmmModelChanger.scaleModelVariable(configRange, operation.getAdaptationOperationDirection()); break; case ContainerrepositoryPackage.CONTAINER_TEMPLATE: - DmmModelChanger.scaleContainerTemplate((ContainerTemplate) actuallyConfiguredInstance, configRange, operation); + DmmModelChanger.scaleContainerTemplate((ContainerTemplate) actuallyConfiguredInstance, configRange, + operation); break; case ResourcelandscapePackage.CONTAINER: /* @@ -128,51 +125,54 @@ public class DmmModelActionHandler implements IActionHandler { logger.debug("Executing DECREASE operation"); Entity scaledEntity = scaleIn(adaptableEntity); if (scaledEntity == null) - throw new OperationNotPerformedException("Could not scale " + adaptableEntity.getName() + ". Minimum has been reached!"); + throw new OperationNotPerformedException("Could not scale " + adaptableEntity.getName() + + ". Minimum has been reached!"); } private void scaleOutModelEntity(Entity adaptableEntity, VariationType range) throws OperationNotPerformedException { logger.debug("Executing INCREASE operation"); RuntimeEnvironment scaledRE = scaleOut(adaptableEntity); - if (!invariantsAreValid(adaptableEntity, (PropertyRange) range)) { // spawn new runtimeEnvironment on different machine - List<Container> possibleTargets = DmmModelActionHelper.queryContainersWithSimilarTemplate(scaledRE.getContainedIn()); + if (!invariantsAreValid(adaptableEntity, (PropertyRange) range)) { // spawn new + // runtimeEnvironment on + // different machine + List<Container> possibleTargets = DmmModelActionHelper.queryContainersWithSimilarTemplate(scaledRE + .getContainedIn()); for (Container nextContainer : possibleTargets) { scaledRE.setContainedIn(nextContainer); if (invariantsAreValid(scaledRE, (PropertyRange) range)) { - logger.debug("Scaling entity " + scaledRE.getName() + - " on altenative container " + nextContainer.getName() + " (ID: " + nextContainer.getId() + ")"); + logger.debug("Scaling entity " + scaledRE.getName() + " on altenative container " + + nextContainer.getName() + " (ID: " + nextContainer.getId() + ")"); return; } } - // in case a violation occured but no alternative - // container was found, print - scaledRE.getContainedIn().getContains().remove(scaledRE); - throw new OperationNotPerformedException("Could not scale " + adaptableEntity.getName() + ". Maxiumum has been reached!"); - } + // in case a violation occured and no alternative container was found + EcoreUtil.delete(scaledRE, true); + throw new OperationNotPerformedException("Could not scale " + adaptableEntity.getName() + + ". Maxiumum has been reached!"); + } } - + private boolean invariantsAreValid(Entity adaptableEntity, PropertyRange range) { - boolean invariant = OclEvaluationHelper.evaluateOclConstraint( - adaptableEntity, - range.getMinValueConstraint().getOclString(), - adaptableEntity.eClass()); - - if (!invariant) logger.debug("Invariant " + range.getMinValueConstraint().getOclString() + " violated"); - - invariant &= OclEvaluationHelper.evaluateOclConstraint( - adaptableEntity, - range.getMaxValueConstraint().getOclString(), - adaptableEntity.eClass()); - - if (!invariant) logger.debug("Invariant " + range.getMaxValueConstraint().getOclString() + " violated"); - + boolean invariant = OclEvaluationHelper.evaluateOclConstraint(adaptableEntity, range.getMinValueConstraint() + .getOclString(), adaptableEntity.eClass()); + + if (!invariant) + logger.debug("Invariant " + range.getMinValueConstraint().getOclString() + " violated"); + + invariant &= OclEvaluationHelper.evaluateOclConstraint(adaptableEntity, range.getMaxValueConstraint() + .getOclString(), adaptableEntity.eClass()); + + if (!invariant) + logger.debug("Invariant " + range.getMaxValueConstraint().getOclString() + " violated"); + return invariant; } private void migrate(Entity adaptableEntity, VariationType targets, AdaptationScope adaptationScope) { switch (adaptableEntity.eClass().getClassifierID()) { case ResourcelandscapePackage.RUNTIME_ENVIRONMENT: - logger.debug("Starting migration of a runtime environment referring to container template " + adaptableEntity.getName() + " (ID: " + adaptableEntity.getId() + ")"); + logger.debug("Starting migration of a runtime environment referring to container template " + + adaptableEntity.getName() + " (ID: " + adaptableEntity.getId() + ")"); /* * 1. Copy RE 2. Attach copy to a target specified in the Adaptation Point 3. Delete * origin @@ -182,7 +182,7 @@ public class DmmModelActionHandler implements IActionHandler { throw new IllegalArgumentException( "Migration could not be performed because no target specification found!"); EList<Entity> migrationTargets = ((SetOfConfigurations) targets).getVariants(); - + RuntimeEnvironment migratedEntity = (RuntimeEnvironment) adaptableEntity; Container parent = migratedEntity.getContainedIn(); // TODO Here you could implement something more sophisticated @@ -198,7 +198,8 @@ public class DmmModelActionHandler implements IActionHandler { parent.getContains().remove(migratedEntity); break; case ContainerrepositoryPackage.CONTAINER_TEMPLATE: - logger.debug("Starting migration of a container referring to container template " + adaptableEntity.getName() + " (ID: " + adaptableEntity.getId() + ")"); + logger.debug("Starting migration of a container referring to container template " + + adaptableEntity.getName() + " (ID: " + adaptableEntity.getId() + ")"); ContainerTemplate ct = (ContainerTemplate) adaptableEntity; Container c = DmmModelActionHelper.findSuitableContainer(ct, adaptationScope); migrate(c, targets, adaptationScope); @@ -264,18 +265,20 @@ public class DmmModelActionHandler implements IActionHandler { if (removedRe != null) template.getReferringContainers().remove(removedRe); - + return removedRe; } - /** Creates a new RuntimeEnvironment. If you want to scale an attribute of the ContainerTemplate, - * ModelVariableConfigurationRange must be used. However, here we are - * scaling a ModelEntityConfigurationRange. + /** + * Creates a new RuntimeEnvironment. If you want to scale an attribute of the ContainerTemplate, + * ModelVariableConfigurationRange must be used. However, here we are scaling a + * ModelEntityConfigurationRange. */ // this is just a helper function private RuntimeEnvironment scaleOutContainerTemplate(ContainerTemplate template) { if (template.getReferringContainers().size() == 0) { - logger.debug("No referring containers found for template " + template.getName() + ", creating new instance from template."); + logger.debug("No referring containers found for template " + template.getName() + + ", creating new instance from template."); return DmmModelChanger.createNewRuntimeEnvironmentInstanceFromTemplate(template); } else { Container referringContainer = template.getReferringContainers().get(0); diff --git a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/util/DmmModelChanger.java b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/util/DmmModelChanger.java index fa1b1b479c08321d6cf101a3c284f900d4145eb0..ec6a13e03976f9337d447507bdea061d96ef33d1 100644 --- a/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/util/DmmModelChanger.java +++ b/edu.kit.ipd.descartes.adaptation/src/edu/kit/ipd/descartes/adaptation/model/dmm/util/DmmModelChanger.java @@ -107,7 +107,7 @@ public class DmmModelChanger { similarEntity.getContainedIn().getContains().remove(similarEntity); logger.info("RuntimeEnvironment " + similarEntity.getName() + " (ID: " + similarEntity.getId() + ") removed."); } else - logger.error("Could not remove a runtime environment instance since no similar entity has been found."); + logger.error("Could not remove a runtime environment instance because no similar entity has been found."); return (RuntimeEnvironment) similarEntity; }