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 |
Greg Dart 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 & saving enumerations [message #1313551 is a reply to message #1313338] |
Thu, 24 April 2014 23:39 |
|
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 & saving enumerations [message #1313930 is a reply to message #1313551] |
Fri, 25 April 2014 04:41 |
Ed Merks Messages: 33145 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/
|
|
| |
Goto Forum:
Current Time: Sat May 04 09:44:45 GMT 2024
Powered by FUDForum. Page generated in 0.02941 seconds
|