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

git-svn-id: https://se1.informatik.uni-wuerzburg.de/usvn/svn/code/code/DMM/trunk@10493 9e42b895-fcda-4063-8a3b-11be15eb1bbd
parents
No related branches found
No related tags found
No related merge requests found
package edu.kit.ipd.descartes.adaptation.util;
import java.util.Set;
import org.apache.log4j.Logger;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.ocl.OCL;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.Query;
import org.eclipse.ocl.ecore.Constraint;
import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
import org.eclipse.ocl.expressions.OCLExpression;
import org.eclipse.ocl.helper.OCLHelper;
/**
* @author nhuber
*
*/
public class OclEvaluationHelper {
private static Logger logger = Logger.getLogger(OclEvaluationHelper.class);
/**
* @param checkedObject the checked object
* @param constraint the constraint to evaluate on the object
* @param context the {@link EClass} of the given object
* @return
* {@code true} if check was successful
*/
public static boolean evaluateOclConstraint(EObject checkedObject, String constraint, EClass context) {
Constraint invariant = null;
OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject> ocl;
ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl.createOCLHelper();
helper.setContext(context);
try {
invariant = helper.createInvariant(constraint);
} catch (ParserException e) {
logger.error("Error parsing OCL string '" + constraint + "'", e);
return false;
}
Query<EClassifier, EClass, EObject> eval = ocl.createQuery(invariant);
return eval.check(checkedObject);
}
/**
* Executes the query on a given object.
*
* @param obj the model instance the query is executed on
* @param queryExpression the OCL query
* @param context the eClass of the model instance
* @return
*/
public static Set<EObject> query(EObject obj, String queryExpression, EClassifier context) {
OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject> ocl;
ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl.createOCLHelper();
helper.setContext(context);
OCLExpression<EClassifier> query;
try {
query = helper.createQuery(queryExpression);
} catch (ParserException e) {
logger.error("Error parsing OCL query '" + queryExpression + "'", e);
return null;
}
// create a Query to evaluate our query expression
Query<EClassifier, EClass, EObject> queryEval = ocl.createQuery(query);
Object result = queryEval.evaluate(obj);
if (result.toString().contains("invalid"))
return null;
@SuppressWarnings("unchecked")
Set<EObject> resultList = (Set<EObject>) result;
return resultList;
}
}
package edu.kit.ipd.descartes.adaptation.util;
import org.eclipse.emf.common.util.EList;
import edu.kit.ipd.descartes.perfdatarepo.Impact;
import edu.kit.ipd.descartes.perfdatarepo.PerformanceDataRepository;
import edu.kit.ipd.descartes.perfdatarepo.Result;
public class PerfDataRepoHelper {
/**
* Queries for the latest impact. The latest impact is determined by the
* {@link Result} with the most recent timestamp. Currently it is the last one in
* the list.
*/
public static Impact getLatestImpact(PerformanceDataRepository perfDataRepo) {
EList<Impact> impactHistory = perfDataRepo.getImpactHistory();
Impact mostRecentImpact = impactHistory.get(impactHistory.size() - 1);
for (Impact currentImpact : impactHistory) {
Result afterResult = currentImpact.getAfter();
if (afterResult.getTimestamp().compareTo(mostRecentImpact.getAfter().getTimestamp()) > 0 )
mostRecentImpact = currentImpact;
}
return mostRecentImpact;
}
/**
* Returns the {@link Impact} at position {@code position} in the
* impact list of the given {@link PerformanceDataRepository}
*
* @param perfDataRepo {@link PerformanceDataRepository} containing all impacts
* @param position The position of the impact to return
* @return
*/
public static Impact getImpactAt(PerformanceDataRepository perfDataRepo, int position) {
EList<Impact> impactHistory = perfDataRepo.getImpactHistory();
// FIXME: Add more data to PerfDataRepo
if (position > impactHistory.size() - 1)
return impactHistory.get(0);
return impactHistory.get(position);
}
}
package edu.kit.ipd.descartes.adaptation.util;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import edu.kit.ipd.descartes.mm.adaptation.WeightedMetric;
import edu.kit.ipd.descartes.mm.adaptation.WeightingFunction;
import edu.kit.ipd.descartes.perfdatarepo.MetricType;
import edu.kit.ipd.descartes.perfdatarepo.MetricValue;
import edu.kit.ipd.descartes.perfdatarepo.Result;
public class WeightingFunctionHelper {
/**
*
* @param function
* @param metricType
* @return
* Returns the weight of the {@code meticType} of the given {@code function}.<br>
* Returns {@code Double.NaN} if the {@code metricType} was not found.
*/
public static double getWeightForMetricType(WeightingFunction function, MetricType metricType) {
EList<WeightedMetric> weightedMetrics = function.getWeightedMetrics();
for (WeightedMetric weightedMetric : weightedMetrics) {
if (EcoreUtil.equals(weightedMetric.getMetricType(), metricType))
return weightedMetric.getWeight();
}
return Double.NaN;
}
/**
* Searches the {@link MetricValue} of the given {@link MetricType} in the
* {@link Result} <code>resultBefore</code>
*
* @param metricType The type of the metric to find
* @param resultBefore The result to search
* @return
*/
public static double getValueForMetricType(MetricType metricType, Result resultBefore) {
EList<MetricValue> metricValues = resultBefore.getMetricValues();
for (MetricValue metricValue : metricValues) {
if (metricValue.getMetricType().equals(metricType))
return metricValue.getValue();
}
return Double.NaN;
}
}
package edu.kit.ipd.descartes.adaptation.weightingfunction;
import edu.kit.ipd.descartes.mm.adaptation.Tactic;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
public interface IWeightingFunctionCalculator {
/**
* Calculates the new weight of the given {@link Tactic}.
*
* @param impact
* @return
*/
double calculateWeight(WeightedTactic weightedTactic);
/**
* Calculates and sets the new weight of the given {@link WeightedTactic}.
*
* @param impact
* @return
*/
void updateWeight(WeightedTactic weightedTactic);
}
package edu.kit.ipd.descartes.adaptation.weightingfunction;
import java.util.Random;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
public class RandomWeightCalculator implements IWeightingFunctionCalculator {
@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));
}
}
package edu.kit.ipd.descartes.adaptation.weightingfunction;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EList;
import edu.kit.ipd.descartes.adaptation.util.WeightingFunctionHelper;
import edu.kit.ipd.descartes.mm.adaptation.WeightedTactic;
import edu.kit.ipd.descartes.mm.adaptation.WeightingFunction;
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 {
private Logger logger = Logger.getLogger(WeightedSumCalculator.class);
private WeightingFunction weightingFunction = null;
public WeightedSumCalculator(WeightingFunction weightingFunction) {
this.weightingFunction = weightingFunction;
}
@Override
public double calculateWeight(WeightedTactic weightedtactic) {
double newWeight = 0.0;
Impact impact = weightedtactic.getLastImpact();
Result before = impact.getBefore();
Result after = impact.getAfter();
EList<MetricValue> afterMetricValues = after.getMetricValues();
for (MetricValue afterMetricValue : afterMetricValues) {
// calculated delta=(after-before)
double afterValue = afterMetricValue.getValue();
double beforeValue = WeightingFunctionHelper
.getValueForMetricType(afterMetricValue.getMetricType(), before);
double delta = afterValue - beforeValue;
// get the corresponding weight
double weightOfMetric = WeightingFunctionHelper.getWeightForMetricType(weightingFunction,
afterMetricValue.getMetricType());
if (Double.isNaN(weightOfMetric))
logger.error("No weight found for metric type: " + afterMetricValue.getMetricType().getName());
else {
// add it
newWeight += weightOfMetric * delta;
logger.debug("Delta for metric type " + afterMetricValue.getMetricType().getName() + " was: " + delta
+ " and weight is " + weightOfMetric);
}
}
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);
}
}
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