| 1 | package de.uka.ipd.sdq.pcm.gmf.resource.helper; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | |
| 6 | import org.eclipse.core.commands.ExecutionException; |
| 7 | import org.eclipse.core.runtime.IAdaptable; |
| 8 | import org.eclipse.core.runtime.IProgressMonitor; |
| 9 | import org.eclipse.emf.common.util.EList; |
| 10 | import org.eclipse.emf.ecore.EObject; |
| 11 | import org.eclipse.emf.ecore.resource.Resource; |
| 12 | import org.eclipse.emf.edit.domain.EditingDomain; |
| 13 | import org.eclipse.emf.query.conditions.eobjects.structuralfeatures.EObjectAttributeValueCondition; |
| 14 | import org.eclipse.emf.query.statements.FROM; |
| 15 | import org.eclipse.emf.query.statements.IQueryResult; |
| 16 | import org.eclipse.emf.query.statements.SELECT; |
| 17 | import org.eclipse.emf.query.statements.WHERE; |
| 18 | import org.eclipse.emf.transaction.util.TransactionUtil; |
| 19 | import org.eclipse.gmf.runtime.common.core.command.CommandResult; |
| 20 | import org.eclipse.gmf.runtime.emf.type.core.commands.ConfigureElementCommand; |
| 21 | import org.eclipse.gmf.runtime.emf.type.core.requests.ConfigureRequest; |
| 22 | import org.eclipse.jface.dialogs.Dialog; |
| 23 | import org.eclipse.ui.PlatformUI; |
| 24 | |
| 25 | import de.uka.ipd.sdq.pcm.core.CoreFactory; |
| 26 | import de.uka.ipd.sdq.pcm.core.PCMRandomVariable; |
| 27 | import de.uka.ipd.sdq.pcm.dialogs.stoex.StochasticExpressionEditDialog; |
| 28 | import de.uka.ipd.sdq.pcm.resourceenvironment.CommunicationLinkResourceSpecification; |
| 29 | import de.uka.ipd.sdq.pcm.resourceenvironment.LinkingResource; |
| 30 | import de.uka.ipd.sdq.pcm.resourcetype.CommunicationLinkResourceType; |
| 31 | import de.uka.ipd.sdq.pcm.resourcetype.ResourcetypePackage; |
| 32 | import de.uka.ipd.sdq.stoex.analyser.visitors.TypeEnum; |
| 33 | |
| 34 | public class SetLatencyThroughputAndLanTypeCommand extends ConfigureElementCommand { |
| 35 | |
| 36 | private static final String LATENCY_DISPLAY_TITLE = "Set Latency"; |
| 37 | private static final String THROUGHPUT_DISPLAY_TITLE = "Set Throughput"; |
| 38 | private ConfigureRequest request = null; |
| 39 | |
| 40 | // id of LAN in PCM resource repository |
| 41 | private static final String LAN_COMMUNICATION_LINK_RESOURCE_TYPE = "_o3sScH2AEdyH8uerKnHYug"; |
| 42 | |
| 43 | public SetLatencyThroughputAndLanTypeCommand(ConfigureRequest request) { |
| 44 | super(request); |
| 45 | this.request = request; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Method opens latency and throughput StoEx-dialogs and sets attributes of |
| 50 | * Communication Link Resource Specification accordingly |
| 51 | */ |
| 52 | @Override |
| 53 | protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { |
| 54 | // get Linking Resource and Communication Link Resource Specification |
| 55 | LinkingResource linkingResource = (LinkingResource) request.getElementToConfigure(); |
| 56 | // this containment was set before by AddCommunicationLinkResourceSpecificationEditHelperAdvice.getBeforeConfigureCommand() |
| 57 | CommunicationLinkResourceSpecification communicationLinkResourceSpecification = linkingResource.getCommunicationLinkResourceSpecifications_LinkingResource(); |
| 58 | |
| 59 | PCMRandomVariable latency = getRandomVariableFromStoExDialog(LATENCY_DISPLAY_TITLE); |
| 60 | if (latency == null) { |
| 61 | return CommandResult.newCancelledCommandResult(); |
| 62 | } |
| 63 | communicationLinkResourceSpecification.setLatency_CommunicationLinkResourceSpecification(latency); |
| 64 | |
| 65 | PCMRandomVariable throughput = getRandomVariableFromStoExDialog(THROUGHPUT_DISPLAY_TITLE); |
| 66 | if (throughput == null) { |
| 67 | return CommandResult.newCancelledCommandResult(); |
| 68 | } |
| 69 | communicationLinkResourceSpecification.setThroughput_CommunicationLinkResourceSpecification(throughput); |
| 70 | |
| 71 | CommunicationLinkResourceType lanType = getLanType(); |
| 72 | communicationLinkResourceSpecification.setCommunicationLinkResourceType_CommunicationLinkResourceSpecification(lanType); |
| 73 | |
| 74 | return CommandResult.newOKCommandResult(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return LAN type from resource repository |
| 79 | */ |
| 80 | private CommunicationLinkResourceType getLanType() { |
| 81 | EObject requestElement = (EObject) request.getElementsToEdit().get(0); |
| 82 | EditingDomain editingDomain = TransactionUtil.getEditingDomain(requestElement); |
| 83 | EList<Resource> resources = editingDomain.getResourceSet().getResources(); |
| 84 | Collection<EObject> c = new ArrayList<EObject>(); |
| 85 | for(Resource r : resources) { |
| 86 | c.addAll(r.getContents()); |
| 87 | } |
| 88 | SELECT statement = new SELECT( |
| 89 | new FROM(c), |
| 90 | new WHERE( |
| 91 | new EObjectAttributeValueCondition( |
| 92 | ResourcetypePackage.eINSTANCE.getCommunicationLinkResourceType().getEIDAttribute(), |
| 93 | new org.eclipse.emf.query.conditions.strings.StringValue(LAN_COMMUNICATION_LINK_RESOURCE_TYPE) |
| 94 | )) |
| 95 | ); |
| 96 | IQueryResult queryResult = statement.execute(); |
| 97 | CommunicationLinkResourceType lanType = (CommunicationLinkResourceType)queryResult.iterator().next(); |
| 98 | return lanType; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Opens a StoxEx dialog and returns the resulting {@link PCMRandomVariable} |
| 103 | * @param displayTitle Title of the StoEx dialog |
| 104 | * @return PCMRandomVariable if user entered valid data and confirmed. Will |
| 105 | * return null if user canceled dialog |
| 106 | */ |
| 107 | private PCMRandomVariable getRandomVariableFromStoExDialog(String displayTitle) { |
| 108 | PCMRandomVariable randomVariable = CoreFactory.eINSTANCE.createPCMRandomVariable(); |
| 109 | randomVariable.setSpecification(""); |
| 110 | |
| 111 | StochasticExpressionEditDialog dialog = new StochasticExpressionEditDialog(PlatformUI.getWorkbench() |
| 112 | .getActiveWorkbenchWindow().getShell(), TypeEnum.DOUBLE, randomVariable); |
| 113 | dialog.setDisplayTitle(displayTitle); |
| 114 | dialog.open(); |
| 115 | |
| 116 | if (dialog.getReturnCode() == Dialog.CANCEL) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | randomVariable.setSpecification(dialog.getResultText()); |
| 121 | return randomVariable; |
| 122 | } |
| 123 | |
| 124 | } |