Skip to content
Snippets Groups Projects
Commit a35d175b authored by Long Bui's avatar Long Bui
Browse files

merged changes from origin

parents aa018d6f 754eb507
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();
}
});
......
......@@ -25,6 +25,29 @@ import tools.descartes.dml.mm.applicationlevel.functions.SampleList;
* @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());
......@@ -55,14 +78,13 @@ public class RandomVariableHelper {
private static Double _getNumericalMean(SampleList sl) {
if (sl instanceof IntSampleList)
return _getNumericalMean((IntSampleList) sl);
if (sl instanceof BoolSampleList)
else if (sl instanceof BoolSampleList)
return _getNumericalMean((BoolSampleList) sl);
if (sl instanceof DoubleSampleList)
else if (sl instanceof DoubleSampleList)
return _getNumericalMean((DoubleSampleList) sl);
if (sl instanceof EnumSampleList)
else if (sl instanceof EnumSampleList)
return _getNumericalMean((EnumSampleList) sl);
throw new UnsupportedOperationException("Given SampleList is not supported.");
throw new IllegalStateException("Unknown type of SampleList: " + sl.getClass().getName());
}
private static Double _getNumericalMean(EnumSampleList enumsl) {
......@@ -70,7 +92,7 @@ public class RandomVariableHelper {
}
private static Double _getNumericalMean(BoolSampleList enumsl) {
throw new UnsupportedOperationException("BooleanSampleLists do not have have a numerical mean.");
throw new UnsupportedOperationException("Booleans do not have a numerical mean.");
}
private static Double _getNumericalMean(BoxedPDF bpdf) {
......@@ -96,7 +118,7 @@ public class RandomVariableHelper {
}
private static Double _getNumericalMean(BooleanLiteral l) {
throw new UnsupportedOperationException("BooleanLiterals do not have have a numerical mean.");
throw new UnsupportedOperationException("Booleans do not have a numerical mean.");
}
private static Double _getNumericalMean(DoubleLiteral l) {
......@@ -108,25 +130,7 @@ public class RandomVariableHelper {
}
private static Double _getNumericalMean(ProbabilityFunction probfn) {
throw new UnsupportedOperationException("Given ProbabilityFunction is not supported.");
}
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);
throw new IllegalStateException("Unknown type of ProbabilityFunction: " + probfn.getClass().getName());
}
}
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