Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Evaluate the XBlockExpression to value?
Evaluate the XBlockExpression to value? [message #1740427] Fri, 12 August 2016 18:07 Go to next message
Dimg Cim is currently offline Dimg CimFriend
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 #1740428 is a reply to message #1740427] Fri, 12 August 2016 18:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member

(1) have a look at http://www.vogella.com/tutorials/EclipseJobs/article.html#eclipsejobs_display
(2) have a look at XbaseInterpreter


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Evaluate the XBlockExpression to value? [message #1740514 is a reply to message #1740428] Mon, 15 August 2016 10:22 Go to previous messageGo to next message
Dimg Cim is currently offline Dimg CimFriend
Messages: 59
Registered: December 2015
Member
Yeah thats helpful.

When I evaluate a XBlockExpression it is ok. But when a XBlockExpression depends on more XBlockExpressions, how can this be done?

For Example:

Berechnung Main depends on more Berechnung b1, b2, b3 to calculate the final result?

So I have recursivly evalaute the XBlockExrepssion?
Re: Evaluate the XBlockExpression to value? [message #1740516 is a reply to message #1740514] Mon, 15 August 2016 10:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
sry i dont have the time to digg into that. can you give more context

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Evaluate the XBlockExpression to value? [message #1740524 is a reply to message #1740516] Mon, 15 August 2016 11:55 Go to previous messageGo to next message
Dimg Cim is currently offline Dimg CimFriend
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 #1740525 is a reply to message #1740524] Mon, 15 August 2016 11:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
what happens if you do so? simply call evaluate on the main() ?

maybe you have to customize invokemethod in the xbase interpreter and intercept the call there i dont know ....


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Evaluate the XBlockExpression to value? [message #1740526 is a reply to message #1740525] Mon, 15 August 2016 12:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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 #1740527 is a reply to message #1740526] Mon, 15 August 2016 12:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
p.s: i leave it to you to implement the params handling

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 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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
Previous Topic:Where ist the formatting package in Xtext 2.10
Next Topic:Accessing the Abstract Syntax Tree of imported files
Goto Forum:
  


Current Time: Fri Apr 19 23:12:58 GMT 2024

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

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

Back to the top