2.20.  Dynamic evaluation of Kermeta expressions

[Warning]Text not verified for kermeta 2

The dynamic expression are currently not implemented in Kermeta 2. However, there are some plan to reimplement them in the future.

Kermeta allow you to evaluate dynamically a Kermeta Expression with a specific context.

Example 1 : my first dynamic expression

var de : DynamicExpression init DynamicExpression.new
de.initializeDefaults
de.formalParameters.put("a", String)
de.parse("stdio.writeln(a)")
var params : Hashtable<String, Object> init Hashtable<String, Object>.new
params.put("a", "hello world!")
de.execute(void, params)

If you want to dynamically evaluate more than one statement, you will have to surround your set of statements with "do.. end" block:

Example 2 : yet another example

// let's get previous example and modify it // [...] (carriage return is not necessary inside the block)
de.parse("do stdio.writeln(a) stdio.writeln("another stdio writeln ... ") end")
// [...]

Example 3 : another more complex sample of dynamic expressions :

package testDynamicExpression;

using kermeta::interpreter
using kermeta::utils
using kermeta::standard

class TestMain
{

    operation TestToto() is do
        stdio.writeln("J'ai essayé de lancer testtoto!")
    end

    operation testDynExp() is do
        var dynExpr : DynamicExpression init DynamicExpression.new
        dynExpr.initializeDefaults()

        self.getMetaClass.ownedOperation.select{op| not( op.name.indexOf("Test")==-1)
and op.name.indexOf("All")==-1}

            .collect{op|op.name}.each{opName|

                    stdio.writeln("execution de "+opName)
                    dynExpr.initializeDefaults
                    dynExpr.parse("testDynamicExpression ::TestMain.new." +opName)
                    dynExpr.execute(void,Hashtable<String,Object>.new)
            }
    end
}
[Caution]Caution

You cannot use "self" inside a dynamic expression