Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » xtend code(writing xtend code)
xtend code [message #1833775] Fri, 23 October 2020 16:23 Go to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
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 16:24]

Report message to a moderator

Re: xtend code [message #1833776 is a reply to message #1833775] Fri, 23 October 2020 16:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
HI, what is your question

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xtend code [message #1833778 is a reply to message #1833776] Fri, 23 October 2020 17:11 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
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 45 times)
Re: xtend code [message #1833779 is a reply to message #1833778] Fri, 23 October 2020 17:15 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xtend code [message #1833781 is a reply to message #1833779] Fri, 23 October 2020 17:29 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
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 17:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xtend code [message #1833783 is a reply to message #1833782] Fri, 23 October 2020 17:35 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
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.


Salman
Re: xtend code [message #1833784 is a reply to message #1833783] Fri, 23 October 2020 17:39 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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");
	}
}



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 23 October 2020 17:50]

Report message to a moderator

Re: xtend code [message #1833785 is a reply to message #1833784] Fri, 23 October 2020 18:02 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
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("}");
}

}


Salman
Re: xtend code [message #1833786 is a reply to message #1833785] Fri, 23 October 2020 18:14 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
I hope that you understand what I am trying to achieve.

Salman
Re: xtend code [message #1833787 is a reply to message #1833786] Fri, 23 October 2020 18:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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




Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xtend code [message #1833788 is a reply to message #1833787] Fri, 23 October 2020 18:27 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
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 48 times)

[Updated on: Fri, 23 October 2020 18:27]

Report message to a moderator

Re: xtend code [message #1833789 is a reply to message #1833788] Fri, 23 October 2020 18:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xtend code [message #1833790 is a reply to message #1833789] Fri, 23 October 2020 18:46 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
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.


Salman
Re: xtend code [message #1833791 is a reply to message #1833790] Fri, 23 October 2020 18:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xtend code [message #1833792 is a reply to message #1833791] Fri, 23 October 2020 18:53 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
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);
}


Salman
Re: xtend code [message #1833793 is a reply to message #1833792] Fri, 23 October 2020 19:14 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
Still not understand?

Salman
Re: xtend code [message #1833794 is a reply to message #1833793] Fri, 23 October 2020 19:55 Go to previous messageGo to next message
Muhammad Salman Tayyab is currently offline Muhammad Salman TayyabFriend
Messages: 36
Registered: March 2020
Location: Germany
Member
generated code will be like this:
video_list{
video1="Any path";
video2="Any path";
}
action{
show(video1) ;
show(video2);
}

Now, you will understand.


Salman
Re: xtend code [message #1833795 is a reply to message #1833794] Fri, 23 October 2020 20:10 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 23 October 2020 20:11]

Report message to a moderator

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


Current Time: Thu Apr 18 04:00:13 GMT 2024

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

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

Back to the top