| 1 | package de.uka.ipd.sdq.stoex.analyser.visitors; |
| 2 | |
| 3 | import java.text.DecimalFormat; |
| 4 | import java.text.DecimalFormatSymbols; |
| 5 | import java.util.Locale; |
| 6 | |
| 7 | import org.eclipse.emf.ecore.EObject; |
| 8 | |
| 9 | import de.uka.ipd.sdq.probfunction.print.ProbFunctionPrettyPrint; |
| 10 | import de.uka.ipd.sdq.stoex.BoolLiteral; |
| 11 | import de.uka.ipd.sdq.stoex.BooleanOperatorExpression; |
| 12 | import de.uka.ipd.sdq.stoex.CompareExpression; |
| 13 | import de.uka.ipd.sdq.stoex.DoubleLiteral; |
| 14 | import de.uka.ipd.sdq.stoex.FunctionLiteral; |
| 15 | import de.uka.ipd.sdq.stoex.IfElseExpression; |
| 16 | import de.uka.ipd.sdq.stoex.IntLiteral; |
| 17 | import de.uka.ipd.sdq.stoex.NamespaceReference; |
| 18 | import de.uka.ipd.sdq.stoex.NegativeExpression; |
| 19 | import de.uka.ipd.sdq.stoex.NotExpression; |
| 20 | import de.uka.ipd.sdq.stoex.Parenthesis; |
| 21 | import de.uka.ipd.sdq.stoex.PowerExpression; |
| 22 | import de.uka.ipd.sdq.stoex.ProbabilityFunctionLiteral; |
| 23 | import de.uka.ipd.sdq.stoex.ProductExpression; |
| 24 | import de.uka.ipd.sdq.stoex.StringLiteral; |
| 25 | import de.uka.ipd.sdq.stoex.TermExpression; |
| 26 | import de.uka.ipd.sdq.stoex.Variable; |
| 27 | import de.uka.ipd.sdq.stoex.VariableReference; |
| 28 | import de.uka.ipd.sdq.stoex.util.StoexSwitch; |
| 29 | |
| 30 | public class StoExPrettyPrintVisitor extends StoexSwitch<String> { |
| 31 | @Override |
| 32 | public String caseBoolLiteral(BoolLiteral object) { |
| 33 | return object.isValue() ? "true" : "false"; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public String casePowerExpression(PowerExpression object) { |
| 38 | return ((String)doSwitch(object.getBase()))+" ^ "+((String)doSwitch(object.getExponent())); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public String caseStringLiteral(StringLiteral object) { |
| 43 | return "\""+object.getValue()+"\""; |
| 44 | } |
| 45 | |
| 46 | /* (non-Javadoc) |
| 47 | * @see de.uka.ipd.sdq.pcm.core.stochastics.util.StochasticsSwitch#caseCompareExpression(de.uka.ipd.sdq.pcm.core.stochastics.CompareExpression) |
| 48 | */ |
| 49 | @Override |
| 50 | public String caseCompareExpression(CompareExpression object) { |
| 51 | String op = ""; |
| 52 | switch(object.getOperation()) |
| 53 | { |
| 54 | case EQUALS: |
| 55 | op = " == "; |
| 56 | break; |
| 57 | case GREATER: |
| 58 | op = " > "; |
| 59 | break; |
| 60 | case GREATEREQUAL: |
| 61 | op = " >= "; |
| 62 | break; |
| 63 | case LESS: |
| 64 | op = " < "; |
| 65 | break; |
| 66 | case LESSEQUAL: |
| 67 | op = " <= "; |
| 68 | break; |
| 69 | case NOTEQUAL: |
| 70 | op = " <> "; |
| 71 | break; |
| 72 | } |
| 73 | return ((String)doSwitch(object.getLeft()))+op+((String)doSwitch(object.getRight())); |
| 74 | } |
| 75 | |
| 76 | /* (non-Javadoc) |
| 77 | * @see de.uka.ipd.sdq.pcm.core.stochastics.util.StochasticsSwitch#caseDoubleLiteral(de.uka.ipd.sdq.pcm.core.stochastics.DoubleLiteral) |
| 78 | */ |
| 79 | @Override |
| 80 | public String caseDoubleLiteral(DoubleLiteral object) { |
| 81 | DecimalFormat df = new DecimalFormat("#0.0###########", new DecimalFormatSymbols(Locale.US)); |
| 82 | //DecimalFormat df = new DecimalFormat("#0.0#", new DecimalFormatSymbols(Locale.US)); |
| 83 | return df.format(object.getValue()); |
| 84 | } |
| 85 | |
| 86 | /* (non-Javadoc) |
| 87 | * @see de.uka.ipd.sdq.pcm.core.stochastics.util.StochasticsSwitch#caseIntLiteral(de.uka.ipd.sdq.pcm.core.stochastics.IntLiteral) |
| 88 | */ |
| 89 | @Override |
| 90 | public String caseIntLiteral(IntLiteral object) { |
| 91 | return Integer.toString(object.getValue()); |
| 92 | } |
| 93 | |
| 94 | /* (non-Javadoc) |
| 95 | * @see de.uka.ipd.sdq.pcm.core.stochastics.util.StochasticsSwitch#caseParantesis(de.uka.ipd.sdq.pcm.core.stochastics.Parantesis) |
| 96 | */ |
| 97 | @Override |
| 98 | public String caseParenthesis(Parenthesis object) { |
| 99 | return "( " + (String)doSwitch(object.getInnerExpression())+" )"; |
| 100 | } |
| 101 | |
| 102 | /* (non-Javadoc) |
| 103 | * @see de.uka.ipd.sdq.pcm.core.stochastics.util.StochasticsSwitch#caseProbabilityFunctionLiteral(de.uka.ipd.sdq.pcm.core.stochastics.ProbabilityFunctionLiteral) |
| 104 | */ |
| 105 | @Override |
| 106 | public String caseProbabilityFunctionLiteral(ProbabilityFunctionLiteral object) { |
| 107 | return new ProbFunctionPrettyPrint().doSwitch(object.getFunction_ProbabilityFunctionLiteral()); |
| 108 | } |
| 109 | |
| 110 | /* (non-Javadoc) |
| 111 | * @see de.uka.ipd.sdq.pcm.core.stochastics.util.StochasticsSwitch#caseProductExpression(de.uka.ipd.sdq.pcm.core.stochastics.ProductExpression) |
| 112 | */ |
| 113 | @Override |
| 114 | public String caseProductExpression(ProductExpression object) { |
| 115 | String op = ""; |
| 116 | switch(object.getOperation()) |
| 117 | { |
| 118 | case MULT: |
| 119 | op = " * "; |
| 120 | break; |
| 121 | case MOD: |
| 122 | op = " % "; |
| 123 | break; |
| 124 | case DIV: |
| 125 | op = " / "; |
| 126 | break; |
| 127 | } |
| 128 | return ((String)doSwitch(object.getLeft()))+op+((String)doSwitch(object.getRight())); } |
| 129 | |
| 130 | /* (non-Javadoc) |
| 131 | * @see de.uka.ipd.sdq.pcm.core.stochastics.util.StochasticsSwitch#caseTermExpression(de.uka.ipd.sdq.pcm.core.stochastics.TermExpression) |
| 132 | */ |
| 133 | @Override |
| 134 | public String caseTermExpression(TermExpression object) { |
| 135 | String op = ""; |
| 136 | switch(object.getOperation()) |
| 137 | { |
| 138 | case ADD: |
| 139 | op = " + "; |
| 140 | break; |
| 141 | case SUB: |
| 142 | op = " - "; |
| 143 | break; |
| 144 | } |
| 145 | return ((String)doSwitch(object.getLeft()))+op+((String)doSwitch(object.getRight())); |
| 146 | } |
| 147 | |
| 148 | /* (non-Javadoc) |
| 149 | * @see de.uka.ipd.sdq.pcm.parameter.util.ParameterSwitch#caseNamespaceReference(de.uka.ipd.sdq.pcm.parameter.NamespaceReference) |
| 150 | */ |
| 151 | @Override |
| 152 | public String caseNamespaceReference(NamespaceReference object) { |
| 153 | return object.getReferenceName()+"."+(String)doSwitch(object.getInnerReference_NamespaceReference()); |
| 154 | } |
| 155 | |
| 156 | /* (non-Javadoc) |
| 157 | * @see de.uka.ipd.sdq.pcm.parameter.util.ParameterSwitch#caseVariableReference(de.uka.ipd.sdq.pcm.parameter.VariableReference) |
| 158 | */ |
| 159 | @Override |
| 160 | public String caseVariableReference(VariableReference object) { |
| 161 | return object.getReferenceName(); |
| 162 | } |
| 163 | |
| 164 | /* (non-Javadoc) |
| 165 | * @see de.uka.ipd.sdq.pcm.core.stochastics.util.StochasticsSwitch#caseVariable(de.uka.ipd.sdq.pcm.core.stochastics.Variable) |
| 166 | */ |
| 167 | @Override |
| 168 | public String caseVariable(Variable object) { |
| 169 | String result = (String)doSwitch(object.getId_Variable()); |
| 170 | // TODO: Move this part: result += "." + object.getCharacterisationType().getLiteral(); |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | public String prettyPrint(EObject theObject) |
| 175 | { |
| 176 | return (String)doSwitch(theObject); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public String caseBooleanOperatorExpression(BooleanOperatorExpression object) { |
| 181 | String result = this.doSwitch(object.getLeft()); |
| 182 | result += " "+object.getOperation().getLiteral()+" "; |
| 183 | return result + this.doSwitch(object.getRight()); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public String caseNegativeExpression(NegativeExpression object) { |
| 188 | return "-"+this.doSwitch(object.getInner()); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public String caseNotExpression(NotExpression object) { |
| 193 | return "NOT "+this.doSwitch(object.getInner())+""; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public String caseFunctionLiteral(FunctionLiteral object) { |
| 198 | String result = object.getId() + "("; |
| 199 | for (int i=0; i < object.getParameters_FunctionLiteral().size()-1; i++) |
| 200 | result += this.doSwitch(object.getParameters_FunctionLiteral().get(i)) + ", "; |
| 201 | result += this.doSwitch(object.getParameters_FunctionLiteral().get(object.getParameters_FunctionLiteral().size()-1)) + ")"; |
| 202 | return result; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public String caseIfElseExpression(IfElseExpression object) { |
| 207 | return this.doSwitch(object.getConditionExpression()) + " ? " + |
| 208 | this.doSwitch(object.getIfExpression()) + " : " + |
| 209 | this.doSwitch(object.getElseExpression()); |
| 210 | } |
| 211 | |
| 212 | } |