Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » Create slot programmatically when Classifier is set on InstanceSpecification
Create slot programmatically when Classifier is set on InstanceSpecification [message #1412586] Tue, 26 August 2014 14:21 Go to next message
Sofía Pérez is currently offline Sofía PérezFriend
Messages: 8
Registered: July 2014
Junior Member
Hello everybody!
I'm trying to create a slot programmatically when the Classifier is set on an InstanceSpecification.
I couldn't find exactly where the classifier is added to the InstanceSpecification, so I tried to add the code to whatever part I could. So I added it to the InstanceSpecificationNameLabelEditPolicy class, in the notifyChanged function:

...
else if (notification.getFeature().equals(UMLPackage.eINSTANCE.getInstanceSpecification_Classifier())) {
	addValueToQoSValue();
	refreshDisplay();
}
...

private void addValueToQoSValue() {
  InstanceSpecification instance = getUMLElement();
  for (Classifier classifier : instance.getClassifiers()) {
    Stereotype stereotype = classifier.getAppliedStereotypes().get(0);
    if ("QoSCharacteristic".equals(stereotype.getName())) { 
      EList<Property> attrs = classifier.getAllAttributes();
      for (Property prop : attrs) {
        Slot slot = instance.createSlot();
	slot.setDefiningFeature(prop);
      }
    }
  }
}


With this I got the following exception:
java.lang.IllegalStateException: Cannot modify resource set without a write transaction

Then I modified my function as follows:
private void addValueToQoSValue() {
  try {
    ServicesRegistry serviceRegistry =   ServiceUtilsForActionHandlers.getInstance().getServiceRegistry();
    TransactionalEditingDomain ed = TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain();
    RecordingCommand c = new RecordingCommand(ed) {
    					
      @Override
      protected void doExecute() {
        InstanceSpecification instance = getUMLElement();
        for (Classifier classifier : instance.getClassifiers()) {
          Stereotype stereotype = classifier.getAppliedStereotypes().get(0);
	  if ("QoSCharacteristic".equals(stereotype.getName())) { 
	    EList<Property> attrs = classifier.getAllAttributes();
	    for (Property prop : attrs) {
	      Slot slot = instance.createSlot();
	      slot.setDefiningFeature(prop);
	    }
	  }
        }
      }
    };
    ed.getCommandStack().execute(c);
  } catch (ServiceException e) {
    e.printStackTrace();
  }
}


But now I'm getting this exception: org.eclipse.emf.transaction.RollbackException: Cannot modify resource set without a write transaction

So my questions are:
1- Is there a better way to detect when the classifier is selected so I could create the slot?
2- If not, how can I make my code work?

Thank you!
Re: Create slot programmatically when Classifier is set on InstanceSpecification [message #1414876 is a reply to message #1412586] Mon, 01 September 2014 13:47 Go to previous message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 638
Registered: February 2011
Senior Member
Hi Sofia,


The exception is due to the Transaction framework, which prevents edition of the model outside a... write transaction, as the exception says.

The simplest way to start a Transaction is to use a Command and the Papyrus CommandStack. For example, if you have an EObject which is already attached to the Papyrus model, use:

TransactionalEditingDomain domain = ServiceUtilsForEObject.getInstance().getTransactionalEditingDomain(eObject);
CommandStack stack = domain.getCommandStack();
stack.execute(myCommand);


Papyrus implements an action when a Classifier is dropped on an InstanceSpecification, to create a set of slots. Have a look at this command to see how it is implemented:

Plugin org.eclipse.papyrus.uml.diagram.dnd:
org.eclipse.papyrus.uml.diagram.dnd.strategy.instancespecification.command.SelectAndCreateSlotsCommand


Regards,
Camille


Camille Letavernier
Papyrus developer
Previous Topic:[SysML Activity Diagram] Activity Allocation Compartment
Next Topic:Document generation from a model
Goto Forum:
  


Current Time: Mon May 25 11:55:54 GMT 2015

Powered by FUDForum. Page generated in 0.03678 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software