Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » xText as standalone lexer, parser, interpreter?
xText as standalone lexer, parser, interpreter? [message #1695802] Tue, 19 May 2015 12:08 Go to next message
Dominik Reinert is currently offline Dominik ReinertFriend
Messages: 14
Registered: May 2015
Junior Member
Hello,

I want xText to simply check if inputs given by the user are in the correct syntax. If so, it should make a database entry with the specification given by the user.

E.g:

1) User enters:
If [300] ( sensor1.temperature >= 200) then (sendEmail ( 'www.example @given.com'))

(means: if the temperature of sensor 1 is higher than 200 ° for 300 milliseconds, the system should send an email)

2) Is the Syntax correct?

3) xText (?) should interprete (?) this expression as follows:

3.1) Does sensor1 exist? Looking for it in Database Table 'Sensors.ID' (primary key).
3.2) Has sensor1 got 'temperature'? Looking for it in another table as one sensor can have many values for different issues.
3.3) Is action 'sendEmail' valid? Table 'Actions'
3.4) In this particular case: Is the email-adress valid?

4) If all these tests went good, the next step would be creating an entry in the 'rules' table (sensor.ID [foreign key] , temperature, condition, action, parameters for action)


Now my question is, if it is possible to write a standalone client which is able to handle the User input and proceding it with the DSL I wrote in xText. This client should more or less only be like an editor or something like that.

The only things I found where many tutorials on how to use the language generator, but that does not seem to be the kind of thing I need.

Thank you in advance


---------------------------------

The DSL-grammar:

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

generate ruleLanguage "http://www.xtext.org/example/mydsl/MyDsl"

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

ruleLanguage:
	'if ['timeInMs=INT'] ('conds=CONDITIONS') then ('actions=ACTIONS')';
	
/* There can be as many conditions as the user wants to */
/* link to another	*/
CONDITIONS returns Expression:
	SCOND ({CONDITIONS.left=current} '&' right=SCOND)*;
	
/* This is how one condition is set up:*/
/* name << value*/
/* for example: */
/* sensor1.temperature << 100°C*/
SCOND returns Expression:
	sname=ID'.'valueLeft=INT COMP valueRight=INT;

/* These are the possible operators (comparison only)  */	
COMP:
	'<<' | '>>' | '==' | '<=' | '>=';

/* The conditions defined earlier can also have as many */
/* actions as the user wants */
ACTIONS returns Expression:
	ACTION ({ACTIONS.left=current}'&' right=ACTION)*;

/* An action has a name and gets a parameter list, e.g.:*/
/* sendEmail ("example@googlemail.com") */
ACTION returns Expression:
	fname=ID '(' params=PARLIST ')';

/* Whatever comes here should just be given to the */
/* called function of the main program */	
PARLIST returns Expression:
	MOREPAR ({PARLIST.left=current} ',' right=MOREPAR)*;
	
MOREPAR  returns Expression:
	{MOREPAR}MOREPARValue=MOREPARValue ;
	
MOREPARValue returns ecore::EString:(INT | ID);	

[Updated on: Tue, 19 May 2015 12:09]

Report message to a moderator

