Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Serialize models containing cross-references(Save models referring each-other using the scoping.ImportNamespacesScopingFragment)
Serialize models containing cross-references [message #925648] Thu, 27 September 2012 23:13
Michael Steiner is currently offline Michael SteinerFriend
Messages: 3
Registered: April 2010
Junior Member
I started with the "Xtext Project" wizard of Xtext 2.3.1 using the default parameters. Note that this will use the scoping.ImportNamespacesScopingFragment that provides an easy-to-use global scoping.

Further, I slightly extended the grammar with a cross-reference:

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl ""


	"Person" name=ID

	"Hello" person=[Person] '!'

Now, I created a small "JUnit Plug-in Test". It exemplary shows how to create a simple model spread over two resources referring to each other:

package org.xtext.example.mydsl.tests;

import java.util.Collections;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.ui.XtextProjectHelper;
import org.junit.Test;
import org.xtext.example.mydsl.myDsl.Greeting;
import org.xtext.example.mydsl.myDsl.Model;
import org.xtext.example.mydsl.myDsl.MyDslFactory;
import org.xtext.example.mydsl.myDsl.Person;
import org.xtext.example.mydsl.ui.internal.MyDslActivator;


public class StoreModelsWithReferenceTest {

	public void test() throws IOException, CoreException {
		// create models (simple example with one reference)
		MyDslFactory factory = MyDslFactory.eINSTANCE;
		Model m1 = factory.createModel();
		Model m2 = factory.createModel();
		Person p1 = factory.createPerson();
		Person p2 = factory.createPerson();
		Greeting g = factory.createGreeting();

		// create project (with Java and Xtext nature)
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		final String PROJECT_NAME = "example";
		IProject project = root.getProject(PROJECT_NAME);
		project.create(new NullProgressMonitor()); NullProgressMonitor());
		IProjectDescription description = project.getDescription();
		String[] natures = description.getNatureIds();
		String[] newNatures = new String[natures.length + 2];
		System.arraycopy(natures, 0, newNatures, 0, natures.length);
		newNatures[natures.length] = JavaCore.NATURE_ID;
		newNatures[natures.length + 1] = XtextProjectHelper.NATURE_ID;
		project.setDescription(description, new NullProgressMonitor());

		// create resources (two Xtext resources for the two models)
		URI uri1 = URI.createPlatformResourceURI("/" + PROJECT_NAME + "/m1.mydsl", true);
		URI uri2 = URI.createPlatformResourceURI("/" + PROJECT_NAME + "/m2.mydsl", true);
		Injector injector = MyDslActivator.getInstance().getInjector(MyDslActivator.ORG_XTEXT_EXAMPLE_MYDSL_MYDSL);
		XtextResourceSet xrs = injector.getInstance(XtextResourceSet.class);
		XtextResource xtextResource1 = (XtextResource) xrs.createResource(uri1);
		XtextResource xtextResource2 = (XtextResource) xrs.createResource(uri2);

		// store resources (incremental build makes it visible to the second);, new NullProgressMonitor());;

I now wonder whether some of these steps in the test can be simplified. Any input is welcome.
Previous Topic:Unable to generate from Arithmetics ex
Next Topic:F3/Open Declaration disabled on cross references
Goto Forum:

Current Time: Thu Mar 23 20:28:33 GMT 2017

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

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