| 1 | package de.uka.ipd.sdq.pcm.transformations; |
| 2 | |
| 3 | import org.eclipse.emf.common.util.EList; |
| 4 | |
| 5 | import de.uka.ipd.sdq.pcm.repository.CollectionDataType; |
| 6 | import de.uka.ipd.sdq.pcm.repository.CompositeDataType; |
| 7 | import de.uka.ipd.sdq.pcm.repository.DataType; |
| 8 | import de.uka.ipd.sdq.pcm.repository.InnerDeclaration; |
| 9 | import de.uka.ipd.sdq.pcm.repository.OperationSignature; |
| 10 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
| 11 | import de.uka.ipd.sdq.pcm.repository.ParameterModifier; |
| 12 | import de.uka.ipd.sdq.pcm.repository.PrimitiveDataType; |
| 13 | |
| 14 | /** |
| 15 | * Responsible for determining a bytesize characterisation string for parameters in a signature. |
| 16 | * @author Steffen |
| 17 | * |
| 18 | */ |
| 19 | public class BytesizeComputationForSignature { |
| 20 | |
| 21 | /** |
| 22 | * @author Snowball |
| 23 | */ |
| 24 | public enum Modifier{ |
| 25 | IN, |
| 26 | OUT |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Composes a String, which includes the sum of BYTESIZE |
| 31 | * characterisation strings for all the parameters of a given signature. |
| 32 | * For example: |
| 33 | * <code>void foo(int bar, File blub)</code> |
| 34 | * would be tranformed into |
| 35 | * <code>"bar.BYTESIZE + blub.BYTESIZE"</code> |
| 36 | * |
| 37 | * @param sig |
| 38 | * @param mod |
| 39 | * @return |
| 40 | */ |
| 41 | public static String getBytesizeForSignature(OperationSignature sig, Modifier mod){ |
| 42 | StringBuffer result = new StringBuffer(); |
| 43 | |
| 44 | EList<Parameter> params = sig.getParameters__OperationSignature(); |
| 45 | for (Parameter param : params){ |
| 46 | DataType dataType = param.getDataType__Parameter(); |
| 47 | ParameterModifier parMod = param.getModifier__Parameter(); |
| 48 | if (mod == Modifier.IN){ |
| 49 | if (parMod == ParameterModifier.IN || |
| 50 | parMod == ParameterModifier.INOUT || |
| 51 | parMod == ParameterModifier.NONE){ |
| 52 | result.append(getCharacterisationString(dataType, param.getParameterName())); |
| 53 | } |
| 54 | } else { |
| 55 | if (parMod == ParameterModifier.INOUT || |
| 56 | parMod == ParameterModifier.OUT){ |
| 57 | result.append(getCharacterisationString(dataType, param.getParameterName())); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (mod == Modifier.OUT){ |
| 63 | DataType returnType = sig.getReturnType__OperationSignature(); |
| 64 | if (returnType != null){ |
| 65 | result.append(getCharacterisationString(returnType, "RETURN")); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int length = result.length(); |
| 70 | if (length > 0) |
| 71 | result.delete(length-3, length); // remove last " + " |
| 72 | else if (length == 0) |
| 73 | result.append("0"); |
| 74 | |
| 75 | return result.toString(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Composes the characterisation string for a single parameter. For collection types and composite types, the inner declarations are respected. |
| 80 | * For example |
| 81 | * |
| 82 | * <code>int bar</code> would be tranformed into <code>"bar.BYTESIZE"</code> |
| 83 | * <code>int[] myIntArray</code> would be tranformed into <code>"myIntArray.BYTESIZE + myIntArray.NUMBER_OF_ELEMENTS * myIntArray.INNER.BYTESIZE"</code> |
| 84 | * With a composite data type <code>Compositum {int a; String b}</code> <code>Compositum myCompositum</code> would be transformed into <code>"myCompositum.BYTESIZE + myCompositum.a.BYTESIZE + myCompositum.b.BYTESIZE"</code> |
| 85 | * |
| 86 | * The method calls itself recursively for non-primitive data types. |
| 87 | * |
| 88 | * @param dataType The data type of the parameter for that the characterisation string is created |
| 89 | * @param name The name of the parameter for that the characterisation string is created |
| 90 | * @return A String with the proper BYTESIZE characterisations for the given type and name of parameter |
| 91 | */ |
| 92 | private static String getCharacterisationString(DataType dataType, String name){ |
| 93 | StringBuffer result = new StringBuffer(); |
| 94 | if (dataType instanceof PrimitiveDataType){ |
| 95 | result.append(name+".BYTESIZE + "); |
| 96 | } else if (dataType instanceof CollectionDataType){ |
| 97 | CollectionDataType collDataType = (CollectionDataType)dataType; |
| 98 | result.append(name+".BYTESIZE + "); |
| 99 | DataType innerDataType = collDataType.getInnerType_CollectionDataType(); |
| 100 | String innerSize = getCharacterisationString(innerDataType, name + ".INNER"); |
| 101 | result.append(name+".NUMBER_OF_ELEMENTS * "+ innerSize); |
| 102 | } else if (dataType instanceof CompositeDataType){ |
| 103 | CompositeDataType compDataType = (CompositeDataType)dataType; |
| 104 | EList<InnerDeclaration> innerList = compDataType.getInnerDeclaration_CompositeDataType(); |
| 105 | result.append("("); |
| 106 | result.append(name+".BYTESIZE + "); |
| 107 | for (InnerDeclaration decl : innerList){ |
| 108 | DataType innerDataType = decl.getDatatype_InnerDeclaration(); |
| 109 | result.append(getCharacterisationString(innerDataType, name+"."+decl.getEntityName())); |
| 110 | } |
| 111 | result.delete(result.length()-3, result.length()); |
| 112 | result.append(") + "); |
| 113 | } |
| 114 | return result.toString(); |
| 115 | } |
| 116 | } |