Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Integrate ocl in xtext grammar or grab all the characters until any language rule appears(Integrate ocl in xtext grammar or grab all the characters until any language rule appears)
Integrate ocl in xtext grammar or grab all the characters until any language rule appears [message #1841343] Wed, 12 May 2021 16:29 Go to next message
Julia Robles is currently offline Julia RoblesFriend
Messages: 3
Registered: April 2021
Junior Member
Hi!

I am creating the grammar of an existing language in xtext to be able to generate an XMI. I have managed to do it all but there is a part that I don't know how to describe in xtext.

The language contains OCL expressions, but I don't need to validate it. It is enough for me to take all the text that represents the expression until another rule of my language appears.
I will explain it better with an example.

The language looks like the following:
class A
	attributes
		att1 : Integer
		att2 : Integer
		list : Sequence(A)
	operations
		  operationname1() = self.att1 + self.att2 < 3
		  operationname2()
			 pre namepre: self.att1 <= self.att2
			 post  namepost: self.list-> notEmpty()
end


I have represented the operations part as follows:
OperationsBase:
	'operations'
		(operations+=OperationType)*;

OperationType:
	OperationComplex | OperationQuery;

OperationQuery:
	operationDeclaration=OperationDeclaration '=' operationbody=OCLExpression (conditions+=ConditionType)*;

OperationComplex:
	operationDeclaration=OperationDeclaration (conditions+=ConditionType)*;

OperationDeclaration:
	name=ID '()';
	
ConditionType:
	Precondition | Postcondition;

Precondition:
	'pre' (name=ID)? ':' oclexpression=OCLExpression;
	
Postcondition:
	'post' (name=ID)? ':' oclexpression=OCLExpression;


I have tested it using STRING instead of OCLExpression (and putting single quotes at the beginning and end of ocl expressions manually) and everything works correctly, my problem is that I don't know how to do the OCLExpression rule.
I have also thought that I could add an OCL library and use the call to that library, but I can't find much information on how to integrate OCL into an xtext grammar. I've been looking at this documentation (https://download.eclipse.org/ocl/doc/6.14.0/ocl.pdf) but I don't know if it has any application to what I want.

Thanks in advance!
Re: Integrate ocl in xtext grammar or grab all the characters until any language rule appears [message #1841348 is a reply to message #1841343] Wed, 12 May 2021 18:33 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7268
Registered: July 2009
Senior Member
Hi

Your language looks very like OCLinEcore in which case you have nothing to do.

Parsing OCL as a String and then putting it through an OCL tool can work, but you will need to fix up all the scopes so that the OCL resolves names properly. And of course you get no content assist/relevant squiggles etc.

A full integration of OCL with Xtext is hard, so you might find that if you cannot live with OCLinEcore as-is you tweak it bit.

Regards

Ed Willink
Re: Integrate ocl in xtext grammar or grab all the characters until any language rule appears [message #1841349 is a reply to message #1841348] Wed, 12 May 2021 19:05 Go to previous messageGo to next message
Julia Robles is currently offline Julia RoblesFriend
Messages: 3
Registered: April 2021
Junior Member
Hi Ed,

First of all, thank you for your reply.

Converting the OCL to String was my first solution, although I have had some problems and so I wanted to look at other solutions.
Regarding OCLinEcore, it is true that it is similar, but not the same, so I would have to modify it. How could I do that? Also, is there a way to import it into my grammar and use it as OCLInEcore :: invariant or something like that?

Regards
Re: Integrate ocl in xtext grammar or grab all the characters until any language rule appears [message #1841357 is a reply to message #1841349] Thu, 13 May 2021 04:46 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7268
Registered: July 2009
Senior Member
Hi

Software is a compromise. If you don't like Java you are free to rewrite it, but you will spend of lot of time for probably limited benefit. Conformance to standards is widely recognized as good.

If you can adapt your requirements to use OCLinEcore you will save a vast amount of development time at the expense of adapting your requirements. Insisting that your non-standard practices are better may cost you.

If you need to ask how to adapt OCLinEcore then there is no doubt that you lack the skills to attempt such an activity.

I really recommend that you take yourself off somewhere where you can reflect on the compromises that you face and the full costs of each alternative.

If you insist on rewriting; I generally try to ensure that users consider Xtext+Xbase as an alternative since the Xbase integration with Xtext is far superior.

Regards

Ed Willink
Re: Integrate ocl in xtext grammar or grab all the characters until any language rule appears [message #1841936 is a reply to message #1841357] Thu, 03 June 2021 19:36 Go to previous messageGo to next message
Julia Robles is currently offline Julia RoblesFriend
Messages: 3
Registered: April 2021
Junior Member
Hi

I solved the problem a few weeks ago and would like to leave the solution in case it could be of use to someone.
I was finally able to integrate OCL with my language. I was trying to do it directly inheriting from the EssentialOCL plugin, but as I saw in other posts, it is complicated and I was constantly getting errors.

Finally I did the following:

  • I used everything that xtext generates for you by default, so I undid everything I had been testing. My .xtext file inherits from org.eclipse.xtext.common.Terminals and only imports ecore.
  • I took the .xtext file from the EssentialOCL plugin source code and copied all the rules I needed to use ExpCS.
  • Instead of importing the other plugins I need (I also got errors) I also took the source code of these and did the same.
  • Since I don't need to validate the OCL, I just need to read it, I replaced all the cross references with simple IDs.
  • Lastly I allowed backtrack in GenerateNAME.mwe2 file as follows:

language = StandardLanguage {
	..
	parserGenerator = {
  		options = {
    		          backtrack = true
  		}
	}
}

Then, in the xtend generator file, to pass it to XMI I had to reverse engineer it, looking at each xtext class and getting it all rewritten together.

Regards
Julia Robles
Re: Integrate ocl in xtext grammar or grab all the characters until any language rule appears [message #1841955 is a reply to message #1841936] Fri, 04 June 2021 09:10 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7268
Registered: July 2009
Senior Member
Hi

Congratulations on coming up with a lightish weight solution that works for you. (I'm not sure why you don't need syntax checking / validation ...)

Your backtracking issue might be resolved by emulating the RetokenizingEssentialOCLParser that installs a RetokenizingTokenSource

/**
 * RetokenizingTokenSource accomodates backtracking limitations in the ANTRL lexer when used from Xtext.
 *
 * The problem is the three overlapping syntaxes
 * INT.INT leading to a Floating Point lteral
 * INT..INT leading to a Collection range
 * INT.ID leading to a numeric navigation
 *
 * ANTLR proceeds to INT. but won't backup when the character after the . is bad.
 *
 * The code here allows the basic lexer to be ignorant of floating point syntax so that it correctly parses
 * INT.INT as INT DOT INT
 * INT..INT as INT DOTDOT INT
 * INT.INTe+INT as INT DOT INT ID PLUS INT
 * so the code here recognises the floating point literal and reconsttructs. As an additional benefit 'e' and 'E' are not keywords.
 */


Regards

Ed Willink
Previous Topic:How to change Xtext DSL file programmatically ? (Xtext 2.24)
Next Topic:How to import EOL for syntax highlighting
Goto Forum:
  


Current Time: Tue Sep 28 13:38:11 GMT 2021

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

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

Back to the top