Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Adding & saving enumerations(It seems enumerations are not written to file by EMF framework?)
Adding & saving enumerations [message #1313338] Thu, 24 April 2014 21:01 Go to next message
Greg Dart is currently offline Greg DartFriend
Messages: 17
Registered: September 2013
Junior Member
Hi,

I'm confused by the following - all help/suggestions gratefully received!

I have what is to all intents and purposes a project management application modelled in EMF, with tasks that are written into a contract. There are links between the tasks.

The tasklinks have a "type" which is an enumeration ("FS", "FF", etc).

To test the app I have code that creates a task and some links associated with it, and then writes/saves this to the contract - which I can view by looking at the underlying XML.

Everything works fine... Except the only attribute of the link that never gets written to the file is the type enumeration! No error is generated... It's just not there!

Any ideas why? Relevant excerpts of the code included below....

Thanks,
Greg

This code creates a task link and attaches it to the task.

	  private void addLinks() {
		  if (!txtLinks.equals("Comma seperated list")) {
			  // Only if text has been changed
			  List<String> items = Arrays.asList(txtLinks.getText().split("\\s*,\\s*"));
			  for (String s : items) {
				  // Each item should be an integer ID of a task - no cross-checking yet!
				  // TODO - add validation & cross-checking
				  TaskLink tl = ContractSpecEditorFactory.eINSTANCE.createTaskLink();
				  tl.setID(Integer.parseInt(s));
				  tl.setDelay(3);
				  tl.setLinktype(TaskLinkType.FS);
				  tl.setTime_unit("Hrs");
				  newTask.getLinks().add(tl);
			  }			  
		  }
	  }




This code writes the task to the model

	private void writeTask(Task task) {
		
	    if (cse != null) {	    
	    	 // editingDomain = AdapterFactoryEditingDomain.getEditingDomainFor(cse);	
	    	
	    	System.out.println("Links: " + task.getLinks().toString());
	    	Command command = AddCommand.create(editingDomain, cse, cse.getContract_tasks(),task);
	    	try {
	    		editingDomain.getCommandStack().execute(command);
	    	}
	    	catch (Exception e) {
	    		System.out.println("Ooops. Error is: " + e.getMessage());
	    	}		
	    }
	    
	    
	}



The output of the "println" is:

ID: 33
Task: testy
Duration: 2
Units: secs
Links: [contractSpecEditor.impl.TaskLinkImpl@3e838c3a (ID: 6, linktype: FS, time_unit: Hrs, delay: 3), contractSpecEditor.impl.TaskLinkImpl@2e6b53e6 (ID: 7, linktype: FS, time_unit: Hrs, delay: 3)]




So the linktype is in the model!

But when looking in the saved file (after a standard, normal, EMF save operation) is

  <contract_tasks ID="33" taskTitle="testy" time_unit="secs" duration="2">
    <links ID="6" time_unit="Hrs" delay="3"/>
    <links ID="7" time_unit="Hrs" delay="3"/>
  </contract_tasks>



Where did the linktype go???!
Re: Adding &amp; saving enumerations [message #1313551 is a reply to message #1313338] Thu, 24 April 2014 23:39 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Greg,

Unless you direct it otherwise, the resource will not serialize the
default values of attributes, including those that are typed by EEnums.
Probably FS is the default value of your enumeration (the
default-default is the first literal of the enumeration).

A slightly more remote possibility is that you accidentally set your
attribute as transient.

HTH,

Christian

On 2014-04-24 21:01:15 +0000, Greg Dart said:

> Hi,
>
> I'm confused by the following - all help/suggestions gratefully received!
>
> I have what is to all intents and purposes a project management
> application modelled in EMF, with tasks that are written into a
> contract. There are links between the tasks.
>
> The tasklinks have a "type" which is an enumeration ("FS", "FF", etc).
>
> To test the app I have code that creates a task and some links
> associated with it, and then writes/saves this to the contract - which
> I can view by looking at the underlying XML.
>
> Everything works fine... Except the only attribute of the link that
> never gets written to the file is the type enumeration! No error is
> generated... It's just not there!
> Any ideas why? Relevant excerpts of the code included below....
>
> Thanks,
> Greg
>
> This code creates a task link and attaches it to the task.
>
>
> private void addLinks() {
> if (!txtLinks.equals("Comma seperated list")) {
> // Only if text has been changed
> List<String> items = Arrays.asList(txtLinks.getText().split("\\s*,\\s*"));
> for (String s : items) {
> // Each item should be an integer ID of a task - no cross-checking yet!
> // TODO - add validation & cross-checking
> TaskLink tl = ContractSpecEditorFactory.eINSTANCE.createTaskLink();
> tl.setID(Integer.parseInt(s));
> tl.setDelay(3);
> tl.setLinktype(TaskLinkType.FS);
> tl.setTime_unit("Hrs");
> newTask.getLinks().add(tl);
> } }
> }
>
>
>
>
> This code writes the task to the model
>
>
> private void writeTask(Task task) {
>
> if (cse != null) { // editingDomain =
> AdapterFactoryEditingDomain.getEditingDomainFor(cse);
>
> System.out.println("Links: " + task.getLinks().toString());
> Command command = AddCommand.create(editingDomain, cse,
> cse.getContract_tasks(),task);
> try {
> editingDomain.getCommandStack().execute(command);
> }
> catch (Exception e) {
> System.out.println("Ooops. Error is: " + e.getMessage());
> }
> }
> }
>
>
>
> The output of the "println" is:
>
>
> ID: 33
> Task: testy
> Duration: 2
> Units: secs
> Links: [contractSpecEditor.impl.TaskLinkImpl@3e838c3a (ID: 6, linktype:
> FS, time_unit: Hrs, delay: 3),
> contractSpecEditor.impl.TaskLinkImpl@2e6b53e6 (ID: 7, linktype: FS,
> time_unit: Hrs, delay: 3)]
>
>
>
>
> So the linktype is in the model!
>
> But when looking in the saved file (after a standard, normal, EMF save
> operation) is
>
>
> <contract_tasks ID="33" taskTitle="testy" time_unit="secs" duration="2">
> <links ID="6" time_unit="Hrs" delay="3"/>
> <links ID="7" time_unit="Hrs" delay="3"/>
> </contract_tasks>
>
>
>
> Where did the linktype go???!
Re: Adding &amp; saving enumerations [message #1313930 is a reply to message #1313551] Fri, 25 April 2014 04:41 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Christian,

Yes, it's most likely an issue with defaults. An EEnums is treated
like a primitive type, so it has an intrinsic default of the first
EEnumLiteral, much as primitives have an intrinsic default of "0". A
feature for which eIsSet is true will not be serialized. There are
several approaches for dealing with that. One is to mark the feature as
unsettable; in that case, when you explicitly set it as Greg shows in
the code (and when it's explicitly present in the XML), eIsSet will be
true, and the feature's value will be serialized; but this makes the API
more complex with isSetypType and unsetType methods. Another approach is
to set an explicit default for the feature in the Ecore model, and use
the org.eclipse.emf.ecore.xmi.XMLResource.OPTION_KEEP_DEFAULT_CONTENT
option when serializing.

On 25/04/2014 1:39 AM, Christian W. Damus wrote:
> Hi, Greg,
>
> Unless you direct it otherwise, the resource will not serialize the
> default values of attributes, including those that are typed by
> EEnums. Probably FS is the default value of your enumeration (the
> default-default is the first literal of the enumeration).
>
> A slightly more remote possibility is that you accidentally set your
> attribute as transient.
>
> HTH,
>
> Christian
>
> On 2014-04-24 21:01:15 +0000, Greg Dart said:
>
>> Hi,
>>
>> I'm confused by the following - all help/suggestions gratefully
>> received!
>>
>> I have what is to all intents and purposes a project management
>> application modelled in EMF, with tasks that are written into a
>> contract. There are links between the tasks.
>>
>> The tasklinks have a "type" which is an enumeration ("FS", "FF", etc).
>>
>> To test the app I have code that creates a task and some links
>> associated with it, and then writes/saves this to the contract -
>> which I can view by looking at the underlying XML.
>>
>> Everything works fine... Except the only attribute of the link that
>> never gets written to the file is the type enumeration! No error is
>> generated... It's just not there!
>> Any ideas why? Relevant excerpts of the code included below....
>>
>> Thanks,
>> Greg
>>
>> This code creates a task link and attaches it to the task.
>>
>>
>> private void addLinks() {
>> if (!txtLinks.equals("Comma seperated list")) {
>> // Only if text has been changed
>> List<String> items =
>> Arrays.asList(txtLinks.getText().split("\\s*,\\s*"));
>> for (String s : items) {
>> // Each item should be an integer ID of a task - no
>> cross-checking yet!
>> // TODO - add validation & cross-checking
>> TaskLink tl =
>> ContractSpecEditorFactory.eINSTANCE.createTaskLink();
>> tl.setID(Integer.parseInt(s));
>> tl.setDelay(3);
>> tl.setLinktype(TaskLinkType.FS);
>> tl.setTime_unit("Hrs");
>> newTask.getLinks().add(tl);
>> } }
>> }
>>
>>
>>
>>
>> This code writes the task to the model
>>
>>
>> private void writeTask(Task task) {
>>
>> if (cse != null) { // editingDomain =
>> AdapterFactoryEditingDomain.getEditingDomainFor(cse);
>>
>> System.out.println("Links: " + task.getLinks().toString());
>> Command command = AddCommand.create(editingDomain, cse,
>> cse.getContract_tasks(),task);
>> try {
>> editingDomain.getCommandStack().execute(command);
>> }
>> catch (Exception e) {
>> System.out.println("Ooops. Error is: " +
>> e.getMessage());
>> }
>> }
>> }
>>
>>
>>
>> The output of the "println" is:
>>
>>
>> ID: 33
>> Task: testy
>> Duration: 2
>> Units: secs
>> Links: [contractSpecEditor.impl.TaskLinkImpl@3e838c3a (ID: 6,
>> linktype: FS, time_unit: Hrs, delay: 3),
>> contractSpecEditor.impl.TaskLinkImpl@2e6b53e6 (ID: 7, linktype: FS,
>> time_unit: Hrs, delay: 3)]
>>
>>
>>
>>
>> So the linktype is in the model!
>>
>> But when looking in the saved file (after a standard, normal, EMF
>> save operation) is
>>
>>
>> <contract_tasks ID="33" taskTitle="testy" time_unit="secs"
>> duration="2">
>> <links ID="6" time_unit="Hrs" delay="3"/>
>> <links ID="7" time_unit="Hrs" delay="3"/>
>> </contract_tasks>
>>
>>
>>
>> Where did the linktype go???!
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Adding &amp; saving enumerations [message #1314390 is a reply to message #1313930] Fri, 25 April 2014 10:43 Go to previous message
Greg Dart is currently offline Greg DartFriend
Messages: 17
Registered: September 2013
Junior Member
Thanks Guys. I've tested it with a different value and it works - you were (of course) correct.

Frustrating - I've wrestled with this for a couple of days, never occurred to me to try a different value...
Previous Topic:OCL query failing since migration to Luna M6
Next Topic:Nullable EList with Opposite
Goto Forum:
  


Current Time: Thu Apr 25 02:05:24 GMT 2024

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

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

Back to the top