Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Referring to source code of Java project in xText
Referring to source code of Java project in xText [message #1806409] Wed, 08 May 2019 15:11 Go to next message
J Bouwhuis is currently offline J BouwhuisFriend
Messages: 17
Registered: April 2019
Junior Member
I am creating a DSL in xText for modeling the functional behaviour of an application. My goal for this is to couple resource demands (e.g. number of CPU cycles, write actions on hard drive) with the functional behaviour I want to model with the DSL. The DSL is written in xText with the Eclipse IDE. The DSL syntax including comments can be found below.

For now it's a really simple DSL to model functional behaviour; combining if/else and for statements and adding libraryFunctions to them. I came up with this latter term myself; it is used to refer to actions which are the basic steps of my functional behaviour (e.g. login, encrypt, display; you can think of them as methods in a programming language). Now I would like to extent my DSL with the option to be able to refer to the source code of a Java Project. I created a small Java Program which resembles a basic program with a login screen and create account screen (see attached class diagram below). In order to model the functional behaviour of this program with the DSL, I want to be able to refer to certain details of the Java Program source code so that I can pull these details right from the source code and use it in the DSL. For example; suppose that I want to refer to certain methods used in the Java Program. Right now I have simple enumeration 'libraryFunctionsEnum' in my DSL, but it would be nice if I could somehow directly refer to the methods used in the source code of the Java Program (so that when I compile the DSL and use it, the xText editor automically provides a list of available methods I can refer to).

I tried to use ecore models to convert class diagrams of my Java Project and integrate them in xText, but I have the feeling that I am a bit lost on what to do. I have also looked at xBase and xTend (two languages directed towards making xText more interoperable with Java), but so far I have found that these are more focussed on automatically generating Java source code from xText models. I want to do it the other way around (refer to Java source code from an external project so that I can use these references in my DSL). I don't know if the methods I mentioned above (ecore, xBase, xTend) are even the right methods to achieve what I want. If you have some better idea or explation, then I am glad to hear it!

By the way, I am still new to xText and DSL modelling/DSL development. I might have forgotten some important details/explanations. Please let me know if you miss something.

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

generate finalDsl "http://www.xtext.org/example/mydsl/FinalDsl"

Model:
    'functionName' name = STRING
    functions += FunctionElements*
;

// Function elements of which the model exists. The model can contain
// library functions, for loops, and if/else statements.
  FunctionElements:
        (   
            functions += libraryFunctionsEnum |
            forLoops += ForLoops |
            ifElseStatements += IfElseStatements
        )
; 

// IfElse Statements requiring if statements and optionally followed by
// one else statement.
IfElseStatements: 
    ifStatements += IfStatements
    (elseStatement = ElseStatement)?
;

// If statements requiring conditions and optionally followed by
// library functions or for loops.
IfStatements:
    'if'
    conditions = Conditions
    (ifFunctions += libraryFunctionsEnum | forLoops += ForLoops)
;

// Else statement requiring one or multiple library functions.
ElseStatement:
        'else' elseFunctions += libraryFunctionsEnum
;

// For loops requiring one condition and followed by zero or more
// library functions
ForLoops:
    'for'
    conditions = Conditions
    libraryFunctions += libraryFunctionsEnum*

;

//*Eventually filled with details from class diagram, but for now we manually fill it for the sake of testing.
enum libraryFunctionsEnum:
createAccount='createInstance'|
login='login'|
hasCode= 'encrypt'|
display='display'
;



Conditions:
    STRING
    operator=logicalOperators
    STRING
;

enum logicalOperators:
greaterThan='>'|
smallerThan='<'|
greaterOrEqualThan='=>'|
smallerOrEqualThan='<='|
equalTo='=='
Re: Referring to source code of Java project in xText [message #1806412 is a reply to message #1806409] Wed, 08 May 2019 15:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
the common type part or xbase is exactly for that
the jvmmodelinferrer allows you to have proper expressions.

from what you have shared its hard to imagine how exaclty you want to integrate
=> can you share some (model) snippets?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referring to source code of Java project in xText [message #1806428 is a reply to message #1806412] Wed, 08 May 2019 22:00 Go to previous message
J Bouwhuis is currently offline J BouwhuisFriend
Messages: 17
Registered: April 2019
Junior Member
@Christian thanks a lot for your quick response. I think that xbase is useful for me. I will dive deeper into xbase and see if I can get it to work with my DSL. If you have any recommendations on books/pages/forums/(video) tutorials which can provide an elaboration on xbase, then I am glad to hear it. Right now I am reading the book "Implementing Domain-Specific Languages with Xtext and Xtend
Second Edition" from Lorenzo Bettini which also contains a chapter on xbase, but I feel that most of the information on xBase online is not as elaborate yet as it is on Xtext (which makes sense as Xtext has of course been around longer than xBase).

Anyway, below you can find a simple example model snippet that I have created with my DSL. My idea is to enable the DSL to refer to external Java projects. For example; suppose I have coded a Java program consisting of classes CreateAccountFrame, DataBase, and LoginFrame. Suppose that the first class contains a method called checkNewAccountDetails(string userID, char [] password, char [] passwordVerification) which handles the logic for checking account details when someone is trying to create a new account. I would like to refer directly to this method (and possibly later on its class name, parameters, underlying statements, and other details but for now I like to keep it simple) when using my DSL to model the functional behaviour of the program. xBase indeed seems a good solution for doing this, so I will investigate this first. Thanks for your effort and information!

functionName "TestFunction" 
	for "Condition1" < "Condition 2" 
		for "ConditionA" <= "ConditionB" 
			createInstance 
		for "ConditionB" < "ConditionD" 
			display

if "ConditionC" < "ConditionE" 
	display
	if "ConditionF" == "ConditionG" 
		createInstance 
	else 
		display
Previous Topic:Multiple Entities within an Entity
Next Topic:Generating a project with two languages using maven
Goto Forum:
  


Current Time: Fri Mar 29 13:42:48 GMT 2024

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

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

Back to the top