| 1 | package de.uka.ipd.sdq.pcmsolver.transformations.pcm2lqn; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import de.uka.ipd.sdq.identifier.Identifier; |
| 7 | import de.uka.ipd.sdq.pcm.allocation.AllocationContext; |
| 8 | import de.uka.ipd.sdq.pcm.core.composition.AssemblyContext; |
| 9 | import de.uka.ipd.sdq.pcm.core.entity.Entity; |
| 10 | import de.uka.ipd.sdq.pcm.repository.CompositeComponent; |
| 11 | import de.uka.ipd.sdq.pcm.repository.ImplementationComponentType; |
| 12 | import de.uka.ipd.sdq.pcm.repository.OperationSignature; |
| 13 | import de.uka.ipd.sdq.pcm.repository.PassiveResource; |
| 14 | import de.uka.ipd.sdq.pcm.resourceenvironment.LinkingResource; |
| 15 | import de.uka.ipd.sdq.pcm.resourcetype.CommunicationLinkResourceType; |
| 16 | import de.uka.ipd.sdq.pcm.resourcetype.ProcessingResourceType; |
| 17 | import de.uka.ipd.sdq.pcm.seff.AbstractAction; |
| 18 | import de.uka.ipd.sdq.pcm.seff.AcquireAction; |
| 19 | import de.uka.ipd.sdq.pcm.seff.ForkedBehaviour; |
| 20 | import de.uka.ipd.sdq.pcm.seff.ReleaseAction; |
| 21 | import de.uka.ipd.sdq.pcm.seff.ResourceDemandingSEFF; |
| 22 | import de.uka.ipd.sdq.pcm.seff.StartAction; |
| 23 | import de.uka.ipd.sdq.pcm.usagemodel.EntryLevelSystemCall; |
| 24 | import de.uka.ipd.sdq.pcm.usagemodel.Loop; |
| 25 | import de.uka.ipd.sdq.pcm.usagemodel.UsageScenario; |
| 26 | import de.uka.ipd.sdq.pcmsolver.transformations.ContextWrapper; |
| 27 | import de.uka.ipd.sdq.pcmsolver.visitors.EMFQueryHelper; |
| 28 | |
| 29 | public class Pcm2LqnHelper { |
| 30 | |
| 31 | static HashMap<String, Integer> guidMap = new HashMap<String, Integer>(); |
| 32 | static int guidCounter = 0; |
| 33 | |
| 34 | static boolean shortenIds = false; |
| 35 | |
| 36 | public static String fixGUID(String id) { |
| 37 | return id.replaceAll("-", "x"); // to make XML valid |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Generates an unique ID for the given Identifier. |
| 42 | * PCM2LQN cannot use the identifier's guid directly, because |
| 43 | * sometimes for actions inside RDSEFF's also the assembly |
| 44 | * context id and the usage context id must be added to make |
| 45 | * the LQN entities unique. |
| 46 | * |
| 47 | * The following code tries to generate 'pretty' ids that |
| 48 | * shall ease reading the LQN solvers' output. |
| 49 | * |
| 50 | * Takes into account the {@link AssemblyContext} hierarchy of |
| 51 | * model elements and the {@link AllocationContext}, so that |
| 52 | * different component instances will get different ids. |
| 53 | * |
| 54 | * If changing this method, take care to change other methods of this |
| 55 | * class accordingly, e.g. {@link #getIdForEntryLevelSystemCall(EntryLevelSystemCall)}. |
| 56 | * |
| 57 | * @param object |
| 58 | * @param cw |
| 59 | * @return |
| 60 | */ |
| 61 | public static String getId(Identifier object, ContextWrapper cw) { |
| 62 | String className = getClassName(object); |
| 63 | String id = ""; |
| 64 | if (object instanceof ResourceDemandingSEFF){ |
| 65 | ResourceDemandingSEFF rdseff = (ResourceDemandingSEFF)object; |
| 66 | ImplementationComponentType ict = (ImplementationComponentType)rdseff.eContainer(); |
| 67 | String compName = ict.getEntityName(); |
| 68 | |
| 69 | // TODO: I had to cast Signature to OperationSignature to navigate to the interface. |
| 70 | // Check if this still works! FB, 13-06-2010 |
| 71 | String ifName = ((OperationSignature)rdseff.getDescribedService__SEFF()).getInterface__OperationSignature().getEntityName(); |
| 72 | String serviceName = rdseff.getDescribedService__SEFF().getEntityName(); |
| 73 | |
| 74 | id = compName+"_" |
| 75 | +ifName+"_" |
| 76 | +serviceName+"_" |
| 77 | +getAssCtxtAllocCtxtIdString(cw)+ |
| 78 | +getNumberForId(cw.getCompUsgCtx()); |
| 79 | } else if (object instanceof AbstractAction){ |
| 80 | AbstractAction aa = (AbstractAction)object; |
| 81 | String actionName = aa.getEntityName(); |
| 82 | id = className+"_" |
| 83 | + actionName+"_" |
| 84 | + aa.getId()+ "_" |
| 85 | +getAssCtxtAllocCtxtIdString(cw)+"_" |
| 86 | +getNumberForId(cw.getCompUsgCtx());//+"_" |
| 87 | //+getNumberForId(object); |
| 88 | } else if (object instanceof Loop){ |
| 89 | id = "UsageScenario_"+className+"_"+ getNumberForId(object); |
| 90 | } else if (object instanceof PassiveResource){ |
| 91 | // create id that is unique for the pair (PassiveResource,AssemblyContext) |
| 92 | id = "PassiveResource_"+((PassiveResource)object).getEntityName()+"_" |
| 93 | +getAssCtxtAllocCtxtIdString(cw)+"_"+getNumberForId(object); |
| 94 | } else if (object instanceof AcquireAction){ |
| 95 | // create id that is unique for the pair (PassiveResource,AssemblyContext) |
| 96 | id = "AcquireAction_"+((AcquireAction)object).getEntityName()+"_" |
| 97 | +getAssCtxtAllocCtxtIdString(cw)+"_"+getNumberForId(object); |
| 98 | } else if (object instanceof ReleaseAction){ |
| 99 | // create id that is unique for the pair (PassiveResource,AssemblyContext) |
| 100 | id = "ReleaseAction_"+((ReleaseAction)object).getEntityName()+"_" |
| 101 | +getAssCtxtAllocCtxtIdString(cw)+"_"+getNumberForId(object); |
| 102 | } else { |
| 103 | //id = className + fixGUID(object.getId()); |
| 104 | id = className + getNumberForId(object); |
| 105 | } |
| 106 | |
| 107 | return shortenID(id); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | private static String shortenID(String id) { |
| 113 | if (shortenIds && id.length() > 50){ |
| 114 | return id.substring(id.length()-50, id.length()); |
| 115 | } else { |
| 116 | return id; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private static String getClassName(Identifier object) { |
| 121 | return object.getClass().getSimpleName().replaceAll("Impl",""); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Create an id string based on the ids of all {@link AssemblyContext}s of the current components |
| 126 | * (taking into account the hierarchy of {@link AssemblyContext}s of nested {@link CompositeComponent}s) |
| 127 | * and based on the id of the allocation context. |
| 128 | * |
| 129 | * @param cw The current {@link ContextWrapper}. |
| 130 | * @return a unique id string |
| 131 | */ |
| 132 | private static String getAssCtxtAllocCtxtIdString(ContextWrapper cw) { |
| 133 | String assCtxtAllocCtxtName = ""; |
| 134 | List<AssemblyContext> assCtxList = cw.getAssCtxList(); |
| 135 | for (AssemblyContext ac : assCtxList){ |
| 136 | assCtxtAllocCtxtName+=getNumberForId(ac); |
| 137 | } |
| 138 | assCtxtAllocCtxtName += getNumberForId(cw.getAllCtx()); |
| 139 | return shortenID(assCtxtAllocCtxtName); |
| 140 | } |
| 141 | |
| 142 | private static int getNumberForId(Identifier object) { |
| 143 | Integer value = guidMap.get(object.getId()); |
| 144 | if (value==null){ |
| 145 | guidMap.put(object.getId(), ++guidCounter); |
| 146 | return guidCounter; |
| 147 | } else { |
| 148 | return value; |
| 149 | } |
| 150 | |
| 151 | } |
| 152 | |
| 153 | public static void clearGuidMap(){ |
| 154 | guidMap = new HashMap<String, Integer>(); |
| 155 | guidCounter = 0; |
| 156 | } |
| 157 | |
| 158 | public static String getIdForUsageScenario(UsageScenario us){ |
| 159 | //return us.getEntityName()+ fixGUID(us.getId()); |
| 160 | return shortenID("UsageScenario_" |
| 161 | +us.getEntityName()+"_" |
| 162 | +getNumberForId(us)); |
| 163 | } |
| 164 | |
| 165 | public static String getIdForCommResource(LinkingResource lr, |
| 166 | CommunicationLinkResourceType clrt) { |
| 167 | //Empty type (i.e. clrt == null) is ok for validation, so do not fail here because of it. |
| 168 | String clrtString = clrt == null ? "none" : clrt.getEntityName(); |
| 169 | return shortenID("LinkingResource_"+lr.getEntityName() + "_" + clrtString); |
| 170 | //+ fixGUID(clrt.getId()); |
| 171 | //+ getNumberForId(clrt); |
| 172 | } |
| 173 | |
| 174 | public static String getIdForThroughput(String commResourceID){ |
| 175 | return shortenID("throughput_"+commResourceID); |
| 176 | } |
| 177 | |
| 178 | public static String getIdForLatency(String commResourceID, CallType callType){ |
| 179 | return shortenID("latency_"+commResourceID+"_"+callType); |
| 180 | } |
| 181 | |
| 182 | public static String getIdForProcResource(Entity rc, |
| 183 | ProcessingResourceType prt) { |
| 184 | return shortenID(rc.getEntityName() + "_" + prt.getEntityName()); |
| 185 | // + Pcm2LqnHelper.fixGUID(prt.getId()); |
| 186 | //+ getNumberForId(prt); |
| 187 | } |
| 188 | |
| 189 | public static String getIdForPassiveResource( |
| 190 | PassiveResource passiveResource, AllocationContext assCtx) { |
| 191 | return shortenID(getClassName(passiveResource) |
| 192 | + "_" + passiveResource.getEntityName() |
| 193 | //+ "_" + getNumberForId(passiveResource) |
| 194 | + "_" + assCtx.getId()); |
| 195 | } |
| 196 | |
| 197 | public static String getIdForEntryLevelSystemCall(EntryLevelSystemCall call){ |
| 198 | String className = getClassName(call); |
| 199 | String id = className + call.getId(); |
| 200 | return shortenID(id); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Get wait entry id based on passed id (appends a String) |
| 205 | * @param id |
| 206 | * @return |
| 207 | */ |
| 208 | public static String getSignalEntryId(String id) { |
| 209 | return shortenID(id + "Signal_Entry"); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Get signal entry id based on passed id (appends a String) |
| 214 | * @param id |
| 215 | * @return |
| 216 | */ |
| 217 | public static String getWaitEntryId(String id) { |
| 218 | return shortenID(id + "Wait_Entry"); |
| 219 | } |
| 220 | |
| 221 | public static String getIdForForkedBehaviour(ForkedBehaviour asyncBeh, |
| 222 | ContextWrapper myContextWrapper) { |
| 223 | StartAction startAction = (StartAction) EMFQueryHelper.getObjectByType( |
| 224 | asyncBeh.getSteps_Behaviour(), StartAction.class); |
| 225 | return shortenID("ForkedBehaviour_"+ getNumberForId(startAction)); |
| 226 | } |
| 227 | |
| 228 | } |