MagicDraw 18.5 Documentation

Skip to end of metadata
Go to start of metadata

'eval' Method

eval(String script)

This method will evaluate a Groovy code from a string and return the result.

$groovy.eval("println 'Hello World!'")

eval(String script, String bindingName, Object bindingObject)

This method will evaluate a Groovy code with a single binding object and specified binding name. The code will be evaluated from a string. The binding name will be used as the name for this object.

#foreach ($c in $Class)
$groovy.eval("println classname", "classname", $c.name)
#end

eval(String script, Map bindingMap)

This method will evaluate a Groovy code with a set of binding arguments (a name and an object). The code will be evaluated from a string. The binding map consists of key-value pairs for the binding name and the binding object.

#set ($dict = $map.createHashMap())
#set ($void = $dict.put("first", "foo"))
#set ($void = $dict.put("last", "bar"))
$groovy.eval("println first + last", $dict)

Or

$groovy.eval("println first + ' ' + last", {"first":"foo", "last":"bar"})

The second code contains curly brackets; '{' and '}' characters, which are not allowed to be used in any RTF template. For the RTF template, use the first code instead.

'execute' method

execute(String filename)

This method will execute a Groovy file. The 'filename' parameter refers to a name of the Groovy file or an absolute path to the Groovy file.

$groovy.execute("filename.groovy")

If the Groovy file contains Groovy functions, you can recall the functions by using 'eval()' methods.

$groovy.execute('filename.groovy')
$groovy.eval("new groovy().functionname()")

execute(String filename, String bindingName, Object bindingObject)

This method will execute a Groovy file with a single binding object and specified binding name. The 'filename' parameter is a file path to the Groovy file. The binding name will be used as the name for this object.

File filename.groovy

"Class name is " + $c.name

The template code

#foreach ($c in $Class)
$groovy.execute("filename.groovy", 'c', $c)
#end

execute(String filename, Map bindingMap)

This method will execute a Groovy file with a set of binding arguments (a name and an object). The 'filename' parameter is a file path to the Groovy file. The binding map consists of key-value pairs for the binding name and the binding object.

File filename.groovy

first + " " + last

The template code

#set ($dict = $map.createHashMap())
#set ($void = $dict.put("first", "foo"))
#set ($void = $dict.put("last", "bar"))
$groovy.execute("filename.groovy", $dict)
  • Absolute Path: If the 'filename' is provided with an absolute path, Groovy Tool will read the Groovy file from an absolute location such as $groovy.execute('c:/mycode/readclass.groovy').
  • Relative Path: If the 'filename' is provided with a relative path, Groovy Tool will read the template from a relative location. This relative location starts from the current directory in which the template is located such as $groovy.execute('readclass.groovy').

 

  • No labels