Using Xbase Expressions in JvmModelInferrer [message #1783851] |
Mon, 19 March 2018 13:32 |
Hermann Ritzenthaler 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 #1783876 is a reply to message #1783873] |
Mon, 19 March 2018 18:59 |
Hermann Ritzenthaler 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 |
|
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 |
Hermann Ritzenthaler 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 #1783929 is a reply to message #1783918] |
Tue, 20 March 2018 13:36 |
Hermann Ritzenthaler 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 #1784045 is a reply to message #1783937] |
Wed, 21 March 2018 21:52 |
Hermann Ritzenthaler 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?
|
|
|
|
Powered by
FUDForum. Page generated in 0.06603 seconds