[go: up one dir, main page]

Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Vorto » Generate several files(How to generate several files)
Generate several files [message #1730381] Mon, 25 April 2016 11:16 Go to next message
Komaromi Mate is currently offline Komaromi MateFriend
Messages: 6
Registered: April 2016
Location: Hungary
Junior Member
Hey guys! I need some help, i hope you can enlight me Smile

Im sitll working on my generator, i want to generate more than one file, but i dont really understand. I have checked the example generator codes, but i didnt get the point. I assume its more complicated compared to what i need. So, can you give me an example of code for this easy problem:

I want one file named "FirstFile", which contains "I am the first generated file!!", and one file named "SecondFile", which contains "I am the second file!!" , in the extend file. This is the code ive got:

package org.eclipse.vorto.example

import org.eclipse.core.runtime.IProgressMonitor
import org.eclipse.vorto.codegen.api.ICodeGenerator
import org.eclipse.vorto.codegen.api.tasks.Generated
import org.eclipse.vorto.codegen.api.tasks.ICodeGeneratorTask
import org.eclipse.vorto.codegen.api.tasks.IOutputter
import org.eclipse.vorto.codegen.api.tasks.eclipse.EclipseProjectGenerator
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel

class MyGenerator implements ICodeGenerator<InformationModel> {

	override generate(InformationModel infomodel, IProgressMonitor monitor) {
		new EclipseProjectGenerator(infomodel.getName()+"Device").addTask(new EmptyJavaGeneratorTask()).javaNature().generate(infomodel,monitor);
	}

	public static class EmptyJavaGeneratorTask implements ICodeGeneratorTask<InformationModel> {
		public override generate(InformationModel infomodel, IOutputter outputter) {
	    	outputter.output(new Generated(infomodel.getName(),null, getContent));
	    }
	    def String getContent() {
	   		return '//"I am the first generated file!!';
	    }
	}

	override getName() {
		return "FirstFile";
	}
}
Re: Generate several files [message #1730391 is a reply to message #1730381] Mon, 25 April 2016 11:44 Go to previous message
Olaf Weinmann is currently offline Olaf WeinmannFriend
Messages: 29
Registered: May 2015
Junior Member
Hello Komaromi,

you can create a code generator task for every file that you want to create. The generator class could look like follows:

public class MyGenerator implements IVortoCodeGenerator {
	
	public IGenerationResult generate(InformationModel infomodel, IMappingContext mappingContext) {
		
		GenerationResultZip zipOutputter = new GenerationResultZip(infomodel,getServiceKey());
		
		ChainedCodeGeneratorTask<InformationModel> generator = new ChainedCodeGeneratorTask<InformationModel>();
		generator.addTask(new EmptyJavaGeneratorTask();
		generator.addTask(new EmptyJavaGeneratorTask2();
		generator.generate(infomodel ,mappingContext, zipOutputter);
		
		return zipOutputter;
	}

	@Override
	public String getServiceKey() {
		return "MyGenerator";
	}
}


One of the related tasks could look like this:

public class EmptyJavaGeneratorTask extends
	AbstractTemplateGeneratorTask<InformationModel> {
	
	
	@Override
	public String getFileName(InformationModel im) {
		return im.getName();
	}
	
	@Override
		public String getPath(InformationModel im) {
		return "myPath";
	}
	
	@Override
	public ITemplate<InformationModel> getTemplate() {
		return new EmptyTemplate1();
	}
}


And a related template xtend-class could look like this:

class EmptyTemplate1 implements ITemplate<InformationModel>{
	override getContent(InformationModel im) {
		'''
			Hello World
		'''
	}
}


Please let me know if this helps!

Cheers,

Olaf
Previous Topic:Java library for accessing Vorto repository
Next Topic:Use of events in Functional Block
Goto Forum:
  


Current Time: Thu Dec 12 16:56:32 GMT 2024

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

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

Back to the top