Skip to main content



      Home
Home » Modeling » TMF (Xtext) » xtend code(writing xtend code)
xtend code [message #1833775] Fri, 23 October 2020 12:23 Go to next message
Eclipse UserFriend
Hey, I have written the Java code and the grammar also. Now, I am facing difficulty in writing the xtend code. I am attaching everything please have a look and help. Thanks in advance.

Grammar
Model:
parts+=ModelPart*;
ModelPart:
Video | Videouse;
Video:
"video_list" "{" videos+=Videodef* "}";
Videodef:
name=ID "=" value=STRING ";";
Videouse:
"actions" "{" playvideo+=Videoshow* "}";
Videoshow:
"show(" vid=[Videodef] ");";

Java Code

import java.util.Scanner;

public class grammarcode {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner input=new Scanner(System.in);

String[] videos= new String[2];
// String[] actions;

System.out.println("Please enter the videos name to play");
for(int i=0;i<(videos.length);i++) {
videos[i]=input.nextLine();
}

System.out.println("video_list{");
for(int i=0;i<(videos.length);i++) {
System.out.println(videos[i] +";");
}
System.out.println("}");

System.out.println("actions{");
for(int i=0;i<(videos.length);i++){
System.out.println("show("+ videos[i]+ ");");
}
System.out.println("}");
}

}

[Updated on: Fri, 23 October 2020 12:24] by Moderator

