From 2d92fd369d3b475ae35106f27d2a09ffbe7809d6 Mon Sep 17 00:00:00 2001 From: Nikolaus Huber <nikolaus.huber@uni-wuerzburg.de> Date: Tue, 14 May 2013 11:35:04 +0000 Subject: [PATCH] evaluation interface introduced git-svn-id: https://se1.informatik.uni-wuerzburg.de/usvn/svn/code/code/DMM/trunk@11547 9e42b895-fcda-4063-8a3b-11be15eb1bbd --- .../adaptation/AdaptationControl.java | 355 +++++++++--------- 1 file changed, 176 insertions(+), 179 deletions(-) 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 26ae6d5b..3f4f3995 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 @@ -9,14 +9,14 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.eclipse.emf.common.util.URI; +import edu.kit.ipd.descartes.adaptation.evaluation.IEvaluator; +import edu.kit.ipd.descartes.adaptation.evaluation.WeightingFunctionEvaluator; import edu.kit.ipd.descartes.adaptation.model.analysis.IModelAnalyzer; import edu.kit.ipd.descartes.adaptation.model.analysis.pcm.PcmModelAnalyzer; import edu.kit.ipd.descartes.adaptation.model.dmm.AdaptationProcessModelManager; import edu.kit.ipd.descartes.adaptation.model.dmm.DmmModelActionHandler; import edu.kit.ipd.descartes.adaptation.model.dmm.PerformanceDataRepositoryModelManager; import edu.kit.ipd.descartes.adaptation.model.dmm.util.PerfDataRepoHelper; -import edu.kit.ipd.descartes.adaptation.weightingfunction.IWeightingFunctionCalculator; -import edu.kit.ipd.descartes.adaptation.weightingfunction.WeightedSumCalculator; import edu.kit.ipd.descartes.mm.adaptation.AdaptationFactory; import edu.kit.ipd.descartes.mm.adaptation.AdaptationProcess; import edu.kit.ipd.descartes.mm.adaptation.Event; @@ -28,106 +28,108 @@ import edu.kit.ipd.descartes.perfdatarepo.PerformanceDataRepository; public class AdaptationControl { - public enum EventType { - SLA_VIOLATION ("SlaViolatedEvent", "12345"), - SCHEDULED_OPTIMIZATION ("ScheduledOptimizationEvent", "_H_MTgIAnEeKW2vVcg5ekRw"), - BOTTLENECK_FOUND ("BottleneckFoundEvent", "_VvZEwKEoEeKDl52Xojo4CQ"); - - private final String name; - private final String id; - EventType(String name, String id) { - this.name = name; - this.id = id; - } - + public enum EventType { + SLA_VIOLATION("SlaViolatedEvent", "12345"), SCHEDULED_OPTIMIZATION("ScheduledOptimizationEvent", + "_H_MTgIAnEeKW2vVcg5ekRw"), BOTTLENECK_FOUND("BottleneckFoundEvent", "_VvZEwKEoEeKDl52Xojo4CQ"); + + private final String name; + private final String id; + + EventType(String name, String id) { + this.name = name; + this.id = id; + } + public static EventType getEventType(String eventTypeName) { - if (eventTypeName.equals(SLA_VIOLATION.name)) return SLA_VIOLATION; - if (eventTypeName.equals(SCHEDULED_OPTIMIZATION.name)) return SCHEDULED_OPTIMIZATION; - if (eventTypeName.equals(BOTTLENECK_FOUND.name)) return BOTTLENECK_FOUND; + if (eventTypeName.equals(SLA_VIOLATION.name)) + return SLA_VIOLATION; + if (eventTypeName.equals(SCHEDULED_OPTIMIZATION.name)) + return SCHEDULED_OPTIMIZATION; + if (eventTypeName.equals(BOTTLENECK_FOUND.name)) + return BOTTLENECK_FOUND; return null; } - } - - private static final String DEFAULT_PROP_FILE_PATH = "./default.properties"; + } + + private static final String DEFAULT_PROP_FILE_PATH = "./default.properties"; private static final String PROP_ADAPTATION_PROCESS_XMI_FILE_PATH_NAME = "adaptationProcessModel"; - private static final String PROP_PERF_DATA_REPO_XMI_FILE_PATH_NAME = "performanceDataRepositoryModel"; + private static final String PROP_PERF_DATA_REPO_XMI_FILE_PATH_NAME = "performanceDataRepositoryModel"; private static final String PROP_ADAPTATION_PROCESS_NAME = "processName"; private static final String PROP_MAX_ITERATIONS = "maxIterations"; private static final String PROP_TRIGGERING_EVENT_TYPE = "eventType"; - - private static Logger logger = Logger.getLogger(AdaptationControl.class); - - private String adaptationProcessXmiFilePath = null; - private String performanceDataRepoXmiFilePath = null; - private String eventType = null; - - private AdaptationProcessModelManager adaptationProcessModelManager = AdaptationProcessModelManager.getInstance(); - private PerformanceDataRepositoryModelManager perfDataModelManager = PerformanceDataRepositoryModelManager - .getInstance(); - private AdaptationProcess adaptationProcess = null; - private PerformanceDataRepository perfDataRepo = null; - private TacticExecutor executor = null; - private IWeightingFunctionCalculator wFunction = null; - private IModelAnalyzer modelAnalyzer = null; - - private boolean problemSolved = false; - private int iteration = 0; - private int maxIterations = 0; - - /** - * @param args - * @throws IOException - */ - public static void main(String[] args) throws IOException { - BasicConfigurator.configure(); - Logger.getRootLogger().setLevel(Level.DEBUG); - - AdaptationControl adaptationController = new AdaptationControl(); - // Initialize controller (load models, create Action handler, etc.) - adaptationController.init(); - Event triggeringEvent = adaptationController.createTriggeringEvent(); - - // trigger adaptation process - adaptationController.doAdaptation(triggeringEvent); - } - - private void loadProperties(String propertiesFilePath) throws IOException { + + private static Logger logger = Logger.getLogger(AdaptationControl.class); + + private String adaptationProcessXmiFilePath = null; + private String performanceDataRepoXmiFilePath = null; + private String eventType = null; + + private AdaptationProcessModelManager adaptationProcessModelManager = AdaptationProcessModelManager.getInstance(); + private PerformanceDataRepositoryModelManager perfDataModelManager = PerformanceDataRepositoryModelManager + .getInstance(); + private AdaptationProcess adaptationProcess = null; + private PerformanceDataRepository perfDataRepo = null; + private TacticExecutor executor = null; + private IModelAnalyzer modelAnalyzer = null; + private WeightingFunctionEvaluator evaluator = null; + + private boolean problemSolved = false; + private int iteration = 0; + private int maxIterations = 0; + + /** + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + BasicConfigurator.configure(); + Logger.getRootLogger().setLevel(Level.DEBUG); + + AdaptationControl adaptationController = new AdaptationControl(); + // Initialize controller (load models, create Action handler, etc.) + adaptationController.init(); + Event triggeringEvent = adaptationController.createTriggeringEvent(); + + // trigger adaptation process + adaptationController.doAdaptation(triggeringEvent); + } + + private void loadProperties(String propertiesFilePath) throws IOException { Properties properties = new Properties(); - FileInputStream propFile = new FileInputStream(propertiesFilePath); + FileInputStream propFile = new FileInputStream(propertiesFilePath); properties.load(propFile); logger.debug("Loaded properties of " + properties.getProperty(PROP_ADAPTATION_PROCESS_NAME)); - adaptationProcessXmiFilePath=properties.getProperty(PROP_ADAPTATION_PROCESS_XMI_FILE_PATH_NAME); - performanceDataRepoXmiFilePath=properties.getProperty(PROP_PERF_DATA_REPO_XMI_FILE_PATH_NAME); - maxIterations=Integer.parseInt(properties.getProperty(PROP_MAX_ITERATIONS)); - eventType=properties.getProperty(PROP_TRIGGERING_EVENT_TYPE).trim(); + adaptationProcessXmiFilePath = properties.getProperty(PROP_ADAPTATION_PROCESS_XMI_FILE_PATH_NAME); + performanceDataRepoXmiFilePath = properties.getProperty(PROP_PERF_DATA_REPO_XMI_FILE_PATH_NAME); + maxIterations = Integer.parseInt(properties.getProperty(PROP_MAX_ITERATIONS)); + eventType = properties.getProperty(PROP_TRIGGERING_EVENT_TYPE).trim(); logger.debug("Maximum iterations till abort: " + maxIterations); } public void init() { - try { - loadProperties(DEFAULT_PROP_FILE_PATH); - adaptationProcess = adaptationProcessModelManager.load(URI - .createFileURI(adaptationProcessXmiFilePath)); - perfDataRepo = perfDataModelManager.load(URI - .createFileURI(performanceDataRepoXmiFilePath)); - executor = new TacticExecutor(new DmmModelActionHandler()); - modelAnalyzer = new PcmModelAnalyzer(); - } catch (IOException e) { - logger.error("Error while loading configuration files", e); - abort(); - return; - } - } - - // Creates an "artifical event". Will be replaced later when integrated in the system + try { + loadProperties(DEFAULT_PROP_FILE_PATH); + adaptationProcess = adaptationProcessModelManager.load(URI.createFileURI(adaptationProcessXmiFilePath)); + perfDataRepo = perfDataModelManager.load(URI.createFileURI(performanceDataRepoXmiFilePath)); + executor = new TacticExecutor(new DmmModelActionHandler()); + modelAnalyzer = new PcmModelAnalyzer(); + evaluator = new WeightingFunctionEvaluator(); + } catch (IOException e) { + logger.error("Error while loading configuration files", e); + abort(); + return; + } + } + + // Creates an "artifical event". Will be replaced later when integrated in the system private Event createTriggeringEvent() { EventType type = EventType.getEventType(eventType); - - if(type==null) { + + if (type == null) { logger.error("Could not create event " + eventType); abort(); } - + Event triggeringEvent = AdaptationFactory.eINSTANCE.createEvent(); triggeringEvent.setName(type.name); triggeringEvent.setId(type.id); @@ -135,48 +137,45 @@ public class AdaptationControl { return triggeringEvent; } - public void doAdaptation(Event triggeringEvent) { - - // Determine the suitable strategy via the objective - // which is currently implemented as the first element - Strategy currentStrategy = AdaptationControl.findTriggeredStrategy( - adaptationProcess, triggeringEvent); - if (currentStrategy == null) { - logger.error("No strategy found to handle this event!"); - abort(); - return; - } - - while (!isProblemSolved() && iteration < maxIterations) { - // Execute the strategie's tactic with the currently highest weight - WeightedTactic currentTactic = currentStrategy.getTacticWithHighestWeight(); - WeightingFunction currentWeightingFunction = currentStrategy.getWeightingFunction(); - - applyTactic(currentTactic); - analyzeModel(); - processResults(currentTactic); - evaluate(currentWeightingFunction, currentTactic); - - iteration++; - } - - printLogInformation(); - - } - - private void evaluate(WeightingFunction function, WeightedTactic tactic) { - // Caluclate the new weight of the tactic - wFunction = new WeightedSumCalculator(function); - wFunction.updateWeight(tactic); - - //save results - try { - logger.debug("Saving tactics evaluation results..."); - adaptationProcessModelManager.saveAll(); - } catch (IOException e) { - logger.error("Error while persisting evaluation results.", e); + public void doAdaptation(Event triggeringEvent) { + + // Determine the suitable strategy via the objective + // which is currently implemented as the first element + Strategy currentStrategy = AdaptationControl.findTriggeredStrategy(adaptationProcess, triggeringEvent); + if (currentStrategy == null) { + logger.error("No strategy found to handle this event!"); + abort(); + return; + } + + while (!isProblemSolved() && iteration < maxIterations) { + // Execute the strategie's tactic with the currently highest weight + WeightedTactic currentTactic = currentStrategy.getTacticWithHighestWeight(); + + applyTactic(currentTactic); + analyzeModel(); + processResults(currentTactic); + evaluate(currentTactic); + + // save results + try { + logger.debug("Saving tactics evaluation results..."); + adaptationProcessModelManager.saveAll(); + } catch (IOException e) { + logger.error("Error while persisting evaluation results.", e); + } + + iteration++; } - } + + printLogInformation(); + + } + + private void evaluate(WeightedTactic currentTactic) { + WeightingFunction f = currentTactic.getParentStrategy().getWeightingFunction(); + evaluator.evaluate(currentTactic); + } private void processResults(WeightedTactic tactic) { Impact latestImpact = PerfDataRepoHelper.getImpactAt(perfDataRepo, iteration); @@ -185,59 +184,57 @@ public class AdaptationControl { tactic.setLastImpact(latestImpact); } - private void analyzeModel() { - modelAnalyzer.analyze(); - } - - private void applyTactic(WeightedTactic weightedTactic) { - executor.applyTactic(weightedTactic); - } - - private boolean isProblemSolved() { - //TODO evaluate the results (problem solved?) - //TODO Implement correct check of objective - return problemSolved; - } - - private void printLogInformation() { - TacticsHistory hist = TacticsHistory.getInstance(); - hist.printToSysOut(); - } - - private static Strategy findTriggeredStrategy(AdaptationProcess staRepo, - Event triggeringEvent) { - - String eventName = triggeringEvent.getName(); - String eventId = triggeringEvent.getId(); - - logger.debug("Caught event: " + eventName + " (ID: " + eventId + ")"); - - for (Strategy strategy : staRepo.getStrategies()) { - if (strategy.getTriggeringEvents().getId().equals(eventId) - && strategy.getTriggeringEvents().getName() - .equals(eventName)) { - logger.debug("Found suitable strategy " + strategy.getName() - + " for event " + triggeringEvent.getName()); - return strategy; - } - } - - return null; - } - - /** - * Graceful stop the adaptation process. - */ - public void stop() { - logger.info("Stopping adaptation process"); - return; - } - - /** - * Aborts the adaptation process because of errors - */ - public void abort() { - logger.error("Stopping adaptation process due to errors."); - stop(); - } + private void analyzeModel() { + modelAnalyzer.analyze(); + } + + private void applyTactic(WeightedTactic weightedTactic) { + executor.applyTactic(weightedTactic); + } + + private boolean isProblemSolved() { + // TODO evaluate the results (problem solved?) + // TODO Implement correct check of objective + return problemSolved; + } + + private void printLogInformation() { + TacticsHistory hist = TacticsHistory.getInstance(); + hist.printToSysOut(); + } + + private static Strategy findTriggeredStrategy(AdaptationProcess staRepo, Event triggeringEvent) { + + String eventName = triggeringEvent.getName(); + String eventId = triggeringEvent.getId(); + + logger.debug("Caught event: " + eventName + " (ID: " + eventId + ")"); + + for (Strategy strategy : staRepo.getStrategies()) { + if (strategy.getTriggeringEvents().getId().equals(eventId) + && strategy.getTriggeringEvents().getName().equals(eventName)) { + logger.debug("Found suitable strategy " + strategy.getName() + " for event " + + triggeringEvent.getName()); + return strategy; + } + } + + return null; + } + + /** + * Graceful stop the adaptation process. + */ + public void stop() { + logger.info("Stopping adaptation process"); + return; + } + + /** + * Aborts the adaptation process because of errors + */ + public void abort() { + logger.error("Stopping adaptation process due to errors."); + stop(); + } } -- GitLab