1 | package de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reportitems; |
2 | |
3 | import java.util.Vector; |
4 | |
5 | import org.eclipse.core.runtime.IStatus; |
6 | import org.rosuda.JRI.REXP; |
7 | |
8 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.RVisualisationPlugin; |
9 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.utils.RConnection; |
10 | |
11 | /**Report item used to display statistical values. The last result |
12 | * returned from R is considered to be the value of interest. |
13 | * @author groenda (comments, refactoring) |
14 | */ |
15 | public class RCommandRReportItem extends AbstractGeneratedTextRReportItem { |
16 | |
17 | /** The value returned from R after executing the provided command. */ |
18 | protected REXP returnedValue = new REXP(); |
19 | /** The R command which execution yields to the required value. */ |
20 | protected String rCommands; |
21 | |
22 | /**Initializes a new report item. |
23 | * @param rCommands R commands that lead to the calculation of the |
24 | * statistical value. |
25 | * @param description Description of this report item. |
26 | */ |
27 | public RCommandRReportItem(final String rCommands, |
28 | final String description) { |
29 | super(description); |
30 | this.rCommands = rCommands; |
31 | } |
32 | |
33 | /** {@inheritDoc} |
34 | */ |
35 | @Override |
36 | public String getText() { |
37 | // Returns the saved value |
38 | if (returnedValue.rtype == REXP.INTSXP) { |
39 | return "" + returnedValue.asInt(); |
40 | } else if (returnedValue.rtype == REXP.REALSXP) { |
41 | return "" + returnedValue.asDouble(); |
42 | } else if (returnedValue.rtype == REXP.STRSXP) { |
43 | return "" + returnedValue.asString(); |
44 | } else { |
45 | return "N/A"; |
46 | } |
47 | } |
48 | |
49 | /** {@inheritDoc} |
50 | */ |
51 | @Override |
52 | public void visit(final IReportRenderingVisitor renderingVisitor) { |
53 | renderingVisitor.visitGeneratedTextReportItem(this); |
54 | } |
55 | |
56 | |
57 | /** {@inheritDoc} |
58 | */ |
59 | @Override |
60 | public void generateData(final RConnection rConnection) { |
61 | Vector<REXP> result = rConnection.execute(rCommands); |
62 | if (!result.isEmpty()) { |
63 | returnedValue = result.lastElement(); |
64 | } else { |
65 | returnedValue = new REXP(); |
66 | RVisualisationPlugin.log( |
67 | IStatus.WARNING, |
68 | "The calculation of the R command report item \"" |
69 | + getDescription() + "\" yield to no result. " |
70 | + "Most probably data import or the calculation in R " |
71 | + "went wrong. Details: lastConsoleMessage=" |
72 | + rConnection.getLastConsoleMessage()); |
73 | } |
74 | } |
75 | } |