Skip to content
Snippets Groups Projects
Commit b70331eb authored by Nikolaus Huber's avatar Nikolaus Huber
Browse files

refactorings in weightingfunction package to provide interface for...

refactorings in weightingfunction package to provide interface for implementing user specific weighting functions

git-svn-id: https://se1.informatik.uni-wuerzburg.de/usvn/svn/code/code/DMM/trunk@11918 9e42b895-fcda-4063-8a3b-11be15eb1bbd
parent 94b88941
No related branches found
No related tags found
No related merge requests found
package edu.kit.ipd.descartes.adaptation.evaluation;
import edu.kit.ipd.descartes.adaptation.evaluation.weightingfunction.WeightedSumCalculator;
import org.apache.log4j.Logger;
import edu.kit.ipd.descartes.adaptation.evaluation.weightingfunction.IWeightingFunction;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
import edu.kit.ipd.descartes.mm.adaptation.WeightingFunction;
......@@ -12,12 +14,9 @@ import edu.kit.ipd.descartes.mm.adaptation.WeightingFunction;
*/
public class WeightingFunctionEvaluator implements IEvaluator {
private WeightedSumCalculator calculator = null;
private IWeightingFunction calculator = null;
private static Logger logger = Logger.getLogger(WeightingFunctionEvaluator.class);
public WeightingFunctionEvaluator() {
calculator = new WeightedSumCalculator();
}
/*
* (non-Javadoc)
*
......@@ -26,7 +25,27 @@ public class WeightingFunctionEvaluator implements IEvaluator {
@Override
public void evaluate(WeightedTactic tactic) {
WeightingFunction f = tactic.getParentStrategy().getWeightingFunction();
calculator.setWeightingFunction(f);
calculator.updateWeight(tactic);
final String functionClassName = f.getWeightingFunction();
Object o = null;
try {
o = Class.forName(functionClassName).newInstance();
} catch (final ClassNotFoundException e) {
e.printStackTrace();
} catch (final InstantiationException e) {
e.printStackTrace();
} catch (final IllegalAccessException e) {
e.printStackTrace();
}
if (!IWeightingFunction.class.isInstance(o)) {
throw new RuntimeException("Expected a class implementing " + IWeightingFunction.class.getName());
}
calculator = (IWeightingFunction) o;
double newWeight = calculator.calculateWeight(tactic);
logger.debug("Setting new weight of " + tactic.getName() + " to " + newWeight);
tactic.setCurrentWeight(newWeight);
}
}
......@@ -3,7 +3,7 @@ package edu.kit.ipd.descartes.adaptation.evaluation.weightingfunction;
import edu.kit.ipd.descartes.mm.adaptation.Tactic;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
public interface IWeightingFunctionCalculator {
public interface IWeightingFunction {
/**
* Calculates the new weight of the given {@link Tactic}.
......@@ -12,13 +12,4 @@ public interface IWeightingFunctionCalculator {
* @return
*/
double calculateWeight(WeightedTactic weightedTactic);
/**
* Calculates and sets the new weight of the given {@link WeightedTactic}.
*
* @param impact
* @return
*/
void updateWeight(WeightedTactic weightedTactic);
}
......@@ -4,16 +4,11 @@ import java.util.Random;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
public class RandomWeightCalculator implements IWeightingFunctionCalculator {
public class RandomWeightsWeightingFunction implements IWeightingFunction {
@Override
public double calculateWeight(WeightedTactic weightedTactic) {
Random r = new Random(System.currentTimeMillis());
return r.nextDouble();
}
@Override
public void updateWeight(WeightedTactic weightedTactic) {
weightedTactic.setCurrentWeight(calculateWeight(weightedTactic));
}
}
......@@ -9,18 +9,15 @@ import edu.kit.ipd.descartes.perfdatarepo.Impact;
import edu.kit.ipd.descartes.perfdatarepo.MetricValue;
import edu.kit.ipd.descartes.perfdatarepo.Result;
public class WeightedSumCalculator implements IWeightingFunctionCalculator {
public class WeightedSum implements IWeightingFunction {
private Logger logger = Logger.getLogger(WeightedSumCalculator.class);
private Logger logger = Logger.getLogger(WeightedSum.class);
private WeightingFunction weightingFunction = null;
public WeightedSumCalculator(WeightingFunction f) {
public WeightedSum(WeightingFunction f) {
setWeightingFunction(f);
}
public WeightedSumCalculator() {
}
public WeightingFunction getWeightingFunction() {
return weightingFunction;
}
......@@ -41,12 +38,12 @@ public class WeightedSumCalculator implements IWeightingFunctionCalculator {
for (MetricValue afterMetricValue : afterMetricValues) {
// calculated delta=(after-before)
double afterValue = afterMetricValue.getValue();
double beforeValue = WeightingFunctionHelper
double beforeValue = WeightedSumWeightingFunctionHelper
.getValueForMetricType(afterMetricValue.getMetricType(), before);
double delta = afterValue - beforeValue;
// get the corresponding weight
double weightOfMetric = WeightingFunctionHelper.getWeightForMetricType(weightingFunction,
double weightOfMetric = WeightedSumWeightingFunctionHelper.getWeightForMetricType(weightingFunction,
afterMetricValue.getMetricType());
if (Double.isNaN(weightOfMetric))
......@@ -61,12 +58,4 @@ public class WeightedSumCalculator implements IWeightingFunctionCalculator {
return newWeight;
}
@Override
public void updateWeight(WeightedTactic weightedTactic) {
double newWeight = calculateWeight(weightedTactic);
logger.debug("Setting new weight of " + weightedTactic.getName() + " to " + newWeight);
weightedTactic.setCurrentWeight(newWeight);
}
}
......@@ -10,9 +10,9 @@ import edu.kit.ipd.descartes.perfdatarepo.MetricType;
import edu.kit.ipd.descartes.perfdatarepo.MetricValue;
import edu.kit.ipd.descartes.perfdatarepo.Result;
public class WeightingFunctionHelper {
public class WeightedSumWeightingFunctionHelper {
static Logger logger = Logger.getLogger(WeightingFunctionHelper.class);
static Logger logger = Logger.getLogger(WeightedSumWeightingFunctionHelper.class);
/**
*
......
......@@ -2,8 +2,7 @@ package edu.kit.ipd.descartes.adaptation.model.dmm.util;
import java.util.List;
import edu.kit.ipd.descartes.adaptation.evaluation.weightingfunction.WeightingFunctionHelper;
import edu.kit.ipd.descartes.adaptation.ql.QueryLanguageAdapter;
import edu.kit.ipd.descartes.adaptation.evaluation.weightingfunction.WeightedSumWeightingFunctionHelper;
import edu.kit.ipd.descartes.mm.adaptation.Event;
import edu.kit.ipd.descartes.mm.adaptation.RelationalOperator;
import edu.kit.ipd.descartes.mm.adaptation.Specification;
......@@ -36,7 +35,7 @@ public class ObjectivesHelper {
for (Specification specification : specs) {
MetricType m = specification.getMetricType();
// TODO: Connect QueryEngine: Double actualValue = QueryLanguageAdapter.getValueForMetricType(m);
Double actualValue = WeightingFunctionHelper.getValueForMetricType(m, result);
Double actualValue = WeightedSumWeightingFunctionHelper.getValueForMetricType(m, result);
Double threshold = specification.getValue();
if (!ObjectivesHelper.compare(actualValue, threshold, specification.getRelationalOperator()))
return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment