-1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <facts>    
    <fact id="ItemPrice" displayName="Item Price">
    <defaultValue>0</defaultValue>
    <script><![CDATA[ 
            Double Value_Sales= 500;
            Double Unit_Sales= 10;
            Double res=Value_Sales/Unit_Sales;
            return res;
    ]]></script>
</fact>
</facts>

Above is the sample groovy script written in xml file for finding item price.

Java code for processing Groovy:

List<Fact> factList = NREUtils.readXml("/SampleDictionary.xml") //cutome API
GroovyShell shell = new GroovyShell();
String scriptStr = factList.get(0).getScript();
Script groovyScript = shell.parse(scriptStr); // return "ItemPrice" script 
Binding binding = new Binding();
groovyScript.setBinding(binding);
Object val = groovyScript.run(); // **Result will be 50**

I would like the corresponding Scala code for the same.

3
  • 1
    Stack Overflow rewards effort. What have you tried? Post some code that doesn't work so we can see where it's going off track. Commented Apr 4, 2018 at 8:01
  • List<Fact> factList = NREUtils.readXml("/SampleDictionary.xml") //cutome API GroovyShell shell = new GroovyShell(); Script groovyScript = shell.parse(factList.get(0).getScript()); // return "ItemPrice" script Binding binding = new Binding(); groovyScript.setBinding(binding); Object val = groovyScript.run(); // Result will be 50 Commented Apr 4, 2018 at 8:46
  • Please edit your question to add additional code and information. Posting code in a comment is, as you can see, rather pointless. (The edit link is in the lower left, below your question tags.) Commented Apr 4, 2018 at 8:51

1 Answer 1

0

Without further information on your problem, and being both languages (scala and groovy) executed over the JVM, I would suggest you just to compile your groovy code and include the jar in the classpath of the JVM running your scala code.

Here you have some ideas about how to turn groovy code into usable bytecode: http://docs.groovy-lang.org/latest/html/documentation/tools-groovyc.html

Then do just do as you would with any java library that you would like to call from scala: Using Java libraries in Scala

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.