Home » Modeling » GMF (Graphical Modeling Framework) » Problems saving a diagram created from a domain model
Problems saving a diagram created from a domain model [message #181095] |
Sun, 06 April 2008 05:59 |
|
Hello GMF team!
I'm working on model transformations with the M2M components of eclipse,
and I'm automating it all so that the user can choose his origin model,
click and get the destiny model. It's all ready, even the programmatic
invocation to the transformation engine, and I successfully obtain a
resource with the domain model as I'm expecting it. After that, if the
user wants to see a graphical representation, he could right-click and
"Initialize Composite Diagram file". However, I'd like to automate this
part as well. I have tried looking up this contribution action in the
diagram's plug-in.xml, understanding the code and then copying it so I can
emulate the same thing as if the user would have clicked that, but I've
had no luck and I've spent several hours with the debugger trying to find
out :(
So far this is the closest I've been to generate the .composite_diagram:
<?xml version="1.0" encoding="UTF-8"?>
<notation:Diagram xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.1/notation"
xmi:id="_zTcV4AOWEd2Rko2hr0Ou_g" type="Sca" name="qyr.composite_diagram"
measurementUnit="Pixel">
<styles xmi:type="notation:DiagramStyle"
xmi:id="_zTcV4QOWEd2Rko2hr0Ou_g"/>
</notation:Diagram>
Where the good version, hand-made clicking "Initialize Composite Diagram
file" on the popup menu would give me this:
<?xml version="1.0" encoding="UTF-8"?>
<notation:Diagram xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.1/notation"
xmlns:sca="http://www.osoa.org/xmlns/sca/1.0"
xmi:id="_iqw6IAObEd2YCoRr3zdTiQ" type="Sca" measurementUnit="Pixel">
<styles xmi:type="notation:DiagramStyle"
xmi:id="_iqw6IQObEd2YCoRr3zdTiQ"/>
<element xmi:type="sca:DocumentRoot" href="qyr.composite#/"/>
</notation:Diagram>
The actual problem is in the process of saving the DiagramImpl object
(which is fine, at debug the object looks the same in both cases,
including the 'element' EReference) referencing the domain model. I have
tried tracking down the progress on the XMLSaveImpl class of the
org.eclipse.emf.ecore.xmi.impl plugin, with no luck. The one thing I have
noticed different is that the method 'int sameDocSingle(EObject o,
EStructuralFeature f)', on this class returns SAME_DOC on the first case
and CROSS_DOC on the second case (where 'o' is of type DiagramImpl and
f.name=="element"). I know this is where the bell rings, but I haven't
been able to guess anything else, or what should I do :(
Finally, this is the code I'm using to create this model, just in case you
want to see it and suggest if I'm missing something. I would appreciate
any kind of help to get the desired diagram file. Thanks a lot!!!
-Juan
public static void initDiagram(Resource modelRes) {
TransactionalEditingDomain editingDomain =
GMFEditingDomainFactory.INSTANCE
.createEditingDomain();
final Resource diagramResource = modelRes.getResourceSet()
.createResource(
modelRes.getURI().trimFileExtension()
.appendFileExtension("composite_diagram"));
final Resource modelResource = modelRes;
final String diagramName = diagramResource.getURI().lastSegment();
DocumentRoot model = ScaFactory.eINSTANCE.createDocumentRoot();
model.setComposite((Composite) modelResource.getContents().get(0));
Diagram diagram = ViewService.createDiagram(model,
DocumentRootEditPart.MODEL_ID,
ScaDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
if (diagram != null) {
diagramResource.getContents().add(diagram);
diagram.setName(diagramName);
diagram.setElement(model);
}
try {
diagramResource .save(org.eclipse.stp.sca.diagram.part.ScaDiagramEditorUtil
.getSaveOptions()); //HERE IS THE PROBLEM!!
} catch (IOException e) {
ScaDiagramEditorPlugin.getInstance().logError(
"Unable to store model and diagram resources", e); //$NON-NLS-1$
}
setCharset(WorkspaceSynchronizer.getFile(modelResource));
setCharset(WorkspaceSynchronizer.getFile(diagramResource));
}
|
|
|
Re: Problems saving a diagram created from a domain model [message #181103 is a reply to message #181095] |
Sun, 06 April 2008 12:22 |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
Juan,
Comments below.
Juan Jose Cadavid G wrote:
> Hello GMF team!
>
> I'm working on model transformations with the M2M components of
> eclipse, and I'm automating it all so that the user can choose his
> origin model, click and get the destiny model. It's all ready, even
> the programmatic invocation to the transformation engine, and I
> successfully obtain a resource with the domain model as I'm expecting
> it. After that, if the user wants to see a graphical representation,
> he could right-click and "Initialize Composite Diagram file". However,
> I'd like to automate this part as well. I have tried looking up this
> contribution action in the diagram's plug-in.xml, understanding the
> code and then copying it so I can emulate the same thing as if the
> user would have clicked that, but I've had no luck and I've spent
> several hours with the debugger trying to find out :(
>
> So far this is the closest I've been to generate the .composite_diagram:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <notation:Diagram xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.1/notation"
> xmi:id="_zTcV4AOWEd2Rko2hr0Ou_g" type="Sca"
> name="qyr.composite_diagram" measurementUnit="Pixel">
> <styles xmi:type="notation:DiagramStyle"
> xmi:id="_zTcV4QOWEd2Rko2hr0Ou_g"/>
> </notation:Diagram>
>
> Where the good version, hand-made clicking "Initialize Composite
> Diagram file" on the popup menu would give me this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <notation:Diagram xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"
> xmlns:notation="http://www.eclipse.org/gmf/runtime/1.0.1/notation"
> xmlns:sca="http://www.osoa.org/xmlns/sca/1.0"
> xmi:id="_iqw6IAObEd2YCoRr3zdTiQ" type="Sca" measurementUnit="Pixel">
> <styles xmi:type="notation:DiagramStyle"
> xmi:id="_iqw6IQObEd2YCoRr3zdTiQ"/>
> <element xmi:type="sca:DocumentRoot" href="qyr.composite#/"/>
> </notation:Diagram>
So this diagram has a cross document reference to qyr.composite, but the
other has no reference to the your model.
>
> The actual problem is in the process of saving the DiagramImpl object
> (which is fine, at debug the object looks the same in both cases,
> including the 'element' EReference) referencing the domain model. I
> have tried tracking down the progress on the XMLSaveImpl class of the
> org.eclipse.emf.ecore.xmi.impl plugin, with no luck. The one thing I
> have noticed different is that the method 'int sameDocSingle(EObject
> o, EStructuralFeature f)', on this class returns SAME_DOC on the first
> case and CROSS_DOC on the second case (where 'o' is of type
> DiagramImpl and f.name=="element"). I know this is where the bell
> rings, but I haven't been able to guess anything else, or what should
> I do :(
In the second case, the root object is contained in a separate resource
"qyr.composite". In the first case, it sounds like perhaps the root
object of your model isn't even contained by a resource, i.e, calling
EObject.eResource() on it returns what?
>
> Finally, this is the code I'm using to create this model, just in case
> you want to see it and suggest if I'm missing something. I would
> appreciate any kind of help to get the desired diagram file. Thanks a
> lot!!!
>
> -Juan
>
>
>
>
> public static void initDiagram(Resource modelRes) {
> TransactionalEditingDomain editingDomain =
> GMFEditingDomainFactory.INSTANCE
> .createEditingDomain();
> final Resource diagramResource = modelRes.getResourceSet()
> .createResource(
> modelRes.getURI().trimFileExtension()
>
> .appendFileExtension("composite_diagram"));
> final Resource modelResource = modelRes;
> final String diagramName =
> diagramResource.getURI().lastSegment();
>
> DocumentRoot model = ScaFactory.eINSTANCE.createDocumentRoot();
>
> model.setComposite((Composite)
> modelResource.getContents().get(0));
Hmmm. You're creating a new document root and set into it the root
object from the model resource. This will remove the object from the
resource it's currently in. Is that what you want? And this new object
you don't even put into a resource?
>
> Diagram diagram = ViewService.createDiagram(model,
> DocumentRootEditPart.MODEL_ID,
> ScaDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
What's the value of model.eResource() at this point? Is
modelResource.getContents() empty now?
> if (diagram != null) {
> diagramResource.getContents().add(diagram);
> diagram.setName(diagramName);
> diagram.setElement(model);
> }
>
> try {
> diagramResource
> .save(org.eclipse.stp.sca.diagram.part.ScaDiagramEditorUtil
> .getSaveOptions()); //HERE IS THE PROBLEM!!
Is the problem a dangling href exception?
> } catch (IOException e) {
>
> ScaDiagramEditorPlugin.getInstance().logError(
> "Unable to store model and diagram resources", e);
> //$NON-NLS-1$
> }
>
> setCharset(WorkspaceSynchronizer.getFile(modelResource));
Did the code doing the save actually use this encoding when it converted
the string to bytes? XMLResource.getEncoding() determines that or you
can use XMLResource.OPTION_ENCODING to specify it. I didn't see that
you actually saved the model resource. I also don't understand why you
are creating a document root. I would have expected the model resource
to have already had a document root, and even if it doesn't, I'd expect
you to have used the root object from that model directly.
> setCharset(WorkspaceSynchronizer.getFile(diagramResource));
>
> }
>
>
>
|
|
|
Re: Problems saving a diagram created from a domain model [message #181111 is a reply to message #181103] |
Sun, 06 April 2008 20:02 |
|
Thanks a lot for your reply mr. Merks!
Based on your comments, I have tried two things:
*FIRST*
"I would have expected the model resource to have already had a document
root, and even if it doesn't, I'd expect you to have used the root object
from that model directly."
I eliminated the creation of the DocumentRoot and used the root object
directly to create the diagram, like this:
Composite model = (Composite) modelResource.getContents().get(0);
Diagram diagram = ViewService.createDiagram(model,
DocumentRootEditPart.MODEL_ID,
ScaDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
if (diagram != null) {
diagramResource.getContents().add(diagram);
diagram.setName(diagramName);
diagram.setElement(model);
}
Please notice that 'model' used to be a DocumentRoot, and now it's a
Composite, which is the root object of the domain model.
However, this is how the qyr.composite_diagram looks now:
<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI"/>
And yes, ViewService.createDiagram() is not returning a DiagramImpl
anymore, it's returning null.
I'd like to explain why did I use DocumentRoot as a wrapping object for
Composite, the root object, and take a moment to put everything on
context. The GMF modeler I'm using is the SCA Composite Modeler, which is
a component of the STP project. They actually have a DocumentRoot object
in their metamodel (sca.ecore), which you can find in the
org.eclipse.stp.sca plug-in. And looking up their code, in
org.eclipse.stp.sca.diagram.part.ScaInitDiagramFileAction they actually
pass a DocumentRoot object, wrapping the Composite object. That's why I
also did it that way.
*SECOND*
"Hmmm. You're creating a new document root and set into it the root
object from the model resource. This will remove the object from the
resource it's currently in."
I had no idea this could happen! So I put the DocumentRoot in a temporary
resource with the same URI as the one containing the Composite object, but
I'm not saving this new one.
DocumentRoot model = ScaFactory.eINSTANCE.createDocumentRoot();
model.setComposite((Composite) modelResource.getContents().get(0));
Resource tempResourceForDocumentRoot = modelRes.getResourceSet()
.createResource(modelRes.getURI());
tempResourceForDocumentRoot.getContents().add(model);
Diagram diagram = ViewService.createDiagram(model,
DocumentRootEditPart.MODEL_ID,
ScaDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
if (diagram != null) {
diagramResource.getContents().add(diagram);
diagram.setName(diagramName);
diagram.setElement(model);
}
And... this worked!!!! WOO HOO!!!! Thank you so much for your help mr.
Merks, and excuse my newbie illiteracy!
-Juan
|
|
|
Goto Forum:
Current Time: Fri Apr 26 07:04:49 GMT 2024
Powered by FUDForum. Page generated in 0.02969 seconds
|