Home » Modeling » EMF » Extended Metadata not working on loading a model
Extended Metadata not working on loading a model [message #519414] |
Mon, 08 March 2010 18:13 |
h1055071 Messages: 335 Registered: July 2009 |
Senior Member |
|
|
Hi,
I followed the instructions of the EMF book Chapter 15.3.5 to customise
the persistence of my model but something's not working when loading it
back in.
Here's the code to create the options map:
private Map<Object, Object> getOptions() {
Map<Object, Object> options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
ExtendedMetaData ext = new
BasicExtendedMetaData(ExtendedMetaData.ANNOTATION_URI,
EPackage.Registry.INSTANCE, new HashMap<EModelElement,
EAnnotation>());
ext.setName(IArchimatePackage.Literals.ARCHIMATE_MODEL__FOLD ERS,
"folder");
ext.setName(IArchimatePackage.Literals.FOLDER__ELEMENTS, "element");
ext.setFeatureKind(IArchimatePackage.Literals.ARCHIMATE_ELEM ENT__DOCUMENTATION,
ExtendedMetaData.ELEMENT_FEATURE);
options.put(XMLResource.OPTION_EXTENDED_META_DATA, ext);
return options;
}
When I use these options to save the Resource everything works as expected:
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(uri);
resource.getContents().add(model);
Map<Object, Object> options = getOptions();
resource.save(options);
However, using the same set of options to load I get the error "Feature
'folder' not found.":
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(uri);
Map<Object, Object> options = getOptions();
resource.load(options);
Phil
|
|
|
Re: Extended Metadata not working on loading a model [message #519426 is a reply to message #519414] |
Mon, 08 March 2010 18:46 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
Phillipus,
Comments below.
Phillipus wrote:
> Hi,
>
> I followed the instructions of the EMF book Chapter 15.3.5 to
> customise the persistence of my model but something's not working when
> loading it back in.
>
> Here's the code to create the options map:
>
> private Map<Object, Object> getOptions() {
> Map<Object, Object> options = new HashMap<Object, Object>();
> options.put(XMLResource.OPTION_ENCODING, "UTF-8");
>
> ExtendedMetaData ext = new
> BasicExtendedMetaData(ExtendedMetaData.ANNOTATION_URI,
> EPackage.Registry.INSTANCE, new HashMap<EModelElement,
> EAnnotation>());
>
> ext.setName(IArchimatePackage.Literals.ARCHIMATE_MODEL__FOLD ERS,
> "folder");
> ext.setName(IArchimatePackage.Literals.FOLDER__ELEMENTS, "element");
>
> ext.setFeatureKind(IArchimatePackage.Literals.ARCHIMATE_ELEM ENT__DOCUMENTATION,
>
> ExtendedMetaData.ELEMENT_FEATURE);
>
> options.put(XMLResource.OPTION_EXTENDED_META_DATA, ext);
>
> return options;
> }
>
> When I use these options to save the Resource everything works as
> expected:
>
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource = resourceSet.createResource(uri);
> resource.getContents().add(model);
> Map<Object, Object> options = getOptions();
> resource.save(options);
>
>
> However, using the same set of options to load I get the error
> "Feature 'folder' not found.":
Maybe you need to indicate explicitly that the "folder" and "element"
features are ELEMENT_FEATUREs.
>
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource = resourceSet.createResource(uri);
> Map<Object, Object> options = getOptions();
> resource.load(options);
>
> Phil
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: Extended Metadata not working on loading a model [message #519433 is a reply to message #519426] |
Mon, 08 March 2010 19:01 |
h1055071 Messages: 335 Registered: July 2009 |
Senior Member |
|
|
On 08/03/2010 18:46, Ed Merks wrote:
> Phillipus,
>
> Comments below.
>
>
> Phillipus wrote:
>> Hi,
>>
>> I followed the instructions of the EMF book Chapter 15.3.5 to
>> customise the persistence of my model but something's not working when
>> loading it back in.
>>
>> Here's the code to create the options map:
>>
>> private Map<Object, Object> getOptions() {
>> Map<Object, Object> options = new HashMap<Object, Object>();
>> options.put(XMLResource.OPTION_ENCODING, "UTF-8");
>>
>> ExtendedMetaData ext = new
>> BasicExtendedMetaData(ExtendedMetaData.ANNOTATION_URI,
>> EPackage.Registry.INSTANCE, new HashMap<EModelElement, EAnnotation>());
>>
>> ext.setName(IArchimatePackage.Literals.ARCHIMATE_MODEL__FOLD ERS,
>> "folder");
>> ext.setName(IArchimatePackage.Literals.FOLDER__ELEMENTS, "element");
>>
>> ext.setFeatureKind(IArchimatePackage.Literals.ARCHIMATE_ELEM ENT__DOCUMENTATION,
>>
>> ExtendedMetaData.ELEMENT_FEATURE);
>>
>> options.put(XMLResource.OPTION_EXTENDED_META_DATA, ext);
>>
>> return options;
>> }
>>
>> When I use these options to save the Resource everything works as
>> expected:
>>
>> ResourceSet resourceSet = new ResourceSetImpl();
>> Resource resource = resourceSet.createResource(uri);
>> resource.getContents().add(model);
>> Map<Object, Object> options = getOptions();
>> resource.save(options);
>>
>>
>> However, using the same set of options to load I get the error
>> "Feature 'folder' not found.":
> Maybe you need to indicate explicitly that the "folder" and "element"
> features are ELEMENT_FEATUREs.
>>
>> ResourceSet resourceSet = new ResourceSetImpl();
>> Resource resource = resourceSet.createResource(uri);
>> Map<Object, Object> options = getOptions();
>> resource.load(options);
>>
>> Phil
Thanks once again Ed, that's exactly it!
Some things to note:
1. You don't need to set this explicitly for saving the Resource, only
loading it.
2. The EMF book omits this information. The example code there is as
follows (p.483 of the 2nd edition):
ext.setQualified(EPO2Package.eINSTANCE, false);
ext.setName(EPO2Package.Literals.PURCHASE_ORDER, "order");
ext.setName(EPO2Package.Literals.PURCHASE_ORDER__ITEMS, "item");
ext.setFeatureKind(EPO2Package.Literals.PURCHASE_ORDER__COMM ENT,
ExtendedMetaData.ELEMENT_FEATURE);
The "items" element becomes "item", just like my "folders" becomes
"folder". Then the authors say to only use the same set of options to
load but not the explicit setting of ELEMENT_FEATURE.
Is it an omission of the book?
Cheers,
Phil
|
|
|
Re: Extended Metadata not working on loading a model [message #519435 is a reply to message #519433] |
Mon, 08 March 2010 19:25 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
Phillipus,
Comments below.
Phillipus wrote:
> On 08/03/2010 18:46, Ed Merks wrote:
>> Phillipus,
>>
>> Comments below.
>>
>>
>> Phillipus wrote:
>>> Hi,
>>>
>>> I followed the instructions of the EMF book Chapter 15.3.5 to
>>> customise the persistence of my model but something's not working when
>>> loading it back in.
>>>
>>> Here's the code to create the options map:
>>>
>>> private Map<Object, Object> getOptions() {
>>> Map<Object, Object> options = new HashMap<Object, Object>();
>>> options.put(XMLResource.OPTION_ENCODING, "UTF-8");
>>>
>>> ExtendedMetaData ext = new
>>> BasicExtendedMetaData(ExtendedMetaData.ANNOTATION_URI,
>>> EPackage.Registry.INSTANCE, new HashMap<EModelElement, EAnnotation>());
>>>
>>> ext.setName(IArchimatePackage.Literals.ARCHIMATE_MODEL__FOLD ERS,
>>> "folder");
>>> ext.setName(IArchimatePackage.Literals.FOLDER__ELEMENTS, "element");
>>>
>>> ext.setFeatureKind(IArchimatePackage.Literals.ARCHIMATE_ELEM ENT__DOCUMENTATION,
>>>
>>>
>>> ExtendedMetaData.ELEMENT_FEATURE);
>>>
>>> options.put(XMLResource.OPTION_EXTENDED_META_DATA, ext);
>>>
>>> return options;
>>> }
>>>
>>> When I use these options to save the Resource everything works as
>>> expected:
>>>
>>> ResourceSet resourceSet = new ResourceSetImpl();
>>> Resource resource = resourceSet.createResource(uri);
>>> resource.getContents().add(model);
>>> Map<Object, Object> options = getOptions();
>>> resource.save(options);
>>>
>>>
>>> However, using the same set of options to load I get the error
>>> "Feature 'folder' not found.":
>> Maybe you need to indicate explicitly that the "folder" and "element"
>> features are ELEMENT_FEATUREs.
>>>
>>> ResourceSet resourceSet = new ResourceSetImpl();
>>> Resource resource = resourceSet.createResource(uri);
>>> Map<Object, Object> options = getOptions();
>>> resource.load(options);
>>>
>>> Phil
>
> Thanks once again Ed, that's exactly it!
I'm such a good guesser. :-P
>
> Some things to note:
>
> 1. You don't need to set this explicitly for saving the Resource, only
> loading it.
> 2. The EMF book omits this information. The example code there is as
> follows (p.483 of the 2nd edition):
>
> ext.setQualified(EPO2Package.eINSTANCE, false);
> ext.setName(EPO2Package.Literals.PURCHASE_ORDER, "order");
> ext.setName(EPO2Package.Literals.PURCHASE_ORDER__ITEMS, "item");
> ext.setFeatureKind(EPO2Package.Literals.PURCHASE_ORDER__COMM ENT,
> ExtendedMetaData.ELEMENT_FEATURE);
>
> The "items" element becomes "item", just like my "folders" becomes
> "folder". Then the authors say to only use the same set of options to
> load but not the explicit setting of ELEMENT_FEATURE.
>
> Is it an omission of the book?
It sounds like it.
>
> Cheers,
>
>
> Phil
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | | | |
Re: Extended Metadata not working on loading a model [message #519661 is a reply to message #519634] |
Tue, 09 March 2010 15:27 |
h1055071 Messages: 335 Registered: July 2009 |
Senior Member |
|
|
On 09/03/2010 14:45, walnut23 wrote:
> Thankyou very much phil!
>
> I don't have the 2nd edition. Is this explained in 2nd edition?
>
>
Hi,
I edited the Resource Factory so it's as follows:
public class MyResourceFactory extends ResourceFactoryImpl {
private Map<Object, Object> options;
public MyResourceFactory() {
super();
}
@Override
public Resource createResource(URI uri) {
MyResource result = new MyResource(uri);
result.getDefaultLoadOptions().putAll(getOptions());
result.getDefaultSaveOptions().putAll(getOptions());
return result;
}
private Map<Object, Object> getOptions() {
if(options == null) {
options = new HashMap<Object, Object>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
ExtendedMetaData ext = new
BasicExtendedMetaData(ExtendedMetaData.ANNOTATION_URI,
EPackage.Registry.INSTANCE, new
HashMap<EModelElement, EAnnotation>());
ext.setName(IArchimatePackage.Literals.ARCHIMATE_MODEL,
"model");
ext.setFeatureKind(IArchimatePackage.Literals.ARCHIMATE_MODE L__FOLDERS,
ExtendedMetaData.ELEMENT_FEATURE); // have to do this explicitly
ext.setName(IArchimatePackage.Literals.ARCHIMATE_MODEL__FOLD ERS, "folder");
ext.setFeatureKind(IArchimatePackage.Literals.FOLDER__ELEMEN TS,
ExtendedMetaData.ELEMENT_FEATURE); // have to do this explicitly
ext.setName(IArchimatePackage.Literals.FOLDER__ELEMENTS,
"element");
ext.setFeatureKind(IArchimatePackage.Literals.ARCHIMATE_ELEM ENT__DOCUMENTATION,
ExtendedMetaData.ELEMENT_FEATURE);
options.put(XMLResource.OPTION_EXTENDED_META_DATA, ext);
}
return options;
}
}
|
|
| | | | | | | | | | | |
Re: Extended Metadata not working on loading a model [message #521056 is a reply to message #521029] |
Tue, 16 March 2010 06:06 |
Ed Merks Messages: 33113 Registered: July 2009 |
Senior Member |
|
|
All the persistence is done using reflection via the classes in the
org.eclipse.emf.ecore.xmi plugin. You can have complete control by
controling the resource implementation created by your resource factory.
walnut23 wrote:
> Thanks Ed! Will try this option.
>
> I am using EMF and GMF to generate the models.I beleieve the following
> is the piece of code(autogenerated in XXXEditor.java) that actually
> does the persistance work while entering the property values via the
> editor.Just curious to know whether all the values are saved as java
> objects by the framework and then persisted? If so is it just one java
> object or one for each model ?
>
>
> @Override
> public void doSave(IProgressMonitor progressMonitor) {
> // Save only resources that have actually changed.
> //
> final Map<Object, Object> saveOptions = new HashMap<Object,
> Object>();
> saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED,
> Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER);
>
> // Do the work within an operation because this is a long
> running activity that modifies the workbench.
> //
> WorkspaceModifyOperation operation =
> new WorkspaceModifyOperation() {
> // This is the method that gets invoked when the
> operation runs.
> //
> @Override
> public void execute(IProgressMonitor monitor) {
> // Save the resources to the file system.
> //
> boolean first = true;
> for (Resource resource :
> editingDomain.getResourceSet().getResources()) {
> if ((first ||
> !resource.getContents().isEmpty() || isPersisted(resource)) &&
> !editingDomain.isReadOnly(resource)) {
> try {
> long timeStamp = resource.getTimeStamp();
> resource.save(saveOptions);
> if (resource.getTimeStamp() !=
> timeStamp) {
> savedResources.add(resource);
> }
> }
> catch (Exception exception) {
> resourceToDiagnosticMap.put(resource,
> analyzeResourceProblems(resource, exception));
> }
> first = false;
> }
> }
> }
> };
>
> updateProblemIndication = false;
> try {
> // This runs the options, and shows progress.
> //
> new ProgressMonitorDialog(getSite().getShell()).run(true,
> false, operation);
>
> // Refresh the necessary state.
> //
>
> ((BasicCommandStack)editingDomain.getCommandStack()).saveIsD one();
> firePropertyChange(IEditorPart.PROP_DIRTY);
> }
> catch (Exception exception) {
> // Something went wrong that shouldn't.
> //
> PrototypeEditorPlugin.INSTANCE.log(exception);
> }
> updateProblemIndication = true;
> updateProblemIndication();
> }
>
> /**
> * This returns whether something has been persisted to the URI of
> the specified resource.
> * The implementation uses the URI converter from the editor's
> resource set to try to open an input stream. * <!--
> begin-user-doc -->
> * <!-- end-user-doc -->
> * @generated
> */
> protected boolean isPersisted(Resource resource) {
> boolean result = false;
> try {
> InputStream stream =
> editingDomain.getResourceSet().getURIConverter().createInput
> Stream(resource.getURI());
> if (stream != null) {
> result = true;
> stream.close();
> }
> }
> catch (IOException e) {
> // Ignore
> }
> return result;
> }
>
> Thanks!
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Thu Mar 28 23:43:41 GMT 2024
Powered by FUDForum. Page generated in 0.05113 seconds
|