Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Trying to generate DSL files using Java(I want to geenrate files following a DSL using a Java program)
Trying to generate DSL files using Java [message #1777723] Sat, 02 December 2017 23:09 Go to next message
Thibaut BERNARD is currently offline Thibaut BERNARDFriend
Messages: 1
Registered: December 2017
Junior Member
Hi !

I need to create a program able to generate files following a DSL i made using Xtext. I have some problems saving the file and I can't have it using my DSL syntax. The only thing I manage to obtain is a XMI file containing the data of the object.
My project is focusing on charts so my DSL extension is ".chart".

This part is where I instantiate a Java object containing the data :
	ChartPackage.eINSTANCE.eClass();
        ChartFactory factory = ChartFactory.eINSTANCE;
        Chart myChart = factory.createChart();
        myChart.setTitle("Population of countries");
        Data data = factory.createData();
        data.setType("Population (millions)");
        
        DataEntry entry1 = factory.createDataEntry();
        entry1.setName("France");
        entry1.getValues().add(66);
        data.getEntries().add(entry1);
        
        DataEntry entry2 = factory.createDataEntry();
        entry2.setName("Canada");
        entry2.getValues().add(31);
        data.getEntries().add(entry2);
        
        DataEntry entry3 = factory.createDataEntry();
        entry3.setName("USA");
        entry3.getValues().add(300);
        data.getEntries().add(entry3);
        
        myChart.setData(data);


And then I am able to create an XMI file using this :
        Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
        Map<String, Object> m = reg.getExtensionToFactoryMap();
        m.put("chart", new XMIResourceFactoryImpl());
        ResourceSet resSet = new ResourceSetImpl();
        Resource resource = resSet.createResource(URI
                .createURI("charts/test.chart"));
        resource.getContents().add(myChart);
        
        try {
            resource.save(Collections.EMPTY_MAP);
            System.out.println("File saved!");
        } catch (IOException e) {
            e.printStackTrace();
        }


The problem is that I want the data in my DSL syntax, not in XMI. I tried this code I found on the forum but it didn't work. The s variable is supposed to contains the text of the object following my syntax ;
        new ChartStandaloneSetup().createInjectorAndDoEMFRegistration();
        Injector injector = Guice.createInjector(new ChartRuntimeModule());  
        Serializer serializer = injector.getInstance(Serializer.class);
        String s = serializer.serialize(myChart);

When executing the previous code I have the following exception (ExecutionPerso is the name of the class where I have my main method) :
Exception in thread "main" java.lang.RuntimeException: No Context for Chart could be found
	at org.eclipse.xtext.serializer.impl.Serializer.getIContext(Serializer.java:165)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:126)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:178)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:55)
	at ExecutionPerso.main(ExecutionPerso.java:62)


I also tried to write it that way :
		Injector injector = new ChartStandaloneSetup().createInjectorAndDoEMFRegistration();
		ResourceSet rs = injector.getInstance(ResourceSet.class);
		Resource r = rs.createResource(URI.createURI("charts/test2.chart"));
                r.getContents().add(myChart);
		try {
			r.save(null);
		} catch (IOException e) {
			e.printStackTrace();
		}

But I have the following exception :
Exception in thread "main" java.lang.NullPointerException: Invalid context: Chart returns Chart
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
	at org.eclipse.xtext.serializer.sequencer.AbstractSyntacticSequencer.init(AbstractSyntacticSequencer.java:442)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:111)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:128)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:192)
	at org.eclipse.xtext.resource.XtextResource.doSave(XtextResource.java:386)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1430)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:999)
	at ExecutionPerso.main(ExecutionPerso.java:54)


Can you help me find what is the problem please ? Thank you !
Re: Trying to generate DSL files using Java [message #1777787 is a reply to message #1777723] Mon, 04 December 2017 15:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12322
Registered: July 2009
Senior Member
can you please provide a complete hello would example?

Injector injector = new ChartStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = injector.getInstance(ResourceSet.class);
Resource r = rs.createResource(URI.createURI("charts/test2.chart"));
r.getContents().add(myChart);
try {
r.save(null);
} catch (IOException e) {
e.printStackTrace();
}

should have worked (when this is te only thing you do)


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Mon, 04 December 2017 15:59]

Report message to a moderator

Re: Trying to generate DSL files using Java [message #1777830 is a reply to message #1777787] Tue, 05 December 2017 07:10 Go to previous message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 635
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Is the DSL's root element actually Chart or some other "Model" type?
Previous Topic:How to extend OpenGeneratedFileHandler to position the cursor in the right region
Next Topic:Accessing Code behind Model
Goto Forum:
  


Current Time: Thu Oct 18 16:47:45 GMT 2018

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

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

Back to the top