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 16:38 Go to next message
Edson Filho is currently offline Edson Filho
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 03:28 Go to previous messageGo to next message
Fabien Giquel is currently offline Fabien Giquel
Messages: 138
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
4, rue du Ch√Ęteau de l'Eraudiere
44324 NANTES CEDEX 03
----------------------------------------------------
Re: From .jar to any model [message #1053047 is a reply to message #1053035] Thu, 02 May 2013 04:06 Go to previous messageGo to next message
Hugo Bruneliere is currently offline Hugo Bruneliere
Messages: 600
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 - R&D Engineer
AtlanMod research team (Inria, EMN & LINA)
Ecole des Mines de Nantes
Nantes - France
------------------------------------------
Re: From .jar to any model [message #1053285 is a reply to message #1053047] Fri, 03 May 2013 10:49 Go to previous messageGo to next message
Edson Filho is currently offline Edson Filho
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 09:12 Go to previous message
Edson Filho is currently offline Edson Filho
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: Thu Jul 24 13:51:11 EDT 2014

Powered by FUDForum. Page generated in 0.02618 seconds