Re: xText as standalone lexer, parser, interpreter? [message #1695805 is a reply to message #1695802] Tue, 19 May 2015 12:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi i am not sure what your specific problem is but here you can find how to read a model https://wiki.eclipse.org/Xtext/FAQ#How_do_I_load_my_model_in_a_standalone_Java_application.C2.A0.3F

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText as standalone lexer, parser, interpreter? [message #1695913 is a reply to message #1695805] Wed, 20 May 2015 09:25 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 159
Registered: November 2014
Location: Russia
Senior Member
You can generate command line version of xtext platform for given gramatik. For this add to MWE2 files the following lines
// generator API
fragment = generator.GeneratorFragment {
generateJavaMain = true
}

In this case you can send the text file as input. May be this is help

Thanks
Alex
Re: xText as standalone lexer, parser, interpreter? [message #1695915 is a reply to message #1695805] Wed, 20 May 2015 09:32 Go to previous messageGo to next message
Dominik Reinert is currently offline Dominik ReinertFriend
Messages: 14
Registered: May 2015
Junior Member
Where do I need to enter this:

new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
Injector injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resource = resourceSet.getResource(
    URI.createURI("platform:/resource/org.xtext.example.mydsl/src/example.mydsl"), true);
Model model = (Model) resource.getContents().get(0);


and how do I assign my language to a simple .txt (or whatever) File.

I wanted to start a new Java Project and wanted to import the .jar File created with the xtext export-option. First problem: It only shows the .jarFile once i click on 'add external jar File' and not if I click 'Add Jar' . Is that a problem?

Then, when I added the jar to my build path, I cannot open a simple 'File.rL' and be asked 'Do you want to apply the xtext grammar to it?'


Remember pls: I am just a stupid student with no experience with all this, feeling pretty overchallenged Sad
Re: xText as standalone lexer, parser, interpreter? [message #1695916 is a reply to message #1695915] Wed, 20 May 2015 09:36 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Simply create a java main method in a new class in the runtime project

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText as standalone lexer, parser, interpreter? [message #1695917 is a reply to message #1695916] Wed, 20 May 2015 09:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
P.s if you have run the main once you can use the export runnable jar file wizard to create a all in one jar (this task is zero xtext specific)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText as standalone lexer, parser, interpreter? [message #1695918 is a reply to message #1695917] Wed, 20 May 2015 09:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
And to get an editor you have to run it as eclipse appplication or install it as plugin

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText as standalone lexer, parser, interpreter? [message #1695932 is a reply to message #1695918] Wed, 20 May 2015 11:40 Go to previous messageGo to next message
Dominik Reinert is currently offline Dominik ReinertFriend
Messages: 14
Registered: May 2015
Junior Member
Okay, thanks so far.

I (think) have installed it as a plugin now. So how to get the xtext nature to a File called 'xxxx.rL'?
import org.eclipse.emf.ecore.xml.type.internal.DataValue.URI;
import org.eclipse.xtend.expression.Resource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;

public class RuleFileParser {

	public static void main (String []args) {
		
		new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
		Injector injector = new RuleFileParser().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
		Resource resource = resourceSet.getResource(
		URI.createURI("C:\\Users\\xxx\\git_repo\\projekt-rbep\\RuleDefinition\\src\\Rules.rL"), true);
		Model model = (Model) resource.getContents().get(0);
	}
}


gets me
- "Injector cannot be resolved to a type " (no import option available, only create)
- "createInjectorAndDoEMFRegistration is undefined" (well okay, seems legit, but what is this?)
- "The method createURI(String) is undefined for the type DataValue.URI"
- "Line breakpoint:RuleFileParser [line: 17] - main(String[])"
- "Model cannot be resolved to a type"
- "The method getContents() is undefined for the type"
Re: xText as standalone lexer, parser, interpreter? [message #1695935 is a reply to message #1695932] Wed, 20 May 2015 11:50 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i still do not understand your usecase:
do you want to work standalone or in the editor.

if you want to use it in the editor + the generator (using the builder) or call some code from a command (use as a starting point https://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/)

if you want to use it standalone simply run the java main (from the project that contains the .xtext file) or run the exported jar.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText as standalone lexer, parser, interpreter? [message #1695970 is a reply to message #1695802] Wed, 20 May 2015 16:18 Go to previous messageGo to next message
Dominik Reinert is currently offline Dominik ReinertFriend
Messages: 14
Registered: May 2015
Junior Member
Hey

Here is a little handwritten picture, hope this helps you to better understand my problem, I don't know how to better describe it.


Maybe like this: I want it to run in a self-written Editor, passing the information (if in the correct grammar) on to a database.

[Updated on: Wed, 20 May 2015 16:21]

Report message to a moderator

Re: xText as standalone lexer, parser, interpreter? [message #1695972 is a reply to message #1695970] Wed, 20 May 2015 16:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
then simply add the exported (runnable jar) file to the java project that holds the standalone app or even put the standalone app into yourdsl project as well

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText as standalone lexer, parser, interpreter? [message #1695973 is a reply to message #1695972] Wed, 20 May 2015 16:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
p.s. but why a self written editor and not eclipse?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText as standalone lexer, parser, interpreter? [message #1695977 is a reply to message #1695973] Wed, 20 May 2015 17:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
p.s: you are the better swing programmer than me but you can take the following as starting point

package org.xtext.example.mydsl;

import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.Diagnostician;
import org.eclipse.xtext.resource.FileExtensionProvider;
import org.eclipse.xtext.util.StringInputStream;
import org.xtext.example.mydsl.myDsl.Greeting;
import org.xtext.example.mydsl.myDsl.Model;

import com.google.inject.Injector;

public class Runner {
	
	private static JTextArea text;
	private static Injector injector;
	private static JFrame frame;

	public static void main(String[] args) {
		injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
		frame = new JFrame();
		frame.setBounds(new Rectangle(800,600));
		frame.setTitle("Demo");
		frame.setVisible(true);
		frame.setLayout(new GridLayout(2,1));
		text = new JTextArea(80,20);
		text.setText("lala");
		frame.add(text);
		
		JButton button = new JButton("go");
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String t = text.getText();
				
				ResourceSet rs = injector.getInstance(ResourceSet.class);
				Resource r = rs.createResource(URI.createURI("dummy." + injector.getInstance(FileExtensionProvider.class).getPrimaryFileExtension()));
				try {
					r.load(new StringInputStream(t), null);
					if (r.getErrors().isEmpty()) {
						//validation
						EObject myModel = r.getContents().get(0);
						Diagnostic diagnostic = Diagnostician.INSTANCE.validate(myModel);
						if (Diagnostic.ERROR == diagnostic.getSeverity()) {
							//TODO better error handling
							for (Diagnostic d : diagnostic.getChildren()) {
								JOptionPane.showMessageDialog(frame, "Found Error: " + d.getMessage());
							}
							return;
						} 
						//TODO warning handling
						//work with model
						if (myModel instanceof Model) {
							for (Greeting g : ((Model) myModel).getGreetings()) {
								JOptionPane.showMessageDialog(frame, "Found: " + g.getName());
							}
						}
						
					} else {
						//TODO better error handling
						for (org.eclipse.emf.ecore.resource.Resource.Diagnostic error : r.getErrors()) {
							JOptionPane.showMessageDialog(frame, "Found Error: " + error.getMessage());
						}
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		frame.add(button);
		
		frame.addWindowListener(new WindowAdapter() {
	         public void windowClosing(WindowEvent windowEvent){
	 	        System.exit(0);
	          }        
	       }); 
	}

}



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: xText as standalone lexer, parser, interpreter? [message #1696229 is a reply to message #1695977] Sun, 24 May 2015 07:20 Go to previous message
Dominik Reinert is currently offline Dominik ReinertFriend
Messages: 14
Registered: May 2015
Junior Member
This is something to start with. This last post was exactly what I was looking for, thank you Smile


I am in your dept now, christian, Thank you very much!
Previous Topic:Generation error after upgrading xtext
Next Topic:Suggesting Java classes
Goto Forum:
  


Current Time: Wed Apr 24 23:02:52 GMT 2024

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

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

Back to the top