Hi mashers,
I'm having problems getting the output from a macro that has an embedded Groovy script to return the hashCode value of a string it is passed. I can get the script to run fine in the main emml script body, returning the hash value I expect, but when the Groovy script is run from within a macro, the output appears to be null.Can anyone see why the returned data from the macro execution is different. Many thanks, Innes
Here's the emml code:
<mashup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jackbe.com/2008-03-01/EMMLSchema/ ../src/schemas/EMMLSpec.xsd"
xmlns="http://www.jackbe.com/2008-03-01/EMMLSchema" xmlns:macro="http://www.jackbe.com/2008-03-01/EMMLMacro"
name="MacroTest">
<output name="result" type="string" />
<input name="inputString" type="string" default="a string to hash" />
<variables>
<variable name="out" type="string" /><!-- Needed for Groovy println -->
<variable name="hashValue" type="number" />
<variable name="mHashValue" type="number" />
<variable name="str2Hash" type="string" />
</variables>
<!-- **************** uncomment this block to AA to run the embedded Groovy script ******************** -->
<!-- <macro name="hashAString">-->
<!-- <output name="hashValue" type="number" />-->
<!-- <input name="str2Hash" type="string" />-->
<!-- <variables>-->
<!-- <variable name="out" type="string" />-->
<!-- <variable name="mHashValue" type="number" />-->
<!-- </variables>-->
<!-- <script type="groovy" inputvariable="str2Hash"-->
<!-- outputvariable="mHashValue">-->
<!-- <![CDATA[ -->
<!-- println str2Hash-->
<!-- hashValue = str2Hash?.hashCode()-->
<!-- ]]>-->
<!-- </script>-->
<!-- <assign fromvariable="mHashValue" outputvariable="hashValue" />-->
<!-- </macro>-->
<!-- <macro:hashAString str2Hash="$inputString" outputvariable="hashValue" />-->
<!-- ***************** AA End of the embedded macro+Groovy block ******************************** -->
<!-- **************** Uncomment to block BB to run the inline Groovy script ********************* -->
<!-- <assign fromvariable="inputString" outputvariable="str2Hash"/>-->
<!-- <script type="groovy" inputvariable="str2Hash"-->
<!-- outputvariable="mHashValue">-->
<!-- <![CDATA[ -->
<!-- println str2Hash-->
<!-- mHashValue = str2Hash?.hashCode()-->
<!-- ]]>-->
<!-- </script>-->
<!-- <assign fromvariable="mHashValue" outputvariable="hashValue" /> -->
<!-- ***************** BB End of the inline Groovy block ******************************** -->
<assign fromvariable="hashValue" outputvariable="result" />
<display message="result is:" variable="result" />
</mashup>
The result from the macro script is:
a string to hash
result is:
while from the inline script it is:
a string to hash
result is: 260647971
- Login or register to post comments
- Email this page

Hi Innes, I believe it is a
Hi Innes,
I believe it is a typo . In the groovy script inside the macro, the variable you are assigning a value to is hashValue. If it was mHashValue you would get the correct result.
Satya.
/presto dev
P.S.
This works for me
<mashup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.jackbe.com/2008-03-01/EMMLSchema/ ../src/schemas/EMMLSpec.xsd"
xmlns="http://www.jackbe.com/2008-03-01/EMMLSchema" xmlns:macro="http://www.jackbe.com/2008-03-01/EMMLMacro"
name="MacroTest">
<output name="result" type="string" />
<input name="inputString" type="string" default="a string to hash" />
<variables>
<variable name="out" type="string" /><!-- Needed for Groovy println -->
<variable name="hashValue" type="number" />
<variable name="mHashValue" type="number" />
<variable name="str2Hash" type="string" />
</variables>
<macro name="hashAString">
<output name="hashValue" type="number" />
<input name="str2Hash" type="string" />
<variables>
<variable name="out" type="string" />
<variable name="mHashValue" type="number" />
</variables>
<script type="groovy" inputvariable="str2Hash"
outputvariable="mHashValue">
<![CDATA[
println str2Hash
mHashValue = str2Hash?.hashCode()
]]>
</script>
<display message="mHash inside macros:" variable="mHashValue" />
<assign fromvariable="mHashValue" outputvariable="hashValue" />
</macro>
<macro:hashAString str2Hash="$inputString" outputvariable="hashValue" />
<assign fromvariable="hashValue" outputvariable="result" />
<display message="result is:" variable="result" />
</mashup>
Hi Satya, Thanks for the
Hi Satya,
Thanks for the reply. When I read your message I went "How could I be so stupid", but then I thought about it a bit more and realised that this wasn't a coding error (I'd changed so many things yesterday to try and get this to work) and that what I'd written was what I had intended.
In the macro version, I was expecting to be able to directly assign the value of the hashCode() statement to the variable declared in the <output> statement of the macro. In other words to assign the hashCode value to "hashValue".
I've played around with the macro version again this morning and this is what I finally arrived at that seems to work. It's unclear to me why I needed to statically type variables or even add in the <assign> as the last step. Attempting to directly set the variable declared in <output> didn't work. The "why" is probably an investigation for someone with way more java/groovy/presto skills than I have.
<macro name="hashAString">
<output name="strHashVal" type="string" />
<input name="str2Hash" type="string" />
<variables>
<variable name="gHashVal" type="string" />
</variables>
<script type="groovy" inputvariables="str2Hash" outputvariable="gHashVal">
<![CDATA[
Integer iHashVal = str2Hash?.hashCode()
gHashVal = iHashVal.toString()
]]>
</script>
<assign fromvariable="gHashVal" outputvariable="strHashVal" />
</macro>
Needless to say, this is the second time I've used a Groovy script within an emml script and both times I needed to resort to some variable swapping and datatyping trickery to get the desired output. It seems there are a few issues around variable access when using Groovy, but that's ok . I like Groovy and will keep using it, but expect to have this issue confront me again in the future.
I'll add some extra sanity checks in to the macro and then post my final cut again later.
Cheers,
Innes