Skip to content
Snippets Groups Projects
Commit 754eb507 authored by Simon's avatar Simon
Browse files

cleaned up code and improved error handling

parent e7f73ed6
No related branches found
No related tags found
1 merge request!1Randomvarhelper
......@@ -18,18 +18,21 @@ public class CalculateMeanCommand extends AbstractHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
RandomVariable var = extractRandomVar(event);
double d = RandomVariableHelper.getNumericalMean(var.getProbFunction());
displayMean(d);
try {
double d = RandomVariableHelper.getNumericalMean(var.getProbFunction());
displayString("The mean of the selected RandomVariable is " + d, "Calculate Mean");
} catch (Exception e) {
displayString(e.getMessage(), "Error");
}
return null;
}
private void displayMean(double d) {
private void displayString(String message, String title) {
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
public void run() {
Shell activeShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
MessageDialog dialog = new MessageDialog(activeShell, "Calculate Mean", null,
"The mean of the selected RandomVariable is " + d, 0, 0, "OK");
MessageDialog dialog = new MessageDialog(activeShell, title, null,
message, 0, 0, "OK");
dialog.open();
}
});
......
......@@ -19,16 +19,41 @@ import tools.descartes.dml.mm.applicationlevel.functions.ProbabilityMassFunction
import tools.descartes.dml.mm.applicationlevel.functions.SampleList;
/**
* Provides helper functions for ProbabilityFunctions, e.g., to compute their mean value.
* Provides helper functions for ProbabilityFunctions, e.g., to compute their
* mean value.
*
* @author Fabian Brosig
*/
public class RandomVariableHelper {
/*
* I know it is ugly...but apparently there is no other solution that works
* (With QVTO blackboxes, the probfn parameter seems to be explicitly casted so
* that method overloading does not work as expected.)
*/
public static Double getNumericalMean(ProbabilityFunction probfn) {
if (probfn instanceof IntLiteral)
return _getNumericalMean((IntLiteral) probfn);
if (probfn instanceof DoubleLiteral)
return _getNumericalMean((DoubleLiteral) probfn);
if (probfn instanceof BooleanLiteral)
return _getNumericalMean((BooleanLiteral) probfn);
if (probfn instanceof ExponentialDistribution)
return _getNumericalMean((ExponentialDistribution) probfn);
if (probfn instanceof NormalDistribution)
return _getNumericalMean((NormalDistribution) probfn);
if (probfn instanceof BoxedPDF)
return _getNumericalMean((BoxedPDF) probfn);
if (probfn instanceof ProbabilityMassFunction)
return _getNumericalMean((ProbabilityMassFunction) probfn);
return _getNumericalMean(probfn);
}
private static Double _getNumericalMean(ProbabilityMassFunction pmf) {
return RandomVariableHelper._getNumericalMean(pmf.getSamples());
}
private static Double _getNumericalMean(IntSampleList intsl) {
int[] singletons = new int[intsl.getItems().size()];
double[] probabilities = new double[intsl.getItems().size()];
......@@ -39,7 +64,7 @@ public class RandomVariableHelper {
EnumeratedIntegerDistribution dist = new EnumeratedIntegerDistribution(singletons, probabilities);
return dist.getNumericalMean();
}
private static Double _getNumericalMean(DoubleSampleList doublesl) {
double[] singletons = new double[doublesl.getItems().size()];
double[] probabilities = new double[doublesl.getItems().size()];
......@@ -52,22 +77,25 @@ public class RandomVariableHelper {
}
private static Double _getNumericalMean(SampleList sl) {
// I know it is ugly...
if (sl instanceof IntSampleList) return _getNumericalMean( (IntSampleList) sl );
else if (sl instanceof BoolSampleList) return _getNumericalMean( (BoolSampleList) sl );
else if (sl instanceof DoubleSampleList) return _getNumericalMean( (DoubleSampleList) sl );
else if (sl instanceof EnumSampleList) return _getNumericalMean( (EnumSampleList) sl );
return null;
if (sl instanceof IntSampleList)
return _getNumericalMean((IntSampleList) sl);
else if (sl instanceof BoolSampleList)
return _getNumericalMean((BoolSampleList) sl);
else if (sl instanceof DoubleSampleList)
return _getNumericalMean((DoubleSampleList) sl);
else if (sl instanceof EnumSampleList)
return _getNumericalMean((EnumSampleList) sl);
throw new IllegalStateException("Unknown type of SampleList: " + sl.getClass().getName());
}
private static Double _getNumericalMean(EnumSampleList enumsl) {
throw new UnsupportedOperationException("Enums do not have a numerical mean.");
}
private static Double _getNumericalMean(BoolSampleList enumsl) {
return null;
throw new UnsupportedOperationException("Booleans do not have a numerical mean.");
}
private static Double _getNumericalMean(BoxedPDF bpdf) {
double mean = 0;
double lastValue = 0;
......@@ -78,47 +106,32 @@ public class RandomVariableHelper {
return mean;
}
private static Double _getNumericalMean(NormalDistribution nd) {
return nd.getMu().doubleValue();
}
private static Double _getNumericalMean(ExponentialDistribution expd) {
double rate = expd.getRate().doubleValue();
return (rate > 0) ? 1/rate : null;
double rate = expd.getRate().doubleValue();
if (rate > 0)
return 1 / rate;
throw new UnsupportedOperationException("ExponentialDistribution can not have a rate of 0");
}
private static Double _getNumericalMean(BooleanLiteral l) {
return null;
throw new UnsupportedOperationException("Booleans do not have a numerical mean.");
}
private static Double _getNumericalMean(DoubleLiteral l) {
return l.getValue().doubleValue();
}
private static Double _getNumericalMean(IntLiteral l) {
return l.getValue().doubleValue();
}
private static Double _getNumericalMean(ProbabilityFunction probfn) {
assert false; // thanks to method overloading, this method should never be invoked
return null;
throw new IllegalStateException("Unknown type of ProbabilityFunction: " + probfn.getClass().getName());
}
public static Double getNumericalMean(ProbabilityFunction probfn) {
// I know it is ugly...but apparently there is no other solution that works
// (With QVTO blackboxes, the probfn parameter seems to be explicitly casted so that method overloading does not work as expected.)
if (probfn instanceof IntLiteral) return _getNumericalMean( (IntLiteral) probfn );
else if (probfn instanceof DoubleLiteral) return _getNumericalMean( (DoubleLiteral) probfn );
else if (probfn instanceof BooleanLiteral) return _getNumericalMean( (BooleanLiteral) probfn );
else if (probfn instanceof ExponentialDistribution) return _getNumericalMean( (ExponentialDistribution) probfn );
else if (probfn instanceof NormalDistribution) return _getNumericalMean( (NormalDistribution) probfn );
else if (probfn instanceof BoxedPDF) return _getNumericalMean( (BoxedPDF) probfn );
else if (probfn instanceof ProbabilityMassFunction) return _getNumericalMean( (ProbabilityMassFunction) probfn );
else if (probfn instanceof ProbabilityMassFunction)
return _getNumericalMean((BoxedPDF) probfn);
else { return _getNumericalMean(probfn); }
}
}
}
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