Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using Xtext and Xtend
Using Xtext and Xtend [message #816120] Thu, 08 March 2012 07:54 Go to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
Hi,


I read Xtend User Guide and examine Xtend examples but I could not decide where to start. I defined my own DSL with Xtext. Now I want to complete a textual editor. With the help of defined Xtext a grammer a user can create an instance model. I want to put some constraints to this model. For example, there is an element with type type1. I want to constraint the user to create only two instance elementes of type1.

For example:

main: (type1|type2)*
type1: name=ID;
type2: name=ID;

instance model:

type1instance1;
type1instance2;


If the user creates one more type, the editor must give an error.

I want to do such constraints and I could not decide what must I do. Can I supply it with Xtend. If yes how? Is there any documentation except Xtend User Guide?


Thank you very much.

Re: Using Xtext and Xtend [message #816354 is a reply to message #816120] Thu, 08 March 2012 13:54 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
Hi,

i am not quite sure what this has to do with Xtend. Yes you could write the Validator using Xtend but it will work with Java as well
to get an idea where to start with validation have a look at
http://www.eclipse.org/Xtext/documentation/2_1_0/060-validation.php

~Christian
Re: Using Xtext and Xtend [message #849578 is a reply to message #816354] Thu, 19 April 2012 05:12 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
Hi,

I can not find any samples for my question. Does anyone can give me a simple example about these constraints? How can I strict the user to define a specific number of instances with Validation Package?

Thanks.
Re: Using Xtext and Xtend [message #849643 is a reply to message #849578] Thu, 19 April 2012 06:27 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
Hi,
if your grammar is like:
Main: elements += (Type1|Type2)* ;
Type1: 'type1' name=ID ';' ;
Type2: 'type2' name=ID ';' ;


Your check function in Validator could look like:
@Check
public void checkOnlyTwoInstancesOfType1(Type1 type1){
	Main main = EcoreUtil2.getContainerOfType(type1, Main.class);
	List<Type1> type1s = EcoreUtil2.getAllContentsOfType(main, Type1.class);
	if(type1s.size()>2){
		error("Too many Type1 objects", MyDslPackage.Literals.TYPE1__NAME);
	}
}	


Regards,
Michal
Re: Using Xtext and Xtend [message #849699 is a reply to message #849643] Thu, 19 April 2012 07:27 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
Hi,

Thank you very much for your answer. I added your solution to my validation package but now I got some new errors:

Main cannot be resolved to a type
List cannot be resolved to a type
Re: Using Xtext and Xtend [message #849708 is a reply to message #849699] Thu, 19 April 2012 07:34 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
You need to import necessary packages...
import java.util.List;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.validation.Check;
import org.xtext.example.mydsl.myDsl.Main;
import org.xtext.example.mydsl.myDsl.MyDslPackage;
import org.xtext.example.mydsl.myDsl.Type1;

Suggestion: Use editor code completion! It adds your imports automatically...

Michal
Re: Using Xtext and Xtend [message #849731 is a reply to message #849708] Thu, 19 April 2012 08:04 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
I am using auto completion, only import org.xtext.example.mydsl.myDsl.Main was missing but when I import it, I received another error:

The import org.xtext.example.mydsl7.myDsl.Main cannot be resolved

I think sth is missing but I couldn't understand

Thank you,
Re: Using Xtext and Xtend [message #849736 is a reply to message #849731] Thu, 19 April 2012 08:07 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
You have to use your package to import. org.xtext.example.mydsl.myDsl is my one ...
Re: Using Xtext and Xtend [message #849747 is a reply to message #849736] Thu, 19 April 2012 08:17 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
I am sorry but again the same problem. I am writing my grammer from the start:



My .xtext file:
grammar org.xtext.example.mydsl7.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl ".../example/mydsl7/MyDsl"
main: (type1|type2)*
type1: name=ID;
type2: name=ID;

MyDslJavaValidator.java:



package org.xtext.example.mydsl7.validation;


import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.validation.Check;
import org.xtext.example.mydsl7.myDsl.MyDslPackage;
import org.xtext.example.mydsl7.myDsl.Plan;
import org.xtext.example.mydsl7.myDsl.Main;
import java.util.List;

@Check
public void checkOnlyTwoInstancesOfType1(Type1 type1){
Main main = EcoreUtil2.getContainerOfType(type1, Main.class);
List<Type1> type1s = EcoreUtil2.getAllContentsOfType(main, Type1.class);
if(type1s.size()>2){
error("Too many Type1 objects", MyDslPackage.Literals.TYPE1__NAME);
}
}

My problems:

1. The import org.xtext.example.mydsl7.myDsl.Main cannot be resolved

2.Multiple markers at this line
- Main cannot be resolved to a type
- Bound mismatch: The generic method getContainerOfType(EObject, Class<T>) of type EcoreUtil2 is not applicable for the arguments (Plan,
Class<Main>). The inferred type Main is not a valid substitute for the bounded parameter <T extends EObject>
- Main cannot be resolved to a type

3.The method error(String, Integer) in the type AbstractDeclarativeValidator is not applicable for the arguments (String, EAttribute)


Re: Using Xtext and Xtend [message #849761 is a reply to message #849747] Thu, 19 April 2012 08:31 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
Hi,
the errors occur as the method I sent you is applicable for the grammar, which I sent you. Certainly, if you use other grammar, you need to adjust the method...
Btw. your grammar does not look valid...
Regards,
Michal
Re: Using Xtext and Xtend [message #849773 is a reply to message #849761] Thu, 19 April 2012 08:44 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
I am sorry, I copied oldest version of the grammer. The existing grammer in my Xtext is as you sent me. I mean the grammer is:

Main: elements += (Type1|Type2)* ;
Type1: 'type1' name=ID ';' ;
Type2: 'type2' name=ID ';' ;

now. But the others are same. I mean I have the same problems that I sent you above.


Thank you very much for your patient, I realy have to solve this problem. Thank you.
Re: Using Xtext and Xtend [message #849777 is a reply to message #849773] Thu, 19 April 2012 08:50 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
Have you run your MWE2 Workflow after you changed grammar?
You have to import "Main" from your package.
Regards,
Michal
Re: Using Xtext and Xtend [message #849784 is a reply to message #849777] Thu, 19 April 2012 08:55 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
I didn't run the MW2 Workflow because validator package is giving errors.

I added this:
import org.xtext.example.mydsl7.myDsl.Main;
But it says : The import org.xtext.example.mydsl7.myDsl.Main cannot be resolved

It seems like I have to have a Main class or interface.
Re: Using Xtext and Xtend [message #849794 is a reply to message #849784] Thu, 19 April 2012 09:04 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
Ignore the errors in validator. Just run the MWE2 Workflow! Main interface will be generated...
Re: Using Xtext and Xtend [message #849808 is a reply to message #849794] Thu, 19 April 2012 09:15 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
I run it but nothigg has changed. "Main" and "error" keyword are giving errors. It says tho change type of TYPE1__NAME to Integer
Re: Using Xtext and Xtend [message #849828 is a reply to message #849808] Thu, 19 April 2012 09:37 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
Are all following files: Main.java, MyDslFactory.java, MyDslPackage.java, Type1.java, Type2.java present in your src-gen folder (in org.xtext.example.mydsl7.myDsl package?)
If yes, the classes must be available in Validator.
If not, generation failed. There must be something wrong in your grammar or in workflow.
Michal

[Updated on: Thu, 19 April 2012 09:38]

Report message to a moderator

Re: Using Xtext and Xtend [message #849835 is a reply to message #849828] Thu, 19 April 2012 09:44 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
Main.java doesn't exist. in my src-gen folder. My grammer seems correct maybe I have to change sth on my workflow file.
Re: Using Xtext and Xtend [message #849839 is a reply to message #849835] Thu, 19 April 2012 09:49 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
The default works perfect on my site. Just right click on the GenerateMyDsl.mwe2 -> Run as -> MWE2 Workflow and Proceed with lunch ... If there is any error, you should see it in the Console output.
Regards,
Michal
Re: Using Xtext and Xtend [message #849843 is a reply to message #849835] Thu, 19 April 2012 09:49 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
hi,

in xtext 2.2. you have to change the workflow to get the main

fragment = generator.GeneratorFragment {
generateMwe = false
generateJavaMain = true
}
Re: Using Xtext and Xtend [message #849848 is a reply to message #849843] Thu, 19 April 2012 09:54 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
I think he does not need this ... He calls "Main" the first rule in the grammar (entry point...).
Main: elements += (Type1|Type2)* ;
Type1: 'type1' name=ID ';' ;
Type2: 'type2' name=ID ';' ;


Michal
Re: Using Xtext and Xtend [message #849849 is a reply to message #849843] Thu, 19 April 2012 09:56 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
Can I add them directly to the workflow or is there sth else to add more? (There are some errors after these additions.)
Re: Using Xtext and Xtend [message #849851 is a reply to message #849849] Thu, 19 April 2012 09:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
Hi,

which xtext version do you use?

~Christian
Re: Using Xtext and Xtend [message #850103 is a reply to message #849851] Thu, 19 April 2012 15:14 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
Hi,

Xtext version:
Version: 1.0.2.v201102150722 (Eclipse Helios)
Re: Using Xtext and Xtend [message #850112 is a reply to message #850103] Thu, 19 April 2012 15:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
The is nö Main in this Version - only the Workflow in the Generator Project
Re: Using Xtext and Xtend [message #850601 is a reply to message #850112] Fri, 20 April 2012 02:57 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
I added workflow
fragment = generator.GeneratorFragment {
generateMwe = false
generateJavaMain = true
}
but it still gives errors.
Then which version should I download and where? (Because I always see this version)

I really have to solve this problem:( Thank you very much for your replies.

Re: Using Xtext and Xtend [message #850610 is a reply to message #850601] Fri, 20 April 2012 03:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian Dietrich
Messages: 6225
Registered: July 2009
Senior Member
Hi,

i somehow lost the overview whatb your actual problem is.
as i said: you need Xtext 2.0 for that

~Christian
Re: Using Xtext and Xtend [message #850632 is a reply to message #850610] Fri, 20 April 2012 03:29 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
My actual problem is I defined a grammer and a user can create a model by regarding this grammer. I want to put some constraints to this grammer. For example, there is an element with type type1. I want to constraint the user to create only two instance elementes of type1. If the user creates one element, editor must give an error.


To do this, I wrote:


@Check
public void checkOnlyTwoInstancesOfType1(Type1 type1){
Main main = EcoreUtil2.getContainerOfType(type1, Main.class);
List<Type1> type1s = EcoreUtil2.getAllContentsOfType(main, Type1.class);
if(type1s.size()>2){
error("Too many Type1 objects", MyDslPackage.Literals.TYPE1__NAME);
}

to my validation package but it gives errors.

Re: Using Xtext and Xtend [message #850671 is a reply to message #850632] Fri, 20 April 2012 04:15 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
Hi,

I dowloaded Xtext 2.0.0 and coppied project there.

Now again I have two errors in validation package


My xtext grammer is this:


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

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

Agent:
'Agent'
(belief+=Belief | plan+=Plan)*;

Belief:
'Belief' name=ID ;
Plan:
'Plan' name=ID ;

I want to make user to create max two instances of these elements. I mean the insance model can be like this:

Agent

Plan plan1
Plan plan2

or like this:

Belief belief1
Belief belief2
Plan plan1

To supply this constraint, I added htis code to the validation package:



package org.xtext.example.mydsl.validation;

import java.awt.List;

import org.apache.log4j.chainsaw.Main;
import org.eclipse.xtend.typesystem.emf.EcoreUtil2;
import org.eclipse.xtext.validation.Check;
import org.xtext.example.mydsl.myDsl.MyDslPackage;
import org.xtext.example.mydsl.myDsl.Plan;

public class MyDslJavaValidator extends AbstractMyDslJavaValidator {

@Check
public void checkOnlyTwoInstancesofPlan(Plan plan)
{
Main main = EcoreUtil2.getContainerOfType(plan, Main.class);
List<Plan> plans = EcoreUtil2.getAllContentsOfType(main, Plan.class);
if(plans.size()>2){
error("Too many Plan objects", MyDslPackage.Literals.PLAN__NAME);
}
}
}

Then, I received these errors:

1.
Multiple markers at this line
- The method getContainerOfType(Plan, Class<Main>) is undefined for the type
EcoreUtil2
- Line breakpoint:MyDslJavaValidator [line: 26] -
checkOnlyTwoInstancesofPlan(Plan)

2.Multiple markers at this line
- The type List is not generic; it cannot be parameterized with arguments <Plan>
- The method getAllContentsOfType(Main, Class<Plan>) is undefined for the type
EcoreUtil2

I am realy confused and very sad. I have been tring to solve this problem for 5 days.

Re: Using Xtext and Xtend [message #850675 is a reply to message #850671] Fri, 20 April 2012 04:19 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
The List, which you want to use is not java.awt.List but java.util.List
Michal
Re: Using Xtext and Xtend [message #850677 is a reply to message #850675] Fri, 20 April 2012 04:23 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
and also you probably want to use Agent.class instead of Main.class (as no Main entity is defined in your grammar and org.apache.log4j.chainsaw.Main is probably not the one you want...)
Do not forget to change Main main ---> Agent main.
Michal
Re: Using Xtext and Xtend [message #850697 is a reply to message #850677] Fri, 20 April 2012 04:39 Go to previous messageGo to next message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
I changed them but the problem is,
getContainerOfType, getAllContentsOfType and List aren't defined by editor.
Re: Using Xtext and Xtend [message #850705 is a reply to message #850697] Fri, 20 April 2012 04:49 Go to previous messageGo to next message
Michal S is currently offline Michal S
Messages: 73
Registered: July 2011
Member
import org.eclipse.xtext.EcoreUtil2 instead of org.eclipse.xtend.typesystem.emf.EcoreUtil2
Re: Using Xtext and Xtend [message #850721 is a reply to message #850705] Fri, 20 April 2012 05:05 Go to previous message
terry tonery is currently offline terry tonery
Messages: 34
Registered: February 2012
Member
Thank you very much!! The problems are fixed !!!!

[Updated on: Fri, 20 April 2012 05:06]

Report message to a moderator

Previous Topic:Adding built-in types to the domain model example
Next Topic:Problems with version
Goto Forum:
  


Current Time: Thu Aug 28 01:32:12 EDT 2014

Powered by FUDForum. Page generated in 0.03966 seconds