Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » MoDisco » From .jar to any model(How to get a representation of a .jar file with modisco?)
From .jar to any model [message #1052873] Tue, 30 April 2013 20:38 Go to next message
Edson Filho is currently offline Edson FilhoFriend
Messages: 6
Registered: April 2013
Junior Member
Hi all,

I want to use Modisco to translate a .jar file into a model, and later query it with OCL.

Is possible to create a model from a jar file?

Thanks in advance.
Re: From .jar to any model [message #1053035 is a reply to message #1052873] Thu, 02 May 2013 07:28 Go to previous messageGo to next message
Fabien Giquel is currently offline Fabien GiquelFriend
Messages: 147
Registered: July 2009
Senior Member
Hi Edson,

there is one Modisco Discoverer which takes a .jar as input for creating some "Java" model.
The discoverer is available for direct use from the workspace : right clibk on ".jar" file -> "Discovery->Discoverers->Discover Java Model From Library..."
The discoverer is available from public java API (org.eclipse.modisco.java.discoverer plugin).

Regards,
Fabien.


----------------------------------------------------
Fabien GIQUEL
R&D Engineer
Mia-Software
rue Nina Simone
44000 NANTES
----------------------------------------------------
Re: From .jar to any model [message #1053047 is a reply to message #1053035] Thu, 02 May 2013 08:06 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo BruneliereFriend
Messages: 674
Registered: July 2009
Senior Member
Hello,

In addition to Fabien's answer, and concerning more the querying part once you have already gotten your Java model, note that you can also use ATL (which is an OCL-based model transformation language).

Hugo


--------------------------------------------------------
Hugo Bruneliere, PhD
NaoMod team (IMT Atlantique & LS2N-CNRS)
Nantes - France
--------------------------------------------------------
Re: From .jar to any model [message #1053285 is a reply to message #1053047] Fri, 03 May 2013 14:49 Go to previous messageGo to next message
Edson Filho is currently offline Edson FilhoFriend
Messages: 6
Registered: April 2013
Junior Member

Thank you for your prompt reply, Smile

Based on the documentation I wrote this little example.

But I have another question, How can I "set" the jar path to the FragmentRoot?

Thanks in advance.

  DiscoverJavaModelFromLibrary discover = new DiscoverJavaModelFromLibrary();
    String path = "/tmp/test.jar";
    IFile jar = ... ;

    IPackageFragmentRoot root = JavaCore.createJarPackageFragmentRootFrom(jar);
    IProgressMonitor monitor = new NullProgressMonitor();

    /* create model */
    discover.discoverElement(root,  monitor);
    Resource resource = discover.getTargetModel();
    Model model = (Model) resource.getContents().get(0);

    /* list methods */
    EList<ClassFile> classFiles = model.getClassFiles();
    for (ClassFile classFile : classFiles) {
      System.out.println(classFile.getName());
    }

Re: From .jar to any model [message #1059738 is a reply to message #1053285] Tue, 21 May 2013 13:12 Go to previous message
Edson Filho is currently offline Edson FilhoFriend
Messages: 6
Registered: April 2013
Junior Member
Thank you Fabien and Hugo,

This is the method to extract the model from a given jar file. The method saves the model into a .xmi file. It's working, but need a refactoring.

Problem solved. Smile

public void getModel(String jar) throws RemoteException {

		if (jar == null)
			return;

		try {
			/* Open eclipse project */
			IWorkspace ws = ResourcesPlugin.getWorkspace();
			IProject project = ws.getRoot().getProject("FeatureExtractor");
			IProgressMonitor monitor = new NullProgressMonitor();

			/* Create a new project, in case there isn't one */
			if (!project.exists()) {
				IProjectDescription description = project.getDescription();
				description.setNatureIds(new String[] {"org.eclipse.jdt.core.javanature"});
				project.setDescription(description, monitor);
				project.create(monitor);
				if (!project.isOpen())
					project.open(monitor);	
			}

			/* Create a link to the jar file, which is outside the project */
			IPath location = new Path(jar);
			IFile file = project.getFile(location.lastSegment());
			file.createLink(location, IResource.REPLACE, monitor);

			/* Create Java project, and 
			 * add location (jar file) to the build path */
			IJavaProject javaProject = JavaCore.create(project);
			IPath filePath = project.getWorkspace().getRoot().getLocation().append(file.getFullPath());

			/* Set project fragment roots */
			Set<IClasspathEntry> e = new HashSet<IClasspathEntry>();
			e.addAll(Arrays.asList(javaProject.getRawClasspath()));			
			boolean entryExist = false;
			for (IClasspathEntry c : javaProject.getRawClasspath()) {
				if ( filePath.toOSString().matches(c.getPath().toOSString()) ) {
					entryExist = true;
					break;
				}
			}			
			IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
			LibraryLocation[] locations= JavaRuntime.getLibraryLocations(vmInstall);
			for (LibraryLocation element : locations) {
				e.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
			}
			for (IPackageFragmentRoot r : javaProject.getPackageFragmentRoots()) {
				if ( filePath.toOSString().matches(r.getPath().toOSString()) ) {
					entryExist = true;
					break;
				}
			}	
			if (!entryExist) {
				e.add(JavaCore.newLibraryEntry(filePath, null, null));
			}
			javaProject.setRawClasspath(e.toArray(new IClasspathEntry[e.size()]), monitor);

			/* Create discover for jar file */
			DiscoverJavaModelFromLibrary discover = new DiscoverJavaModelFromLibrary();
			IPackageFragmentRoot root = JavaCore.createJarPackageFragmentRootFrom(file);
			if (root == null) {
				System.out.println("Error: Fragment root is null. Aborting...");
				return null;		
			}

			/* Create model from jar file */
			discover.discoverElement(root, monitor);
			Resource resource = discover.getTargetModel();
			// Model model = (Model) resource.getContents().get(0);

			/* Save Model XMI */
			exportModelToXMI(resource, filePath.toOSString().replace(".jar", ".xmi"));

		} catch (CoreException e) {
			e.getMessage();
			e.printStackTrace();
		} catch (DiscoveryException e) {
			e.getMessage();
			e.printStackTrace();
		} catch (ParserException e) {
			e.getMessage();
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
Previous Topic:A simpler ecore java model?
Next Topic:How to extract the complete source code, not only the signatures
Goto Forum:
  


Current Time: Fri Apr 19 15:38:55 GMT 2024

Powered by FUDForum. Page generated in 0.01886 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top