Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: impact79 on June 13, 2007, 05:12:30 am

Title: Automated reverse engineering
Post by: impact79 on June 13, 2007, 05:12:30 am
Hello everyone !

I have to automate the reverse engineering and class diagramm creation of c# projects.

Here is what i have to do:
Check the sourcecode folder for new sourcecode files and reverse-engineer them into the existing EA-Project file.

The big question is:
Can this be accomplished using the Automation Interface ?

I´ve already gone through most threads of the forum, but couldn´t find anything suitable except one thread, stating that reverse engineering can´t be done through AI. Is that right ?

Thanks,
impact79
Title: Re: Automated reverse engineering
Post by: impact79 on June 13, 2007, 07:29:24 am
Ok, i found some functions in the Project Interface.
(ImportDirectory() / ImportFile())

But i can´t add anything :(
Here is my code:
Code: [Select]

// instantiate repository
Repository mEARep = new Repository();

// load empty .eap file
try {
       mEARep.OpenFile(eaFile);
   } catch( Exception ex ) {
       Debug.Assert(false, "Error while reading " + eaFile + Environment.NewLine + ex.Message);
   }

// get project interface
Project mProject = mEARep.GetProjectInterface();

// get root package
Package root = (mEARep.Models.GetAt(0) as Package);

// get guid of root package
String guid = mProject.GUIDtoXML(root.PackageGUID);

// import sourcecode directory to root package recursively
mProject.ImportDirectory(guid, "English", @"c:\test\", "recurse=1");



What am i doing wrong ?
Title: Re: Automated reverse engineering
Post by: «Midnight» on June 13, 2007, 10:11:21 am
Impact79,

Take a look at the fourth parameter (at least in the case of ImportDirectory), ExtraOptions. You'll need to experiment a bit wtih the settings, which are not documented. These parallel the options on the Import Source dialog.

David
Title: Re: Automated reverse engineering
Post by: Eve on June 13, 2007, 12:55:41 pm
Import Source Directory can't be executed on root packages.  (As with the User Interface)
Title: Re: Automated reverse engineering
Post by: «Midnight» on June 13, 2007, 01:05:33 pm
Yes of course; thanks Simon.

If memory serves, this has also been documented elsewhere in the forum, and perhaps the EA documentation.

David
Title: Re: Automated reverse engineering
Post by: impact79 on June 14, 2007, 11:55:57 pm
@Midnight and simonm:
Thanks for your help, i can import and synchronize codefiles now. That´s the first step.

Now i need further information about the ExtraOptions. These are the equivalents to the checkboxes and radiobuttons used to configure the "Import Source Directory" Dialog, if i understand midnight right.

How do i address these options using the "ExtraOptions" String ?
Title: Re: Automated reverse engineering
Post by: Eve on June 17, 2007, 12:59:52 pm
http://www.sparxsystems.com.au/EAUserGuide/project_2.htm

The ExtraOptions string for ImportDirectory currently accepts a string such as "recurse=1;" to specify that it should recurse subdirectories.  No other options are currently handled.

The ExtraOptions string for ImportFile currently is only there for future expansion.
Title: Re: Automated reverse engineering
Post by: impact79 on June 17, 2007, 11:00:04 pm
Again, thanks for your help, simonm.

2 more questions:

- How do i change the ImportDirectory() behaviour of creating packages on namespace basis to creating packages on folder basis ?

- Is there a way to suppress the "logical class-diagram per package" creation during the import ?
Title: Re: Automated reverse engineering
Post by: jeanfrancois on May 04, 2010, 12:48:20 am
Sorry for bringing back this old topic. But my question is closely related.

I am trying to use ImportDirectory to reverse engineer code into EA using the API:

Code: [Select]
repository.OpenFile("C:\\temp\\Test.eap");
project = repository.GetProjectInterface();
Package rootPackage = repository.Models.GetAt(0);

Package outputPackage = GetPackage(rootPackage, "Test"); // custom function

if (outputPackage == null)
{
    outputPackage = (Package)rootPackage.Packages.AddNew("Test", string.Empty);
    outputPackage.Update();
    rootPackage.Update();
}

string guid = project.GUIDtoXML(outputPackage.PackageGUID);

if (project.ImportDirectory(guid, "C#", @"c:\temp\code\", "recurse=1") {
   outputPackage.Update();
   rootPackage.Update();
}


But nothing happens, it returns false from the ImportDirectory method, but I do not know why? Did I use it incorrectly. The directory I give does exist, and the output package also exists (as well as the root package) and is created successfully.

Additionally, I would love to know if it is possible to use string options to 'import from directory' too, like the last post, if anyone knows? But just getting a result is a first objective  :) (probably doing something silly? Should I specify the language differently? somehow include the extension .cs?)
Title: Re: Automated reverse engineering
Post by: jeanfrancois on May 04, 2010, 06:41:09 pm
Well, this works for me now. I don't know why. I wrote an add-in tried ImportDirectory to the same package I use in above code, and it worked.

Then I closed EA (after deleting the imported diagrams), I ran my external app (above code) again and this time it worked. I have no clue why. I changed my package too in external app, still it now works. Is there something I could have 'enabled' by running the add-in.

I'm totally confused. The add-in also did not work the very first time I tried it, but from second time on it worked. App haven't worked at all until after add-in worked for first time.  :-?

I have the same question also as Impact79: can I import with "Create Package From Directory"? Is it possible to specify this in the options string?
Title: Re: Automated reverse engineering
Post by: Geert Bellekens on May 04, 2010, 07:31:18 pm
You might have set some kind of option in the registry which enables the reverse engineering...  maybe... :-/

Geert
Title: Re: Automated reverse engineering
Post by: Eve on May 05, 2010, 08:36:23 am
It may be related to lazy load options, which I have found can cause problems.  A fix for this is scheduled for the next patch release.
Title: Re: Automated reverse engineering
Post by: jeanfrancois on May 05, 2010, 11:37:15 pm
I tried to change the USE_LAZY_LOADING registry value with no luck.

For some reason project.ImportDirectory does not work at all (after reboot this morning) in my app. ImportFile though does work. Why is that? Yesterday some automatic configuration got it working, maybe some registry setting like Geert says? Note, it works in my add-in, but not in my console application using API (only yesterday after I must have enabled something through using EA somehow).

Can anyone else use ImportDirectory though an external application? Any advice? Since it's worked, it must be some setting (authentication or something?).

-----------------------------------


Ok, it works now. It seems it only works when you call an update on the output package, for example:

This worked:
Code: [Select]
outputPackage = GetPackage(rootPackage, “Test”); // custom function
outputPackage.Update();
string guid = project.GUIDtoXML(outputPackage.PackageGUID);
project.ImportDirectory(guid, “C#”, path, “recurse=1”);
This did not work:
Code: [Select]
outputPackage = GetPackage(rootPackage, “Test”); // custom function
string guid = project.GUIDtoXML(outputPackage.PackageGUID);
project.ImportDirectory(guid, “C#”, path, “recurse=1”);

You were right I think, thanks Simon. Also, changing to "Create Package per Directory" option is done by changing the registry values for EA (IMPORT_TREE_PKG_STRUCT_NS = 0 for per directory, = 1 for per namespace, etc).


------

This problem is fixed in build 856.