Evaluate the XBlockExpression to value? [message #1740427] |
Fri, 12 August 2016 18:07 |
Dimg Cim Messages: 59 Registered: December 2015 |
Member |
|
|
Hello,
now its my next challenge when I use my DSL and want to evaluate the result of a calculation, how can I do this?
For example I have the model with this xtext description
Berechnung:
'Berechnung' name=ValidID '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
body=XBlockExpression
;
And I use it on this way:
Berechnung fSchlange(double hm, double m, double bws) {
m+hm+bws
}
Berechnung main(){
val hm = 10.0
val m = 2.2
val bws = 4.2
val ergebnis = fSchlange(hm, m, bws)
ergebnis
}
So how can I get the result of fSchlange (result is 16,4).
Furhter I want to delegate this result to a ViewPart.
import org.eclipse.jface.viewers.ISelection
import org.eclipse.swt.SWT
import org.eclipse.swt.widgets.Composite
import org.eclipse.swt.widgets.Text
import org.eclipse.ui.ISelectionListener
import org.eclipse.ui.IWorkbenchPart
import org.eclipse.ui.PlatformUI
import org.eclipse.ui.part.ViewPart
import org.eclipse.xtext.ui.editor.model.IXtextDocument
import org.eclipse.xtext.xbase.ui.editor.XbaseEditor
class DiagrammPart extends ViewPart implements ISelectionListener{
Text text
IXtextDocument document
override createPartControl(Composite parent) {
text = new Text(parent, SWT.BORDER.bitwiseOr(SWT.MULTI).bitwiseOr(SWT.WRAP).bitwiseOr(SWT.H_SCROLL))
PlatformUI.workbench.activeWorkbenchWindow.selectionService.addSelectionListener(this)
}
override setFocus() {
}
override selectionChanged(IWorkbenchPart part, ISelection selection) {
if(part instanceof XbaseEditor){
val xtextEditor = part as XbaseEditor
if(document!= xtextEditor.document){
document = xtextEditor.document
document.addModelListener[ iXtextResource|
val result = iXtextResource.parseResult
val bc= result.rootASTElement as BerechnungContainer
val body = bc.berechnungen.filter[it.name=='main'].get(0)
// HOW CAN I RESOLVE THE BODY WITH THE RESULT ON THIS STEP?
text.text = ''Ergebnis der Berechnung: <<body>>''']
}
}
}
}
HOW CAN I RESOLVE THE BODY WITH THE RESULT ON THIS STEP?
And I got the Invalid Thread Access?
What is the best way to do all of this?
Best regards
Dim
|
|
|
|
|
|
Re: Evaluate the XBlockExpression to value? [message #1740524 is a reply to message #1740516] |
Mon, 15 August 2016 11:55 |
Dimg Cim Messages: 59 Registered: December 2015 |
Member |
|
|
Detailled Example:
Berechnung b1(double a, double b){
a/b
}
Berechnung b2(double a, double b){
a*b
}
Berechnung b2(double a, double b){
a*b
}
Berechnung main(){
val a = 10.0
val b =12.0
val term1 = b1 (a, b)
val term2=b2(term1, a)
val term3 =b3(term2,b)
val result = term1+term2+term3*2/20
result
}
So when i evaluate the main XBlockExpression it should know the b1, b2, b3 XBlockExpression result, but how can I evaluate it when I use the Interpreter only for the main Expression?
[Updated on: Mon, 15 August 2016 11:57] Report message to a moderator
|
|
|
|
Re: Evaluate the XBlockExpression to value? [message #1740526 is a reply to message #1740525] |
Mon, 15 August 2016 12:12 |
|
quick and dirty test seems to work:
package org.eclipse.xtext.example.domainmodel.tests
import javax.inject.Inject
import org.eclipse.ui.internal.Model
import org.eclipse.xtext.example.domainmodel.DomainmodelInjectorProvider
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.junit4.util.ParseHelper
import org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter
import org.junit.Test
import org.junit.runner.RunWith
import org.eclipse.xtext.example.domainmodel.domainmodel.DomainModel
import org.eclipse.xtext.example.domainmodel.domainmodel.Entity
import org.eclipse.xtext.example.domainmodel.domainmodel.Operation
import org.eclipse.xtext.xbase.interpreter.impl.DefaultEvaluationContext
import org.eclipse.xtext.common.types.JvmOperation
import org.eclipse.xtext.xbase.XAbstractFeatureCall
import org.eclipse.xtext.xbase.interpreter.IEvaluationContext
import org.eclipse.xtext.util.CancelIndicator
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations
@RunWith(XtextRunner)
@InjectWith(DomainmodelInjectorProvider)
class XXXXXXTest {
@Inject
DomainmodelXbaseInterpreter i;
@Inject
ParseHelper<DomainModel> ph;
@Test
def void testXXXX() {
val model = ph.parse('''
entity Demo {
op o1() : Integer {
1
}
op o2() : Integer {
1
}
op o3() : Integer {
o1 + o2
}
}
''')
val op = (model.elements.filter(Entity).head.features.get(2) as Operation).body
val ctx = new DefaultEvaluationContext
val r = i.evaluate(op, ctx, null)
println(r.result)
}
static class DomainmodelXbaseInterpreter extends XbaseInterpreter {
@Inject
extension IJvmModelAssociations
override protected _invokeFeature(JvmOperation operation, XAbstractFeatureCall featureCall, Object receiver, IEvaluationContext context, CancelIndicator indicator) {
if (operation.eResource.URI.fileExtension == "dmodel") {
val source = operation.primarySourceElement
if (source instanceof Operation) {
val result = evaluate(source.body)
return result.result
}
}
super._invokeFeature(operation, featureCall, receiver, context, indicator)
}
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Re: Evaluate the XBlockExpression to value? [message #1740530 is a reply to message #1740527] |
Mon, 15 August 2016 12:34 |
|
e.g. somehting like this
static class DomainmodelXbaseInterpreter extends XbaseInterpreter {
@Inject
extension IJvmModelAssociations
override protected Object invokeOperation(JvmOperation operation, Object receiver, List<Object> argumentValues) {
if (operation.eResource.URI.fileExtension == "dmodel") {
val source = operation.primarySourceElement
if (source instanceof Operation) {
val newCtx = new DefaultEvaluationContext
var i = 0;
for (v : argumentValues) {
newCtx.newValue(QualifiedName.create(source.params.get(i).name), v)
i++
}
val result = evaluate(source.body, newCtx, null)
return result.result
}
}
super.invokeOperation(operation, receiver, argumentValues)
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Powered by
FUDForum. Page generated in 0.04002 seconds