Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Xpand2]Loading multiple model files
[Xpand2]Loading multiple model files [message #675932] Thu, 02 June 2011 01:39 Go to next message
Serge  is currently offline Serge
Messages: 3
Registered: April 2011
Junior Member
Howdy!

I'm currently loading both Xtext models and XMI models using the org.eclipse.xtext.mwe.Reader and org.eclipse.emf.mwe.utils.Reader components respectively:

component = Reader {
	uri = "platform:/resource/org.xtext.example.generator/src/model/default.graph"
	modelSlot = "qgraph"
}	
component = org.eclipse.xtext.mwe.Reader {
	// lookup all resources on the classpath
	// useJavaClassPath = true

	// or define search scope explicitly
	path = modelPath

	// this class will be generated by the xtext generator 
	register = org.xtext.RpgdslStandaloneSetup {}
	load = {
		slot = "model"
		type = "Model"
	}
}


Is there a way to load every XMI file in a folder? Or better yet, every file with a given extension? As opposed to defining what exactly is the file we want to load that is. I know the Xtext reader can do that, it would be useful if the other one did as well!

Thank you for your time,
Sérgio Silva
Re: [Xpand2]Loading multiple model files [message #676028 is a reply to message #675932] Thu, 02 June 2011 14:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
Hi,

you could of course come with such a workflow component yourself. or you provide a own ResourceServiceProvider for the non xtext files then the org.eclipse.xtext.mwe.Reader will read these files too. Xtext comes with a ResourceServiceProvider for .ecore files. see plugins org.eclipse.xtext.ecore and org.eclipse.xtext.ui.ecore. therefore it woulkd be helpful if the xmi files have a metamodel specific file extension.

~Christian

[Updated on: Thu, 02 June 2011 14:58]

Report message to a moderator

Re: [Xpand2]Loading multiple model files [message #676036 is a reply to message #676028] Thu, 02 June 2011 15:07 Go to previous messageGo to next message
Serge  is currently offline Serge
Messages: 3
Registered: April 2011
Junior Member
Hello Christian,

Thank you for replying!

I've been trying to implement my own component, but unfortunately I'm not yet familiarized with the API.

My current solution does what I want but the slot containing all the resources has type List[org::eclipse::emf::ecore::xmi::impl::XMIResourceImpl]. Is there a way to specify the type of the resource?

public void invoke(IWorkflowContext ctx) {
	File f = new File(xmiLocation);
	String files[] = f.list(new FilenameFilter(){
		public boolean accept(File arg0, String arg1) {
			return arg1.endsWith(extension);
		}
	});
	ResourceSet set = new ResourceSetImpl();
	for(String file : files){
		String nuri = uri + file;
		set.createResource(URI.createURI(nuri));
	}
	
	ctx.put(getModelSlot(), set.getResources());
}


xmiLocation is the relative path to the folder containing the files, extension is the file extension we want to process and uri is the uri to the folder containing the files. I know it's a bit redundant having both relative path and URI, but it was the only way I was able to find to obtain all the files with a given extension.

What am I doing wrong?

Thanks again,
Sérgio
Re: [Xpand2]Loading multiple model files [message #676041 is a reply to message #676036] Thu, 02 June 2011 15:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
Hi,

what about asking the resources for their contents. and add all the contents to the slot. see the org.eclipse.emf.mwe.utils.Reader component.
final Resource res;
		try {
			res = resourceSet.getResource(URI.createURI(uri), true);
			if (res == null)
				throw new WorkflowInterruptedException("Couldn't find resource under " + uri);
			if (!res.isLoaded()) {
				res.load(Collections.EMPTY_MAP);
			}
		}
		catch (final WrappedException e) {
			if (ignoreMissingModel) {
				return null;
			}
			throw new WorkflowInterruptedException("Couldn't load resource under " + uri + " : " + e.getMessage());
		}
		catch (final IOException e) {
			if (ignoreMissingModel) {
				return null;
			}
			throw new WorkflowInterruptedException("Couldn't load resource under " + uri + " : " + e.getMessage());
		}
		final EList<EObject> result = res.getContents();
		if (firstElementOnly) {
			if (result.isEmpty())
				return null;
			return result.iterator().next();
		}
		else
			return result;



~Christian
Re: [Xpand2]Loading multiple model files [message #676125 is a reply to message #676041] Thu, 02 June 2011 21:57 Go to previous messageGo to next message
Serge  is currently offline Serge
Messages: 3
Registered: April 2011
Junior Member
It worked! Thank you very much!

Sérgio
Re: [Xpand2]Loading multiple model files [message #676563 is a reply to message #676125] Sun, 05 June 2011 09:39 Go to previous messageGo to next message
Stefan is currently offline Stefan
Messages: 9
Registered: June 2011
Junior Member
Hello together,

I have the same or a similar problem. I want to process multiple .xmi (dynamic instances of a ecore model) files with data in one Xpand step.
So the .xmi must be merged at some point.

The purpose is I want to generate one single output file from multiple xmi files.
Each xmi file contains only a part of the complete data.
You can say the model data is distributed over several xmi files, an is not concentrated (merged) in a single xmi file.

I understood from posts above this can be achived by by loading multiple xmi into the same model-slot. But I do not understand how to do this.

Does the code above do the job? I guess it is java code
I have no java in my project. Only a Ecore model, dynamic instances, a workflow and a xpand template.
Re: [Xpand2]Loading multiple model files [message #676608 is a reply to message #676563] Sun, 05 June 2011 17:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6388
Registered: July 2009
Senior Member
Hi,

you have to write (as Serge did) a custom workflow component that does this for you. this component could look like (quick n dirty) or a mix of serges and my post above.
package components;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.mwe.core.WorkflowContext;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.lib.WorkflowComponentWithModelSlot;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;

public class MultiModelReader extends WorkflowComponentWithModelSlot {

	private List<String> uris = new ArrayList<String>();
	
	public void addUri(String uri) {
		uris.add(uri);
	}
	
	@Override
	protected void invokeInternal(WorkflowContext ctx, ProgressMonitor monitor,
			Issues issues) {
		
		ResourceSet set = new ResourceSetImpl();
		List<EObject> allContents = new ArrayList<EObject>();
		for(String uri : uris){
			Resource resource = set.getResource(URI.createURI(uri), true);
			allContents.addAll(resource.getContents());
		}
		ctx.set(getModelSlot(), allContents);
	}

}


<?xml version="1.0"?>
<workflow>

	<property name="src-gen" value="src-gen" />
	
	<!-- set up EMF for standalone execution -->
	<bean class="org.eclipse.emf.mwe.utils.StandaloneSetup" >
		
		<platformUri value=".."/>
		<registerEcoreFile value="src/metamodel/metamodel.ecore" />
	</bean>
	
	<!-- instantiate metamodel -->
	<bean id="mm_emf" class="org.eclipse.xtend.typesystem.emf.EmfRegistryMetaModel"/>

	<!-- load model and store it in slot 'model' -->
	<component class="components.MultiModelReader">
		<uri value="platform:/resource/my.generator.project/src/model/Model.xmi" />
		<uri value="platform:/resource/my.generator.project/src/model/Model2.xmi" />
		<modelSlot value="model" />
	</component>



	<!--  generate code -->
	<component class="org.eclipse.xpand2.Generator">
		<metaModel idRef="mm_emf"/>
		<expand
			value="template::Template::main FOREACH model" />
		<outlet path="${src-gen}" >
			<postprocessor class="org.eclipse.xpand2.output.JavaBeautifier" />
		</outlet>
	</component>
</workflow>



if you want to scan a directory you need somthing like serge + but as discussed before you have to add all contents of the resources to a list as i do it in my code.

if you want to call a template for a list simpoly ajust the definition

«DEFINE main FOR List[Model]»

~Christian

[Updated on: Sun, 05 June 2011 17:38]

Report message to a moderator

Re: [Xpand2]Loading multiple model files [message #676622 is a reply to message #676608] Sun, 05 June 2011 18:23 Go to previous message
Stefan is currently offline Stefan
Messages: 9
Registered: June 2011
Junior Member
Thank you Christian,

works almost perfectly Smile

(I am wondering why "List" is known, but it works)
Previous Topic:Ecore Model & Xpand: Uppercase Attributes lead to errors
Next Topic:[Acceleo] Ref: Bug 319375 - Using ant task with model referring to UML Primitive types
Goto Forum:
  


Current Time: Sat Oct 25 11:18:54 GMT 2014

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

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