Re: xtend code [message #1833776 is a reply to message #1833775] Fri, 23 October 2020 12:25 Go to previous messageGo to next message
Eclipse UserFriend
HI, what is your question
Re: xtend code [message #1833778 is a reply to message #1833776] Fri, 23 October 2020 13:11 Go to previous messageGo to next message
Eclipse UserFriend
Can you convert my Java code into the xtend code which I can write in the mydslGenerator.xtend file. So that after running the mydslGenerator.xtend file java code will be generated?

Please see the attachment.
  • Attachment: xtend.PNG
    (Size: 34.50KB, Downloaded 100 times)
Re: xtend code [message #1833779 is a reply to message #1833778] Fri, 23 October 2020 13:15 Go to previous messageGo to next message
Eclipse UserFriend
I still don't under stand

Given which model file
Which java file should be generated

You java file does not mention
Video and Video use instances at all
Re: xtend code [message #1833781 is a reply to message #1833779] Fri, 23 October 2020 13:29 Go to previous messageGo to next message
Eclipse UserFriend
This is my Grammar:

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

generate myDsl "http://www.video1.org/example/mydsl/MyDsl"

Model:
parts+=ModelPart*;
ModelPart:
Video | Videouse;
Video:
"video_list" "{" videos+=Videodef* "}";
Videodef:
name=ID "=" value=STRING ";";
Videouse:
"actions" "{" playvideo+=Videoshow* "}";
Videoshow:
"show(" vid=[Videodef] ");";

That how my grammar will look when it is tested
video_list{
video1="C://new_folder/video/move_hand.mp4";
video2="C://new_folder/video/make_sound.mp3";
}
actions{
show(video1);
show(video2);
}

Now from the grammar I want to generate the java code
I am also attaching the screenshots of my grammar file, tested grammar file, and both the other screenshot where I tried to write the code( named logic file) so that java code will be generated and its output what I get.



Re: xtend code [message #1833782 is a reply to message #1833781] Fri, 23 October 2020 13:34 Go to previous messageGo to next message
Eclipse UserFriend
yes but given

			video_list {
				oneVideo = "This is a vid";
				twoVideo = "This is the second vid";
				anotherVid = "This is another vid";
			} 
			actions {
				show(oneVideo);
				show(anotherVid);
			}


which code should be generated
Re: xtend code [message #1833783 is a reply to message #1833782] Fri, 23 October 2020 13:35 Go to previous messageGo to next message
Eclipse UserFriend
video_list {
oneVideo = "This is a vid";
twoVideo = "This is the second vid";
anotherVid = "This is another vid";
}
actions {
show(oneVideo);
show(anotherVid);
}
The above code will be generated.
Re: xtend code [message #1833784 is a reply to message #1833783] Fri, 23 October 2020 13:39 Go to previous messageGo to next message
Eclipse UserFriend
i still dont see the java code you want to generate.
i just see
- grammar
- sample model
- some generator code

but what is the code given the sample model you want to output

e.g.

package org.xtext.example.mydsl5.generator;

public class videodecl {
	public static void main(String[] args) {
		System.out.println("There is the video \"This is a vid\" with name oneVideo");
		System.out.println("There is the video \"This is the second vid\" with name twoVideo");
		System.out.println("There is the video \"This is another vid\" with name anotherVid");
		System.out.println("playing oneVideo");
		System.out.println("playing anotherVid");
	}
}

[Updated on: Fri, 23 October 2020 13:50] by Moderator

Re: xtend code [message #1833785 is a reply to message #1833784] Fri, 23 October 2020 14:02 Go to previous messageGo to next message
Eclipse UserFriend
Below java code I want to generate

import java.util.Scanner;

public class grammarcode {

public static void main(String[] args) {
// TODO Auto-generated method stub

Scanner input=new Scanner(System.in);

String[] videos= new String[2];
// String[] actions;

System.out.println("Please enter the videos name to play");
for(int i=0;i<(videos.length);i++) {
videos[i]=input.nextLine();
}

System.out.println("video_list{");
for(int i=0;i<(videos.length);i++) {
System.out.println(videos[i] +";");
}
System.out.println("}");

System.out.println("actions{");
for(int i=0;i<(videos.length);i++){
System.out.println("show("+ videos[i]+ ");");
}
System.out.println("}");
}

}
Re: xtend code [message #1833786 is a reply to message #1833785] Fri, 23 October 2020 14:14 Go to previous messageGo to next message
Eclipse UserFriend
I hope that you understand what I am trying to achieve.
Re: xtend code [message #1833787 is a reply to message #1833786] Fri, 23 October 2020 14:19 Go to previous messageGo to next message
Eclipse UserFriend
in the code you want to generate there is no

oneVideo , twoVideo
there is no "This is another vid"

and there is no play anotherVid

so how does your code relate to the model
and how does it change if you delete
twoVideo = "This is the second vid";
from the model


Re: xtend code [message #1833788 is a reply to message #1833787] Fri, 23 October 2020 14:27 Go to previous messageGo to next message
Eclipse UserFriend
Now, If you understand the scenario please update my code and you can clearly see in my code, first I am taking input from the user and I am also attaching a screenshot of my code output.
I write the code in a simple java environment and not in the xtend environment.Through xtend I want to generate java code.

I am newbie that's why I don't know how to write the logic in .xtend file.
  • Attachment: output.PNG
    (Size: 9.80KB, Downloaded 78 times)

[Updated on: Fri, 23 October 2020 14:27] by Moderator

Re: xtend code [message #1833789 is a reply to message #1833788] Fri, 23 October 2020 14:37 Go to previous messageGo to next message
Eclipse UserFriend
i still dont understand.
you show a java app and a use interacts with it
how does this relate to xtext generator.

if you use this

	override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
		fsa.generateFile('greetings.txt', '''
		import java.util.Scanner;
		
		public class grammarcode {
		
		public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner input=new Scanner(System.in);
		
		String[] videos= new String[2];
		// String[] actions;
		
		System.out.println("Please enter the videos name to play");
		for(int i=0;i<(videos.length);i++) {
		videos[i]=input.nextLine();
		}
		
		System.out.println("video_list{");
		for(int i=0;i<(videos.length);i++) {
		System.out.println(videos[i] +";");
		}
		System.out.println("}");
		
		System.out.println("actions{");
		for(int i=0;i<(videos.length);i++){
		System.out.println("show("+ videos[i]+ ");");
		}
		System.out.println("}");
		}
		
		}
		''')
	}


the generator will generate your java code
Re: xtend code [message #1833790 is a reply to message #1833789] Fri, 23 October 2020 14:46 Go to previous messageGo to next message
Eclipse UserFriend
I just show you the java app for understanding and also write java code for my own understanding. This code has nothing to do with the generator.
I think you still not understand my point.
Can you please write a code in the mydslgenerator.xtend file according to my grammar and the tested grammar? So that I can understand.
Right now, I am facing difficulty in the syntax of the xtend.
Re: xtend code [message #1833791 is a reply to message #1833790] Fri, 23 October 2020 14:49 Go to previous messageGo to next message
Eclipse UserFriend
i said: i can do if you tell me the java code which you want to generate for this model

			video_list {
				oneVideo = "This is a vid";
				twoVideo = "This is the second vid";
				anotherVid = "This is another vid";
			} 
			actions {
				show(oneVideo);
				show(anotherVid);
			}


i am asking you:

how shall the generated java code for this model file look like
you said

public class videodecl {
	public static void main(String[] args) {
		System.out.println("There is the video \"This is a vid\" with name oneVideo");
		System.out.println("There is the video \"This is the second vid\" with name twoVideo");
		System.out.println("There is the video \"This is another vid\" with name anotherVid");
		System.out.println("playing oneVideo");
		System.out.println("playing anotherVid");
	}
}


is wrong.
so which code should be generated instead?
Re: xtend code [message #1833792 is a reply to message #1833791] Fri, 23 October 2020 14:53 Go to previous messageGo to next message
Eclipse UserFriend
First of all, thank you for your time.
Just to be simple. Please write the code which gives the output like this model below.

video_list {
oneVideo = "This is a vid";
twoVideo = "This is the second vid";
anotherVid = "This is another vid";
}
actions {
show(oneVideo);
show(anotherVid);
}
Re: xtend code [message #1833793 is a reply to message #1833792] Fri, 23 October 2020 15:14 Go to previous messageGo to next message
Eclipse UserFriend
Still not understand?
Re: xtend code [message #1833794 is a reply to message #1833793] Fri, 23 October 2020 15:55 Go to previous messageGo to next message
Eclipse UserFriend
generated code will be like this:
video_list{
video1="Any path";
video2="Any path";
}
action{
show(video1) ;
show(video2);
}

Now, you will understand.
Re: xtend code [message #1833795 is a reply to message #1833794] Fri, 23 October 2020 16:10 Go to previous message
Eclipse UserFriend
that is easy

override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
		for (m : resource.allContents.toIterable.filter(Model)) {
			fsa.generateFile("video/videodecl.txt", '''
			video_list{
			«FOR v : m.parts.filter(Video)»
				«FOR vd : v.videos»
					name="«Strings.convertToJavaString(vd.value)»";
				«ENDFOR»
			«ENDFOR»
			}
			action{
			«FOR u : m.parts.filter(Videouse)»
				«FOR play : u.playvideo»
					show(«play.vid.name»);
				«ENDFOR»
			«ENDFOR»
			}
			''')
		}
	}


but this aint java code that is generated

[Updated on: Fri, 23 October 2020 16:11] by Moderator

Previous Topic:Performance issue in QueuedBuildData
Next Topic:Adding generated objects to the index
Goto Forum:
  


Current Time: Wed Jul 23 13:44:57 EDT 2025

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

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

Back to the top