Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Using Xbase Expressions in JvmModelInferrer
Using Xbase Expressions in JvmModelInferrer [message #1783851] Mon, 19 March 2018 13:32 Go to next message
Hermann Ritzenthaler is currently offline Hermann RitzenthalerFriend
Messages: 14
Registered: February 2018
Junior Member
Hello,

So i have a question about my xtext grammar, which i wanted to extend with Xbase.
So my xtext Grammar looks like that:

JoinExpr :
	(NaturalJoinExpr | LeftOuterJoinExpr | ThetaJoinExpr) 'as' targetType=CPX_ID 

NaturalJoinExpr:	
...
;

LeftOuterJoinExpr:	
...
;

ThetaJoinExpr:
	'theta' 'join' left=[ecore::EClass|CPX_ID] 'with' right=[ecore::EClass|CPX_ID] 'where' condition = STRING
;


Now I want to validate the STRING part with xbase, so i extended xtext with xbase and replaced STRING with XExpression.


Do i use XExpression correctly in my grammar?
Seems like there could be a problem with 'as' targetType=CPX_ID which would be after the XExpression. So am i able to write:
where (condition) as ... ?
Where condition is any XExpression?

Now i should have the possibility to validate the XExpression in my JvmModelInferrer.
The XExpression should be validated to true or false.
So my Inferrer look like that:

acceptor.accept(element.toClass("edu.kit.ipd.sdq.mdsd.mj.Modelchecker" ) [
if (el instanceof ThetaJoinExpr) {
						members += el.toMethod("isValid", el.newTypeRef(Boolean::TYPE)) [
							parameters += el.left.toParameter("leftClass", el.left.newTypeRef(EClass))
							parameters += el.right.toParameter("rightClass", el.right.newTypeRef(EClass))
	body = 	
							'''
							«el.condition»

								return true;	
						]


Does my condition now gets validated? Or how can i use Xbase Expressions in the jvmModelInferrer?
The condition in my query would be any condition as we know it from sql.
So I want that to be represented in the file generated from the inferrer as condition. How to achieve that?

Thx in advance

[Updated on: Mon, 19 March 2018 15:24]

Report message to a moderator

Re: Using Xbase Expressions in JvmModelInferrer [message #1783862 is a reply to message #1783851] Mon, 19 March 2018 16:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
This does not work.

If you want to validate a expression
-it needs to be an expression in grammar
-it needs to be assigned to a initializer or body directly
- or you need a Expression subclass and adapt compilation and type computation

Simply having a string won't work


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1783868 is a reply to message #1783862] Mon, 19 March 2018 17:45 Go to previous messageGo to next message
Hermann Ritzenthaler is currently offline Hermann RitzenthalerFriend
Messages: 14
Registered: February 2018
Junior Member
So I try to describe my problem a bit better:

So instead of the String I want to use an XExpression which refers to enties in the query metioned before. For example:
Join EClass A with EClass B where XExpression.
And the XExpression would be any sql-like query like:
EClass A EAttribute anyAttribute = "whatever"
So in my infererrer i would have the EClass A and EClass B as parameters in a method. And the body of that method would use that XExperssion and returns true or false depending on the XExpression and on the EClasses A and B.
Can't i use and validate XBase Expressions in the inferrer?
Re: Using Xbase Expressions in JvmModelInferrer [message #1783869 is a reply to message #1783868] Mon, 19 March 2018 17:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
you already do this right:

you have a method that takes the two objects as params
and has the expression as body.

so i dont understand what is not working for you with that aproach


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1783871 is a reply to message #1783869] Mon, 19 March 2018 18:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
the only bogus thing is:

el.left.newTypeRef(EClass)

you shoud use "x.y.Z".typeRef with "x.y.Z" beeing the java class name.
maybe you can reuse GenModelUtil2 to determine the javaclassname.
or you directly reference java classes instead of eclasses


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1783872 is a reply to message #1783869] Mon, 19 March 2018 18:13 Go to previous messageGo to next message
Hermann Ritzenthaler is currently offline Hermann RitzenthalerFriend
Messages: 14
Registered: February 2018
Junior Member
The Inferrer seems like not realy understanding the XExpression:

This is my query:

theta join library.Book with imdb.Film as jointarget.Book where library.Book.title = imdb.Film.title {
					keep attributes library.Item.publicationDate



And i recieve an error: no viable alternative at input 'library.Book.title'
So it seems the XExpression don't really can resolve my ecore models?

I tried it with a test, just to check out what the inferrer created:

theta join library.Book with imdb.Film as jointarget.Book where "test" == "test" {
					keep attributes library.Item.publicationDate
}


Now realy java code gets generated:
But the class looks like that:

public class Modelchecker18 {
  public boolean isValid(final EClass leftElement, final EClass rightElement) {
    EClass a = leftElement;
    EClass b = rightElement;
    
    return <XStringLiteralImpl> == <XStringLiteralImpl> ;
  }
}


Any idea how the method in the inferrer needs to look like?






Re: Using Xbase Expressions in JvmModelInferrer [message #1783873 is a reply to message #1783872] Mon, 19 March 2018 18:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Can you please provide a complete sample grammar and inferrerr and sample project

No viable alternative is a parser error


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1783876 is a reply to message #1783873] Mon, 19 March 2018 18:59 Go to previous messageGo to next message
Hermann Ritzenthaler is currently offline Hermann RitzenthalerFriend
Messages: 14
Registered: February 2018
Junior Member
So this is my grammar:

grammar ...ModelJoin with org.eclipse.xtext.xbase.Xbase

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

generate modelJoin "...ModelJoin"

Grammar:
	(imports+=Import)*
	(target+=Target)
	(joinExpr+=JoinExpr)*;

JoinExpr :
	(NaturalJoinExpr 'as' targetType=CPX_ID | LeftOuterJoinExpr 'as' targetType=CPX_ID | ThetaJoinExpr) 
	('{'  
	(keepAttributesExpr+=KeepAttributesExpr)*
	(KeepCalculatedAttributeExpr+=KeepCalculatedAttributeExpr)*
	(KeepCalculatedOutgoingExpr+=KeepCalculatedOutgoingExpr)*
	(keepAggregatesExpr+=KeepAggregateExpr)*
	(keepExpr+=KeepExpr)*
	(sourceAttributeExpr+=SourceAttributeExpr)*
	'}')?
	;

NaturalJoinExpr:	
	'natural' 'join' left=[ecore::EClass|CPX_ID] 'with' right=[ecore::EClass|CPX_ID] 
;

LeftOuterJoinExpr:	
	'left' 'outer' 'join' left=[ecore::EClass|CPX_ID] 'with' right=[ecore::EClass|CPX_ID] 
;

ThetaJoinExpr:
	'theta' 'join' left=[ecore::EClass|CPX_ID] 'with' right=[ecore::EClass|CPX_ID] 'as' targetType=CPX_ID  'where' condition = XExpression
;



I left the next parts out, because its kinda large, but that isn't the part which got changed.
And it worked before i changed condtion to be an XExperssion.


This is my inferrer:

class ModelJoinJvmModelInferrer extends AbstractModelInferrer {


	@Inject XbaseCompiler compiler
	@Inject extension JvmTypesBuilder


	def dispatch void infer(Grammar element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {

		var count = 0;
		for(el : element.joinExpr){
			count = (Math.random()*100)  as int;
			if (el instanceof ThetaJoinExpr) {
			acceptor.accept(element.toClass("edu.kit.ipd.sdq.mdsd.mj.Modelchecker" + count.toString)) [
						members += el.toMethod("isValid", el.newTypeRef(Boolean::TYPE)) [
							parameters += el.left.toParameter("leftElement", el.left.newTypeRef(EClass))
							parameters += el.right.toParameter("rightElement", el.right.newTypeRef(EClass))
							body = 													
								'''
								EClass a = leftElement;
								EClass b = rightElement;

								if(«el.condition»){
									return true;
									}
									else{
										return false;
									}
							'''
						]
				]
			}


And this my example:
theta join library.Book with imdb.Film as jointarget.Book where library.Book.title == imdb.Film.title {
					keep attributes library.Item.publicationDate
}


So the library.Book and imdb.Film can be resolved.
But somehow not if i want the part after the where keyword to be an XExpression? And the XExpression holds EClasses and EAttributes.
Do I need to change XExpression to something else?
Or can't i use EClasses in XExpressions?

[Updated on: Mon, 19 March 2018 19:11]

Report message to a moderator

Re: Using Xbase Expressions in JvmModelInferrer [message #1783884 is a reply to message #1783876] Mon, 19 March 2018 22:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
the main problem is

(a) you mix xbase and non xbase
(b) you use the same names for java refs and ecore refs

=> with a inferrer like

class MyDslJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder

	def dispatch void infer(ThetaJoinExpr element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
		var count = 0
		acceptor.accept(element.toClass("edu.kit.ipd.sdq.mdsd.mj.Modelchecker" + count.toString)) [
			members += element.toMethod("isValid", Boolean.TYPE.typeRef) [
				// add org.eclipse.xtext.xtext.generator to deps or copy code
				val leftName = GenModelUtil2.getJavaTypeName(element.left, element.eResource.resourceSet)
				parameters += element.left.toParameter("leftElement", leftName.typeRef())
				val rightName = GenModelUtil2.getJavaTypeName(element.left, element.eResource.resourceSet)
				parameters += element.right.toParameter("rightElement", rightName.typeRef())
				body = element.condition
			]
		]
	}

}


model would look like

theta join extlibrary.Book with imdb.Film as jointarget.Book where leftElement.title == rightElement.title


as you can see the param names are leftElement and rightElement as you have defined it.
since . is a separator you cannot really name the params imdb.Film or something like you do it.

so the question would be how much your code can do with expressions that makes the usage of xbase useful
or how much you are free in syntax to e.g. give the join parts explicit names/alias and use that ones
as parameter names.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1783914 is a reply to message #1783884] Tue, 20 March 2018 10:35 Go to previous messageGo to next message
Hermann Ritzenthaler is currently offline Hermann RitzenthalerFriend
Messages: 14
Registered: February 2018
Junior Member
Thx, for your answer,

I think i am nearly done, but somehow it cant't resolve leftElement and rightElement in my runtime Eclipse.
This is the error i received:

"Could not find a GenModel for EPackage 'http://mdsd.sdq.ipd.kit.edu/mj#library' from platform:/resource/edu.kit.ipd.sdq.mdsd.mj.test/models/library.ecore.
If the missing GenModel has been generated via EMFGeneratorFragment2, make sure to run it first in the workflow.
If you have a *.genmodel-file, make sure to register it via StandaloneSetup.registerGenModelFile(String)."

So in my runtime application i have a folder with my models. Ecore and genmodel files.
So i think there is something missing in my workflow:

My workflow:

Workflow {
        
	component = XtextGenerator {
		
		configuration = {
			
			project = StandardProjectConfig {
				baseName = "edu.kit.ipd.sdq.mdsd.mj.xtext"
				rootPath = rootPath
				eclipsePlugin = {
					enabled = true
				}
				createEclipseMetaData = true
			}
			code = {
				encoding = "windows-1252"
				fileHeader = "/*\n * generated by Xtext \${version}\n */"
			}
		}
		
		language = StandardLanguage {
			name = "edu.kit.ipd.sdq.mdsd.ModelJoin"
			fileExtensions = "mj"
			serializer = {
				generateStub = false
			}
		}
	}



Any idea, what i need to add to my workflow?

[Updated on: Tue, 20 March 2018 10:45]

Report message to a moderator

Re: Using Xbase Expressions in JvmModelInferrer [message #1783918 is a reply to message #1783914] Tue, 20 March 2018 11:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
I would expect something like the following in the Lang section

referencedResource="platform:/resource/Projectname/folder/your.genmodel"



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1783929 is a reply to message #1783918] Tue, 20 March 2018 13:36 Go to previous messageGo to next message
Hermann Ritzenthaler is currently offline Hermann RitzenthalerFriend
Messages: 14
Registered: February 2018
Junior Member
I added referencedResource to my mwe2 workflow.
When i run it everything seems ok:
...
387 [main] INFO clipse.emf.mwe.utils.GenModelHelper - Registered GenModel 'http://mdsd.sdq.ipd.kit.edu/mj#library' from 'platform:/resource/edu.kit.ipd.sdq.mdsd.mj.xtext/models/extlibrary.genmodel'
389 [main] INFO clipse.emf.mwe.utils.GenModelHelper - Registered GenModel 'http://mdsd.sdq.ipd.kit.edu/mj#imdb' from 'platform:/resource/edu.kit.ipd.sdq.mdsd.mj.xtext/models/imdb.genmodel'
...

But if i save the query file in the runtime application:
I still get the same error:

Could not find a GenModel for EPackage 'http://mdsd.sdq.ipd.kit.edu/mj#library' from platform:/resource/edu.kit.ipd.sdq.mdsd.mj.test/models/extlibrary.ecore.
If the missing GenModel has been generated via EMFGeneratorFragment2, make sure to run it first in the workflow.
If you have a *.genmodel-file, make sure to register it via StandaloneSetup.registerGenModelFile(String).

Do i need to add more than just referencedResource to the mwe2 workflow?

[Updated on: Tue, 20 March 2018 13:54]

Report message to a moderator

Re: Using Xbase Expressions in JvmModelInferrer [message #1783930 is a reply to message #1783929] Tue, 20 March 2018 13:57 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
then do you exactly get the error. running the workflow?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1783934 is a reply to message #1783930] Tue, 20 March 2018 14:15 Go to previous messageGo to next message
Hermann Ritzenthaler is currently offline Hermann RitzenthalerFriend
Messages: 14
Registered: February 2018
Junior Member
I receive the error, modifing my query file and saving it.
So when he is trying to call the inferrer:

Full stack trace is:
!ENTRY org.apache.log4j 4 0 2018-03-20 15:11:49.718
!MESSAGE org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder  - Error initializing JvmElement

!STACK 0
java.lang.RuntimeException: Could not find a GenModel for EPackage 'http://mdsd.sdq.ipd.kit.edu/mj#library' from platform:/resource/edu.kit.ipd.sdq.mdsd.mj.test/models/extlibrary.ecore.
If the missing GenModel has been generated via EMFGeneratorFragment2, make sure to run it first in the workflow.
If you have a *.genmodel-file, make sure to register it via StandaloneSetup.registerGenModelFile(String).

	at org.eclipse.xtext.xtext.generator.util.GenModelUtil2.getGenModelResource(GenModelUtil2.java:180)
	at org.eclipse.xtext.xtext.generator.util.GenModelUtil2.getGenPackage(GenModelUtil2.java:109)
	at org.eclipse.xtext.xtext.generator.util.GenModelUtil2.getGenClassifier(GenModelUtil2.java:43)
	at org.eclipse.xtext.xtext.generator.util.GenModelUtil2.getJavaTypeName(GenModelUtil2.java:247)
	at edu.kit.ipd.sdq.mdsd.jvmmodel.ModelJoinJvmModelInferrer.lambda$1(ModelJoinJvmModelInferrer.java:44)
	at org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder.initializeSafely(JvmTypesBuilder.java:211)
	at org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder.toMethod(JvmTypesBuilder.java:692)
	at edu.kit.ipd.sdq.mdsd.jvmmodel.ModelJoinJvmModelInferrer.lambda$0(ModelJoinJvmModelInferrer.java:54)
	at org.eclipse.xtext.xbase.jvmmodel.JvmModelAssociator$1.run(JvmModelAssociator.java:397)
	at org.eclipse.xtext.xbase.jvmmodel.JvmModelAssociator.installDerivedState(JvmModelAssociator.java:407)
	at org.eclipse.xtext.resource.DerivedStateAwareResource.installDerivedState(DerivedStateAwareResource.java:242)
	at org.eclipse.xtext.xbase.resource.BatchLinkableResource.getContents(BatchLinkableResource.java:148)
	at org.eclipse.xtext.xbase.typesystem.internal.LogicalContainerAwareBatchTypeResolver.getEntryPoints(LogicalContainerAwareBatchTypeResolver.java:44)
	at org.eclipse.xtext.xbase.typesystem.internal.DefaultBatchTypeResolver.getTypeResolver(DefaultBatchTypeResolver.java:84)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$1.get(CachingBatchTypeResolver.java:49)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver$1.get(CachingBatchTypeResolver.java:46)
	at org.eclipse.xtext.util.OnChangeEvictingCache.get(OnChangeEvictingCache.java:77)
	at org.eclipse.xtext.xbase.typesystem.internal.CachingBatchTypeResolver.doResolveTypes(CachingBatchTypeResolver.java:46)
	at org.eclipse.xtext.xbase.typesystem.internal.AbstractBatchTypeResolver.resolveTypes(AbstractBatchTypeResolver.java:69)
	at org.eclipse.xtext.xbase.resource.BatchLinkingService.resolveBatched(BatchLinkingService.java:71)
	at org.eclipse.xtext.xbase.resource.BatchLinkableResource.resolveLazyCrossReferences(BatchLinkableResource.java:165)
	at org.eclipse.xtext.EcoreUtil2.resolveLazyCrossReferences(EcoreUtil2.java:498)
	at org.eclipse.xtext.builder.clustering.ClusteringBuilderState.doUpdate(ClusteringBuilderState.java:230)
	at org.eclipse.xtext.builder.builderState.AbstractBuilderState.update(AbstractBuilderState.java:116)
	at org.eclipse.xtext.builder.impl.XtextBuilder.doBuild(XtextBuilder.java:287)
	at org.eclipse.xtext.builder.impl.XtextBuilder.incrementalBuild(XtextBuilder.java:267)
	at org.eclipse.xtext.builder.impl.XtextBuilder.build(XtextBuilder.java:161)
	at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:735)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:246)
	at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:301)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:304)
	at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:360)
	at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:383)
	at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:142)
	at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:232)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:56)



Do i need to add something to my runtime eclipse dependencies, so it will find the genmodel files?
Re: Using Xbase Expressions in JvmModelInferrer [message #1783936 is a reply to message #1783934] Tue, 20 March 2018 14:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
I assume this has to do with your ecore not in source folder
This breaks the genmodelutil.
Unfortunately I don't have the time to provide you with a solution that works always


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1783937 is a reply to message #1783936] Tue, 20 March 2018 14:35 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
=> how does you model project look like
where is the dsl file
where is the ecore
where is the genmodel
where is the generated java code


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Using Xbase Expressions in JvmModelInferrer [message #1784045 is a reply to message #1783937] Wed, 21 March 2018 21:52 Go to previous messageGo to next message
Hermann Ritzenthaler is currently offline Hermann RitzenthalerFriend
Messages: 14
Registered: February 2018
Junior Member
One more question, is there also a possibility to just need the ecore model and not the genmodel for my jvmModelInferrer?

class MyDslJvmModelInferrer extends AbstractModelInferrer {

	@Inject extension JvmTypesBuilder

	def dispatch void infer(ThetaJoinExpr element, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
		var count = 0
		acceptor.accept(element.toClass("edu.kit.ipd.sdq.mdsd.mj.Modelchecker" + count.toString)) [
			members += element.toMethod("isValid", Boolean.TYPE.typeRef) [
				// add org.eclipse.xtext.xtext.generator to deps or copy code
				val leftName = GenModelUtil2.getJavaTypeName(element.left, element.eResource.resourceSet)
				parameters += element.left.toParameter("leftElement", leftName.typeRef())
				val rightName = GenModelUtil2.getJavaTypeName(element.left, element.eResource.resourceSet)
				parameters += element.right.toParameter("rightElement", rightName.typeRef())
				body = element.condition
			]
		]
	}

}


This was your solution above, but therefore i need the genmodel, which somehow can't get loaded. Is there also a solution if i just have .ecore files?
Re: Using Xbase Expressions in JvmModelInferrer [message #1784046 is a reply to message #1784045] Wed, 21 March 2018 22:35 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
no i dont know how this would work with xbase.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Editor to outline view linking
Next Topic:Serializing model
Goto Forum:
  


Current Time: Fri Mar 29 01:35:28 GMT 2024

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

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

Back to the top