Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Lutz on June 21, 2008, 12:26:44 am

Title: How to modify an open model using Java
Post by: Lutz on June 21, 2008, 12:26:44 am
I am trying to write automations in Java which perform changes to the model I have currently loaded into my running EA instance.
I configured (Tools/Customize, Tools tab) the java VM to execute a java class, that starts as described in the EA documentation:

Repository r = new Repository();
r.OpenFile(projectName))

I then perform changes to model elements, e.g. assign as stereotype to a package. Finally I finish my work using

r.CloseFile();
r.Exit();

My problem: the changes are neither stored in the eap file nor do I see them in the already opened EA instance.
Probably I should do something like r.Save(), but I did not find anything the like.
Moreover, it looks like with the above I start a new EA instance instead of connecting to the already running one, as is described in the automation documentation for VB, using GetObject; is there any way to connect to the running EA instance using Java? :-?
Title: Re: How to modify an open model using Java
Post by: Frank Horn on June 21, 2008, 01:06:55 am
Lutz,

I don't know if there is anything like GetObject() in Java, but as to your changes not being saved: a reason for this could be that you don't call the update methods. E.g. when you assign a stereotype to a package, it will not be written to the database (or eap file) until you call Package.Update() or Package.Element.Update() or whatever. When adding or deleting elements, you have to call Refresh() on the parent collection.

It's hard to keep track of which Update() or Refresh() method is needed in which situation. I tend to call them all whenever I'm in doubt, but that's not exactly boosting my AddIn's performance.

Frank
Title: Re: How to modify an open model using Java
Post by: Lutz on June 21, 2008, 04:05:05 am
Frank,
thanks, that did it.
At least the changes are written to the eap file, but they are not reflected in my already running EA instance. Maybe I will have to do what I wanted to avoid: switch to VB  :'(
Anyway, thanks again.
Lutz
Title: Re: How to modify an open model using Java
Post by: «Midnight» on June 21, 2008, 05:24:14 am
Lutz,

I think you are halfway there. Frank is quite correct; nothing is saved into the model until you call Update().

This is fine if you are going to query the model again via automation. If you are have a running instance of EA, or if you have diagrams open while you update element information you might have to do some more work.

There are several Refresh or Reload (I don't have help open) calls to handle this kind of thing. These are methods of the Repository or Project classes. They reload diagrams, packages (I think) and entire models. Try them out as appropriate, starting with the least general. One or more of these should help. You can search the forum for additional hints once you have looked up the correct method names, since you are not the first person to have this kind of problem.

As far as the Java API goes, if it will not come through for you when other languages - through the COM API - will do so, then send a note to Sparx support immediately (and start a specific thread here in the forum). Sparx takes this kind of thing seriously and is pretty good at resolving such issues.

David
Title: Re: How to modify an open model using Java
Post by: Eve on June 23, 2008, 08:19:47 am
The Java API doesn't provide any means to access an already running version of EA, so you've come as close as you're going to get from Java.
Title: Re: How to modify an open model using Java
Post by: Lutz on June 24, 2008, 04:47:20 am
Now this I do not understand .. at least if I try to interprete what happens if I follow Franks' suggestion,

pkg.GetElement().SetStereotypeEx("MyStereotype");
pkg.GetElement().Update();

On first glance, nothing changes in the running EA session, but if I double-click the corresponding package, the stereotype appears, so the Java program must have some connection to the running EA session. Or does EA check if the eap file has changed and reloads it if required?
Title: Re: How to modify an open model using Java
Post by: Eve on June 24, 2008, 08:39:40 am
When you call Update(), the changes are written to the database.

The running instance of EA goes to the database for different things on a regular basis.

There is no connection to the running instance of EA.  Only shared data.
Title: Re: How to modify an open model using Java
Post by: Frank Horn on June 24, 2008, 04:33:13 pm
Quote
The Java API doesn't provide any means to access an already running version of EA

You can write an AddIn with Java though. This would be running in the application context of a running EA instance. Then within your AddIn you could provide a Java interface to which an external Java application could connect (I don't know much about Java, but certainly there must be something like .Net Remoting, i.e. a means of communication between two processes).
Title: Re: How to modify an open model using Java
Post by: Eve on June 25, 2008, 08:16:22 am
Actually you can't write add-ins.  See the readme.txt in the Java API directory.

Quote
Limitations and known issues:

1. You cannot currently use thie API to write plug-ins for EA. It is only suitable for accessing the automation server API. Plug-in capability is currently being planned.

2. Due to the nature of Java interacting with native methods and COM, garbage collection is not be optimal. Native COM classes and memory allocated for these is not seen by the Java garbage collector, so you should explicitly invoke a garbage collection from time to time when working with many objects using the Java API. This will ensure native memory is freed in a timely fashion.
Title: Re: How to modify an open model using Java
Post by: Frank Horn on June 25, 2008, 04:35:37 pm
Quote
Actually you can't write add-ins

There goes my workaround idea. But perhaps it could be done with a third party Java-COM bridge, or with a .Net-Java interaction thing like http://www.ikvm.net/.

Only worth the effort of course if you absolutely can't wait for Sparx to come up with the planned plug-in capability.
Title: Re: How to modify an open model using Java
Post by: Lutz on June 25, 2008, 05:17:06 pm
Well, thanks then. Good that I did not yet find the time to start any efforts to implement a Plug-In in Java ...
My somewhat offline-solution where synchonization happens via the file on the disk will have to sufficient for the time being then.
Simon, is there already any idea in which release and/or when a Java plug-in mechanism will be available?
Title: Re: How to modify an open model using Java
Post by: Eve on June 26, 2008, 08:36:09 am
Sorry, I have no idea.

It's an entirely different situation from the existing work on the Java API.  (EA calling querying and calling Java instead of Java calling EA)  Although I have a good idea what needs to be done, it's not something I've had a chance to look at yet.
Title: Re: How to modify an open model using Java
Post by: Eric Johannsen on September 03, 2008, 07:30:38 am
I'm not a Java expert but it looks like this might solve the problem of calling the COM layer from Java:

https://com4j.dev.java.net/

As I think a previous poster mentioned there are also some commercial Java/COM bridges like EZJCom, but these tend to be expensive for what you get.

If you can access COM, you can latch on to an already-running instance of EA by looking for it in the Running Object Table (I can provide a code example if someone needs it, send me a private message here if you do).

This is only a solution for automating EA from the outside with Java, not for writing an Add-In using Java.  I do recall reading that some of the commercial Java/COM bridges can expose Java classes with a COM interface, but haven't tried to see if you can really write a plugin with that type of technology.