Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Edit semantic model programmatically (Edit semantic model programmatically )
Edit semantic model programmatically [message #1697780] Mon, 08 June 2015 13:03 Go to next message
Yassine BEN ATITALLAH is currently offline Yassine BEN ATITALLAHFriend
Messages: 3
Registered: June 2013
Junior Member
Hello,


I need to edit some features of some model objects via plugin eclipse.

My method to set "devicename" property of "SystemMode" object is defined like below :
	public void setDevicenameofSystem(){
		System.out.println(s.getSemanticResources().iterator().next());
		
		XMIResourceImpl xmiressourceImpl = (XMIResourceImpl) s.getSemanticResources().iterator().next();
		final SystemModeImpl var = (SystemModeImpl) xmiressourceImpl.getContents().get(0);
		System.out.println(var.toString());
		
		final ModelAccessor ma = s.getModelAccessor();
		System.out.println("can edit device name : " +ma.getPermissionAuthority().canEditFeature(var, "devicename"));
		
		try {
	    TransactionalEditingDomain ted = s.getTransactionalEditingDomain();
		CommandStack stack = ted.getCommandStack();
		System.out.println(ted.getResourceSet());
		RecordingCommand cmd = new RecordingCommand(ted) {

			@Override
			protected void doExecute() {
			var.setDevicename("Test");
				
			}
		};
		
		stack.execute(cmd);
		}
		catch (ServiceException e) {
			e.printStackTrace();
		}	

		
}


I get as results :
ExternalJavaActionModelAnalysis
Work Session :Local Session: representations.aird
org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl@10412e2 uri='platform:/resource/Demo/My.modemodeling'
modemodeling.impl.SystemModeImpl@f508fb (devicename: SystemMode)
can edit device name : true


!ENTRY org.eclipse.equinox.registry 4 1 2015-06-08 12:32:51.363
!MESSAGE Plug-in "xxx.modelanalysis" was unable to instantiate class "xxx.modelanalysis.ExternalJavaActionModelAnalysis".
!STACK 0
java.lang.IllegalStateException: Cannot activate read/write transaction in read-only transaction context
	at org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl.acquire(TransactionalEditingDomainImpl.java:576)
	at org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl.activate(TransactionalEditingDomainImpl.java:508)
	at org.eclipse.emf.transaction.impl.TransactionImpl.start(TransactionImpl.java:204)
....


I read documentation about "TransactionalEditingDomain", "Session", "ModelAccessor" and "PermissionAuthority" but I don't find a clear example for my requirement.

Thank you.
Re: Edit semantic model programmatically [message #1697819 is a reply to message #1697780] Mon, 08 June 2015 15:49 Go to previous messageGo to next message
Steve Monnier is currently offline Steve MonnierFriend
Messages: 572
Registered: May 2011
Senior Member
Hello,

Executing a recording command using the command stack of the
Transactional Editing domain is the proper way.
How do you call this method? As there is no parameter I guess this is
not a Java Service[1], called from a Sirius Tool operation[2]. Maybe it
would work better this way.

Regards,
Steve

[1]
https://www.eclipse.org/sirius/doc/specifier/general/Writing_Queries.html#service_methods
[2]
https://www.eclipse.org/sirius/doc/specifier/general/Model_Operations.html#extensions


Le 08/06/2015 16:08, Yassine BEN ATITALLAH a écrit :
> Hello,
>
>
> I need to edit some features of some model objects via plugin eclipse.
> My method to set "devicename" property of "SystemMode" object is defined
> like below :
>
> public void setDevicenameofSystem(){
> System.out.println(s.getSemanticResources().iterator().next());
>
> XMIResourceImpl xmiressourceImpl = (XMIResourceImpl)
> s.getSemanticResources().iterator().next();
> final SystemModeImpl var = (SystemModeImpl)
> xmiressourceImpl.getContents().get(0);
> System.out.println(var.toString());
>
> final ModelAccessor ma = s.getModelAccessor();
> System.out.println("can edit device name : "
> +ma.getPermissionAuthority().canEditFeature(var, "devicename"));
>
> try {
> TransactionalEditingDomain ted =
> s.getTransactionalEditingDomain();
> CommandStack stack = ted.getCommandStack();
> System.out.println(ted.getResourceSet());
> RecordingCommand cmd = new RecordingCommand(ted) {
>
> @Override
> protected void doExecute() {
> var.setDevicename("Test");
>
> }
> };
>
> stack.execute(cmd);
> }
> catch (ServiceException e) {
> e.printStackTrace();
> }
>
>
> }
>
>
> I get as results :
> ExternalJavaActionModelAnalysis
> Work Session :Local Session: representations.aird
> org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl@10412e2
> uri='platform:/resource/Demo/My.modemodeling'
> modemodeling.impl.SystemModeImpl@f508fb (devicename: SystemMode)
> can edit device name : true
>
>
> !ENTRY org.eclipse.equinox.registry 4 1 2015-06-08 12:32:51.363
> !MESSAGE Plug-in "xxx.modelanalysis" was unable to instantiate class
> "xxx.modelanalysis.ExternalJavaActionModelAnalysis".
> !STACK 0
> java.lang.IllegalStateException: Cannot activate read/write transaction
> in read-only transaction context
> at
> org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl.acquire(TransactionalEditingDomainImpl.java:576)
>
> at
> org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl.activate(TransactionalEditingDomainImpl.java:508)
>
> at
> org.eclipse.emf.transaction.impl.TransactionImpl.start(TransactionImpl.java:204)
>
> ...
>
>
> I read documentation about "TransactionalEditingDomain", "Session",
> "ModelAccessor" and "PermissionAuthority" but I don't find a clear
> example for my requirement.
> Thank you.
>


--
Steve Monnier - Obeo

Need professional services for Sirius?
http://www.obeodesigner.com/sirius


Steve Monnier - Obeo Canada
Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Edit semantic model programmatically [message #1697820 is a reply to message #1697819] Mon, 08 June 2015 15:59 Go to previous messageGo to next message
Steve Monnier is currently offline Steve MonnierFriend
Messages: 572
Registered: May 2011
Senior Member
I forgot to add, if you use this code in a Java Service, executed from a
tool with a set operation, you should have the context (change context
operation) on your SystemMode element. In your java service, you should
have a SystemMode as first parameter (public void
setDevicenameofSystem(SystemMode systemMode){). Then in your expression
you can directly call it [setDevicenameofSystem()/] as the first
parameter will use the current context.

Regards,
Steve

Le 08/06/2015 17:49, Steve Monnier a écrit :
> Hello,
>
> Executing a recording command using the command stack of the
> Transactional Editing domain is the proper way.
> How do you call this method? As there is no parameter I guess this is
> not a Java Service[1], called from a Sirius Tool operation[2]. Maybe it
> would work better this way.
>
> Regards,
> Steve
>
> [1]
> https://www.eclipse.org/sirius/doc/specifier/general/Writing_Queries.html#service_methods
>
> [2]
> https://www.eclipse.org/sirius/doc/specifier/general/Model_Operations.html#extensions
>
>
> Le 08/06/2015 16:08, Yassine BEN ATITALLAH a écrit :
>> Hello,
>>
>>
>> I need to edit some features of some model objects via plugin eclipse.
>> My method to set "devicename" property of "SystemMode" object is defined
>> like below :
>>
>> public void setDevicenameofSystem(){
>> System.out.println(s.getSemanticResources().iterator().next());
>>
>> XMIResourceImpl xmiressourceImpl = (XMIResourceImpl)
>> s.getSemanticResources().iterator().next();
>> final SystemModeImpl var = (SystemModeImpl)
>> xmiressourceImpl.getContents().get(0);
>> System.out.println(var.toString());
>>
>> final ModelAccessor ma = s.getModelAccessor();
>> System.out.println("can edit device name : "
>> +ma.getPermissionAuthority().canEditFeature(var, "devicename"));
>>
>> try {
>> TransactionalEditingDomain ted =
>> s.getTransactionalEditingDomain();
>> CommandStack stack = ted.getCommandStack();
>> System.out.println(ted.getResourceSet());
>> RecordingCommand cmd = new RecordingCommand(ted) {
>>
>> @Override
>> protected void doExecute() {
>> var.setDevicename("Test");
>>
>> }
>> };
>>
>> stack.execute(cmd);
>> }
>> catch (ServiceException e) {
>> e.printStackTrace();
>> }
>>
>>
>> }
>>
>>
>> I get as results :
>> ExternalJavaActionModelAnalysis
>> Work Session :Local Session: representations.aird
>> org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl@10412e2
>> uri='platform:/resource/Demo/My.modemodeling'
>> modemodeling.impl.SystemModeImpl@f508fb (devicename: SystemMode)
>> can edit device name : true
>>
>>
>> !ENTRY org.eclipse.equinox.registry 4 1 2015-06-08 12:32:51.363
>> !MESSAGE Plug-in "xxx.modelanalysis" was unable to instantiate class
>> "xxx.modelanalysis.ExternalJavaActionModelAnalysis".
>> !STACK 0
>> java.lang.IllegalStateException: Cannot activate read/write transaction
>> in read-only transaction context
>> at
>> org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl.acquire(TransactionalEditingDomainImpl.java:576)
>>
>>
>> at
>> org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl.activate(TransactionalEditingDomainImpl.java:508)
>>
>>
>> at
>> org.eclipse.emf.transaction.impl.TransactionImpl.start(TransactionImpl.java:204)
>>
>>
>> ...
>>
>>
>> I read documentation about "TransactionalEditingDomain", "Session",
>> "ModelAccessor" and "PermissionAuthority" but I don't find a clear
>> example for my requirement.
>> Thank you.
>>
>
>


--
Steve Monnier - Obeo

Need professional services for Sirius?
http://www.obeodesigner.com/sirius


Steve Monnier - Obeo Canada
Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Edit semantic model programmatically [message #1698000 is a reply to message #1697820] Wed, 10 June 2015 08:47 Go to previous messageGo to next message
Yassine BEN ATITALLAH is currently offline Yassine BEN ATITALLAHFriend
Messages: 3
Registered: June 2013
Junior Member
Hello,

Thank you. I resolved the problem to edit model elements via java method.

In the first time, I call my method via Viewpoint Specification with an External Java Action in a Double Click to edit element. My Java class is defined in plug in extension point "org.eclipse.sirius.externalJavaAction". I use Session service to get TransactionalEditingDomain. But in my test i get an error message "Cannot activate read/write transaction in read-only transaction context".


In the second time, I call my method via Viewpoint Specification with a Java Services following your steps detailed in your last message. And I use the same implementation with Session service and TransactionalEditingDomain and finally i can edit the element of the model via my method.


Thank you.
Yassine.
Re: Edit semantic model programmatically [message #1698044 is a reply to message #1698000] Wed, 10 June 2015 14:59 Go to previous message
Steve Monnier is currently offline Steve MonnierFriend
Messages: 572
Registered: May 2011
Senior Member
Hello,

I am glad you resolved the problem.

Ok you were using an External Java Action. I am surprised of the
read/write transaction exception. However, the External Java Action is
already executed in a transaction so in that case you did not need a
RecordingCommand executed on the command stack. This embedded execution
probably caused this issue, but this should be reproduced on our side to
verify.

Regards,
Steve

Le 10/06/2015 10:47, Yassine BEN ATITALLAH a écrit :
> Hello,
>
> Thank you. I resolved the problem to edit model elements via java method.
>
> In the first time, I call my method via Viewpoint Specification with an
> External Java Action in a Double Click to edit element. My Java class is
> defined in plug in extension point
> "org.eclipse.sirius.externalJavaAction". I use Session service to get
> TransactionalEditingDomain. But in my test i get an error message
> "Cannot activate read/write transaction in read-only transaction context".
>
>
> In the second time, I call my method via Viewpoint Specification with a
> Java Services following your steps detailed in your last message. And I
> use the same implementation with Session service and
> TransactionalEditingDomain and finally i can edit the element of the
> model via my method.
>
>
> Thank you.
> Yassine.
>


--
Steve Monnier - Obeo

Need professional services for Sirius?
http://www.obeodesigner.com/sirius


Steve Monnier - Obeo Canada
Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Previous Topic:Display Selection Wizard at Java Service
Next Topic:Recursively nested containers possible?
Goto Forum:
  


Current Time: Sat Apr 20 01:35:01 GMT 2024

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

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

Back to the top